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 Statistics Sweden and The Swedish Agency for Economic and Regional Growth. Maps include counties, municipalities and FA regions. The original geometries have been transformed from SWEREF 99 TM to WGS 84 for better out of the box compatibility with different tools. 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 an Arrow tabular object (such as a PyArrow table) 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 example

Another possibility is to load the GeoParquet into a GeoDataFrame.

>>> import geopandas as gpd

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

>>> gdf.head()

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

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

län

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.1.4.tar.gz (451.9 kB view details)

Uploaded Source

Built Distribution

swemaps-0.1.4-py3-none-any.whl (242.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: swemaps-0.1.4.tar.gz
  • Upload date:
  • Size: 451.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.12

File hashes

Hashes for swemaps-0.1.4.tar.gz
Algorithm Hash digest
SHA256 9279e0fdbb92068d0af843eb848ca912515b3f810b0c64579fed6d64bb3ff741
MD5 25ea7d9c92a1b43c275469fc55c6645d
BLAKE2b-256 12eb325afdcf94d32c00f3339dafd07b056c67ea4d6040527e29595b82b284a5

See more details on using hashes here.

File details

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

File metadata

  • Download URL: swemaps-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 242.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.12

File hashes

Hashes for swemaps-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 cc447e5ca1a777bfa4e139179e92b9bab368a6f8ed2c4726f5e32339f7415a74
MD5 f5edb1a1cc0547bb8b5b6599dc065a30
BLAKE2b-256 1266105db1ee83e831379c9319a4b6194bbb35385f7cecbb30f2ea15eaa13bc0

See more details on using hashes here.

Supported by

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