Skip to content

Commit

Permalink
f
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiasschaub committed Jun 29, 2021
1 parent 495a05a commit 3adef2e
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 35 deletions.
35 changes: 21 additions & 14 deletions workers/ohsome_quality_analyst/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,29 +63,32 @@ def int_or_str_param_type(value: str) -> Union[int, str]:
raise ValueError("Given parameter is not a valid integer or string")


async def load_bpolys(bpolys: str) -> Union[Polygon, MultiPolygon]:
async def load_bpolys(bpolys: str) -> Feature:
"""Load as GeoJSON object, validate and check size of bpolys"""
# TODO: Return API response with error message

async def check_geom_size(geom):
if await db_client.get_area_of_bpolys(geom) > GEOM_SIZE_LIMIT:
raise ValueError(
"Input GeoJSON Object is too big. "
+ "The area should be less than {0} sqkm.".format(GEOM_SIZE_LIMIT)
)

bpolys = geojson.loads(bpolys)

if bpolys.is_valid is False:
raise ValueError("Input geometry is not valid")

if isinstance(bpolys, Feature):
bpolys = bpolys["geometry"]

if isinstance(bpolys, (Polygon, MultiPolygon)) is False:
elif isinstance(bpolys, Feature):
await check_geom_size(bpolys.geometry)
return bpolys
elif isinstance(bpolys, (Polygon, MultiPolygon)):
await check_geom_size(bpolys)
return Feature(geometry=bpolys)
else:
raise ValueError(
"Input GeoJSON Objects have to be of type Feature, Polygon or MultiPolygon"
)

if await db_client.get_area_of_bpolys(bpolys) > GEOM_SIZE_LIMIT:
raise ValueError(
"Input GeoJSON Object is too big. "
+ "The area should be less than {0} sqkm.".format(GEOM_SIZE_LIMIT)
)
return bpolys


@app.get("/indicator/{name}")
async def get_indicator(
Expand Down Expand Up @@ -184,7 +187,11 @@ async def _fetch_report(
feature_id = int_or_str_param_type(feature_id)

report = await oqt.create_report(
name, bpolys=bpolys, dataset=dataset, feature_id=feature_id, fid_field=fid_field
name,
feature=bpolys,
dataset=dataset,
feature_id=feature_id,
fid_field=fid_field,
)

response = empty_api_response()
Expand Down
26 changes: 10 additions & 16 deletions workers/ohsome_quality_analyst/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def create_indicator(
oqt.create_indicator(
indicator_name,
layer_name,
bpolys=feature.geometry,
feature=feature,
feature_id=feature_id,
dataset=dataset_name,
fid_field=fid_field,
Expand All @@ -156,7 +156,7 @@ def create_indicator(
oqt.create_indicator(
indicator_name,
layer_name,
bpolys=None,
feature=None,
feature_id=feature_id,
dataset=dataset_name,
fid_field=fid_field,
Expand All @@ -166,12 +166,9 @@ def create_indicator(
if outfile:
if fid_field is None:
fid_field = DATASETS[dataset_name]["default"]
feature_collection = asyncio.run(
db_client.get_bpolys_from_db(dataset_name, feature_id, fid_field)
)
for feature in feature_collection.features:
feature = update_features_indicator(feature, indicator)
write_geojson(outfile, feature_collection)
feature = asyncio.run(db_client.get_region_from_db(feature_id, fid_field))
feature = update_features_indicator(feature, indicator)
write_geojson(outfile, feature)
else:
click.echo(indicator.metadata)
click.echo(indicator.result)
Expand Down Expand Up @@ -203,7 +200,7 @@ def create_report(
report = asyncio.run(
oqt.create_report(
report_name,
bpolys=feature.geometry,
feature=feature,
dataset=dataset_name,
feature_id=feature_id,
fid_field=fid_field,
Expand All @@ -221,7 +218,7 @@ def create_report(
report = asyncio.run(
oqt.create_report(
report_name,
bpolys=None,
feature=None,
dataset=dataset_name,
feature_id=feature_id,
fid_field=fid_field,
Expand All @@ -231,12 +228,9 @@ def create_report(
if outfile:
if fid_field is None:
fid_field = DATASETS[dataset_name]["default"]
feature_collection = asyncio.run(
db_client.get_bpolys_from_db(dataset_name, feature_id, fid_field)
)
for feature in feature_collection.features:
feature = update_features_report(feature, report)
write_geojson(outfile, feature_collection)
feature = asyncio.run(db_client.get_region_from_db(feature_id, fid_field))
feature = update_features_report(feature, report)
write_geojson(outfile, feature)
else:
click.echo(report.metadata)
click.echo(report.result)
Expand Down
2 changes: 1 addition & 1 deletion workers/tests/integrationtests/test_api_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def setUp(self):
infile = os.path.join(
os.path.dirname(os.path.abspath(__file__)),
"fixtures",
"heidelberg-altstadt-feature.g1ojson",
"heidelberg-altstadt-feature.geojson",
)
with open(infile, "r") as f:
self.bpolys = geojson.load(f)
Expand Down
8 changes: 4 additions & 4 deletions workers/tests/unittests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import os
import unittest

from geojson import MultiPolygon, Polygon
from geojson import Feature

from ohsome_quality_analyst.api import int_or_str_param_type, load_bpolys

Expand Down Expand Up @@ -31,7 +31,7 @@ def test_load_bpolys_size(self):
bpolys = file.read()
bpolys = asyncio.run(load_bpolys(bpolys))
self.assertTrue(bpolys.is_valid)
self.assertIsInstance(bpolys, (Polygon, MultiPolygon))
self.assertIsInstance(bpolys, Feature)

# Error should be raised
infile = os.path.join(
Expand Down Expand Up @@ -65,7 +65,7 @@ def test_load_bpolys_feature(self):
bpolys = f.read()
bpolys = asyncio.run(load_bpolys(bpolys))
self.assertTrue(bpolys.is_valid)
self.assertIsInstance(bpolys, (Polygon, MultiPolygon))
self.assertIsInstance(bpolys, Feature)

def test_load_bpolys_geometry(self):
infile = os.path.join(
Expand All @@ -77,7 +77,7 @@ def test_load_bpolys_geometry(self):
bpolys = f.read()
bpolys = asyncio.run(load_bpolys(bpolys))
self.assertTrue(bpolys.is_valid)
self.assertIsInstance(bpolys, (Polygon, MultiPolygon))
self.assertIsInstance(bpolys, Feature)

def test_int_or_str_param_type(self):
self.assertIsInstance(int_or_str_param_type("1"), int)
Expand Down

0 comments on commit 3adef2e

Please sign in to comment.