From 1f31f4663bab9e3e61b1e936f3d136f685df7860 Mon Sep 17 00:00:00 2001 From: gman Date: Sun, 4 Feb 2024 11:07:08 +0300 Subject: [PATCH] convert function --- geojson2vt.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 geojson2vt.py diff --git a/geojson2vt.py b/geojson2vt.py new file mode 100644 index 0000000..1bb5ee8 --- /dev/null +++ b/geojson2vt.py @@ -0,0 +1,37 @@ +import uuid +import subprocess +from pathlib import Path +import json + +def geojson2vt(uploaded_geojson) -> str: + """Convert geojson file to vector tiles + + Args: + uploaded_geojson: GeoJSON file uploaded by user + + Returns: + str: vector tiles id + """ + # save the uploaded file + # get a path to the saved geojson + uploaded_geojson_path = "land.geojson" # replace value with the path to the saved geojson + vector_tiles_id = uploaded_geojson_path + uuid.uuid4().hex + tippecanoe_command = f"tippecanoe --maximum-zoom=g --output-to-directory={vector_tiles_id} --drop-densest-as-needed --no-tile-compression {uploaded_geojson_path}" + subprocess.run(tippecanoe_command, shell=True) # https://stackoverflow.com/a/51950538/14742462 + metadata_path = Path(vector_tiles_id, "metadata.json") + with open(metadata_path, "r") as metadata_file: + metadata = json.load(metadata_file) + + with open("catalog.json", "r") as catalog_file: + catalog = json.load(catalog_file) + catalog.append(metadata) + + with open("catalog.json", "w") as catalog_file: + json.dump(catalog, catalog_file, ensure_ascii=False) + return vector_tiles_id + +geojson2vt("ert") + + + +