Question

I'm trying to include a Google Maps widget in my admin-interface using this snippet on a Linux system (presently running locally on a Bitnami django stack in VMWare Player).

The map renders, but point features (any features really) in my database are not showing up on the map, and when trying to register points through the map interface, I get an error that:

An error occurred when transforming the geometry to the SRID of the geometry form field.

I realized from the geodjango docs that the Googles spatial reference system is not included when initializing the spatialite/sqlite database, and the solution should be to issue the following commands, to add the SRS:

$ python manage shell
>>> from django.contrib.gis.utils import add_srs_entry
>>> add_srs_entry(900913)

However, when I do this from my project directory, I get:

ERROR 6: EPSG PCS/GCS code 900913 not found in EPSG support files.  Is this a valid
EPSG coordinate system?

I have confirmed that GDAL, GEOS and PROJ4 is installed, and I have added environment variables GDAL_DATA and PROJ_LIB to my .profile. I have checked the /usr/local/share/gdal/gcs.csv file which appears to not have an entry for 900913 (I have googled for other versions of gcs.csv, but none seem to contain 900913). I assume this is causing the error. However, the cubewerx_extra.wkt in the same directory does have a WKT entry for 900913.

My question is: How do I make add_srs_entry find the right SRS representation in order to add it to my database? Or is there a work-around, e.g. somehow converting the WKT representation and inserting it manually in gcs.csv?

I appreciate any help!

EDIT: I found a way to manually insert the EPSG 900913 into the spatialite database. The solution is inspired by the sql-statement found at http:// trac.osgeo.org/openlayers/wiki/SphericalMercator (sorry, I don't have enough reputation to post more links) and issued to the database backend using raw sql (as described in the docs at https:// docs.djangoproject.com/en/dev/topics/db/sql/#executing-custom-sql-directly):

from django.db import connection, transaction
cursor = connection.cursor()
sql = "INSERT into spatial_ref_sys (srid, auth_name, auth_srid, ref_sys_name, proj4text) values (900913 ,'EPSG',900913,'Google Maps Global Mercator','+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +no_defs');"
cursor.execute(sql)
transaction.commit_unless_managed()

I've confirmed that the entry is now in the spatial_ref_sys table. But I am still getting the same error when trying to add points in the admin-interface. Points can be added to the map, but when trying to save the feature, I get the error:

An error occurred when transforming the geometry to the SRID of the geometry form field.

Is the above sql statement correct? Is it sufficient, or does the add_srs_entry do other things as well?

Finally it could be a coding problem in my application, I will work on a minimal test-example and post it...

Was it helpful?

Solution

OK, I found the answer to the main question, as also indicated under the edit-post.

For future reference, here is a method how to add the Google spherical projection to a spatialite database (which must be already be spatially enabled):

1) Create a text file with the following content:

BEGIN;

INSERT into spatial_ref_sys (srid, auth_name, auth_srid, ref_sys_name, proj4text) values (900913,'EPSG',900913,'Google Maps Global Mercator','+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +no_defs');

COMMIT;

2) Save the file with a name like init_EPSG900913.sql in the directory holding you spatialite database.

3) Issue the following command to execute the SQL statement on the database:

spatialite some_database.sqlite < init_EPSG900913.sql

Alternative method - From inside django-script or in "python manage.py shell":

from django.db import connection, transaction
cursor = connection.cursor()
sql = "INSERT into spatial_ref_sys (srid, auth_name, auth_srid, ref_sys_name, proj4text) values (900913 ,'EPSG',900913,'Google Maps Global Mercator','+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +no_defs');"
cursor.execute(sql)
transaction.commit_unless_managed()

With either of these two methods, your database will have the Google Maps reference sytstem registered.

OTHER TIPS

It turns out, that the missing EPSG definition was only part of the problem. The other part was related to the fact that the app is running on a bitnami ubuntu django stack.

When following the installation guide in the geodjango docs on the bitnami ubuntu django stack, all the extra python packages and spatial libraries are installed in the system folders /user/local/..something.. not in the self-contained bitnami environment.

For future reference, make sure to issue the following statements before installing additional python packages:

$ sudo su
$ /opt/bitnami/use_djangostack

Then packages will be installed in the bitnami environment.

Also, when configuring builds of the different spatial libraries with the ./configure command, extra options must be added to place the shared files in the bitnami environment. I have typically used something like:

$ ./configure --prefix=/opt/bitnami/common

Additional arguments might have to be passed as described in the geodjango docs - but the paths specified in these arguments must be changed to point to the proper subdirectories of /opt/bitnami/...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top