我正在尝试使用 this snippet 在我的管理界面中包含 Google map 小部件在 Linux 系统上(目前在 VMWare Player 中的 Bitnami django 堆栈上本地运行)。
map 呈现,但我的数据库中的点要素(实际上是任何要素)没有显示在 map 上,当尝试通过 map 界面注册点时,我收到错误:
An error occurred when transforming the geometry to the SRID of the geometry form field.
我从geodjango docs中意识到初始化spatialite/sqlite数据库时不包含Google的空间引用系统,解决方案应该是发出以下命令,添加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 插入空间数据库的方法。该解决方案的灵感来自于 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 表中。但当我尝试在管理界面中添加点时,我仍然遇到相同的错误。可以将点添加到 map 上,但是当尝试保存该功能时,出现错误:
An error occurred when transforming the geometry to the SRID of the geometry form field.
上面的sql语句正确吗?这是否足够,或者 add_srs_entry 还可以做其他事情吗?
最后,这可能是我的应用程序中的编码问题,我将编写一个最小的测试示例并将其发布...
最佳答案
好的,我找到了主要问题的答案,正如编辑帖子下所示。
为了供将来引用,以下是如何将 Google 球形投影添加到空间数据库(必须已启用空间功能)的方法:
1)创建一个包含以下内容的文本文件:
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) 将文件以 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 map 引用系统。
关于google-maps - 如何将 EPSG 900913 添加到 geodjango Spatialite 数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13231676/