-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: convert multi to line strings (LAN-794)
- Loading branch information
1 parent
0448921
commit adca96b
Showing
2 changed files
with
44 additions
and
0 deletions.
There are no files selected for viewing
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
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,43 @@ | ||
import json | ||
|
||
import frappe | ||
|
||
|
||
def execute(): | ||
""" | ||
Water Body: convert all MultiLineString to LineString in the geo shapes. | ||
Leaflet Draw doesn't support editing the MultiLineString (imported from the | ||
former GIS), so we need to convert them to LineString. Also, we drop | ||
obsolete properties from the GeoJSON data. | ||
""" | ||
for wb_name in frappe.get_all( | ||
"Water Body", filters={"location": ("like", "%MultiLineString%")}, pluck="name" | ||
): | ||
geojson_str = frappe.db.get_value("Water Body", wb_name, "location") | ||
geojson = json.loads(geojson_str) | ||
|
||
if "features" not in geojson: | ||
continue | ||
|
||
features = [] | ||
modified = False | ||
for feature in geojson["features"]: | ||
if feature["geometry"]["type"] != "MultiLineString": | ||
features.append(feature) | ||
else: | ||
features.extend( | ||
{ | ||
"type": "Feature", | ||
"geometry": {"type": "LineString", "coordinates": line}, | ||
"properties": { | ||
"osm_id": feature["properties"].get("osm_id"), | ||
}, | ||
} | ||
for line in feature["geometry"]["coordinates"] | ||
) | ||
modified = True | ||
|
||
if modified: | ||
geojson["features"] = features | ||
frappe.db.set_value("Water Body", wb_name, "location", json.dumps(geojson)) |