SRSExceptions

40 views
Skip to first unread message

Zac

unread,
Mar 30, 2017, 10:28:31 PM3/30/17
to geodjango
I have a couple of questions on SRIDs in GeoDjango that I'm hoping someone can shed some light on.  

Question 1:

Lets assume that I create a custom SRS and add it to PostGIS' spatial_ref_sys table something similar to:

cursor.execute("""INSERT into spatial_ref_sys (srid, auth_name, auth_srid, proj4text)
                          VALUES ( 96630, 'sr-org', 6630, '+proj=aea +lat_1=29.5 +lat_2=45.5 +lat_0=23 +lon_0=-96 +x_0=0 +y_0=0 +ellps=GRS80 +datum=NAD83 +units=m +no_defs ')""")

I have no problem using that SRID on geometry fields (perhaps only because they are unmanaged):

class Units(models.Model):
year = models.IntegerField(blank=True, null=True)
unit_type = models.CharField(max_length=40, blank=True, null=True)
metadata = JSONField()
source = models.CharField(max_length=40, blank=True, null=True)
source_id = models.CharField(max_length=40, blank=True, null=True)
geom = models.MultiPolygonField(srid=96630, blank=True, null=True)
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)

class Meta:
managed = False
db_table = 'units'
verbose_name = "Unit"


Later on when I try to take WGS84 Lat/Long coordinates from a form and transform it to my SRID 96630 I get an SRSException similar to the following:

GDAL_ERROR 6: EPSG PCS/GCS code 96630 not found in EPSG support files.  Is this a valid
EPSG coordinate system?
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "C:\Users\jzmiller1\VirtualEnvs\GeoDjangox64\lib\site-packages\django\contrib\gis\geos\geometry.py", line 534, in transform
    g = gdal.OGRGeometry(self.wkb, srid)
  File "C:\Users\jzmiller1\VirtualEnvs\GeoDjangox64\lib\site-packages\django\contrib\gis\gdal\geometries.py", line 117, in __init__
    self.srs = srs
  File "C:\Users\jzmiller1\VirtualEnvs\GeoDjangox64\lib\site-packages\django\contrib\gis\gdal\geometries.py", line 273, in _set_srs
    sr = SpatialReference(srs)
  File "C:\Users\jzmiller1\VirtualEnvs\GeoDjangox64\lib\site-packages\django\contrib\gis\gdal\srs.py", line 95, in __init__
    self.import_epsg(srs_input)
  File "C:\Users\jzmiller1\VirtualEnvs\GeoDjangox64\lib\site-packages\django\contrib\gis\gdal\srs.py", line 288, in import_epsg
    capi.from_epsg(self.ptr, epsg)
  File "C:\Users\jzmiller1\VirtualEnvs\GeoDjangox64\lib\site-packages\django\contrib\gis\gdal\prototypes\errcheck.py", line 119, in check_errcode
    check_err(result, cpl=cpl)
  File "C:\Users\jzmiller1\VirtualEnvs\GeoDjangox64\lib\site-packages\django\contrib\gis\gdal\error.py", line 72, in check_err
    raise e(msg)
SRSException: Unsupported SRS.

Can I somehow add my custom SRIDs to the GFAL EPSG support files or would that be a bad thing to do?  Could I specify somewhere at the GeoDjango level that transformations should be do at the PostGIS level somehow?

Question 2:

Is is possible to create a geometry column with an unspecified SRID and store geometries with an SRID on a per row basis?



Sergey Fedoseev

unread,
Mar 31, 2017, 1:34:53 PM3/31/17
to geodjango
Maybe this could help:

In [44]: from django.contrib.gis.gdal import SpatialReference

In [45]: from django.contrib.gis.geos import Point

In [46]: srs = SpatialReference('+proj=aea +lat_1=29.5 +lat_2=45.5 +lat_0=23 +lon_0=-96 +x_0=0 +y_0=0 +ellps=GRS80 +datum=NAD83 +units=m +no_defs ')

In [47]: p = Point(1, 2, srid=4326)

In [48]: print(p.transform(srs, clone=True))
SRID=4269;POINT (10311643.19648741 3605390.220549587)


Zac Miller

unread,
Mar 31, 2017, 5:02:18 PM3/31/17
to geod...@googlegroups.com
That got me thinking for sure.  I was able to make it work by going into my GDAL_DATA and mofiying epsg.wkt to add an additional include for a new custom.wkt file.  I added my custom EPSG/SRID and an OGR WKT description of the coordinate system and the transform worked fine through GeoDjango.  I'll just have to add some code in my initial migration to not only add my new coordinate system to spatial_ref_sys in PostGIS but also to add it in the GDAL_DATA.

Thanks!

I'm still interested in any ideas people might have for question #2.


  

--
You received this message because you are subscribed to the Google Groups "geodjango" group.
To unsubscribe from this group and stop receiving emails from it, send an email to geodjango+unsubscribe@googlegroups.com.
To post to this group, send email to geod...@googlegroups.com.
Visit this group at https://groups.google.com/group/geodjango.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages