Skip to content

Commit

Permalink
fix: convert MultiPolygons to Polygons (LAN-794)
Browse files Browse the repository at this point in the history
Water Body: Leaflet Draw doesn't support editing the MutiPolygons (imported
from the former GIS), so we need to convert them to Polygons.
  • Loading branch information
barredterra committed Nov 15, 2023
1 parent 29d5bf6 commit cedf00b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions landa/patches.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ landa.patches.set_hide_custom_in_user_workspaces
landa.patches.cleanup_addresses_and_contacts
landa.patches.lease_rent_per_year
landa.patches.add_water_body_title_in_stocking
landa.patches.multi_to_polygon
40 changes: 40 additions & 0 deletions landa/patches/multi_to_polygon.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import json

import frappe


def execute():
"""
Water Body: convert all MultiPolygons to Polygons in the geo shapes.
Leaflet Draw doesn't support editing the MutiPolygons (imported from the
former GIS), so we need to convert them to Polygons.
"""
for wb_name in frappe.get_all(
"Water Body", filters={"location": ("like", "%MultiPolygon%")}, 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"] != "MultiPolygon":
features.append(feature)
else:
features.extend(
{
"type": "Feature",
"geometry": {"type": "Polygon", "coordinates": polygon},
"properties": feature["properties"],
}
for polygon in feature["geometry"]["coordinates"]
)
modified = True

if modified:
geojson["features"] = features
frappe.db.set_value("Water Body", wb_name, "location", json.dumps(geojson))

0 comments on commit cedf00b

Please sign in to comment.