使用Python构建GeoJSON [英] Building a GeoJSON with Python

查看:988
本文介绍了使用Python构建GeoJSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想动态生成具有可变数量的多边形的geoJSON. 2个多边形的示例:

I want to generate dynamically a geoJSON with a variable number of polygons. Example for 2 polygons:

{
    "type": "FeatureCollection", 
    "features": [
      {"geometry": {
          "type": "GeometryCollection", 
          "geometries": [
              {
                  "type": "Polygon", 
                  "coordinates": 
                      [[11.0878902207, 45.1602390564],
                       [0.8251953125, 41.0986328125], 
                       [7.63671875, 48.96484375], 
                       [15.01953125, 48.1298828125]]
              }, 
              {
                  "type": "Polygon", 
                  "coordinates": 
                      [[11.0878902207, 45.1602390564], 
                       [14.931640625, 40.9228515625], 
                       [11.0878902207, 45.1602390564]]
              }
          ]
      }, 
      "type": "Feature", 
      "properties": {}}
    ]
}

我有一个函数,该函数可以为我提供每个多边形的坐标列表,因此我可以创建一个多边形列表,因此可以构建一个for循环对其进行迭代的geoJSON.

I have a function which gives me the list of coordinates for each polygon, so I can create a list of polygons, so I am able to build the geoJSON iterating it with a for loop.

问题是我不知道如何轻松地做到这一点(例如,我认为以字符串形式返回列表,但是将geoJSON构造为字符串似乎是个坏主意).

The problem is that I don't see how to do it easily (I thought for example in returning the list as a string, but building the geoJSON as a string looks like a bad idea).

有人向我提出了一个非常Python化的想法:

I have been suggested this very pythonic idea:

geo_json = [ {"type": "Feature",,
              "geometry": {
                  "type": "Point",
                  "coordinates": [lon, lat] }}
              for lon, lat in zip(ListOfLong,ListOfLat) ] 

但是因为我要添加可变数量的Polygons而不是点列表,所以这种解决方案似乎不合适.或者至少我不知道如何适应它.

But since I am adding a variable number of Polygons instead of a list of points, this solutions does not seem suitable. Or at least I don't know how to adapt it.

我可以将其构建为字符串,但是我想以一种更聪明的方式来实现.有什么主意吗?

I could build it as a string, but I'd like to do it in a smarter way. Any idea?

推荐答案

如果可以安装库,则django有一些不错的工具可以处理几何对象,并且这些对象具有geojson属性,可让您访问对象的GeoJSON表示形式:

If you can get the libraries installed, django has some good tools for dealing with geometry objects, and these objects have a geojson attribute, giving you access to the GeoJSON representation of the object:

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

>>> from django.contrib.gis.geos import Polygon, Point, MultiPoint, GeometryCollection
>>>
>>> poly = Polygon( ((0, 0), (0, 1), (1, 1), (0, 0)) )
>>> gc = GeometryCollection(Point(0, 0), MultiPoint(Point(0, 0), Point(1, 1)), poly)
>>> gc.geojson
u'{ "type": "GeometryCollection", "geometries": [ { "type": "Point", "coordinates": [ 0.0, 0.0 ] }, { "type": "MultiPoint", "coordinates": [ [ 0.0, 0.0 ], [ 1.0, 1.0 ] ] }, { "type": "Polygon", "coordinates": [ [ [ 0.0, 0.0 ], [ 0.0, 1.0 ], [ 1.0, 1.0 ], [ 0.0, 0.0 ] ] ] } ] }'

GeometryCollection也可以接受一系列几何对象:

GeometryCollection can also accept a list of geometry objects:

>>> polys = []
>>> for i in range(5):
...     poly = Polygon( ((0, 0), (0, 1), (1, 1), (0, 0)) )
...     polys.append(poly)
...
>>> gc = GeometryCollection(polys)

更新2019 :

匀称 github.com/alekzvik/shapely-geojson"rel =" nofollow noreferrer> shapely-geojson 现在可用,因为它不需要django,因此可能更容易引入.

shapely with shapely-geojson is now available can may be more easily to introduce as it doesn't required django.

这篇关于使用Python构建GeoJSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆