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

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: swemaps-0.1.5.tar.gz
  • Upload date:
  • Size: 451.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.6

File hashes

Hashes for swemaps-0.1.5.tar.gz
Algorithm Hash digest
SHA256 f7a023a786b34f14986dc885722e6ad3ac138ee39eaebe20493eed15d07867df
MD5 e341ff697372cdb9ef29ab18501ea00b
BLAKE2b-256 54c8a489cbeb703e5f621601497ee5835eb8d37dab9c1707cbdf0e812adb0c57

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for swemaps-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 ab9d860486ce6e3e0dea0b8b9cad752bd084a1fcf5c10a9819ca4fb1472b2863
MD5 c37ba36e3cd05e0ac6dea500202681a7
BLAKE2b-256 0a2e5023fe19c59e50b3fecb37039006ff8008330e1ab9008e78e34f2eaf0639

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