Skip to content

Commit

Permalink
fix a bug on determining srid when upload a vector layer (#437)
Browse files Browse the repository at this point in the history
* fix a bug on determining srid when upload a vector layer
* replace GDAL's DataSource with osr function for proper EPSG code detection
* add the error message when prj file not present so it can pass the unit test

fix #289

fix #435
  • Loading branch information
boney-bun authored Jun 30, 2018
1 parent 0ea02f2 commit 30828ab
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions geonode/layers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,10 +344,22 @@ def get_bbox(filename):
bbox_x0, bbox_y0, bbox_x1, bbox_y1 = None, None, None, None

if is_vector(filename):
# gdal's SourceData seems to be unreliable in determining EPSG code.
# obtain EPSG code from a prj file instead
prj_path = filename.split(".shp")[0] + ".prj"
try:
prj_file = open(prj_path, 'r')
except Exception:
raise GeoNodeException("Invalid Projection. Layer is missing CRS!")
prj_txt = prj_file.read()
srs = osr.SpatialReference(wkt=prj_txt)
srs.AutoIdentifyEPSG()
epsg_code = srs.GetAuthorityCode(None)
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'
# 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 +387,8 @@ 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'
# eliminate default EPSG srid as it will be added when this function returned
srid = srs.GetAuthorityCode(None) if srs else '4326'

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

Expand Down

0 comments on commit 30828ab

Please sign in to comment.