Map of Norway #3443
-
Hi, first, I must say that I am rather inexperienced at programming, so, perhaps this question is very simple to some of you. I am trying to create a combined map where I want to use a map of Norway as base map and then I would add some data that I am processing on top of that. Thus far I have used the following code as basis background = alt.topo_feature(data.world_110m.url, 'countries') Create a base Altair mapbase_map = alt.Chart(background).mark_geoshape( However, it does show the entire world map and not just Norway. So my question is if someone could help me get just Norway as base map and not the entire World map? Thanks a lot in advance. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Have a look to the geoshape docs here: https://altair-viz.github.io/user_guide/marks/geoshape.html And parts of this blog on 'Engaging geovisualisations with Vega-Altair' is also useful: https://mattijn.github.io/posts/2023-03-07-talk-geopython-2023/. Example for Norway: import altair as alt
import pandas as pd
import geopandas as gpd
url = "https://naciscdn.org/naturalearth/110m/cultural/ne_110m_admin_0_countries.zip"
gdf = gpd.read_file(url) # zipped shapefile
gdf = gdf[["NAME", "CONTINENT", "POP_EST", 'geometry']]
# gdf.head()
# gdf_NO = gdf[gdf.NAME == 'Norway']
extent_roi = gdf.query("NAME == 'Norway'")
xmin, ymin, xmax, ymax = extent_roi.total_bounds # fit object should be a GeoJSON-like Feature or FeatureCollection
extent_roi_feature = {
"type": "Feature",
"geometry": {"type": "Polygon",
"coordinates": [[
[xmax, ymax],
[xmax, ymin],
[xmin, ymin],
[xmin, ymax],
[xmax, ymax]]]},
"properties": {}
}
chart_base = alt.Chart(gdf).mark_geoshape(clip=True, fill='lightgray', stroke='white', strokeWidth=0.5).project(
fit=extent_roi_feature,
type='equalEarth'
)
chart_base df = pd.DataFrame({'city': ['Oslo'], 'lat': [59.91], 'lon': [10.75]})
gdf_pts = gpd.GeoDataFrame(df, geometry=gpd.points_from_xy(df.lon, df.lat), crs="EPSG:4326")
gdf_pts chart_pts = alt.Chart(gdf_pts).mark_geoshape().encode(
fill='city'
)
chart_base + chart_pts chart_text = alt.Chart(gdf_pts).mark_text(
align='right', dy=-10
).encode(
longitude="lon", latitude="lat", text="city"
)
chart_base + chart_pts + chart_text Good luck with your project! |
Beta Was this translation helpful? Give feedback.
-
Thanks a lot for your prompt and useful reply, |
Beta Was this translation helpful? Give feedback.
Have a look to the geoshape docs here: https://altair-viz.github.io/user_guide/marks/geoshape.html
And parts of this blog on 'Engaging geovisualisations with Vega-Altair' is also useful: https://mattijn.github.io/posts/2023-03-07-talk-geopython-2023/.
Example for Norway:
# fit object should be a Geo…