Skip to content

Commit

Permalink
Merge pull request #42 from nihar1024/main
Browse files Browse the repository at this point in the history
Fixes to WFS (external service) file upload endpoint, require street network layer project ID in catchment area request
  • Loading branch information
majkshkurti authored Sep 3, 2024
2 parents b887abf + 59063db commit 9ea3ee0
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 18 deletions.
23 changes: 16 additions & 7 deletions src/core/config.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from typing import Any, Dict, Optional
from uuid import UUID

import boto3
from pydantic import BaseSettings, HttpUrl, PostgresDsn, validator
from uuid import UUID


class AsyncPostgresDsn(PostgresDsn):
allowed_schemes = {"postgres+asyncpg", "postgresql+asyncpg"}
Expand All @@ -24,15 +25,17 @@ class Settings(BaseSettings):
CUSTOMER_SCHEMA: Optional[str] = "customer"
REGION_MAPPING_PT_TABLE: Optional[str] = "basic.region_mapping_pt"
BASE_STREET_NETWORK: Optional[UUID] = "903ecdca-b717-48db-bbce-0219e41439cf"
STREET_NETWORK_EDGE_DEFAULT_LAYER_PROJECT_ID = 36126
STREET_NETWORK_NODE_DEFAULT_LAYER_PROJECT_ID = 37319

ASYNC_CLIENT_DEFAULT_TIMEOUT: Optional[float] = (
10.0 # Default timeout for async http client
)
ASYNC_CLIENT_READ_TIMEOUT: Optional[float] = (
30.0 # Read timeout for async http client
)
CRUD_NUM_RETRIES: Optional[int] = 20 # Number of times to retry calling an endpoint
CRUD_RETRY_INTERVAL: Optional[int] = 2 # Number of seconds to wait between retries
CRUD_NUM_RETRIES: Optional[int] = 20 # Number of times to retry calling an endpoint
CRUD_RETRY_INTERVAL: Optional[int] = 2 # Number of seconds to wait between retries

HEATMAP_GRAVITY_MAX_SENSITIVITY: int = 1000000

Expand Down Expand Up @@ -138,9 +141,15 @@ def assemble_s3_client(cls, v: Optional[str], values: Dict[str, Any]) -> Any:
region_name=values.get("AWS_REGION"),
)

DEFAULT_PROJECT_THUMBNAIL: Optional[str] = "https://assets.plan4better.de/img/goat_new_project_artwork.png"
DEFAULT_LAYER_THUMBNAIL: Optional[str] = "https://assets.plan4better.de/img/goat_new_dataset_thumbnail.png"
DEFAULT_REPORT_THUMBNAIL: Optional[str] = "https://goat-app-assets.s3.eu-central-1.amazonaws.com/logos/goat_green.png"
DEFAULT_PROJECT_THUMBNAIL: Optional[str] = (
"https://assets.plan4better.de/img/goat_new_project_artwork.png"
)
DEFAULT_LAYER_THUMBNAIL: Optional[str] = (
"https://assets.plan4better.de/img/goat_new_dataset_thumbnail.png"
)
DEFAULT_REPORT_THUMBNAIL: Optional[str] = (
"https://goat-app-assets.s3.eu-central-1.amazonaws.com/logos/goat_green.png"
)
ASSETS_URL: Optional[str] = None
THUMBNAIL_DIR_LAYER: Optional[str] = None

Expand All @@ -162,7 +171,7 @@ def set_thumbnail_dir_project(cls, v: Optional[str], values: Dict[str, Any]) ->

MARKER_DIR: Optional[str] = "icons/maki"
MARKER_PREFIX: Optional[str] = "goat-marker-"

class Config:
case_sensitive = True

Expand Down
21 changes: 10 additions & 11 deletions src/core/layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ def __init__(
self.file_path = os.path.join(self.folder_path, "file." + self.file_ending)
else:
self.file_path = os.path.join(
self.folder_path, "file." + FileUploadType.gpkg.value
self.folder_path, "file." + FileUploadType.geojson.value
)

async def _fetch_and_write(self):
Expand Down Expand Up @@ -247,10 +247,11 @@ class OGRExternalServiceFetching:
def __init__(self, url: HttpUrl, output_file: str):
self.url = url

# Initialize output GeoPackage data source
output_driver = ogr.GetDriverByName(OgrDriverType.gpkg.value)
# Initialize output GeoJSON data source
driver_type = OgrDriverType.geojson
output_driver = ogr.GetDriverByName(driver_type)
if output_driver is None:
raise Exception(f"{OgrDriverType.gpkg.value} driver is not available.")
raise Exception(f"{driver_type} driver is not available.")

self.output_data_source = output_driver.CreateDataSource(output_file)
if self.output_data_source is None:
Expand All @@ -262,7 +263,7 @@ def fetch_wfs(self, layer_name: str):
ogr.UseExceptions()

# Initialize WFS data source
wfs_data_source = ogr.Open(str(self.url))
wfs_data_source = ogr.Open(f"WFS:{str(self.url)}")
if wfs_data_source is None:
raise Exception(f"Could not open WFS service at {self.url}")

Expand All @@ -271,11 +272,6 @@ def fetch_wfs(self, layer_name: str):
if input_layer is None:
raise Exception(f"Could not find layer {layer_name} in WFS service.")

# Get geometry column name and type
geom_column = input_layer.GetGeometryColumn()
if not geom_column:
raise Exception("Could not determine geometry column for WFS layer.")

geom_type = input_layer.GetGeomType()
if geom_type == ogr.wkbUnknown:
first_feature = input_layer.GetNextFeature()
Expand All @@ -291,7 +287,6 @@ def fetch_wfs(self, layer_name: str):
layer_name,
srs=input_layer.GetSpatialRef(),
geom_type=geom_type,
options=[f"GEOMETRY_NAME={geom_column}"],
)
if output_layer is None:
raise Exception(
Expand All @@ -308,6 +303,10 @@ def fetch_wfs(self, layer_name: str):
for feature in input_layer:
output_layer.CreateFeature(feature)

# Cleanup
self.output_data_source = None
wfs_data_source = None

ogr.DontUseExceptions()

async def fetch_mvt(self):
Expand Down
4 changes: 4 additions & 0 deletions src/crud/crud_catchment_area.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,8 @@ async def catchment_area(
),
"layer_id": str(layer_id),
"scenario_id": str(params.scenario_id) if params.scenario_id else None,
"street_network_edge_layer_project_id": params.street_network_edge_layer_project_id,
"street_network_node_layer_project_id": params.street_network_node_layer_project_id,
}

await call_routing_endpoint(
Expand Down Expand Up @@ -658,6 +660,8 @@ async def catchment_area(
),
"layer_id": str(layer_id),
"scenario_id": str(params.scenario_id) if params.scenario_id else None,
"street_network_edge_layer_project_id": params.street_network_edge_layer_project_id,
"street_network_node_layer_project_id": params.street_network_node_layer_project_id,
}

await call_routing_endpoint(
Expand Down
21 changes: 21 additions & 0 deletions src/schemas/catchment_area.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from pydantic import BaseModel, Field, validator

from src.core.config import settings
from src.schemas.colors import ColorRangeType
from src.schemas.layer import ToolType
from src.schemas.toolbox_base import (
Expand Down Expand Up @@ -278,6 +279,16 @@ class ICatchmentAreaActiveMobility(BaseModel):
title="Scenario ID",
description="The ID of the scenario that is to be applied on the input layer or base network.",
)
street_network_edge_layer_project_id: Optional[int] = Field(
default=settings.STREET_NETWORK_EDGE_DEFAULT_LAYER_PROJECT_ID,
title="Street Network Edge Layer Project ID",
description="The layer project ID of the street network edge layer.",
)
street_network_node_layer_project_id: Optional[int] = Field(
default=settings.STREET_NETWORK_NODE_DEFAULT_LAYER_PROJECT_ID,
title="Street Network Node Layer Project ID",
description="The layer project ID of the street network node layer.",
)
catchment_area_type: CatchmentAreaTypeActiveMobility = Field(
...,
title="Return Type",
Expand Down Expand Up @@ -470,6 +481,16 @@ class ICatchmentAreaCar(BaseModel):
title="Scenario ID",
description="The ID of the scenario that is to be applied on the input layer or base network.",
)
street_network_edge_layer_project_id: Optional[int] = Field(
default=settings.STREET_NETWORK_EDGE_DEFAULT_LAYER_PROJECT_ID,
title="Street Network Edge Layer Project ID",
description="The layer project ID of the street network edge layer.",
)
street_network_node_layer_project_id: Optional[int] = Field(
default=settings.STREET_NETWORK_NODE_DEFAULT_LAYER_PROJECT_ID,
title="Street Network Node Layer Project ID",
description="The layer project ID of the street network node layer.",
)
catchment_area_type: CatchmentAreaTypeCar = Field(
...,
title="Return Type",
Expand Down

0 comments on commit 9ea3ee0

Please sign in to comment.