Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create bounding box area calculation process #301

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,14 @@ dependencies:
- botocore>=1.33.10 # Minimum version that is compatible with python >= 3.10 is botocore>=1.13.0
- scrapy==2.11.1
- gevent==23.9.1
- pywps==4.6.0
- pip:
- flask_swagger_ui==4.11.1
- geovoronoi==0.4.0
- geoapis==0.3.2
- celery==5.2.7
- gunicorn==20.1.0
- git+https://github.com/LukeParky/[email protected]
- pywps==4.6.0 # pywps is available in conda but our fork of OWSLib is required for bounding boxes
- git+https://github.com/GeospatialResearch/OWSLib.git

prefix:
55 changes: 55 additions & 0 deletions otakaro/area.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# # -*- coding: utf-8 -*-
"""Defines PyWPS WebProcessingService processes for calculating area of geometries."""

from geopandas import GeoDataFrame
from pywps import BoundingBoxInput, Process, LiteralOutput, WPSRequest
from pywps.response.execute import ExecuteResponse
from shapely import box


class BoundingBoxAreaProcess(Process):
"""Class representing a WebProcessingService process for calculating area of a bounding box"""

def __init__(self) -> None:
"""Define inputs and outputs of the WPS process, and assogm process handler."""
# Create bounding box WPS inputs.
inputs = [BoundingBoxInput('bboxin', 'box in', crss=['epsg:4326'])]
# Create area WPS outputs.
outputs = [LiteralOutput('area', 'Area', data_type='string')]

# Initialise the process.
super().__init__(
self._handler,
identifier='area',
title="Calculate the area of the polygon.",
inputs=inputs,
outputs=outputs,
)
Comment on lines +13 to +27
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def __init__(self) -> None:
"""Define inputs and outputs of the WPS process, and assogm process handler."""
# Create bounding box WPS inputs.
inputs = [BoundingBoxInput('bboxin', 'box in', crss=['epsg:4326'])]
# Create area WPS outputs.
outputs = [LiteralOutput('area', 'Area', data_type='string')]
# Initialise the process.
super().__init__(
self._handler,
identifier='area',
title="Calculate the area of the polygon.",
inputs=inputs,
outputs=outputs,
)
def __init__(self) -> None:
"""Define inputs and outputs of the WPS process, and assogm process handler."""
# Create bounding box WPS inputs
inputs = [BoundingBoxInput('bboxin', 'box in', crss=['epsg:4326'])]
# Create area WPS outputs
outputs = [LiteralOutput('area', 'Area', data_type='string')]
# Initialise the process
super().__init__(
self._handler,
identifier='area',
title="Calculate the area of the polygon.",
inputs=inputs,
outputs=outputs,
)
Suggested change
def __init__(self) -> None:
"""Define inputs and outputs of the WPS process, and assogm process handler."""
# Create bounding box WPS inputs.
inputs = [BoundingBoxInput('bboxin', 'box in', crss=['epsg:4326'])]
# Create area WPS outputs.
outputs = [LiteralOutput('area', 'Area', data_type='string')]
# Initialise the process.
super().__init__(
self._handler,
identifier='area',
title="Calculate the area of the polygon.",
inputs=inputs,
outputs=outputs,
)
def __init__(self) -> None:
"""Define inputs and outputs of the WPS process, and assogm process handler."""
# Create bounding box WPS inputs.
inputs = [BoundingBoxInput('bboxin', 'box in', crss=['epsg:4326'])]
# Create area WPS outputs.
outputs = [LiteralOutput('area', 'Area', data_type='string')]
# Initialise the process.
super().__init__(
self._handler,
identifier='area',
title="Calculate the area of the polygon.",
inputs=inputs,
outputs=outputs,
)


@staticmethod
def _handler(request: WPSRequest, response: ExecuteResponse) -> None:
Comment on lines +29 to +30
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@staticmethod
def _handler(request: WPSRequest, response: ExecuteResponse) -> None:
def _handler(self, request: WPSRequest, response: ExecuteResponse) -> None:

"""
Process handler for calculating bounding box area.

Parameters
----------
request : WPSRequest
The WPS request, containing input parameters.
response : ExecuteResponse
The WPS response, containing output data.
"""
# Get coordinates from bounding box input
bounding_box_input = request.inputs['bboxin'][0]
ymin, xmin = bounding_box_input.ll # lower left
ymax, xmax = bounding_box_input.ur # upper right

# Form bounding box into standard shapely.box
bounding_box = box(xmin, ymin, xmax, ymax)
# Create GeoDataFrame with unit of measurement in metres.
gdf = GeoDataFrame(index=[0], crs="epsg:4326", geometry=[bounding_box]).to_crs(epsg=2193)

# Calculate area in square metres
area = gdf.geometry[0].area
# Format area
display_area = f"{area:.0f} m²"
response.outputs['area'].data = display_area
4 changes: 3 additions & 1 deletion otakaro/blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@

from src.check_celery_alive import check_celery_alive
from otakaro import tasks
from otakaro.area import BoundingBoxAreaProcess
from otakaro.pollution_model.medusa_process_service import MedusaProcessService

otakaro_blueprint = Blueprint('otakaro', __name__)

processes = [
MedusaProcessService()
MedusaProcessService(),
BoundingBoxAreaProcess(),
]

process_descriptor = {process.identifier: process.abstract for process in processes}
Expand Down
7 changes: 7 additions & 0 deletions terriajs/catalog.json
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,13 @@
"url": "$BACKEND_URL/wps",
"identifier": "medusa",
"storeSupported": true
},
{
"type": "wps",
"name": "Area",
"description": "Calculate the area of the polygon.",
"url": "$BACKEND_URL/wps",
"identifier": "area"
}
]
}
Expand Down
Loading