diff --git a/landa/patches.txt b/landa/patches.txt index 6df12736..dc3a3b42 100644 --- a/landa/patches.txt +++ b/landa/patches.txt @@ -30,3 +30,4 @@ landa.patches.lease_rent_per_year landa.patches.add_water_body_title_in_stocking landa.patches.multi_to_polygon landa.patches.add_award_types +landa.patches.multi_to_line diff --git a/landa/patches/multi_to_line.py b/landa/patches/multi_to_line.py new file mode 100644 index 00000000..95f10679 --- /dev/null +++ b/landa/patches/multi_to_line.py @@ -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))