Creating GeoJSON with Python

I want to dynamically generate 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": {}} ] } 

I have a function that gives me a list of coordinates for each polygon so that I can create a list of polygons so that I can build geoJSON by repeating it with a for loop.

The problem is that I don’t see how easy it is to do (I thought, for example, returning a list as a string, but building geoJSON as a string seems like a bad idea).

I was offered this very pythonic idea:

 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 solution does not seem appropriate. Or at least I don’t know how to adapt it.

I could build it as a string, but I would like to make it smarter. Any idea?

+8
source share
3 answers

If you can install libraries, django has some good tools for working with geometric 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 can also accept a list of geometric objects:

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

Update 2019:

shapely with shapely-geojson is now available, it can be easier to enter, since it does not require django.

+7
source

There is a python-geojson library ( https://github.com/frewsxcv/python-geojson ) that seems to make this task a lot 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"} 
+15
source
  • Since you already know how to build a point , it is very similar to a polygon object.
  • you can use json.dumps to convert python object to string

Sort of:

 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 
+7
source

Source: https://habr.com/ru/post/1484427/


All Articles