MySQL 支持GeoDjango的简单说明

来源:互联网 发布:自学java多久能就业 编辑:程序博客网 时间:2024/05/17 03:57

配置GeoDjango的具体过程可参考官方文档:

https://docs.djangoproject.com/en/1.9/ref/contrib/gis/


1.安装

(1)安装GEOS

Database

Library Requirements

Supported Versions

Notes

PostgreSQL

GEOS, PROJ.4, PostGIS

9.1+

Requires PostGIS.

MySQL

GEOS

5.5+

Not OGC-compliant.

Oracle

GEOS

11.2+

XE not supported.

SQLite

GEOS, GDAL, PROJ.4, SpatiaLite

3.6.+

Requires SpatiaLite 2.4+, pysqlite2 2.5+

 

从上表可知:对于MySQL,只需安装GEOS;

           对于PostgreSQL,需要安装PROJ.4、PostGIS

           对于SQLite,需安装PROJ.4、GDAL、SpatiaLite

 

GEOS是一个支持空间几何运算C++库,关于它的详细介绍,可参考官方文档:

https://docs.djangoproject.com/en/1.9/ref/contrib/gis/install/geolibs/

 

MySQL没有完全支持OGC标准,所以对空间数据的CRUD会有一些限制,详细可参考文档:

https://docs.djangoproject.com/en/1.9/ref/contrib/gis/db-api/#mysql-spatial-limitations

 

由于目前我们服务器上使用的是MySQL,所以按MySQL进行配置:安装GEOS

先下载geos

$ wget http://download.osgeo.org/geos/geos-3.4.2.tar.bz2$ tar xjf geos-3.4.2.tar.bz2

然后解压、编译、安装

$ cd geos-3.4.2$ ./configure$ make$ sudo make install$ cd ..

执行完上述几步,想知道GEOS是否安装正确,可用下面的命令:

进入python shell,键入下面的命令,如果不报错,则认为安装正确。

from django.contrib.gis.dbimport models

(2)修改settings.py

a.  在INSTALLED_APPS中添加 'django.contrib.gis',

b. 修改数据库引擎为  'ENGINE': 'django.contrib.gis.db.backends.mysql',

(3)在models.py中引入models

from django.contrib.gis.db import models

同时可以注释掉from django.db import models

(4)测试:

在models.py(比如Person表)中,添加以下字段:

point = models.PointField(null=True,blank=True)

然后运行: 

./manage.py makemigrations

如果不报错,则视为正确。

除了PointField以外,还有多个空间类型,如下:

  • Spatial Field Types
    • GeometryField
    • PointField
    • LineStringField
    • PolygonField
    • MultiPointField
    • MultiLineStringField
    • MultiPolygonField
    • GeometryCollectionField
    • RasterField

具体可参考GeoDjango Models API的文档:

https://docs.djangoproject.com/en/1.9/ref/contrib/gis/model-api/#spatial-field-types

0 0
原创粉丝点击