使用 Python 构建 GeoJSON
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16920700/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Building a GeoJSON with Python
提问by Roman Rdgz
I want to generate dynamically a geoJSON with a variable number of polygons. Example for 2 polygons:
我想动态生成具有可变数量多边形的 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": {}}
]
}
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 并使用 for 循环对其进行迭代。
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).
问题是我不知道如何轻松完成(例如,我认为将列表作为字符串返回,但将 geoJSON 构建为字符串看起来是个坏主意)。
I have been suggested this very pythonic idea:
有人向我提出了这个非常 Pythonic 的想法:
geo_json = [ {"type": "Feature",,
"geometry": {
"type": "Point",
"coordinates": [lon, lat] }}
for lon, lat in zip(ListOfLong,ListOfLat) ]
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?
我可以将它构建为字符串,但我想以更智能的方式来构建它。任何的想法?
采纳答案by monkut
If you can get the libraries installed, django has some good tools for dealing with geometry objects, and these objects have a geojsonattribute, giving you access to the GeoJSON representation of the object:
如果你可以安装这些库,django 有一些很好的工具来处理几何对象,这些对象有一个geojson属性,让你可以访问对象的 GeoJSON 表示:
https://docs.djangoproject.com/en/2.0/ref/contrib/gis/install/
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 can also accept a list of geometry objects:
GeometryCollection 还可以接受几何对象列表:
>>> polys = []
>>> for i in range(5):
... poly = Polygon( ((0, 0), (0, 1), (1, 1), (0, 0)) )
... polys.append(poly)
...
>>> gc = GeometryCollection(polys)
Update 2019:
2019 年更新:
shapelywith shapely-geojsonis now available can may be more easily to introduce as it doesn't required django.
匀称与匀称,以GeoJSON现已能可能会更容易,因为它不要求Django的介绍。
回答by gongzhitaao
- Since you've already know how to construct a
point, it's quite similar to construct apolygonobject. - you could use
json.dumpsto convert a python object to string
- 由于您已经知道如何构造 a
point,因此它与构造polygon对象非常相似。 - 您可以使用
json.dumps将python对象转换为字符串
Something like:
就像是:
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
回答by crisscross
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:
有 python-geojson 库(https://github.com/frewsxcv/python-geojson),这似乎使这项任务也容易得多。来自图书馆页面的示例:
>>> 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"}

