Skip to content

Commit

Permalink
Merge pull request #120 from Vizzuality/hotfix/cloudfunction/shcema_p…
Browse files Browse the repository at this point in the history
…ostgis

Hotfix/cloudfunction/shcema postgis
  • Loading branch information
aagm authored Dec 15, 2023
2 parents a658c5c + 849b3bb commit b6e7e03
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
7 changes: 5 additions & 2 deletions cloud_functions/analysis/.gcloudignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
dockerfile
Dockerfile
docker-compose.yml
*.env
init.sh
init.sh
.gitignore
.dockerignore
README.md
1 change: 1 addition & 0 deletions cloud_functions/analysis/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion cloud_functions/analysis/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
functions-framework==3.*
sqlalchemy
sqlalchemy==2.*
pg8000
geojson
11 changes: 7 additions & 4 deletions cloud_functions/analysis/src/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit b6e7e03

Please sign in to comment.