Skip to content

Commit

Permalink
collect projectarea keys
Browse files Browse the repository at this point in the history
  • Loading branch information
lastminutediorama committed Aug 20, 2024
1 parent e5aea16 commit 8c2e8c2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
18 changes: 16 additions & 2 deletions src/planscape/planning/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ def feature_to_project_area(idx: int, user_id: int, scenario, feature):
action_object=proj_area_obj,
target=scenario,
)

return proj_area_obj

except Exception as e:
logger.error(f"Unable to create project area for {scenario.name} {e}")
raise e
Expand All @@ -148,7 +151,11 @@ def create_scenario_from_upload(
)

if "type" in uploaded_geom and uploaded_geom["type"] == "Polygon":
feature_to_project_area(1, scenario.user, scenario, json.dumps(uploaded_geom))
new_feature = feature_to_project_area(
1, scenario.user, scenario, json.dumps(uploaded_geom)
)
uploaded_geom.setdefault("properties", {})
uploaded_geom["properties"]["project_id"] = new_feature.pk

# # this handles a format provided by shpjs when a shapefile has multiple features
# if "geometry" in uploaded_geom and "coordinates" in uploaded_geom["geometry"]:
Expand All @@ -159,9 +166,16 @@ def create_scenario_from_upload(
# this handles a more standard FeatureCollection
if "features" in uploaded_geom:
for idx, f in enumerate(uploaded_geom["features"], 1):
feature_to_project_area(
print(f"do we have...mutliple feature? {f}")
new_feature = feature_to_project_area(
idx, scenario.user, scenario, json.dumps(f["geometry"])
)
# TODO: add new feature details back to uploaded_geom
f.setdefault("properties", {})
f["properties"]["project_id"] = new_feature.pk

# Store updated in ScenarioResult.result
ScenarioResult.objects.create(scenario=scenario, result=uploaded_geom)

return scenario

Expand Down
3 changes: 1 addition & 2 deletions src/planscape/planning/views_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,9 @@ def toggle_status(self, request, pk=None):

@action(methods=["POST"], detail=False)
def upload_shapefiles(self, request, pk=None, *args, **kwargs):
# stuff we expect
stand_size = request.data["stand_size"]

# TODO: cleanup dict vs string here

uploaded_geom = request.data["geometry"]
if isinstance(uploaded_geom, str):
uploaded_geojson = json.loads(uploaded_geom)
Expand Down Expand Up @@ -222,6 +220,7 @@ def upload_shapefiles(self, request, pk=None, *args, **kwargs):
"planning_area": pa.pk,
"user": request.user.pk,
"configuration": {"stand_size": stand_size},
## TODO: add an origin (e.g., uploaded)
}

scenario_serializer = UploadedScenarioSerializer(data=scenario_data)
Expand Down

0 comments on commit 8c2e8c2

Please sign in to comment.