Skip to content

Commit

Permalink
generic getters + geonames fillers
Browse files Browse the repository at this point in the history
  • Loading branch information
wschuell committed Nov 20, 2023
1 parent 9957cab commit 83281f1
Show file tree
Hide file tree
Showing 9 changed files with 522 additions and 80 deletions.
18 changes: 18 additions & 0 deletions gis_fillers/fillers/metafiller.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from .fillers import Fillers
from .zones import zaehlsprengel, countries, geonames


class MetaFiller(Filler):
def after_insert(self):
self.db.add_filler(
zaehlsprengel.ZaehlsprengelFiller(data_folder=self.data_folder)
)
self.db.add_filler(
zaehlsprengel.SimplifiedZSFiller(data_folder=self.data_folder)
)
self.db.add_filler(
zaehlsprengel.PopulationZSFiller(data_folder=self.data_folder)
)
self.db.add_filler(zaehlsprengel.PLZFiller(data_folder=self.data_folder))
self.db.add_filler(geonames.GeonamesFiller(data_folder=self.data_folder))
self.db.add_filler(countries.CountriesFiller(data_folder=self.data_folder))
1 change: 1 addition & 0 deletions gis_fillers/fillers/zones/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
from . import countries
from . import hexagons
from . import generic
from . import geonames
58 changes: 58 additions & 0 deletions gis_fillers/fillers/zones/geonames.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
from psycopg2 import extras
import zipfile
import os
from .. import fillers


class GeonamesFiller(fillers.Filler):
def __init__(
self,
force=False,
url_geonames="https://download.geonames.org/export/zip/allCountries.zip",
zipname="geonames_allCountries.zip",
**kwargs
):
self.force = force
self.url_geonames = url_geonames
self.zipname = zipname
fillers.Filler.__init__(self, **kwargs)

def prepare(self):
fillers.Filler.prepare(self)
if not self.force and self.check_done():
self.done = True
elif not os.path.exists(os.path.join(self.data_folder, self.zipname)):
self.download(url=self.url_geonames, destination=self.zipname)

def gen_extract(self):
with zipfile.ZipFile(os.path.join(self.data_folder, self.zipname), "r") as zf:
with zf.open("allCountries.txt", "r") as f:
for line in f:
elt = line.decode("utf8").split("\t")
yield dict(
country_code=elt[0],
zip_code=elt[1],
geo_lat=elt[-3],
geo_long=elt[-2],
)

def check_done(self):
self.db.cursor.execute("SELECT 1 FROM geonames_zipcodes LIMIT 1;")
return self.db.cursor.fetchone() == (1,)

def apply(self):
extras.execute_batch(
self.db.cursor,
"""
INSERT INTO geonames_zipcodes(country_code,zip_code,geom)
VALUES(
%(country_code)s,
%(zip_code)s,
ST_SetSRID(ST_MakePoint(%(geo_long)s::double precision,%(geo_lat)s::double precision),4326)
)
ON CONFLICT DO NOTHING;
""",
self.gen_extract(),
page_size=10**4,
)
self.db.connection.commit()
2 changes: 1 addition & 1 deletion gis_fillers/getters/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .generic_getters import Getter
from .generic_getters import Getter, GISGetter
Loading

0 comments on commit 83281f1

Please sign in to comment.