Fix bugs in produce_lod1.py
Browse files- produce_lod1.py +52 -55
produce_lod1.py
CHANGED
|
@@ -5,6 +5,16 @@ from pathlib import Path
|
|
| 5 |
from typing import Tuple, List
|
| 6 |
import argparse
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
# ============================================================
|
| 10 |
# 1. JSON → SQLite (streaming, memory safe)
|
|
@@ -46,78 +56,65 @@ def build_sqlite_index(json_path: Path, sqlite_path: Path):
|
|
| 46 |
# 2. GeoJSON enrichment (streaming)
|
| 47 |
# ============================================================
|
| 48 |
def enrich_geojson(
|
| 49 |
-
geojson1: Path,
|
| 50 |
-
geojson2: Path,
|
| 51 |
-
sqlite_path: Path,
|
| 52 |
geojson_out: Path,
|
| 53 |
):
|
| 54 |
geojson_out.parent.mkdir(parents=True, exist_ok=True)
|
| 55 |
|
| 56 |
# ---------------------------------------------------------
|
| 57 |
-
#
|
| 58 |
# ---------------------------------------------------------
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
-
with fiona.open(geojson2, "r") as src2:
|
| 62 |
-
for feat in src2:
|
| 63 |
-
p = feat["properties"]
|
| 64 |
key = (
|
| 65 |
str(p.get("source", "")) +
|
| 66 |
str(p.get("id", "")) +
|
| 67 |
str(p.get("region", ""))
|
| 68 |
)
|
| 69 |
-
props_index[key] = dict(p)
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
# ---------------------------------------------------------
|
| 78 |
-
# 3. Stream GeoJSON1 and merge
|
| 79 |
-
# ---------------------------------------------------------
|
| 80 |
-
with fiona.open(geojson1, "r") as src1:
|
| 81 |
-
meta = src1.meta
|
| 82 |
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
|
| 89 |
-
|
| 90 |
-
|
| 91 |
|
| 92 |
-
|
|
|
|
| 93 |
for feat in src1:
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
)
|
| 101 |
-
|
| 102 |
-
# ---- merge properties from GeoJSON2
|
| 103 |
-
if key in props_index:
|
| 104 |
-
p.update(props_index[key])
|
| 105 |
-
|
| 106 |
-
# ---- lookup height / var
|
| 107 |
-
cur.execute(
|
| 108 |
-
"SELECT height, var FROM kv WHERE key=?",
|
| 109 |
-
(key,)
|
| 110 |
-
)
|
| 111 |
-
row = cur.fetchone()
|
| 112 |
-
|
| 113 |
-
if row:
|
| 114 |
-
p["height"], p["var"] = row
|
| 115 |
-
else:
|
| 116 |
-
p["height"] = None
|
| 117 |
-
p["var"] = None
|
| 118 |
-
|
| 119 |
-
feat["properties"] = p
|
| 120 |
-
dst.write(feat)
|
| 121 |
|
| 122 |
conn.close()
|
| 123 |
# ============================================================
|
|
|
|
| 5 |
from typing import Tuple, List
|
| 6 |
import argparse
|
| 7 |
|
| 8 |
+
SCHEMA = {
|
| 9 |
+
"geometry": "Polygon",
|
| 10 |
+
"properties":{
|
| 11 |
+
"source": "str",
|
| 12 |
+
"id": "str",
|
| 13 |
+
"region": "str",
|
| 14 |
+
"height": "float",
|
| 15 |
+
"var": "float"
|
| 16 |
+
}
|
| 17 |
+
}
|
| 18 |
|
| 19 |
# ============================================================
|
| 20 |
# 1. JSON → SQLite (streaming, memory safe)
|
|
|
|
| 56 |
# 2. GeoJSON enrichment (streaming)
|
| 57 |
# ============================================================
|
| 58 |
def enrich_geojson(
|
| 59 |
+
geojson1: Path,
|
| 60 |
+
geojson2: Path,
|
| 61 |
+
sqlite_path: Path,
|
| 62 |
geojson_out: Path,
|
| 63 |
):
|
| 64 |
geojson_out.parent.mkdir(parents=True, exist_ok=True)
|
| 65 |
|
| 66 |
# ---------------------------------------------------------
|
| 67 |
+
# Open SQLite
|
| 68 |
# ---------------------------------------------------------
|
| 69 |
+
conn = sqlite3.connect(sqlite_path)
|
| 70 |
+
cur = conn.cursor()
|
| 71 |
+
|
| 72 |
+
# ---------------------------------------------------------
|
| 73 |
+
# Read schema from geojson1 (assumed compatible)
|
| 74 |
+
# ---------------------------------------------------------
|
| 75 |
+
meta = {}
|
| 76 |
+
meta["schema"] = SCHEMA
|
| 77 |
+
meta["crs"] = "EPSG:3857"
|
| 78 |
+
|
| 79 |
+
# ---------------------------------------------------------
|
| 80 |
+
# Write output
|
| 81 |
+
# ---------------------------------------------------------
|
| 82 |
+
with fiona.open(geojson_out, "w", **meta) as dst:
|
| 83 |
+
|
| 84 |
+
# ---- helper to enrich & write one feature
|
| 85 |
+
def process_feature(feat):
|
| 86 |
+
p = dict(feat["properties"])
|
| 87 |
|
|
|
|
|
|
|
|
|
|
| 88 |
key = (
|
| 89 |
str(p.get("source", "")) +
|
| 90 |
str(p.get("id", "")) +
|
| 91 |
str(p.get("region", ""))
|
| 92 |
)
|
|
|
|
| 93 |
|
| 94 |
+
cur.execute(
|
| 95 |
+
"SELECT height, var FROM kv WHERE key=?",
|
| 96 |
+
(key,)
|
| 97 |
+
)
|
| 98 |
+
row = cur.fetchone()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
+
if row:
|
| 101 |
+
p["height"], p["var"] = row
|
| 102 |
+
else:
|
| 103 |
+
p["height"] = None
|
| 104 |
+
p["var"] = None
|
| 105 |
|
| 106 |
+
feat["properties"] = p
|
| 107 |
+
dst.write(feat)
|
| 108 |
|
| 109 |
+
# ---- stream geojson1
|
| 110 |
+
with fiona.open(geojson1, "r") as src1:
|
| 111 |
for feat in src1:
|
| 112 |
+
process_feature(feat)
|
| 113 |
+
|
| 114 |
+
# ---- stream geojson2
|
| 115 |
+
with fiona.open(geojson2, "r") as src2:
|
| 116 |
+
for feat in src2:
|
| 117 |
+
process_feature(feat)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
|
| 119 |
conn.close()
|
| 120 |
# ============================================================
|