当前位置: 首页 > 工具软件 > GeoDjango > 使用案例 >

GeoDjango原文阅读

张晨朗
2023-12-01

Putting your data on the map

Geographic Admin

图形数据后台管理

GeoDjango extends Django’s admin application with support for editing geometry fields.

Basics

GeoDjango also supplements the Django admin by allowing users to create and modify geometries on a JavaScript slippy map (powered by OpenLayers).

Let’s dive right in. Create a file called admin.py inside the world application with the following code:

from django.contrib.gis import admin
from .models import WorldBorder

admin.site.register(WorldBorder, admin.GeoModelAdmin)

Next, edit your urls.py in the geodjango application folder as follows:

from django.contrib.gis import admin
from django.urls import include, path

urlpatterns = [
    path('admin/', admin.site.urls),
]

大致意思是可以采用admin对geo数据进行管理,方式方法和常规的admin类似,不知道支持不支持xadmin。

GeoDjango Forms API

GeoDjango provides some specialized form fields and widgets in order to visually display and edit geolocalized data on a map. By default, they use OpenLayers-powered maps, with a base WMS layer provided by NASA.

Field arguments

In addition to the regular form field arguments, GeoDjango form fields take the following optional arguments.

srid

Field.srid

This is the SRID code that the field value should be transformed to. For example, if the map widget SRID is different from the SRID more generally used by your application or database, the field will automatically convert input values into that SRID.

geom_type

Field.geom_type

You generally shouldn’t have to set or change that attribute which should be setup depending on the field class. It matches the OpenGIS standard geometry name.

Form field classes

GeometryField

class GeometryField

PointField

class PointField

LineStringField

class LineStringField

PolygonField

class PolygonField

MultiPointField

class MultiPointField

MultiLineStringField

class MultiLineStringField

MultiPolygonField

class MultiPolygonField

GeometryCollectionField

class GeometryCollectionField

GeoDjango Database API

Spatial Backends

GeoDjango currently provides the following spatial database backends:

  • django.contrib.gis.db.backends.postgis
  • django.contrib.gis.db.backends.mysql
  • django.contrib.gis.db.backends.oracle
  • django.contrib.gis.db.backends.spatialite

MySQL Spatial Limitations

MySQL’s spatial extensions only support bounding box operations (what MySQL calls minimum bounding rectangles, or MBR). Specifically,MySQL does not conform to the OGC standard:

Currently, MySQL does not implement these functions [ContainsCrossesDisjointIntersectsOverlapsTouchesWithin] according to the specification. Those that are implemented return the same result as the corresponding MBR-based functions.

In other words, while spatial lookups such as contains are available in GeoDjango when using MySQL, the results returned are really equivalent to what would be returned when using bbcontains on a different spatial backend.

Warning

True spatial indexes (R-trees) are only supported with MyISAM tables on MySQL. [5] In other words, when using MySQL spatial extensions you have to choose between fast spatial lookups and the integrity of your data – MyISAM tables do not support transactions or foreign key constraints.

Raster Support

RasterField is currently only implemented for the PostGIS backend. Spatial lookups are available for raster fields, but spatial database functions and aggregates aren’t implemented for raster fields.

Compatibility Tables

Spatial Lookups

The following table provides a summary of what spatial lookups are available for each spatial database backend. The PostGIS Raster (PGRaster) lookups are divided into the three categories described in the raster lookup details: native support N, bilateral native support B, and geometry conversion support C.

Lookup TypePostGISOracleMySQL [6]SpatiaLitePGRaster
bbcontainsX XXN
bboverlapsX XXN
containedX XXN
containsXXXXB
contains_properlyX   B
coveredbyXX  B
coversXX  B
crossesX  XC
disjointXXXXB
distance_gtXX XN
distance_gteXX XN
distance_ltXX XN
distance_lteXX XN
dwithinXX XB
equalsXXXXC
exactXXXXB
intersectsXXXXB
isvalidXXX (≥ 5.7.5)X (LWGEOM) 
overlapsXXXXB
relateXX XC
same_asXXXXB
touchesXXXXB
withinXXXXB
leftX   C
rightX   C
overlaps_leftX   B
overlaps_rightX   B
overlaps_aboveX   C
overlaps_belowX   C
strictly_aboveX   C
strictly_belowX   C

GeoDjango Model API  Spatial Field Types//

Spatial fields consist of a series of geometry field types and one raster field type. Each of the geometry field types correspond to the OpenGIS Simple Features specification [1]. There is no such standard for raster data.

GeometryField

class GeometryField

The base class for geometry fields.

PointField

class PointField

Stores a Point.

LineStringField

class LineStringField

Stores a LineString.

PolygonField

class PolygonField

Stores a Polygon.

MultiPointField

class MultiPointField

Stores a MultiPoint.

MultiLineStringField

class MultiLineStringField

Stores a MultiLineString.

MultiPolygonField

class MultiPolygonField

Stores a MultiPolygon.

GeometryCollectionField

class GeometryCollectionField

Stores a GeometryCollection.

RasterField

class RasterField

Stores a GDALRaster.

RasterField is currently only implemented for the PostGIS backend.

 类似资料: