MCPcopy
hub / github.com/thampiman/reverse-geocoder / extract

Method extract

reverse_geocoder/__init__.py:154–250  ·  view source on GitHub ↗

Function loads the already extracted GeoNames cities file or downloads and extracts it if it doesn't exist locally Args: local_filename (str): Path to local RG_FILE

(self, local_filename)

Source from the content-addressed store, hash-verified

152 return geo_coords, locations
153
154 def extract(self, local_filename):
155 """
156 Function loads the already extracted GeoNames cities file or downloads and extracts it if
157 it doesn't exist locally
158 Args:
159 local_filename (str): Path to local RG_FILE
160 """
161 if os.path.exists(local_filename):
162 if self.verbose:
163 print('Loading formatted geocoded file...')
164 rows = csv.DictReader(open(local_filename, 'rt'))
165 else:
166 gn_cities1000_url = GN_URL + GN_CITIES1000 + '.zip'
167 gn_admin1_url = GN_URL + GN_ADMIN1
168 gn_admin2_url = GN_URL + GN_ADMIN2
169
170 cities1000_zipfilename = GN_CITIES1000 + '.zip'
171 cities1000_filename = GN_CITIES1000 + '.txt'
172
173 if not os.path.exists(cities1000_zipfilename):
174 if self.verbose:
175 print('Downloading files from Geoname...')
176 try: # Python 3
177 import urllib.request
178 urllib.request.urlretrieve(gn_cities1000_url, cities1000_zipfilename)
179 urllib.request.urlretrieve(gn_admin1_url, GN_ADMIN1)
180 urllib.request.urlretrieve(gn_admin2_url, GN_ADMIN2)
181 except ImportError: # Python 2
182 import urllib
183 urllib.urlretrieve(gn_cities1000_url, cities1000_zipfilename)
184 urllib.urlretrieve(gn_admin1_url, GN_ADMIN1)
185 urllib.urlretrieve(gn_admin2_url, GN_ADMIN2)
186
187
188 if self.verbose:
189 print('Extracting cities1000...')
190 _z = zipfile.ZipFile(open(cities1000_zipfilename, 'rb'))
191 open(cities1000_filename, 'wb').write(_z.read(cities1000_filename))
192
193 if self.verbose:
194 print('Loading admin1 codes...')
195 admin1_map = {}
196 t_rows = csv.reader(open(GN_ADMIN1, 'rt'), delimiter='\t')
197 for row in t_rows:
198 admin1_map[row[ADMIN_COLUMNS['concatCodes']]] = row[ADMIN_COLUMNS['asciiName']]
199
200 if self.verbose:
201 print('Loading admin2 codes...')
202 admin2_map = {}
203 for row in csv.reader(open(GN_ADMIN2, 'rt'), delimiter='\t'):
204 admin2_map[row[ADMIN_COLUMNS['concatCodes']]] = row[ADMIN_COLUMNS['asciiName']]
205
206 if self.verbose:
207 print('Creating formatted geocoded file...')
208 writer = csv.DictWriter(open(local_filename, 'wt'), fieldnames=RG_COLUMNS)
209 rows = []
210 for row in csv.reader(open(cities1000_filename, 'rt'), \
211 delimiter='\t', quoting=csv.QUOTE_NONE):

Callers 1

__init__Method · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected