Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 35 additions & 26 deletions src/osm_osw_reformatter/serializer/osm/osm_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -618,52 +618,61 @@ def to_geojson(self, *args) -> None:
polygon_features = []
for n, d in self.G.nodes(data=True):
d_copy = {**d}
d_copy["_id"] = str(n)[1:]
d_copy['ext:osm_id'] = str(d_copy.get('osm_id', d_copy["_id"]))

if OSWPointNormalizer.osw_point_filter(d):
geometry = mapping(d_copy.pop("geometry"))
node_id_str = str(n)
if isinstance(n, str) and node_id_str and node_id_str[0].isalpha():
stripped_id = node_id_str[1:]
else:
stripped_id = node_id_str

base_properties = {
"_id": stripped_id,
"ext:osm_id": str(d_copy.get("osm_id", stripped_id)),
}

geometry_obj = d_copy.pop("geometry", None)
if geometry_obj is None and "lon" in d_copy and "lat" in d_copy:
geometry_obj = Point(d_copy["lon"], d_copy["lat"])

if geometry_obj is None:
continue

if "lon" in d_copy:
d_copy.pop("lon")
geometry = mapping(geometry_obj)

if "lat" in d_copy:
d_copy.pop("lat")
d_copy.pop("lon", None)
d_copy.pop("lat", None)

if OSWPointNormalizer.osw_point_filter(d):
normalized_props = OSWPointNormalizer(d).normalize()
properties = {**normalized_props, **base_properties}

point_features.append(
{"type": "Feature", "geometry": geometry, "properties": d_copy}
{"type": "Feature", "geometry": geometry, "properties": properties}
)
elif OSWLineNormalizer.osw_line_filter(d):
geometry = mapping(d_copy.pop("geometry"))
properties = {**d_copy, **base_properties}

line_features.append(
{"type": "Feature", "geometry": geometry, "properties": d_copy}
{"type": "Feature", "geometry": geometry, "properties": properties}
)
elif OSWZoneNormalizer.osw_zone_filter(d):
geometry = mapping(d_copy.pop("geometry"))
properties = {**d_copy, **base_properties}

zone_features.append(
{"type": "Feature", "geometry": geometry, "properties": d_copy}
{"type": "Feature", "geometry": geometry, "properties": properties}
)
elif OSWPolygonNormalizer.osw_polygon_filter(d):
geometry = mapping(d_copy.pop("geometry"))
properties = {**d_copy, **base_properties}

polygon_features.append(
{"type": "Feature", "geometry": geometry, "properties": d_copy}
{"type": "Feature", "geometry": geometry, "properties": properties}
)
else:
d_copy['_id'] = str(n)

geometry = mapping(d_copy.pop('geometry'))

if 'lon' in d_copy:
d_copy.pop('lon')

if 'lat' in d_copy:
d_copy.pop('lat')
elif OSWNodeNormalizer.osw_node_filter(d):
normalized_props = OSWNodeNormalizer(d).normalize()
properties = {**normalized_props, **base_properties}

node_features.append(
{'type': 'Feature', 'geometry': geometry, 'properties': d_copy}
{"type": "Feature", "geometry": geometry, "properties": properties}
)
nodes_fc = {**OSW_JSON_HEADER, **{"features": node_features}}
points_fc = {**OSW_JSON_HEADER, **{"features": point_features}}
Expand Down
Loading