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

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for swemaps-0.1.2.tar.gz
Algorithm Hash digest
SHA256 279fa224edc3961cbf51e626caa57e1a269987e1269029f5cbe0980dad7243e9
MD5 a0970012d0a0f509d4958ef38fa299cb
BLAKE2b-256 2bfe22f6c82e33e62b10c0840625e9ba0507af90e3ead3bf68ae25432e44c3c1

See more details on using hashes here.

File details

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

File metadata

  • Download URL: swemaps-0.1.2-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.11.10

File hashes

Hashes for swemaps-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 ceec5f5c0aca9bc2086112d2485bb2d9d81e0a2ca4c84db7b05d49c8919aa9d2
MD5 88de243d3174eb7625a851ac6704de8d
BLAKE2b-256 cd5838f6f71defaed3f2bf2e791ade1628074f69f8ae2702036f824070d178fc

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