Skip to content

Commit

Permalink
fix a srid bug when uploading vector layers
Browse files Browse the repository at this point in the history
  • Loading branch information
boney-bun committed Jul 25, 2018
1 parent 7feb3a8 commit 1e854d4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
32 changes: 29 additions & 3 deletions geonode/layers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,15 +339,41 @@ def get_resolution(filename):

def get_bbox(filename):
"""Return bbox in the format [xmin,xmax,ymin,ymax]."""
from django.contrib.gis.gdal import DataSource
from django.contrib.gis.gdal import DataSource, SRSException
srid = None
bbox_x0, bbox_y0, bbox_x1, bbox_y1 = None, None, None, None

if is_vector(filename):
y_min = -90
y_max = 90
x_min = -180
x_max = 180
datasource = DataSource(filename)
layer = datasource[0]
bbox_x0, bbox_y0, bbox_x1, bbox_y1 = layer.extent.tuple
srid = layer.srs.srid if layer.srs else 'EPSG:4326'
srs = layer.srs
try:
if not srs:
raise GeoNodeException('Invalid Projection. Layer is missing CRS!')
srs.identify_epsg()
except SRSException:
pass
epsg_code = srs.srid
# can't find epsg code, then check if bbox is within the 4326 boundary
if epsg_code is None and (x_min <= bbox_x0 <= x_max and
x_min <= bbox_x1 <= x_max and
y_min <= bbox_y0 <= y_max and
y_min <= bbox_y1 <= y_max):
# set default epsg code
epsg_code = '4326'
elif epsg_code is None:
# otherwise, stop the upload process
raise GeoNodeException(
"Invalid Layers. "
"Needs an authoritative SRID in its CRS to be accepted")

# eliminate default EPSG srid as it will be added when this function returned
srid = epsg_code if epsg_code else '4326'
elif is_raster(filename):
gtif = gdal.Open(filename)
gt = gtif.GetGeoTransform()
Expand Down Expand Up @@ -375,7 +401,7 @@ def get_bbox(filename):
bbox_y0 = min(ext[0][1], ext[2][1])
bbox_x1 = max(ext[0][0], ext[2][0])
bbox_y1 = max(ext[0][1], ext[2][1])
srid = srs.GetAuthorityCode(None) if srs else 'EPSG:4326'
srid = srs.GetAuthorityCode(None) if srs else '4326'

return [bbox_x0, bbox_x1, bbox_y0, bbox_y1, "EPSG:%s" % str(srid)]

Expand Down
6 changes: 6 additions & 0 deletions geonode/tests/integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,12 @@ def test_layer_upload_metadata(self):
uploaded.metadata_xml = thelayer_metadata
regions_resolved, regions_unresolved = resolve_regions(regions)
self.assertIsNotNone(regions_resolved)
except GeoNodeException as e:
# layer have projection file, but has no valid srid
self.assertEqual(
str(e),
"Invalid Layers. "
"Needs an authoritative SRID in its CRS to be accepted")
# except:
# # Sometimes failes with the message:
# # UploadError: Could not save the layer air_runways,
Expand Down

0 comments on commit 1e854d4

Please sign in to comment.