-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2e11a43
commit 3d63723
Showing
6 changed files
with
120 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 45 additions & 35 deletions
80
kuwala/pipelines/admin-boundaries/src/geonames_controller.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,71 @@ | ||
import os | ||
import re | ||
import zipfile | ||
from python_utils.src.file_converter import txt_to_csv | ||
from python_utils.src.FileDownloader import download_file | ||
|
||
from pyspark.sql.functions import split | ||
from pyspark.sql.types import DateType, DoubleType, IntegerType, StructType, StringType | ||
from pyspark.sql.types import DateType, DoubleType, IntegerType, StringType, StructType | ||
from python_utils.src.FileDownloader import download_file | ||
from python_utils.src.file_converter import txt_to_csv | ||
|
||
|
||
def download_geonames_file(dump_name, file_path): | ||
download_file(url=f'https://download.geonames.org/export/dump/{dump_name}.zip', path=file_path) | ||
download_file( | ||
url=f"https://download.geonames.org/export/dump/{dump_name}.zip", path=file_path | ||
) | ||
|
||
with zipfile.ZipFile(file_path, 'r') as zip_ref: | ||
zip_ref.extractall(file_path.split(f'/{dump_name}.zip')[0]) | ||
with zipfile.ZipFile(file_path, "r") as zip_ref: | ||
zip_ref.extractall(file_path.split(f"/{dump_name}.zip")[0]) | ||
|
||
os.remove(file_path) | ||
|
||
|
||
def get_schema(): | ||
return StructType() \ | ||
.add('geoname_id', IntegerType()) \ | ||
.add('name', StringType()) \ | ||
.add('ascii_name', StringType()) \ | ||
.add('alternate_names', StringType()) \ | ||
.add('latitude', DoubleType()) \ | ||
.add('longitude', DoubleType()) \ | ||
.add('feature_class', StringType()) \ | ||
.add('feature_code', StringType()) \ | ||
.add('country_code', StringType()) \ | ||
.add('alternate_country_codes', StringType()) \ | ||
.add('admin_1_code', StringType()) \ | ||
.add('admin_2_code', StringType()) \ | ||
.add('admin_3_code', StringType()) \ | ||
.add('admin_4_code', StringType()) \ | ||
.add('population', IntegerType()) \ | ||
.add('elevation', IntegerType()) \ | ||
.add('digital_elevation_model', IntegerType()) \ | ||
.add('timezone', StringType()) \ | ||
.add('modification_date', DateType()) | ||
return ( | ||
StructType() | ||
.add("geoname_id", IntegerType()) | ||
.add("name", StringType()) | ||
.add("ascii_name", StringType()) | ||
.add("alternate_names", StringType()) | ||
.add("latitude", DoubleType()) | ||
.add("longitude", DoubleType()) | ||
.add("feature_class", StringType()) | ||
.add("feature_code", StringType()) | ||
.add("country_code", StringType()) | ||
.add("alternate_country_codes", StringType()) | ||
.add("admin_1_code", StringType()) | ||
.add("admin_2_code", StringType()) | ||
.add("admin_3_code", StringType()) | ||
.add("admin_4_code", StringType()) | ||
.add("population", IntegerType()) | ||
.add("elevation", IntegerType()) | ||
.add("digital_elevation_model", IntegerType()) | ||
.add("timezone", StringType()) | ||
.add("modification_date", DateType()) | ||
) | ||
|
||
|
||
def get_geonames_cities(sp): | ||
dump_name = 'cities500' | ||
dump_name = "cities500" | ||
script_dir = os.path.dirname(__file__) | ||
file_path_zip = os.path.join(script_dir, f'../../../tmp/kuwala/admin_boundary_files/{dump_name}.zip') | ||
file_path_txt = file_path_zip.replace('.zip', '.txt') | ||
file_path_csv = file_path_zip.replace('.zip', '.csv') | ||
r = re.compile('([a-zA-Z]+)([0-9]+)') | ||
file_path_zip = os.path.join( | ||
script_dir, f"../../../tmp/kuwala/admin_boundary_files/{dump_name}.zip" | ||
) | ||
file_path_txt = file_path_zip.replace(".zip", ".txt") | ||
file_path_csv = file_path_zip.replace(".zip", ".csv") | ||
r = re.compile("([a-zA-Z]+)([0-9]+)") | ||
m = r.match(dump_name) | ||
file_path_parquet = file_path_zip.replace(f'{dump_name}.zip', f'{m.group(1)}_{m.group(2)}.parquet') | ||
file_path_parquet = file_path_zip.replace( | ||
f"{dump_name}.zip", f"{m.group(1)}_{m.group(2)}.parquet" | ||
) | ||
|
||
download_geonames_file(dump_name=dump_name, file_path=file_path_zip) | ||
txt_to_csv(file_path=file_path_txt) | ||
|
||
df = sp.read.csv(file_path_csv, schema=get_schema()) | ||
df = df.withColumn('alternate_names', split('alternate_names', ',')) \ | ||
.withColumn('alternate_country_codes', split('alternate_country_codes', ',')) | ||
df = df.withColumn("alternate_names", split("alternate_names", ",")).withColumn( | ||
"alternate_country_codes", split("alternate_country_codes", ",") | ||
) | ||
|
||
df.write.mode('overwrite').parquet(file_path_parquet) | ||
df.write.mode("overwrite").parquet(file_path_parquet) | ||
os.remove(file_path_txt) | ||
os.remove(file_path_csv) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule osm-parquetizer
updated
2 files
+43 −0 | .github/workflows/osm_parquetizer.yml | |
+7 −3 | dockerfile |