Skip to content

Commit

Permalink
set SHDI values for cells outside of intersection (#362)
Browse files Browse the repository at this point in the history
* set SHDI values for cells outside of intersection

to the mean of the SHDI value of all other cells..
  • Loading branch information
matthiasschaub authored Jun 23, 2022
1 parent 11ef4d4 commit 05f20d5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
3 changes: 2 additions & 1 deletion workers/ohsome_quality_analyst/geodatabase/select_shdi.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ WITH bpoly AS (
)
SELECT
SUM(ST_Area (ST_Intersection (shdi.geom, bpoly.geom)::geography) / ST_Area
(bpoly.geom::geography) * shdi.shdi) AS shdi
(bpoly.geom::geography) * shdi.shdi) AS shdi,
rownumber as rownumber
FROM
shdi,
bpoly
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ async def preprocess(self) -> None:
item["result"][0]["value"] for item in query_results["groupByResult"]
]
# Get covariates (input parameters or X)
shdi = await db_client.get_shdi(hex_cells)
ghs_pop = raster_client.get_zonal_stats(
hex_cells,
get_raster_dataset("GHS_POP_R2019A"),
Expand All @@ -109,7 +108,7 @@ async def preprocess(self) -> None:
get_raster_dataset("VNL"),
stats=["sum"],
)
self.covariates["shdi"] = [i["shdi"] for i in shdi]
self.covariates["shdi"] = await get_shdi(hex_cells)
self.covariates["vnl"] = [i["sum"] for i in vnl]
self.covariates["ghs_pop"] = [i["sum"] or 0 for i in ghs_pop]
self.covariates["ghs_pop_density"] = [
Expand Down Expand Up @@ -203,7 +202,7 @@ def create_figure(self) -> None:
label="Weighted Average: {0}%".format(int(self.result.value * 100)),
)
ax.legend(loc="lower center", bbox_to_anchor=(0.5, -0.45))
# has to be excuted after "major formatter setting"
# has to be executed after "major formatter setting"
plt.xlim(0, 1)
plt.ylim(0, 10)
img_data = StringIO()
Expand Down Expand Up @@ -271,3 +270,21 @@ async def get_hex_cells(feature: Feature) -> FeatureCollection:
if feature_collection["features"] is None:
raise HexCellsNotFoundError
return feature_collection


async def get_shdi(featurecollection: FeatureCollection) -> list:
"""Get SHDI values for each feature.
If feature does not intersect with the SHDI geometry take the mean of all present
SHDI values.
"""
records = await db_client.get_shdi(featurecollection)
if len(records) == len(featurecollection):
return [r["shdi"] for r in records]
else:
# Not every feature has a SHDI value
default = np.mean([r["shdi"] for r in records]) # Default value
shdi = [default for _ in featurecollection.features] # List of default values
for r in records:
shdi[r["rownumber"] - 1] = r["shdi"] # Overwrite default values
return shdi
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from ohsome_quality_analyst.indicators.building_completeness.indicator import (
BuildingCompleteness,
get_hex_cells,
get_shdi,
get_smod_class_share,
)
from ohsome_quality_analyst.utils.exceptions import HexCellsNotFoundError
Expand Down Expand Up @@ -101,6 +102,11 @@ def test_get_hex_cells_not_found(self):
with self.assertRaises(HexCellsNotFoundError):
asyncio.run(get_hex_cells(feature))

def test_get_shdi(self):
result = asyncio.run(get_shdi(FeatureCollection(features=[self.feature])))
self.assertIsInstance(result, list)
self.assertEqual(len(result), 1)


if __name__ == "__main__":
unittest.main()

0 comments on commit 05f20d5

Please sign in to comment.