django-rest框架的geodata扩展
django-spillwa的Python项目详细描述
Django和Django REST Framework集成光栅和基于特征的地理数据。
溢洪道建立在伟大的Django休息框架上,通过提供 用于处理地理空间格式(如geotiff、geojson和 公里/公里z。
特别注意几何图形的快速序列化 空间后端,避免了在 Python。
基本用法
向api添加向量响应格式,如geojson、kml/kmz和svg。
# models.pyfromdjango.contrib.gis.dbimportmodelsfromspillway.queryimportGeoQuerySetclassLocation(models.Model):slug=models.SlugField()geom=models.GeometryField()objects=GeoQuerySet.as_manager()# urls.pyfromdjango.conf.urlsimporturlfromspillwayimportgenericsfrom.modelsimportLocationurlpatterns=[url(r'^locations/(?P<slug>[\w-]+)/$',generics.GeoDetailView.as_view(queryset=Location.objects.all()),name='location'),url(r'^locations/$',generics.GeoListView.as_view(queryset=Location.objects.all()),name='location-list'),]
作为geojson检索所有位置:
curl -H 'Accept: application/vnd.geo+json' 127.0.0.1:8000/locations/
将几何图形简化并重新投影到另一个坐标系:
curl -H 'Accept: application/vnd.geo+json' '127.0.0.1:8000/locations/?srs=3857&simplify=100'
任何spatial lookup 后端支持的可用于搜索。例如,找到 与特定点相交:
curl -g '127.0.0.1:8000/locations/?intersects={"type":"Point","coordinates":[-120,38]}'
还提供光栅数据支持。
# models.pyfromspillway.modelsimportAbstractRasterStorefromspillway.queryimportGeoQuerySetclassRasterStore(AbstractRasterStore):objects=GeoQuerySet.as_manager()# urls.pyfromdjango.conf.urlsimporturlfromspillwayimportgenericsfrom.modelsimportRasterStoreurlpatterns=[url(r'^rstores/(?P<slug>[\w-]+)/$',generics.RasterDetailView.as_view(queryset=RasterStore.objects.all()),name='rasterstore'),url(r'^rstores/$',generics.RasterListView.as_view(queryset=RasterStore.objects.all()),name='rasterstore-list'),]
返回json,其中包含给定边界框的二维像素值数组:
curl 'http://127.0.0.1:8000/rstores/tasmax/?bbox=-107.74,37.39,-106.95,38.40'
可以裁剪带有几何图形的光栅图像并返回 结果:
curl -H 'Accept: application/zip' 'http://127.0.0.1:8000/rstores/?g=-107.74,37.39,-106.95,38.40'
常规视图
溢洪道使用geojson和kml/kmz扩展了rest框架的通用视图 地理数据的渲染器。这包括对功能和所有 正在使用的空间后端的可用空间查找/筛选器。
视图集
对于地理熟悉的和栅格化的模型,视图集合存在于熟悉的用法之后。 django-rest框架的模式。当前,可写光栅视图集需要 添加和测试,尽管只读的品种是可用的。fromspillwayimportviewsetsfrom.modelsimportLocation,RasterStoreclassLocationViewSet(viewsets.GeoModelViewSet):queryset=Location.objects.all()classRasterViewSet(viewsets.ReadOnlyRasterModelViewSet):queryset=RasterStore.objects.all()
地图块
tileview和rastertileview分别可用于生成 矢量或图像贴图平铺。图像平铺需要可选的依赖关系mapnik, 所以一定要安装。在本例中,geojson或png tiles可以是 为位置地理模型请求,或为rasterstore数据集请求png平铺。 这里显示的url使用“/{z}/{x}/{y}.{format}”的方案。
fromdjango.conf.urlsimporturlfromspillwayimportviews,urlsfrom.modelsimportLocation,RasterStoreurlpatterns=[url(urls.tilepath('^locations/),views.TileView.as_view(queryset=Location.objects.all()),name='location-tiles'),url(urls.tilepath('^tiles/(?P<slug>\d+)/'),views.RasterTileView.as_view(queryset=RasterStore.objects.all()),name='map-tiles'),]
确保通过web服务器或django的配置缓存映射块 在开发环境之外提供服务时使用缓存框架。
渲染器
到目前为止,已经有了通用栅格和矢量数据格式的渲染器,即 压缩的geotiff、jpeg、png和erdas imagine,以及geojson、kml/kmz和svg。
测试
使用创建virtualenvvirtualenvwrapper, 安装依赖项并运行测试。在Python2.7上,使用 Spatialite需要启用扩展加载的pysqlite构建。在3.x上, 没有它一切都很好。
mkvirtualenv spillway # Only the following when testing on 2.7, not needed with 3.x. pip install --global-option=build_ext --global-option='-USQLITE_OMIT_LOAD_EXTENSION' pysqlite pip install -r requirements.txt Pillow make check