如何将EPSG 900913添加到geodjango spatialite数据库?

时间:2012-11-05 11:59:00

标签: google-maps geodjango bitnami spatialite epsg

我正在尝试在Linux系统上使用this snippet在我的管理界面中添加Google地图窗口小部件(目前在VMWare播放器中的Bitnami django堆栈上本地运行)。

地图渲染,但我的数据库中的点要素(真正的任何特征)没有显示在地图上,当试图通过地图界面注册点时,我收到错误:

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

我从geodjango docs实现了初始化spatialite / sqlite数据库时不包含谷歌空间参考系统,解决方案应该是发出以下命令,添加SRS:

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

但是,当我从项目目录中执行此操作时,我得到:

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

我已确认已安装GDAL,GEOS和PROJ4,并且已将环境变量GDAL_DATA和PROJ_LIB添加到我的.profile。我检查了 /usr/local/share/gdal/gcs.csv 文件,该文件似乎没有900913的条目(我已经google了其他版本的gcs.csv,但似乎没有包含900913)。我认为这是导致错误。但是,同一目录中的 cubewerx_extra.wkt 确实有900913的WKT条目。

我的问题是:如何让 add_srs_entry 找到正确的SRS表示以便将其添加到我的数据库中?或者有一种解决方法,例如以某种方式转换WKT表示并手动将其插入gcs.csv?

我感谢任何帮助!

修改 我找到了一种手动将EPSG 900913插入spatialite数据库的方法。该解决方案的灵感来自http:// trac.osgeo.org/openlayers/wiki/SphericalMercator上的sql语句(抱歉,我没有足够的声誉发布更多链接)并使用原始sql发布到数据库后端(如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()

我已确认该条目现在位于spatial_ref_sys表中。但是在尝试在admin-interface中添加点时,我仍然遇到同样的错误。点可以添加到地图中,但在尝试保存该功能时,我收到错误:

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

上面的sql语句是否正确?是否足够,或者add_srs_entry还做其他事情吗?

最后,它可能是我的应用程序中的编码问题,我将处理一个最小的测试示例并发布它...

2 个答案:

答案 0 :(得分:1)

好的,我找到了主要问题的答案,正如编辑帖子所示。

为了将来参考,以下是一种如何将Google球形投影添加到空间数据库(必须已在空间上启用)的方法:

1)创建一个包含以下内容的文本文件:

  

BEGIN;

     

INSERT到spatial_ref_sys(srid,auth_name,auth_srid,ref_sys_name,proj4text)值(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)在包含spaceite数据库的目录中使用名称 init_EPSG900913.sql 保存文件。

3)发出以下命令在数据库上执行SQL语句:

  

spatialite some_database.sqlite< init_EPSG900913.sql

替代方法 - 来自django-script或“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()

使用这两种方法中的任何一种,您的数据库都会注册Google Maps reference sytstem。

答案 1 :(得分:1)

事实证明,缺少的EPSG定义只是问题的一部分。 另一部分与应用程序在bitnami ubuntu django堆栈上运行的事实有关。

当遵循bitnami ubuntu django堆栈上的geodjango文档中的安装指南时,所有额外的python包和空间库都安装在系统文件夹/user/local/..something中..而不是在自包含的bitnami中环境。

为了将来参考,请确保在安装其他python包之前发出以下语句:

$ sudo su
$ /opt/bitnami/use_djangostack

然后将在bitnami环境中安装包。

此外,使用./configure命令配置不同空间库的构建时,必须添加额外选项以将共享文件放在bitnami环境中。 我通常使用类似的东西:

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

可能必须按照geodjango文档中的描述传递其他参数 - 但必须将这些参数中指定的路径更改为指向/ opt / bitnami /...的适当子目录。