Skip to content

Commit

Permalink
Merge pull request #421 from agoenergy/joAschauer/issue419
Browse files Browse the repository at this point in the history
scatterpoint maps
  • Loading branch information
markushal authored Jun 6, 2024
2 parents 77f26af + 4620788 commit 56c6bcc
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 19 deletions.
52 changes: 33 additions & 19 deletions app/plot_functions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# -*- coding: utf-8 -*-
"""Functions for plotting input data and results (cost_data)."""
import json
from pathlib import Path
from typing import Literal

Expand Down Expand Up @@ -205,12 +204,14 @@ def _choropleth_map_world(
if custom_data_func_kwargs is None:
custom_data_func_kwargs = {}
df = remove_subregions(api=api, df=df, country_name=st.session_state["country"])
fig = px.choropleth(
fig = px.scatter_geo(
locations=df.index,
locationmode="country names",
color=df[color_col],
custom_data=custom_data_func(df, **custom_data_func_kwargs),
color_continuous_scale=agora_continuous_color_scale(),
size=[15] * len(df.index),
opacity=1,
)
return fig

Expand All @@ -226,35 +227,49 @@ def _choropleth_map_deep_dive_country(
if custom_data_func_kwargs is None:
custom_data_func_kwargs = {}
# subsetting 'df' for the selected deep dive country
df = select_subregions(df, deep_dive_country)
# missing value removal necessary for wind offshore
df = select_subregions(df, deep_dive_country).dropna(subset=color_col)
# need to calculate custom data befor is03166 column is appended.
hover_data = custom_data_func(df, **custom_data_func_kwargs)
# get dataframe with info about iso 3166-2 codes and map them to res_costs
ddc_info = api.get_dimension("region")
df["iso3166_code"] = df.index.map(
pd.Series(ddc_info["iso3166_code"], index=ddc_info["region_name"])
)

geojson_file = (
Path(__file__).parent.parent.resolve()
/ "data"
/ f"{deep_dive_country.lower().replace(' ', '_')}_subregions.geojson"
# load representative points data
lon_lat = pd.read_csv(
(
Path(__file__).parent.parent.resolve()
/ "data"
/ "subregion_representative_points.csv"
)
)
with geojson_file.open("r", encoding="utf-8") as f:
subregion_shapes = json.load(f)
# merge points to data
df = df.merge(lon_lat, left_on="iso3166_code", right_on="iso_3166_2")

fig = px.choropleth(
locations=df["iso3166_code"],
featureidkey="properties.iso_3166_2",
fig = px.scatter_geo(
lon=df["lon"],
lat=df["lat"],
color=df[color_col],
geojson=subregion_shapes,
custom_data=hover_data,
color_continuous_scale=agora_continuous_color_scale(),
size=[15] * len(df.index),
opacity=1,
)

bboxes = {
"Argentina": (-73.4154357571, -55.25, -53.628348965, -21.8323104794),
"Morocco": (-17.0204284327, 21.4207341578, -1.12455115397, 35.7599881048),
"South Africa": (16.3449768409, -34.8191663551, 32.830120477, -22.0913127581),
}

bbox = bboxes[deep_dive_country]
pad = 3
fig.update_geos(
fitbounds="locations",
visible=True,
center_lon=(bbox[0] + bbox[2]) / 2.0,
center_lat=(bbox[1] + bbox[3]) / 2.0,
lonaxis_range=[bbox[0] - pad, bbox[2] + pad],
lataxis_range=[bbox[1] - pad, bbox[3] + pad],
)
return fig

Expand All @@ -281,10 +296,9 @@ def _set_map_layout(fig: go.Figure, colorbar_title: str) -> go.Figure:
"""
# update layout:
fig.update_geos(
showcountries=True, # Show country borders
resolution=50,
showcountries=False, # do not show country borders
showcoastlines=True, # Show coastlines
countrycolor="black", # Set default border color for other countries
countrywidth=0.2, # Set border width
coastlinewidth=0.2, # coastline width
coastlinecolor="black", # coastline color
showland=True, # show land areas
Expand Down
46 changes: 46 additions & 0 deletions data/subregion_representative_points.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
iso_3166_2,lon,lat
MA-11,-11.487535149205273,26.291022599999998
MA-01,-5.434766372913305,35.2181678
MA-02,-2.3906063870173955,33.5811814
MA-08,-5.58317463160672,31.28770725
MA-09,-8.157695785778756,29.7357752
MA-10,-10.084427221839995,28.385906
MA-06,-7.799432071783302,33.065160649999996
MA-07,-8.306409084756796,31.806335949999998
MA-12,-14.741994773441643,22.735386249999998
MA-04,-6.328725938578254,34.08533075
MA-03,-4.847581884998955,33.74727775
MA-05,-6.280445836675398,32.4092902
AR-E,-59.41391734740016,-32.09481027848068
AR-A,-63.2488630052977,-24.188186947408994
AR-Y,-66.05131162400237,-23.19339019145417
AR-P,-60.056795466789765,-24.694332167145802
AR-N,-54.408009579418504,-26.838980814499898
AR-H,-60.69169607218846,-26.04543832741686
AR-W,-57.80329050740136,-29.00667836190994
AR-K,-67.51184361556847,-27.649319756801333
AR-F,-66.86724287983822,-29.835207004049977
AR-J,-68.76400612946446,-30.369850969333534
AR-M,-68.47196659910384,-34.75436330149996
AR-Q,-69.55620607085012,-38.61386932399996
AR-U,-68.47608492458708,-43.999489582499905
AR-R,-66.42178269981838,-39.78144744254544
AR-Z,-70.40714935693845,-49.193291924999926
AR-V,-68.09468684709897,-53.841798372999904
AR-B,-60.113853483913914,-37.10019296649992
AR-C,-58.44119878492826,-34.642826064273805
AR-S,-60.97677432423949,-31.164195651608026
AR-T,-65.39223122835818,-27.06493682212667
AR-G,-63.37956470637425,-27.840677585851893
AR-D,-65.95764785447187,-33.936842950791544
AR-L,-65.8186469775226,-37.06892567313133
AR-X,-63.68163713228595,-32.214312432772545
ZA-NC,22.022372549742485,-28.848704728999934
ZA-NL,30.414604596802526,-28.941989841499947
ZA-FS,26.606093929174868,-28.668094377346847
ZA-EC,26.5770656739886,-32.091735934615684
ZA-LP,29.333422657090793,-23.779038391499938
ZA-NW,25.24337419152684,-26.37090419793683
ZA-MP,30.160392889928794,-25.74002980499995
ZA-WC,22.14966623331297,-32.64267852138539
ZA-GT,28.020037353796795,-26.0096414140351

0 comments on commit 56c6bcc

Please sign in to comment.