Skip to main content

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 helper function is included to quickly convert a PyArrow table to GeoJSON.

Made for Python with inspiration from swemaps2.

Municipalities and counties

Municipalities Counties
municipalities counties

PyArrow example with Plotly

>>> import pyarrow.parquet as pq
>>> import swemaps

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

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

# This helper function returns GeoJSON from a PyArrow table
>>> geojson = swemaps.pyarrow_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   
└──────────┴───────┘

>>> 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 could also 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
-- schema metadata --
geo: '{"version":"1.0.0","primary_column":"geometry","columns":{"geometry' + 1478

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

>>> geojson = swemaps.pyarrow_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"
    )

fig.show()

skåne

GeoPandas example

You can load the GeoParquet into a GeoDataFrame as well.

>>> 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  POLYGON ((17.36606 59.61224, 17.35475 59.60292...
2      04  Södermanlands  POLYGON ((15.95815 58.96497, 15.86130 58.99856...
3      05  Östergötlands  POLYGON ((14.93369 58.13112, 14.89472 58.08986...
4      06     Jönköpings  POLYGON ((14.98311 57.93450, 15.00458 57.89598...

# 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.0.tar.gz (357.2 kB view details)

Uploaded Source

Built Distribution

swemaps-0.1.0-py3-none-any.whl (199.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: swemaps-0.1.0.tar.gz
  • Upload date:
  • Size: 357.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.2

File hashes

Hashes for swemaps-0.1.0.tar.gz
Algorithm Hash digest
SHA256 1331661256ec071238ab058ffc7dde147db734662a16da571de206d02aed0ee5
MD5 12ad02d07ff3c448de26f5f06d9b7733
BLAKE2b-256 97b311f3293141ae11952b6e5372580140f0d8fc9822d711a2fda5ab00aaacc4

See more details on using hashes here.

File details

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

File metadata

  • Download URL: swemaps-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 199.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.2

File hashes

Hashes for swemaps-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 75340c2d55d3332eabb3683141270183eb5e2efa7583f8122c13616c1f6a1ba0
MD5 ac22729c3d4fb97c59eafc5cc5bf93c4
BLAKE2b-256 8c8b9430967da4336adae81fd9b3c2ebf973ccfeb11d555fea983fe30682bf53

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