Gent Bytyqi
Gent Bytyqi

Reputation: 419

Geodjango create a new SRID

Hello guys I've been trying to add a shapefile but I'm facing some difficulties since my country's coordinate system isn't part of the gdal library. How can i create a new SRID.I have already created the srs and defined the spatial reference system in postgis.

Upvotes: 2

Views: 941

Answers (2)

Antonin Rousset
Antonin Rousset

Reputation: 131

If your country SRID is not supported by GDAL - or if you want to add your own reference system - I was able to achieve this using postGIS by adding an entry in public.spatial_ref_sys either manually:

INSERT INTO public.spatial_ref_sys (srid, proj4text) VALUES
($srid, $proj);

or with django by getting the table:

from django.db import DEFAULT_DB_ALIAS, connections

database = DEFAULT_DB_ALIAS
SpatialRefSys = connections[database].ops.spatial_ref_sys()
SpatialRefSys.objects.create(srid=$srid, proj4text=$proj)

Have in mind that you won't be able to transform GeosGeometry objects as this uses GDAL (which does not read spatial_ref_sys). Instead use the database function as follow:

model.objects.annotate(position_wgs84=Transform('position', 4326))

Upvotes: 0

Jieter
Jieter

Reputation: 4229

PostGIS stores Spatial reference systems in a table called spatial_ref_sys. Django has a model to access the data in this table called SpatialRefSys, but it's not documented.

Also not documented is the utility function add_srs_entry(), which takes an SpatialReference instance as first argument, which can be defined from a PROJ.4 string.

So something like this should work:

from django.contrib.gis.utils.srs import add_srs_entry
from django.contrib.gis.gdal import SpatialReference

srs = SpatialReference('''...PROJ.4 string...''')
add_srs_entry(srs)

Alternatively, you can add the entry using a database management tool

Upvotes: 2

Related Questions