From 6d7aa5575bfe95df5d382bdbda1be05445137c8d Mon Sep 17 00:00:00 2001 From: Anuj Gupta <84966248+Anuj-Gupta4@users.noreply.github.com> Date: Tue, 31 Dec 2024 13:06:28 +0545 Subject: [PATCH] Auto basemap generation if tms url is input during project creation (#2027) * feat: add background task for basemap generation during project file generation * feat: add 'tms' option to tile_source for basemap generation * feat: update basemap generation to support custom tile source --- src/backend/app/projects/project_routes.py | 23 +++++++++++++++++++-- src/backend/app/projects/project_schemas.py | 4 +++- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/backend/app/projects/project_routes.py b/src/backend/app/projects/project_routes.py index 3ade2b7b4..e21b22582 100644 --- a/src/backend/app/projects/project_routes.py +++ b/src/backend/app/projects/project_routes.py @@ -881,6 +881,7 @@ async def add_additional_entity_list( async def generate_files( db: Annotated[Connection, Depends(db_conn)], project_user_dict: Annotated[ProjectUserDict, Depends(project_manager)], + background_tasks: BackgroundTasks, xlsform_upload: Annotated[ Optional[BytesIO], Depends(central_deps.read_optional_xlsform) ], @@ -905,6 +906,7 @@ async def generate_files( created (i.e. the project form references multiple geometries). db (Connection): The database connection. project_user_dict (ProjectUserDict): Project admin role. + background_tasks (BackgroundTasks): FastAPI background tasks. Returns: json (JSONResponse): A success message containing the project ID. @@ -980,6 +982,13 @@ async def generate_files( }, ) + if project.custom_tms_url: + basemap_in = project_schemas.BasemapGenerate( + tile_source="custom", file_format="pmtiles", tms_url=project.custom_tms_url + ) + org_id = project.organisation_id + await generate_basemap(project_id, org_id, basemap_in, db, background_tasks) + return JSONResponse( status_code=HTTPStatus.OK, content={"message": "success"}, @@ -999,7 +1008,19 @@ async def generate_project_basemap( project_id = project_user.get("project").id org_id = project_user.get("project").organisation_id + await generate_basemap(project_id, org_id, basemap_in, db, background_tasks) # Create task in db and return uuid + return {"Message": "Tile generation started"} + + +async def generate_basemap( + project_id: int, + org_id: int, + basemap_in: project_schemas.BasemapGenerate, + db: Connection, + background_tasks: BackgroundTasks, +): + """Generate basemap tiles for a project.""" log.debug( "Creating generate_project_basemap background task " f"for project ID: {project_id}" @@ -1023,8 +1044,6 @@ async def generate_project_basemap( basemap_in.tms_url, ) - return {"Message": "Tile generation started"} - @router.patch("/{project_id}", response_model=project_schemas.ProjectOut) async def update_project( diff --git a/src/backend/app/projects/project_schemas.py b/src/backend/app/projects/project_schemas.py index 82be5f104..7323ad38f 100644 --- a/src/backend/app/projects/project_schemas.py +++ b/src/backend/app/projects/project_schemas.py @@ -274,7 +274,9 @@ class ProjectUserContributions(BaseModel): class BasemapGenerate(BaseModel): """Params to generate a new basemap.""" - tile_source: Annotated[Literal["esri", "bing", "google"], Field(default="esri")] + tile_source: Annotated[ + Literal["esri", "bing", "google", "custom"], Field(default="esri") + ] file_format: Annotated[ Literal["mbtiles", "sqlitedb", "pmtiles"], Field(default="mbtiles"),