Skip to content

Commit

Permalink
fix: convert multi to line strings (LAN-794)
Browse files Browse the repository at this point in the history
  • Loading branch information
barredterra committed Nov 23, 2023
1 parent 0448921 commit adca96b
Show file tree
Hide file tree
Showing 2 changed files with 44 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 @@ -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
43 changes: 43 additions & 0 deletions landa/patches/multi_to_line.py
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))

0 comments on commit adca96b

Please sign in to comment.