Skip to content

Commit

Permalink
Use the geometry field for location
Browse files Browse the repository at this point in the history
  • Loading branch information
norkans7 committed Jan 10, 2025
1 parent e1a60f2 commit 9846017
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 83 deletions.
4 changes: 2 additions & 2 deletions temba/api/v2/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ def get_aliases(self, obj):
return [alias.name for alias in obj.aliases.all()]

def get_geometry(self, obj):
if self.context["include_geometry"] and obj.simplified_geometry:
return obj.simplified_geometry
if self.context["include_geometry"] and obj.geometry:
return obj.geometry
else:
return None

Expand Down
2 changes: 1 addition & 1 deletion temba/api/v2/tests/test_boundaries.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def test_endpoint(self):
LocationAlias.create(self.org, self.admin, self.state2, "East Prov")
LocationAlias.create(self.org2, self.admin2, self.state1, "Other Org") # shouldn't be returned

self.state1.simplified_geometry = {
self.state1.geometry = {
"type": "MultiPolygon",
"coordinates": [[[[1, 1], [1, -1], [-1, -1], [-1, 1], [1, 1]]]],
}
Expand Down
2 changes: 1 addition & 1 deletion temba/locations/management/commands/import_geojson.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def import_file(self, filename, file):

kwargs = dict(osm_id=osm_id, name=name, level=level, parent=parent)
if is_simplified:
kwargs["simplified_geometry"] = geometry
kwargs["geometry"] = geometry

# if this is an update, just update with those fields
if location:
Expand Down
2 changes: 1 addition & 1 deletion temba/locations/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def get_geojson_feature(self):
return geojson.Feature(
properties=dict(name=self.name, osm_id=self.osm_id, id=self.pk, level=self.level),
zoomable=True if self.children.all() else False,
geometry=None if not self.simplified_geometry else self.simplified_geometry,
geometry=None if not self.geometry else self.geometry,
)

def get_geojson(self):
Expand Down
10 changes: 9 additions & 1 deletion temba/locations/tests/test_geojson.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from django.test.utils import captured_stdout

from temba.locations.models import Location, LocationAlias
from temba.tests import TembaTest
from temba.tests import TembaTest, matchers
from temba.utils import json


Expand Down Expand Up @@ -390,6 +390,14 @@ def test_zipfiles_parsing(self):

self.assertOSMIDs({"R1000"})

def test_import_geojson(self):
self.assertEqual(0, Location.objects.all().count())
call_command("import_geojson", "test-data/rwanda.zip")
self.assertEqual(9, Location.objects.all().count())

country_location = Location.objects.filter(level=0).get()
self.assertEqual(country_location.geometry, matchers.Dict())

def assertOSMIDs(self, ids):
self.assertEqual(set(ids), set(Location.objects.values_list("osm_id", flat=True)))

Expand Down
10 changes: 4 additions & 6 deletions temba/locations/tests/test_location.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,21 +307,19 @@ def test_location_create(self):
# create a simple location
location = Location.create(osm_id="-1", name="Null Island", level=0)
self.assertEqual(location.path, "Null Island")
self.assertIsNone(location.simplified_geometry)
self.assertIsNone(location.geometry)

# create a simple location with parent
child_location = Location.create(osm_id="-2", name="Palm Tree", level=1, parent=location)
self.assertEqual(child_location.path, "Null Island > Palm Tree")
self.assertIsNone(child_location.simplified_geometry)
self.assertIsNone(child_location.geometry)

wkb_geometry = {"type": "MultiPolygon", "coordinates": [[[[71.83225, 39.95415], [71.82655, 39.9563]]]]}

# create a simple location with parent and geometry
geom_location = Location.create(
osm_id="-3", name="Plum Tree", level=1, parent=location, simplified_geometry=wkb_geometry
)
geom_location = Location.create(osm_id="-3", name="Plum Tree", level=1, parent=location, geometry=wkb_geometry)
self.assertEqual(geom_location.path, "Null Island > Plum Tree")
self.assertIsNotNone(geom_location.simplified_geometry)
self.assertIsNotNone(geom_location.geometry)

# path should not be defined when calling Location.create
self.assertRaises(TypeError, Location.create, osm_id="-1", name="Null Island", level=0, path="some path")
69 changes: 0 additions & 69 deletions temba/locations/tests/test_migrations.py

This file was deleted.

4 changes: 2 additions & 2 deletions temba/locations/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def derive_url_pattern(cls, path, action):
return r"^%s/%s/(?P<osmId>\w+\.?\d+\.?\d?\_?\d?)/$" % (path, action)

def get_object(self):
return Location.geometries.get(osm_id=self.kwargs["osmId"])
return Location.objects.get(osm_id=self.kwargs["osmId"])

def render_to_response(self, context):
if self.object.children.all().count() > 0:
Expand All @@ -68,7 +68,7 @@ def derive_url_pattern(cls, path, action):
return r"^%s/%s/(?P<osmId>[\w\.]+)/$" % (path, action)

def get_object(self):
return Location.geometries.get(osm_id=self.kwargs["osmId"])
return Location.objects.get(osm_id=self.kwargs["osmId"])

def post(self, request, *args, **kwargs):
# try to parse our body
Expand Down

0 comments on commit 9846017

Please sign in to comment.