From df3faf2434d773a9dfe564dc051f524faeeade62 Mon Sep 17 00:00:00 2001 From: Boney Bun Date: Fri, 22 Jun 2018 02:55:25 +0700 Subject: [PATCH 1/3] fix a bug on determining srid when upload a vector layer fix #289 fix #435 --- geonode/layers/utils.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/geonode/layers/utils.py b/geonode/layers/utils.py index 3f83fd63184..45bd21759c7 100644 --- a/geonode/layers/utils.py +++ b/geonode/layers/utils.py @@ -347,7 +347,9 @@ def get_bbox(filename): 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 = layer.srs.srid \ + if layer.srs and layer.srs.srid is not None else '4326' elif is_raster(filename): gtif = gdal.Open(filename) gt = gtif.GetGeoTransform() @@ -375,7 +377,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)] From a3cd0c075ef3a82459ce85ddc278ee313357ab8d Mon Sep 17 00:00:00 2001 From: Boney Bun Date: Sat, 23 Jun 2018 15:37:20 +0700 Subject: [PATCH 2/3] replace GDAL's DataSource with osr function for proper EPSG code detection --- geonode/layers/utils.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/geonode/layers/utils.py b/geonode/layers/utils.py index 45bd21759c7..331e8565514 100644 --- a/geonode/layers/utils.py +++ b/geonode/layers/utils.py @@ -344,12 +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 Exception("A prj file can't be found. Please add prj file") + 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 # eliminate default EPSG srid as it will be added when this function returned - srid = layer.srs.srid \ - if layer.srs and layer.srs.srid is not None else '4326' + srid = epsg_code if epsg_code else '4326' elif is_raster(filename): gtif = gdal.Open(filename) gt = gtif.GetGeoTransform() From 45e0ec4d959b2bfa96b34a274d6da3086dd281a8 Mon Sep 17 00:00:00 2001 From: Boney Bun Date: Sat, 30 Jun 2018 22:51:17 +0700 Subject: [PATCH 3/3] change the error message when prj file not present so it can pass the unit test --- geonode/layers/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/geonode/layers/utils.py b/geonode/layers/utils.py index 331e8565514..788db9128bd 100644 --- a/geonode/layers/utils.py +++ b/geonode/layers/utils.py @@ -350,7 +350,7 @@ def get_bbox(filename): try: prj_file = open(prj_path, 'r') except Exception: - raise Exception("A prj file can't be found. Please add prj file") + raise GeoNodeException("Invalid Projection. Layer is missing CRS!") prj_txt = prj_file.read() srs = osr.SpatialReference(wkt=prj_txt) srs.AutoIdentifyEPSG()