Skip to content

Commit

Permalink
expose mbtile max_zoom level to the upload enpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
hambsch committed Jul 9, 2024
1 parent c118454 commit 2ceaa91
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 8 deletions.
4 changes: 2 additions & 2 deletions server/guppy/endpoints/endpoints_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
logger = logging.getLogger(__name__)


def upload_file(layer_name: str, label: str, file: UploadFile, db: Session, is_rgb: bool = False):
def upload_file(layer_name: str, label: str, file: UploadFile, db: Session, is_rgb: bool = False, max_zoom: int = 17):
"""
Args:
layer_name (str): The name of the layer.
Expand All @@ -39,7 +39,7 @@ def upload_file(layer_name: str, label: str, file: UploadFile, db: Session, is_r

write_input_file_to_disk(file, tmp_file_location)

is_mbtile = create_preprocessed_layer_file(ext, file_location, sanitized_filename, sanitized_layer_name, tmp_file_location)
is_mbtile = create_preprocessed_layer_file(ext, file_location, sanitized_filename, sanitized_layer_name, tmp_file_location, max_zoom)

insert_into_layer_metadata(layer_uuid=f"{sanitized_layer_name}_{sanitized_filename}", label=label, file_path=file_location, db=db, is_rgb=is_rgb, is_mbtile=is_mbtile)
return f"Upload successful: Layer {sanitized_layer_name}_{sanitized_filename} uploaded with label {label}."
Expand Down
8 changes: 4 additions & 4 deletions server/guppy/endpoints/upload_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def check_layer_exists(layer_name: str, db: Session):
raise create_error(code=400, message=f"Upload failed: Layer {layer_name} already exists.")


def create_preprocessed_layer_file(ext: str, file_location: str, sanitized_filename: str, sanitized_layer_name: str, tmp_file_location: str) -> bool:
def create_preprocessed_layer_file(ext: str, file_location: str, sanitized_filename: str, sanitized_layer_name: str, tmp_file_location: str, max_zoom: int = 17) -> bool:
"""
Args:
ext (str): The extension of the file.
Expand Down Expand Up @@ -146,7 +146,7 @@ def create_preprocessed_layer_file(ext: str, file_location: str, sanitized_filen
if 'bounds' not in df.columns:
df['bounds'] = df.geometry.envelope.to_wkt()
df.to_file(gpkg_loc, index=False, layer=sanitized_layer_name, driver='GPKG')
to_mbtiles(sanitized_layer_name, gpkg_loc, file_location)
to_mbtiles(sanitized_layer_name, gpkg_loc, file_location, max_zoom)
os.remove(tmp_file_location)
df.drop(columns=['geometry'], inplace=True)
engine = create_engine(f'sqlite:///{file_location.replace(".mbtiles", ".sqlite")}')
Expand Down Expand Up @@ -213,7 +213,7 @@ def write_input_file_to_disk(file: UploadFile, tmp_file_location: str):
file.file.close()


def to_mbtiles(name: str, input_file_path: str, output_file_path: str):
def to_mbtiles(name: str, input_file_path: str, output_file_path: str, max_zoom=17):
"""
Args:
name: A string representing the name of the MBTiles file.
Expand All @@ -228,7 +228,7 @@ def to_mbtiles(name: str, input_file_path: str, output_file_path: str):
'-dsco', 'MAX_FEATURES=5000000',
'-dsco', 'MAX_SIZE=5000000',
'-dsco', 'MINZOOM=0',
'-dsco', 'MAXZOOM=17',
'-dsco', f'MAXZOOM={max_zoom}',
'-dsco', f'NAME={name}',
'-lco', f'NAME={name}',
'-preserve_fid',
Expand Down
2 changes: 2 additions & 0 deletions server/guppy/html/upload.html
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@
<input type="text" id="layerLabel" name="layerLabel">
<label for="layerName">Layer Prefix:</label>
<input type="text" id="layerName" name="layerName">
<label for="maxZoom">Max Zoom:</label>
<input type="text" id="maxZoom" name="maxZoom" value="17">
<label for="file">File:</label>
<input type="file" id="file" name="file" accept=".tiff,.tif,.asc,.gpkg,.geojson,.mbtiles">
<input type="submit" value="Upload">
Expand Down
4 changes: 2 additions & 2 deletions server/guppy/routes/admin_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@


@router.post("/upload", description="Upload a file (GeoTiff or Gpkg) to the server.")
def upload_file(layerName: str = Form(...), layerLabel: str = Form(...), isRgb: bool = Form(False), file: UploadFile = File(...), db: Session = Depends(get_db)):
return endpoints_upload.upload_file(layer_name=layerName, label=layerLabel, file=file, is_rgb=isRgb, db=db)
def upload_file(layerName: str = Form(...), layerLabel: str = Form(...), isRgb: bool = Form(False), maxZoom: int = Form(17), file: UploadFile = File(...), db: Session = Depends(get_db)):
return endpoints_upload.upload_file(layer_name=layerName, label=layerLabel, file=file, is_rgb=isRgb, max_zoom=maxZoom, db=db)


@router.get("/{layer_name}/generate_db", description="Generate sqlite file for a mbtiles layer")
Expand Down

0 comments on commit 2ceaa91

Please sign in to comment.