Skip to content

Commit

Permalink
feat: refactored code so community saves coordinate data in geojson f…
Browse files Browse the repository at this point in the history
…ormat
  • Loading branch information
lc-hd committed Oct 21, 2024
1 parent b38cf60 commit c7c7007
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 20 deletions.
15 changes: 7 additions & 8 deletions communities/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from django import forms
from django.forms.utils import ErrorList

from helpers.schema import GEOJSON_MULTI_POLYGON_TYPE
from .models import Community, InviteMember, JoinRequest, Boundary
from django.utils.translation import gettext_lazy as _
from django.core.exceptions import ValidationError
Expand Down Expand Up @@ -112,14 +113,12 @@ def save_boundary(self):
# update current boundary
updated_boundary = self.supplementary_boundary_data.get('current_boundary')
if updated_boundary and self.instance.boundary.id == int(updated_boundary['id']):
self.instance.boundary.coordinates = updated_boundary['value']

# todo: will save in boundary geometry
# self.instance.boundary.geometry = {
# 'type': 'MULTIPOLYGON', 'polygons': [
# updated_boundary['value']
# ],
# }

self.instance.boundary.geometry = {
'type': GEOJSON_MULTI_POLYGON_TYPE, 'coordinates': [
[updated_boundary['value']]
],
}
self.instance.boundary.save()
return

Expand Down
23 changes: 16 additions & 7 deletions communities/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from django.contrib.auth.models import User
from django_countries.fields import CountryField

from helpers.schema import validate_multipolygon
from helpers.schema import MultiPolygonSchema, GEOJSON_MULTI_POLYGON_TYPE
from institutions.models import Institution
from serviceproviders.models import ServiceProvider
import uuid
Expand Down Expand Up @@ -41,13 +41,17 @@ class Boundary(models.Model):
),
blank=True, null=True
)
geometry = MultiPolygonField(
null=True, validators=[validate_multipolygon]
)
geometry = MultiPolygonField(null=True)

def validate_geometry(self):
if not self.geometry:
return
MultiPolygonSchema().load(self.geometry)

def save(self, *args, **kwargs):
# added so validators are run before saving
self.full_clean()
# self.full_clean()
self.validate_geometry()
super().save(*args, **kwargs)

@staticmethod
Expand Down Expand Up @@ -159,13 +163,18 @@ def approved_label(self):

def create_or_update_boundary(self, boundary_coordinates):
boundary_coordinates = boundary_coordinates if boundary_coordinates else []
geometry = {
'type': GEOJSON_MULTI_POLYGON_TYPE, 'coordinates': [
[boundary_coordinates]
],
}

if self.boundary:
# update boundary when it exists
self.boundary.coordinates = boundary_coordinates
self.boundary.geometry = geometry
else:
# create boundary when it does not exist
self.boundary = Boundary(coordinates=boundary_coordinates)
self.boundary = Boundary(geometry=geometry)

self.boundary.save()

Expand Down
4 changes: 0 additions & 4 deletions helpers/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,3 @@ class MultiPolygonSchema(BaseSchema):
required=True,
metadata=dict(example=GEOJSON_MULTI_POLYGON_EXAMPLE["coordinates"]),
)


def validate_multipolygon(value):
MultiPolygonSchema().load(value)
17 changes: 16 additions & 1 deletion tests/unit/test_storing_community_territory_data_as_geojson.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,21 @@ def test_geojson_raises_error_for_invalid_geojson_lat_lon(self):
assert e.value.args[0]['coordinates'][0][0][0][1] == ['Latitude must be between -90, 90']
assert e.value.args[0]['coordinates'][0][0][1][0] == ['Longitude must be between -180, 180']

def test_geojson_coordinates_works_with_empty_arrays(self):
geojson_coordinates = {
'type': GEOJSON_MULTI_POLYGON_TYPE, 'coordinates': [
[
[]
],
],
}
boundary = Boundary(
geometry=geojson_coordinates,
)
boundary.save()
boundary_coordinates_read = boundary.get_coordinates()
assert boundary_coordinates_read == []

def test_non_geojson_coordinate_values_override_geojson_one(self):
"""
This ensures that until preexisting community accounts data have been
Expand All @@ -105,6 +120,6 @@ def test_non_geojson_coordinate_values_override_geojson_one(self):
coordinates=non_geojson_coordinates,
geometry=geojson_coordinates,
)

boundary.save()
boundary_coordinates_read = boundary.get_coordinates()
assert boundary_coordinates_read == non_geojson_coordinates

0 comments on commit c7c7007

Please sign in to comment.