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

feat(building-comparison): add map with clipped AOI #758

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
102 changes: 80 additions & 22 deletions ohsome_quality_api/indicators/building_comparison/indicator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
from string import Template

import geojson
import plotly.graph_objects as pgo
import plotly.express as px
import plotly.subplots as sp
from dateutil import parser
from geojson import Feature, MultiPolygon, Polygon
from numpy import mean
Expand Down Expand Up @@ -111,33 +112,90 @@ def calculate(self) -> None:

def create_figure(self) -> None:
if self.result.label == "undefined" and self.check_major_edge_cases():
logging.info("Result is undefined. Skipping figure creation.")
return
fig = pgo.Figure()
fig.add_trace(
pgo.Bar(
name="OSM",
x=["OSM"],
y=[round(self.area_osm, 2)],
marker_color=Color.GREEN.value,
logging.info(
"Result is undefined and major edge case is present. "
+ "Skipping figure creation."
)
return

fig = sp.make_subplots(
rows=1,
cols=2,
subplot_titles=("Area", "Coverage"),
specs=[[{"type": "xy"}, {"type": "mapbox"}]],
)
for name, area in self.area_references.items():
fig.add_trace(
pgo.Bar(
name=name,
x=[name],
y=[round(area, 2)],
marker_color=Color.PURPLE.value,
)
)

fig.update_layout(title_text=("Building Comparison"), showlegend=True)
fig.update_yaxes(title_text="Building Area [km²]")
fig.update_xaxes(title_text="Datasets")
x_list = ["OSM"]
y_list = [round(self.area_osm, 2)]
for reference_set in ["EUBUCCO"]:
x_list.append(reference_set)
y_list.append(round(self.area_references[reference_set], 2))

color_list = [Color.RED.value, Color.BLUE.value]

trace1 = px.bar(
x=x_list,
y=y_list,
color=x_list,
color_discrete_sequence=color_list,
)

coords = self.feature["geometry"]["coordinates"]
if self.feature["geometry"]["type"] == "Polygon":
coords = [coords]
lon = [
innermost[0]
for outermost in coords
for inner in outermost
for innermost in inner
]
lat = [
innermost[1]
for outermost in coords
for inner in outermost
for innermost in inner
]
trace2 = px.choropleth_mapbox(
{"name": [self.feature["properties"]["name"]], "area": self.area_osm},
geojson=self.feature,
locations="name",
featureidkey="properties.name",
color=[self.coverage["EUBUCCO"]],
color_continuous_scale="Viridis",
range_color=(0, 1),
)
# Create a custom hover template
hover_template = "<b>Name</b>: %{location}<br><b>EUBUCCO Coverage</b>: %{z}<br>"
trace2.update_traces(hovertemplate=hover_template)

#
for trace, col in zip([trace1, trace2], range(1, 3)):
for data in trace.data:
fig.add_trace(data, row=1, col=col)

fig.update_layout(
mapbox_style="open-street-map",
mapbox_bounds={
"west": min(lon) - 0.5,
"east": max(lon) + 0.5,
"south": min(lat) - 0.5,
"north": max(lat) + 0.5,
},
showlegend=False,
coloraxis=dict(showscale=False),
)
fig.update(
layout_coloraxis_showscale=False,
)
raw = fig.to_dict()
raw["layout"].pop("template") # remove boilerplate
for i in range(len(raw["data"])):
if i < (len(raw["data"]) - 1):
raw["data"][i]["x"] = raw["data"][i]["x"].tolist()
raw["data"][i]["y"] = raw["data"][i]["y"].tolist()
else:
raw["data"][i]["z"] = raw["data"][i]["z"].tolist()
raw["data"][i]["locations"] = raw["data"][i]["locations"].tolist()
self.result.figure = raw

def check_major_edge_cases(self) -> bool:
Expand Down