Skip to main content

Maps of Sweden in GeoParquet for easy usage.

Project description

swemaps

Maps of Sweden in GeoParquet for easy usage.

The parquets have been created from files published by:

The map data includes counties, municipalities, electoral districts and FA regions. The original geometries have been transformed from SWEREF 99 TM (EPSG:3006) to WGS 84 (EPSG:4326) for better out-of-the-box compatibility with interactive and web-based toolkits such as Folium and Plotly. The column names have also been somewhat sanitized (e.g. KnKod -> kommun_kod).

The package gets you the file path so that you can load it with your prefered tool, for example PyArrow or GeoPandas. An extra convenience function is included to quickly convert a PyArrow Table object to GeoJSON.

Made for Python with inspiration from swemaps2.

Municipalities and counties

Municipalities Counties
municipalities counties

PyArrow example with Plotly

>>> import plotly.express as px
>>> import pyarrow.parquet as pq
>>> import swemaps

# Load the map for the specified type
>>> kommuner = pq.read_table(swemaps.get_path("kommun"))

>>> kommuner.column_names
['kommun_kod', 'kommun', 'geometry']

# The convenience function returns GeoJSON from a PyArrow table object
>>> geojson = swemaps.table_to_geojson(kommuner)

# Here's a dataframe with municipalities and some random values that we can plot
>>> df.head()
shape: (5, 2)
┌──────────┬───────┐
 Kommun    Value 
 ---       ---   
 str       i64   
╞══════════╪═══════╡
 Ale       544   
 Alingsås  749   
 Alvesta   771   
 Aneby     241   
 Arboga    763   
└──────────┴───────┘

# Use Plotly to create a choropleth using the dataframe and GeoJSON
>>> fig = px.choropleth(
        df,
        geojson=geojson,
        color="Value",
        locations="Kommun",
        featureidkey="properties.kommun",
        projection="mercator",
        color_continuous_scale="Viridis",
        fitbounds="locations",
        basemap_visible=False,
    )

You might want to subset the map of municipalities for a specific county or a group of counties. Since the geometry is loaded as a PyArrow table the filter operation is straightforward.

>>> import pyarrow.compute as pc

>>> kommuner.schema 

kommun_kod: string
kommun: string
geometry: binary
  -- field metadata --
  ARROW:extension:metadata: '{"crs":{"$schema":"https://proj.org/schemas/' + 1296
  ARROW:extension:name: 'geoarrow.wkb'
-- schema metadata --
geo: '{"version":"1.1.0","primary_column":"geometry","columns":{"geometry' + 1621

# County code for Skåne is 12
>>> kommuner = kommuner.filter(pc.starts_with(pc.field("kommun_kod"), "12"))

>>> geojson = swemaps.table_to_geojson(kommuner)

You could also use list comprehension on the GeoJSON to filter it.

>>> geojson["features"] = [
        feature
        for feature in geojson["features"]
        if feature["properties"]["kommun_kod"].startswith("12")
        ]

Anyway, now we can plot Skåne.

>>> skane = px.choropleth(
        df,
        geojson=geojson,
        color="Value",
        locations="Kommun",
        featureidkey="properties.kommun",
        projection="mercator",
        color_continuous_scale="Viridis",
        fitbounds="locations",
        basemap_visible=False,
        title="Skåne municipalities"
    )

skane.show()

skåne

GeoPandas and plotnine

Another possibility is to load the GeoParquet into a GeoDataFrame.

>>> import geopandas as gpd

>>> gdf = gpd.read_parquet(swemaps.get_path("lan"))

>>> gdf.head()

  lan_kod                lan                                           geometry
0      01     Stockholms län  MULTIPOLYGON (((17.24034 59.24219, 17.28475 59...
1      03        Uppsala län  MULTIPOLYGON (((17.36606 59.61224, 17.35475 59...
2      04  Södermanlands län  MULTIPOLYGON (((15.95815 58.96497, 15.8613 58....
3      05  Östergötlands län  MULTIPOLYGON (((14.93369 58.13112, 14.89472 58...
4      06     Jönköpings län  MULTIPOLYGON (((14.98311 57.9345, 15.00458 57....

# And with matplotlib installed as well we can have quick look
>>> gdf.plot()

län

For best results with plotnine you can either reproject to SWEREF 99 TM or set the aspect ratio in coord_fixed(). A ratio of around 1.96 to 1.98 should be near optimal.

>>> gdf = gpd.read_parquet(swemaps.get_path("kommun"))

# Insert some random values
>>> gdf["value"] = np.random.randint(1, 600, size=len(gdf["kommun"]))

# Reproject back to SWEREF 99 TM
>>> gdf = gdf.to_crs(epsg=3006)

>>> (
    ggplot(gdf, aes(fill="value"))
    + geom_map(show_legend=False)
    + coord_fixed() # Or skip the reprojection above and set ratio manually here
    + scale_fill_cmap("YlGnBu")
    + theme(
        axis_ticks=element_blank(),
        panel_background=element_rect(fill="white"),
        axis_text_x=element_blank(),
        axis_text_y=element_blank(),
    )
  )
SWEREF 99 TM WGS 84
sweref99tm wgs84

Additional map data

Larger datasets including geometries for electoral districts, RegSO, and DeSO can be manually fetched. Once downloaded the files are cached for efficient reuse.

>>> districts = swemaps.fetch_map("valdistrikt_2022")
>>> districts
PosixPath('/home/stefur/.cache/swemaps-data/v0.2.0/valdistrikt_2022.parquet')

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

swemaps-0.2.2.tar.gz (1.2 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

swemaps-0.2.2-py3-none-any.whl (244.5 kB view details)

Uploaded Python 3

File details

Details for the file swemaps-0.2.2.tar.gz.

File metadata

  • Download URL: swemaps-0.2.2.tar.gz
  • Upload date:
  • Size: 1.2 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.28 {"installer":{"name":"uv","version":"0.9.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for swemaps-0.2.2.tar.gz
Algorithm Hash digest
SHA256 360640b9d38de271e0320552c7e836b63eb233ccd077b184e406e63f28d94a4e
MD5 6ae7802ee820484d6258731634acf489
BLAKE2b-256 07a645810ab26716240e21cb2b83e702c4c47c991a95f230a06d60ee69cc18de

See more details on using hashes here.

File details

Details for the file swemaps-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: swemaps-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 244.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.28 {"installer":{"name":"uv","version":"0.9.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for swemaps-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 42cb3b955b40cbcb3428a2c276914b45e7efeb365745eb3b75cbadfd419e18f9
MD5 8cbfcea52559a57e9e77cd8f54f82ef2
BLAKE2b-256 5e469d037be57317131c42f519cf74f712070a3e740ac92f07a519a52a1028cd

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page