14

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

{
    "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": {}}
    ]
}

我有一个函数,它给了我每个多边形的坐标列表,所以我可以创建一个多边形列表,所以我可以构建 geoJSON 并使用 for 循环对其进行迭代。

问题是我看不到如何轻松做到这一点(例如,我认为将列表作为字符串返回,但将 geoJSON 构建为字符串看起来是个坏主意)。

我被建议这个非常pythonic的想法:

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

但是由于我添加了可变数量的多边形而不是点列表,因此这种解决方案似乎不合适。或者至少我不知道如何适应它。

我可以将其构建为字符串,但我想以更智能的方式进行。任何的想法?

4

3 回答 3

26

There is the python-geojson library (https://github.com/frewsxcv/python-geojson), which seems to make this task also much easier. Example from the library page:

>>> from geojson import Polygon

>>> Polygon([[(2.38, 57.322), (23.194, -20.28), (-120.43, 19.15), (2.38,   57.322)]])  
{"coordinates": [[[2.3..., 57.32...], [23.19..., -20.2...], [-120.4..., 19.1...]]], "type": "Polygon"}
于 2015-04-18T22:34:24.913 回答
11

如果你可以安装库,django 有一些很好的工具来处理几何对象,这些对象有一个geojson属性,让你可以访问对象的 GeoJSON 表示:

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 也可以接受几何对象的列表:

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

2019 年更新

shapelyshapely-geojson现在可用,因为它不需要 django,所以可能更容易引入。

于 2013-06-04T15:26:37.470 回答
10
  1. 由于您已经知道如何构造 a ,因此构造对象point非常相似。polygon
  2. 您可以使用json.dumps将 python 对象转换为字符串

就像是:

geos = []
for longs,lats in LongLatList
    poly = {
        'type': 'Polygon',
        'coordinates': [[lon,lat] for lon,lat in zip(longs,lats) ]
    }
    geos.append(poly)

geometries = {
    'type': 'FeatureCollection',
    'features': geos,
}

geo_str = json.dumps(geometries) // import json
于 2013-06-04T15:12:16.537 回答