Skip to content

Commit

Permalink
changed single polygon to merged polygon POI if uploaded multi polygo…
Browse files Browse the repository at this point in the history
…n during project area edit (#829)

* fix: changed single polygon to merged polygon POI if uploaded multi polygon during project area edit

* fix: changed single polygon to merged polygon POI if uploaded multi polygon during project area edit

* fix: changed single polygon to multipolygon preview for multipolygon geojson in task split by square method

* fix: multi polygon task in custom data extract using area as task for splitting algorithm

* fix: added missing try except block

---------

Co-authored-by: sujanadh <sujanadh07@gmail.com>
  • Loading branch information
Sujanadh and sujanadh authored Sep 25, 2023
1 parent 1217fc6 commit 98f0349
Showing 1 changed file with 54 additions and 20 deletions.
74 changes: 54 additions & 20 deletions src/backend/app/projects/project_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,10 +481,20 @@ def remove_z_dimension(coord):
)

""" Apply the lambda function to each coordinate in its geometry ro remove the z-dimension - if it exists"""
multi_polygons = []
for feature in features:
list(map(remove_z_dimension, feature["geometry"]["coordinates"][0]))
if feature["geometry"]["type"] == "MultiPolygon":
multi_polygons.append(Polygon(feature["geometry"]["coordinates"][0][0]))


boundary = shape(features[0]["geometry"])
"""Update the boundary polyon on the database."""
if multi_polygons:
boundary = multi_polygons[0]
for geom in multi_polygons[1:]:
boundary = boundary.union(geom)
else:
boundary = shape(features[0]["geometry"])

minx, miny, maxx, maxy = boundary.bounds

Expand Down Expand Up @@ -833,15 +843,20 @@ def remove_z_dimension(coord):
)

""" Apply the lambda function to each coordinate in its geometry """
multi_polygons = []
for feature in features:
list(map(remove_z_dimension, feature["geometry"]["coordinates"][0]))
if feature["geometry"]["type"] == "MultiPolygon":
multi_polygons.append(Polygon(feature["geometry"]["coordinates"][0][0]))


"""Update the boundary polyon on the database."""
outline = shape(features[0]["geometry"])

# If the outline is a multipolygon, use the first polygon
if isinstance(outline, MultiPolygon):
outline = outline.geoms[0]
if multi_polygons:
outline = multi_polygons[0]
for geom in multi_polygons[1:]:
outline = outline.union(geom)
else:
outline = shape(features[0]["geometry"])

db_project.outline = outline.wkt
db_project.centroid = outline.centroid.wkt
Expand Down Expand Up @@ -1144,27 +1159,46 @@ def upload_custom_data_extracts(

for feature in features_data["features"]:
feature_shape = shape(feature["geometry"])

if not (shape(project_geojson).contains(feature_shape)):
continue
if isinstance(feature_shape, MultiPolygon):
wkb_element = from_shape(Polygon(feature["geometry"]["coordinates"][0][0]), srid=4326)
else:
wkb_element = from_shape(feature_shape, srid=4326)

# If the osm extracts contents do not have a title, provide an empty text for that.
feature["properties"]["title"] = ""
properties = flatten_dict(feature["properties"])

feature_shape = shape(feature["geometry"])

wkb_element = from_shape(feature_shape, srid=4326)
feature_mapping = {
"project_id": project_id,
"geometry": wkb_element,
"properties": feature["properties"],
}
featuree = db_models.DbFeatures(**feature_mapping)
db.add(featuree)
db.commit()
db_feature = db_models.DbFeatures(
project_id=project_id,
geometry = wkb_element,
properties=properties
)
db.add(db_feature)
db.commit()

return True

def flatten_dict(d, parent_key='', sep='_'):
"""
Recursively flattens a nested dictionary into a single-level dictionary.
Args:
d (dict): The input dictionary.
parent_key (str): The parent key (used for recursion).
sep (str): The separator character to use in flattened keys.
Returns:
dict: The flattened dictionary.
"""
items = {}
for k, v in d.items():
new_key = f"{parent_key}{sep}{k}" if parent_key else k
if isinstance(v, dict):
items.update(flatten_dict(v, new_key, sep=sep))
else:
items[new_key] = v
return items


def generate_task_files(
db: Session,
Expand Down

0 comments on commit 98f0349

Please sign in to comment.