Skip to content

Commit

Permalink
Work in progress, simplify extraction of all content
Browse files Browse the repository at this point in the history
  • Loading branch information
mgcth committed Apr 25, 2024
1 parent 9cb11e8 commit 45bc04f
Show file tree
Hide file tree
Showing 15 changed files with 309 additions and 443 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ authors = [
{ name = "Mladen Gibanica", email = "[email protected]" },
]
requires-python = ">=3.9"
dependencies = ["geopandas ~= 0.14", "pyogrio ~= 0.7.0", "pyarrow ~= 14.0"]
dependencies = ["geopandas ~= 0.14", "pyogrio ~= 0.7", "pyarrow ~= 16.0", "unidecode ~= 1.3"]

[project.optional-dependencies]
lint = [
Expand Down
107 changes: 107 additions & 0 deletions scripts/extract_geojson.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
"""Extract GEOJson from GPKG files."""

import glob
import logging
from multiprocessing import Pool, set_start_method

import fiona
import geopandas as gpd
import pandas as pd
from lantmateriet.config import Config50
from unidecode import unidecode

WORKERS = 8

set_start_method("fork")
logger = logging.getLogger(__name__)
config = Config50()


def read_unique_names(file: str, layer: str, field: str) -> list[str]:
"""Read unique names from specified field in file."""
return sorted(
list(
set(
gpd.read_file(
file,
use_arrow=True,
include_fields=[field],
ignore_geometry=True,
layer=layer,
)[field]
)
)
)


def read_first_entry(file: str, layer: str) -> gpd.GeoDataFrame:
"""Read info from file."""
return gpd.read_file(file, use_arrow=True, layer=layer, rows=1)


def normalise_item_names(item_names: list[str]) -> dict[str, str]:
"""Normalise item names to save format."""
return {
x: "{:02d}_".format(i + 1)
+ unidecode(x.lower())
.replace(" ", "_")
.replace("-", "")
.replace(",", "")
.replace("/", "_")
for i, x in enumerate(item_names)
}


def extract_geojson(file: str, layer: str):
"""Extract and save geojson files."""
print(f"Working on {file} - {layer}")
field = "objekttyp"

if "text" in file or "text" in layer:
field = "texttyp"

file_names = read_unique_names(file, layer, field)
normalised_names = normalise_item_names(file_names)
geometry_type = type(read_first_entry(file, layer).geometry[0])
geometry_object = config.file_geometry_mapping[geometry_type]

if "mark" in file:
df_sverige = None

for name, output_name in normalised_names.items():
geo_object = geometry_object(file, "50", layer, name, field)
if geo_object.df is not None:
geo_object.process()
geo_object.save("tmp", output_name)

if "mark" in file:
if df_sverige is None:
df_sverige = geo_object.df.dissolve().explode(index_parts=False)
else:
df_sverige = (
pd.concat([df_sverige, geo_object.df])
.dissolve()
.explode(index_parts=False)
)

if "mark" in file:
df_sverige["area_m2"] = df_sverige.area
df_sverige["length_m"] = df_sverige.length
df_sverige = df_sverige.df.to_crs(geo_object.config.epsg_4326)
df_sverige.to_file(
"tmp/mark_sverige/mark/00_sverige" + ".geojson", driver="GeoJSON"
)

print(f"Saved {file} - {layer}")


files = glob.glob("topografi_50/*.gpkg")

all_files = []
for file in files:
available_layers = fiona.listlayers(file)
for layer in available_layers:
all_files.append((file, layer))

with Pool(WORKERS) as pool:
pool.starmap(extract_geojson, all_files)
59 changes: 0 additions & 59 deletions src/lantmateriet/communication.py

This file was deleted.

131 changes: 18 additions & 113 deletions src/lantmateriet/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@
- 50: https://www.lantmateriet.se/globalassets/geodata/geodataprodukter/pb-topografi-50-nedladdning-vektor.pdf
"""

from typing import TypeVar
import shapely

from lantmateriet.line import Line
from lantmateriet.point import Point
from lantmateriet.polygon import Polygon

Geometry = TypeVar("Geometry", Line, Polygon, Point)

Check failure on line 16 in src/lantmateriet/config.py

View workflow job for this annotation

GitHub Actions / lint (3.10)

Ruff (I001)

src/lantmateriet/config.py:9:1: I001 Import block is un-sorted or un-formatted


class BaseConfig:
"""Base config class."""
Expand All @@ -19,6 +28,15 @@ class BaseConfig:
border_county: str = "Länsgräns"
border_municipality: str = "Kommungräns"

file_geometry_mapping: dict[str, Geometry] = {
shapely.Point: Point,
shapely.MultiPoint: Point,
shapely.MultiLineString: Line,
shapely.LineString: Line,
shapely.Polygon: Polygon,
shapely.MultiPolygon: Polygon,
}

def __getitem__(self, key):
"""Get item.
Expand All @@ -34,35 +52,6 @@ def __getitem__(self, key):
class Config1M(BaseConfig):
"""Topography 1M config class."""

ground: dict[str, dict[str, str]] = {
"mark": {
"Sverige": "00_sverige.geojson",
"Vattenyta": "01_vattenyta.geojson",
"Glaciär": "05_glaciar.geojson",
"Kalfjäll": "08_kalfjall.geojson",
"Skog": "02_skog.geojson",
"Öppen mark": "15_oppen_mark.geojson",
"Bebyggelse": "06_bebygelse.geojson",
"Hav": "16_hav.geojson",
"Ej karterat område": "17_ej_kartlagt.geojson",
},
"markkantlinje": {},
"sankmark": {},
}
construction: dict[str, dict[str, str]] = {"byggnadspunkt": {}}
communication: dict[str, dict[str, str]] = {
"vaglinje": {
"Motorväg": "01_motorvag.geojson",
"Motortrafikled": "02_motortrafikled.geojson",
"Landsväg": "03_landsvag.geojson",
"Landsväg liten": "04_landsvag_liten.geojson",
"Småväg": "05_smavag.geojson",
},
"farjeled": {"Färjeled": "01_farjeled.geojson"},
"ovrig_vag": {"Vandringsled": "01_vandringsled.geojson"},
"ralstrafik": {"Järnväg": "01_jarnvag.geojson"},
}

exclude = {"Hav", "Ej karterat område", "Sverige"}
exteriorise = {"Skog"}
ground_water = {
Expand All @@ -74,90 +63,6 @@ class Config1M(BaseConfig):
class Config50(BaseConfig):
"""Config class."""

total_ground = ("00_sverige.geojson", "00_sverige.geojson")
ground: dict[str, dict[str, str]] = {
"mark": {
"Sverige": "00_sverige.geojson",
"Anlagt vatten": "01_anlagt_vatten.geojson",
"Vattendragsyta": "02_vattendragsyta.geojson",
"Sjö": "03_sjo.geojson",
"Glaciär": "04_glaciar.geojson",
"Kalfjäll": "05_kalfjall.geojson",
"Fjällbjörkskog": "06_fjallbjorkskog.geojson",
"Barr- och blandskog": "07_barr_blandskog.geojson",
"Lövskog": "08_lovskog.geojson",
"Åker": "09_aker.geojson",
"Fruktodling": "10_fruktodling.geojson",
"Öppen mark": "11_oppen_mark.geojson",
"Hög bebyggelse": "12_hog_bebygelse.geojson",
"Låg bebyggelse": "13_lag_bebygelse.geojson",
"Sluten bebyggelse": "14_sluten_bebygelse.geojson",
"Industri- och handelsbebyggelse": "15_industri_handel.geojson",
"Hav": "16_hav.geojson",
"Ej karterat område": "17_ej_kartlagt.geojson",
},
"markkantlinje": {},
"sankmark": {},
"markframkomlighet": {},
}
construction: dict[str, dict[str, str]] = {
"byggnad": {
"Bostad": "01_bostad.geojson",
"Industri": "02_industri.geojson",
"Samhällsfunktion": "03_samhallsfunktion.geojson",
"Verksamhet": "04_verksamhet.geojson",
"Ekonomibyggnad": "05_ekonomibyggnad.geojson",
"Komplementbyggnad": "06_komplementbyggnad.geojson",
"Övrig byggnad": "07_ovrig.geojson",
},
"byggnadsanlaggningslinje": {},
"byggnadsanlaggningspunkt": {},
"byggnadspunkt": {},
}
communication: dict[str, dict[str, str]] = {
"vaglinje": {
"Motorväg": "01_motorvag.geojson",
"Motortrafikled": "02_motortrafikled.geojson",
"Mötesfri väg": "03_motesfri_vag.geojson",
"Landsväg": "04_landsvag.geojson",
"Landsväg liten": "05_landsvag_liten.geojson",
"Småväg": "06_smavag.geojson",
"Småväg enkel standard": "07_smavag_enkel_standard.geojson",
"Övergripande länk": "08_overgripande_lank.geojson",
"Huvudgata": "09_huvudgata.geojson",
"Lokalgata stor": "10_lokalgata_stor.geojson",
"Lokalgata liten": "11_lokalgata_liten.geojson",
},
"vagpunkt": {},
"farjeled": {"Färjeled": "01_farjeled.geojson"},
"ovrig_vag": {
"Parkväg": "01_parkvag.geojson",
"Cykelväg": "02_cykelvag.geojson",
"Gångstig": "03_gangstig.geojson",
"Elljusspår": "04_elljusspar.geojson",
"Traktorväg": "05_traktorvag.geojson",
"Vandringsled": "06_vandringsled.geojson",
"Vandrings- och vinterled": "07_vandrings_vinterled.geojson",
"Vinterled": "08_vinterled.geojson",
},
"transportled_fjall": {
"Lämplig färdväg": "01_lamplig_fardvag.geojson",
"Rennäringsled": "02_rennaringsled.geojson",
"Fångstarm till led": "03_fangstarm_till_led.geojson",
"Roddled": "04_roddled.geojson",
"Svårorienterad gångstig": "05_svarorienterad_gangstig.geojson",
"Skidspår": "06_skidspar.geojson",
"Båtdrag": "07_batdrag.geojson",
"Trafikerad båtled": "08_trafikerad_batled.geojson",
},
"ledintressepunkt_fjall": {},
"ralstrafik": {
"Järnväg": "01_jarnvag.geojson",
"Museijärnväg": "02_museijarnvag.geojson",
},
"ralstrafikstation": {},
}

exclude = {"Hav", "Ej karterat område", "Sverige"}
exteriorise = {"Barr- och blandskog"}
ground_water = {
Expand Down
62 changes: 0 additions & 62 deletions src/lantmateriet/construction.py

This file was deleted.

Loading

0 comments on commit 45bc04f

Please sign in to comment.