diff --git a/cloud_functions/analysis/.gcloudignore b/cloud_functions/analysis/.gcloudignore index 28effcb3..55d7f4de 100644 --- a/cloud_functions/analysis/.gcloudignore +++ b/cloud_functions/analysis/.gcloudignore @@ -1,4 +1,7 @@ -dockerfile +Dockerfile docker-compose.yml *.env -init.sh \ No newline at end of file +init.sh +.gitignore +.dockerignore +README.md diff --git a/cloud_functions/analysis/main.py b/cloud_functions/analysis/main.py index ece76637..f2b47d62 100644 --- a/cloud_functions/analysis/main.py +++ b/cloud_functions/analysis/main.py @@ -45,6 +45,7 @@ def index(request): # Set CORS headers for the main request headers = {"Access-Control-Allow-Origin": "*"} + geometry = ({**request.args, **request.get_json()}).get("geometry", None) if not geometry: raise ValueError("geometry is required") diff --git a/cloud_functions/analysis/requirements.txt b/cloud_functions/analysis/requirements.txt index 43ce204e..d9e91e3b 100644 --- a/cloud_functions/analysis/requirements.txt +++ b/cloud_functions/analysis/requirements.txt @@ -1,4 +1,4 @@ functions-framework==3.* -sqlalchemy +sqlalchemy==2.* pg8000 geojson \ No newline at end of file diff --git a/cloud_functions/analysis/src/analysis.py b/cloud_functions/analysis/src/analysis.py index bfed5ad0..eb6e131d 100644 --- a/cloud_functions/analysis/src/analysis.py +++ b/cloud_functions/analysis/src/analysis.py @@ -47,10 +47,13 @@ def get_locations_stats(db: sqlalchemy.engine.base.Engine, geojson: JSON) -> dic with db.connect() as conn: stmt = sqlalchemy.text( """ - with user_data as (select ST_GeomFromGeoJSON(:geometry) as geom), - user_data_stats as (select *, round((st_area(st_transform(geom, 'ESRI:54009'))/1e6)) user_area_km2 from user_data) - select area_km2, iso_sov1, iso_sov2, iso_sov3, round((st_area(st_transform(st_makevalid(st_intersection(the_geom, user_data_stats.geom)), 'ESRI:54009'))/1e6)) portion_area_km2, user_data_stats.user_area_km2 from data.eez_minus_mpa emm, user_data_stats - where st_intersects(the_geom, user_data_stats.geom) + with user_data as (select data.ST_GeomFromGeoJSON(:geometry) as geom), + user_data_stats as (select *, round((data.st_area(data.st_transform(geom,'+proj=longlat +datum=WGS84 +no_defs +type=crs', '+proj=moll +lon_0=0 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs +type=crs'))/1e6)) user_area_km2 from user_data) + select area_km2, iso_sov1, iso_sov2, iso_sov3, + round((data.st_area(data.st_transform(data.st_makevalid(data.st_intersection(the_geom, user_data_stats.geom)),'+proj=longlat +datum=WGS84 +no_defs +type=crs', '+proj=moll +lon_0=0 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs +type=crs'))/1e6)) portion_area_km2, + user_data_stats.user_area_km2 + from data.eez_minus_mpa emm, user_data_stats + where data.st_intersects(the_geom, user_data_stats.geom) """ ) data_response = conn.execute(