parent
7e39964fed
commit
5d5f1424db
@ -0,0 +1,43 @@
|
||||
import uuid
|
||||
import subprocess
|
||||
import shutil
|
||||
from pathlib import Path
|
||||
import json
|
||||
|
||||
def geojson2vt(uploaded_geojson, storage_path = None) -> str:
|
||||
"""Convert geojson file to vector tiles
|
||||
|
||||
Args:
|
||||
uploaded_geojson: GeoJSON file uploaded by user
|
||||
|
||||
Returns:
|
||||
str: vector tiles id
|
||||
"""
|
||||
storage_path = storage_path or ""
|
||||
# save the uploaded file
|
||||
# get a path to the saved geojson
|
||||
geojson_uuid = uuid.uuid4().hex
|
||||
uploaded_geojson_path = Path(storage_path, "geojsons", geojson_uuid + ".geojson")
|
||||
with open(uploaded_geojson_path, mode="xb") as geojson_file:
|
||||
shutil.copyfileobj(uploaded_geojson.file, geojson_file)
|
||||
vector_tiles_path = Path(storage_path, "vector_tiles", geojson_uuid)
|
||||
tippecanoe_command = f"tippecanoe --maximum-zoom=g --output-to-directory={vector_tiles_path} --drop-densest-as-needed --no-tile-compression {uploaded_geojson_path}"
|
||||
subprocess.run(tippecanoe_command, shell=True, check=True) # https://stackoverflow.com/a/51950538/14742462
|
||||
metadata_path = Path(vector_tiles_path, "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
|
||||
|
||||
if __name__ == "__main__":
|
||||
geojson2vt("ert")
|
||||
|
||||
|
||||
|
||||
|
||||
@ -1,7 +1,34 @@
|
||||
from fastapi import FastAPI
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
from fastapi import Depends, FastAPI, UploadFile
|
||||
|
||||
from .geojson2vt import geojson2vt
|
||||
|
||||
#prepare data storage
|
||||
def set_storage():
|
||||
storage_path = os.environ.get("STATE_DIRECTORY")
|
||||
if storage_path:
|
||||
try:
|
||||
os.mkdir(Path(storage_path, "geojsons"))
|
||||
except FileExistsError:
|
||||
pass
|
||||
try:
|
||||
os.mkdir(Path(storage_path, "vector_tiles"))
|
||||
except FileExistsError:
|
||||
pass
|
||||
return storage_path
|
||||
|
||||
STORAGE_PATH = set_storage()
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
@app.get("/")
|
||||
async def root():
|
||||
return {"message": "Hello World!"}
|
||||
|
||||
@app.post("/upload/")
|
||||
async def create_upload_file(file: UploadFile):
|
||||
geojson2vt(file, storage_path=STORAGE_PATH)
|
||||
return {"filename": file.filename}
|
||||
|
||||
@ -1,37 +0,0 @@
|
||||
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")
|
||||
|
||||
|
||||
|
||||
|
||||
Loading…
Reference in new issue