Skip to content

Commit

Permalink
force uploaded geoms to be 2d
Browse files Browse the repository at this point in the history
  • Loading branch information
bdon committed Nov 19, 2017
1 parent 073cf38 commit 7ba9dbf
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
3 changes: 2 additions & 1 deletion jobs/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from hdx_exports.hdx_export_set import HDXExportSet
from feature_selection.feature_selection import FeatureSelection
from utils import FORMAT_NAMES
from utils.aoi_utils import simplify_geom, get_geodesic_area, check_extent
from utils.aoi_utils import simplify_geom, get_geodesic_area, check_extent, force2d
from django.contrib import admin

LOG = logging.getLogger(__name__)
Expand Down Expand Up @@ -126,6 +126,7 @@ def area(self):
return get_geodesic_area(self.the_geom)

def save(self, *args, **kwargs):
self.the_geom = force2d(self.the_geom)
self.simplified_geom = simplify_geom(self.the_geom,force_buffer=self.buffer_aoi)
super(Job, self).save(*args, **kwargs)

Expand Down
6 changes: 6 additions & 0 deletions utils/aoi_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import math
from collections import namedtuple
from django.contrib.gis.geos import GEOSGeometry, Polygon
from django.contrib.gis.geos.prototypes.io import wkt_w
import requests

# goals:
Expand All @@ -27,6 +28,11 @@

ValidateResult = namedtuple('ValidateResult',['valid','message','params','area'])

def force2d(geom):
# force geom to be 2d: https://groups.google.com/forum/#!topic/django-users/7c1NZ76UwRU
wkt = wkt_w(dim=2).write(geom).decode()
return GEOSGeometry(wkt)

def simplify_geom(geom,force_buffer=False):
if geom.num_coords > 10000:
geom = geom.simplify(0.01)
Expand Down
14 changes: 13 additions & 1 deletion utils/tests/test_manager.py → utils/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
from utils.shp import Shapefile
from utils.garmin_img import GarminIMG
from utils.osmand_obf import OsmAndOBF
from utils.aoi_utils import force2d
from django.contrib.gis.geos import GEOSGeometry

class TestManager(unittest.TestCase):
class TestUtils(unittest.TestCase):

def test_map_names_to_formats(self):
m = map_names_to_formats([
Expand All @@ -31,3 +33,13 @@ def test_map_names_to_formats(self):
GarminIMG,
OsmAndOBF
])

def test_force_2d(self):
geojson3d = '{"type":"Polygon","coordinates":[[[-17.279434,14.732386,0],[-17.229996,14.732386,0],[-17.229996,14.779531,0],[-17.279434,14.779531,0],[-17.279434,14.732386,0]]]}}'
aoi_geom = GEOSGeometry(geojson3d)
aoi_geom = force2d(aoi_geom)
self.assertEqual(len(aoi_geom.coords[0][0]),2)




0 comments on commit 7ba9dbf

Please sign in to comment.