Skip to content

Commit e949781

Browse files
added thanados imports
1 parent 382597f commit e949781

File tree

5 files changed

+152
-8
lines changed

5 files changed

+152
-8
lines changed
696 KB
Binary file not shown.
188 KB
Binary file not shown.
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import json
2+
from dataclasses import dataclass
3+
from pathlib import Path
4+
from typing import Any, Optional
5+
6+
import fiona
7+
from shapely import MultiPolygon
8+
from shapely.geometry import mapping, shape
9+
10+
from openatlas import app
11+
from openatlas.models.entity import Entity
12+
from openatlas.models.gis import insert_gis
13+
14+
BASE_DIR = Path(__file__).resolve().parent
15+
GPKG_PATH = BASE_DIR / "churches.gpkg"
16+
GPKG_STR = str(GPKG_PATH)
17+
18+
19+
@dataclass
20+
class ChurchRecord:
21+
osm_id: str
22+
openatlas_id: int
23+
name: Optional[str]
24+
geometry: dict[str, Any]
25+
26+
@classmethod
27+
def from_fiona(cls, feature_: dict[str, Any]) -> 'ChurchRecord':
28+
props = feature_.get('properties', {})
29+
geom_input = feature_.get('geometry', {})
30+
poly_obj = shape(geom_input)
31+
if isinstance(poly_obj, MultiPolygon):
32+
poly_obj = max(poly_obj.geoms, key=lambda p: p.area)
33+
clean_geom = mapping(poly_obj)
34+
return cls(
35+
osm_id=props.get('osm_id'),
36+
openatlas_id=props.get('@id'),
37+
name=props.get('name_2'),
38+
geometry=clean_geom)
39+
40+
41+
with app.test_request_context():
42+
app.preprocess_request()
43+
44+
church_objects: list['ChurchRecord'] = []
45+
with fiona.open(GPKG_STR, layer=fiona.listlayers(GPKG_STR)[0]) as gpkg:
46+
for feature in gpkg:
47+
record = ChurchRecord.from_fiona(feature)
48+
church_objects.append(record)
49+
50+
for church in church_objects:
51+
entity = Entity.get_by_id(church.openatlas_id)
52+
if not entity:
53+
continue
54+
55+
geom = {
56+
"type": "Feature",
57+
"geometry": church.geometry,
58+
"properties": {
59+
"name": f"OSM: {church.osm_id}",
60+
"description": "Imported from OpenStreetMap",
61+
"id": -999999999,
62+
"shapeType": "shape"}}
63+
64+
data = {
65+
'point': '[]',
66+
'line': '[]',
67+
'polygon': f'[{json.dumps(geom)}]'}
68+
69+
insert_gis(entity.location, data)
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import json
2+
from dataclasses import dataclass
3+
from pathlib import Path
4+
from typing import Any, Optional
5+
6+
import fiona
7+
from shapely import MultiPolygon
8+
from shapely.geometry import mapping, shape
9+
10+
from openatlas import app
11+
from openatlas.models.entity import Entity
12+
from openatlas.models.gis import get_gis_by_id
13+
14+
BASE_DIR = Path(__file__).resolve().parent
15+
GPKG_PATH = BASE_DIR / "polys_aut.gpkg"
16+
GPKG_STR = str(GPKG_PATH)
17+
18+
19+
@dataclass
20+
class ChurchRecord:
21+
osm_id: str
22+
openatlas_id: int
23+
name: Optional[str]
24+
geometry: dict[str, Any]
25+
26+
@classmethod
27+
def from_fiona(cls, feature_: dict[str, Any]) -> 'ChurchRecord':
28+
props = feature_.get('properties', {})
29+
geom_input = feature_.get('geometry', {})
30+
poly_obj = shape(geom_input)
31+
if isinstance(poly_obj, MultiPolygon):
32+
poly_obj = max(poly_obj.geoms, key=lambda p: p.area)
33+
clean_geom = mapping(poly_obj)
34+
return cls(
35+
osm_id=props.get('osm_id'),
36+
openatlas_id=props.get('@id'),
37+
name=props.get('name_2'),
38+
geometry=clean_geom)
39+
40+
41+
with app.test_request_context():
42+
app.preprocess_request()
43+
44+
church_objects: list['ChurchRecord'] = []
45+
with fiona.open(GPKG_STR, layer=fiona.listlayers(GPKG_STR)[0]) as gpkg:
46+
for feature in gpkg:
47+
record = ChurchRecord.from_fiona(feature)
48+
church_objects.append(record)
49+
50+
for church in church_objects:
51+
entity = Entity.get_by_id(church.openatlas_id)
52+
if not entity:
53+
continue
54+
location = get_gis_by_id(entity.location.id)
55+
56+
geom = {
57+
"type": "Feature",
58+
"geometry": church.geometry,
59+
"properties": {
60+
"name": f"OSM: {church.osm_id}",
61+
"description": "Imported from OpenStreetMap",
62+
"id": -1770996400834,
63+
"shapeType": "shape"}}
64+
65+
data = {
66+
'point': '[]',
67+
'line': '[]',
68+
'polygon': f'[{json.dumps(geom)}]'}
69+
70+
entity.update_gis(data)

install/import_scripts/zbiva_churches.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import re
44
from dataclasses import dataclass
55
from pathlib import Path
6-
from typing import Final
6+
from typing import Any, Final
77

88
from shapely import Point
99
from shapely.geometry import mapping
@@ -26,9 +26,9 @@ class Entry:
2626
description: str
2727
point: Point
2828
established: str
29-
first_source: str
30-
last_source: str
31-
citation: dict[str, dict[str, str]]
29+
first_source: str | None
30+
last_source: str | None
31+
citation: Any # dict[str, dict[str, str]]
3232

3333

3434
def process_citation(citation_strings: list[str]) -> dict[str, str]:
@@ -48,7 +48,8 @@ def process_citation(citation_strings: list[str]) -> dict[str, str]:
4848
succeeding_name = name
4949
try:
5050
citation_output[name] = splitted_citation[1]
51-
except: # todo: there are many failed citations, handle them
51+
# todo: there are many failed citations, handle them
52+
except Exception:
5253
continue
5354
return citation_output
5455

@@ -103,7 +104,7 @@ def process_row(row: dict[str, str]) -> Entry:
103104
replico = Entity.get_by_id(198155)
104105
zbiva = Entity.get_by_id(239450)
105106
church_type = Entity.get_by_id(285)
106-
zbiva_project = 6
107+
ZBIVA_PROJECT = 6
107108

108109
for entry in entries:
109110
place = insert({
@@ -136,5 +137,9 @@ def process_row(row: dict[str, str]) -> Entry:
136137
'begin_to': entry.last_source})
137138
modification.link('P31', place)
138139

139-
import_data(zbiva_project, place.id, 23, f'{entry.id}_church')
140-
import_data(zbiva_project, modification.id, 23, f'{entry.id}_modification')
140+
import_data(ZBIVA_PROJECT, place.id, 23, f'{entry.id}_church')
141+
import_data(
142+
ZBIVA_PROJECT,
143+
modification.id,
144+
23,
145+
f'{entry.id}_modification')

0 commit comments

Comments
 (0)