Skip to content

Commit

Permalink
[Fixes #3953] "bbox_to_projection" flips coords and does not honor th…
Browse files Browse the repository at this point in the history
…e geonode convention [x0, x1, y0, y1]
  • Loading branch information
afabiani committed Oct 3, 2018
1 parent 9f5c263 commit df49b5b
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 4 deletions.
34 changes: 34 additions & 0 deletions geonode/layers/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@
from geonode.tests.utils import NotificationsTestsHelper
from geonode.layers import LayersAppConfig

import logging

logger = logging.getLogger(__name__)


class LayersTest(GeoNodeBaseTestSupport):

Expand Down Expand Up @@ -123,6 +127,7 @@ def test_upload_layer(self):
# Test redirection to login form when not logged in
response = self.client.get(reverse('layer_upload'))
self.assertEquals(response.status_code, 302)

# Test return of upload form when logged in
self.client.login(username="bobby", password="bob")
response = self.client.get(reverse('layer_upload'))
Expand Down Expand Up @@ -163,6 +168,35 @@ def test_layer_attributes(self):
self.assertEqual(custom_attributes[1].sum, "NA")
self.assertEqual(custom_attributes[1].unique_values, "NA")

def test_layer_bbox(self):
lyr = Layer.objects.all().first()
layer_bbox = lyr.bbox[0:4]
logger.info(layer_bbox)

def decimal_encode(bbox):
import decimal
_bbox = []
for o in [float(coord) for coord in bbox]:
if isinstance(o, decimal.Decimal):
o = (str(o) for o in [o])
_bbox.append(o)
# Must be in the form : [x0, x1, y0, y1
return [_bbox[0], _bbox[2], _bbox[1], _bbox[3]]

from geonode.utils import bbox_to_projection
projected_bbox = decimal_encode(
bbox_to_projection([float(coord) for coord in layer_bbox] + [lyr.srid, ],
target_srid=4326)[:4])
logger.info(projected_bbox)
self.assertEquals(projected_bbox, [-180.0, -90.0, 180.0,90.0])
logger.info(lyr.ll_bbox)
self.assertEquals(lyr.ll_bbox, [-180.0, 180.0, -90.0, 90.0, u'EPSG:4326'])
projected_bbox = decimal_encode(
bbox_to_projection([float(coord) for coord in layer_bbox] + [lyr.srid, ],
target_srid=3857)[:4])
logger.info(projected_bbox)
self.assertEquals(projected_bbox, [-19926188.852, -30240971.9584, 19926188.852, 30240971.9584])

def test_layer_attributes_feature_catalogue(self):
""" Test layer feature catalogue functionality
"""
Expand Down
5 changes: 3 additions & 2 deletions geonode/layers/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ def layer_upload(request, template='upload/layer_upload.html'):
else:
saved_layer = Layer.objects.get(alternate=title)
if not saved_layer:
msg = 'Failed to process. Could not find matching layer.'
msg = 'Failed to process. Could not find matching layer.'
raise Exception(msg)
sld = open(base_file).read()
set_layer_style(saved_layer, title, base_file, sld)
Expand Down Expand Up @@ -316,7 +316,8 @@ def decimal_encode(bbox):
if isinstance(o, decimal.Decimal):
o = (str(o) for o in [o])
_bbox.append(o)
return _bbox
# Must be in the form : [x0, x1, y0, y1
return [_bbox[0], _bbox[2], _bbox[1], _bbox[3]]

def sld_definition(style):
from urllib import quote
Expand Down
3 changes: 2 additions & 1 deletion geonode/maps/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,8 @@ def decimal_encode(bbox):
if isinstance(o, decimal.Decimal):
o = (str(o) for o in [o])
_bbox.append(o)
return _bbox
# Must be in the form : [x0, x1, y0, y1
return [_bbox[0], _bbox[2], _bbox[1], _bbox[3]]

def sld_definition(style):
from urllib import quote
Expand Down
5 changes: 4 additions & 1 deletion geonode/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,10 @@ def _v(coord, x, source_srid=4326, target_srid=3857):
srid=source_srid)
poly = GEOSGeometry(wkt, srid=source_srid)
poly.transform(target_srid)
return tuple([str(x) for x in poly.extent]) + ("EPSG:%s" % poly.srid,)
projected_bbox = [str(x) for x in poly.extent]
# Must be in the form : [x0, x1, y0, y1, EPSG:<target_srid>)
return tuple([projected_bbox[0], projected_bbox[2], projected_bbox[1], projected_bbox[3]]) + \
("EPSG:%s" % poly.srid,)
except BaseException:
tb = traceback.format_exc()
logger.debug(tb)
Expand Down

0 comments on commit df49b5b

Please sign in to comment.