-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
46 additions
and
45 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
"""Choropleth module.""" | ||
|
||
from pathlib import Path | ||
|
||
import geopandas as gpd | ||
from shapely import geometry, ops, polygonize | ||
|
||
|
||
def extract_choropleth(all_files: list[Path]): | ||
"""Extract Choropleth polygons from list of LineStrings.""" | ||
all_data = [ | ||
geo | ||
for file in all_files | ||
for geo in gpd.read_file( | ||
file, use_arrow=True, engine="pyogrio" | ||
).geometry.to_list() | ||
] | ||
|
||
return polygonize(ops.linemerge(geometry.MultiLineString(all_data)).geoms) | ||
|
||
|
||
def get_municipalities_choropleth(administrative_path: str, scb_path: str): | ||
admin = Path(administrative_path) | ||
scb = Path(scb_path) | ||
|
||
all_files = [ | ||
admin / "04_riksgrans.geojson", | ||
admin / "05_sjoterritoriets_grans_i_havet.geojson", | ||
admin / "01_kommungrans.geojson", | ||
admin / "02_lansgrans.geojson", | ||
] | ||
|
||
return extract_choropleth(all_files) | ||
|
||
|
||
def get_regions_choropleth(administrative_path: str, scb_path: str): | ||
admin = Path(administrative_path) | ||
scb = Path(scb_path) | ||
|
||
all_files = [ | ||
admin / "04_riksgrans.geojson", | ||
admin / "05_sjoterritoriets_grans_i_havet.geojson", | ||
admin / "02_lansgrans.geojson", | ||
] | ||
|
||
return extract_choropleth(all_files) |