2016-06-10 11 views
0
GeoJSON erstellen

Ich habe ein Feature Collection von Polygons und MultiPolygons und ich muß es zuerst in einer temporären Datei schreiben, um sie dann mit geopandas.GeoDataFrame.from_file laden (tmp_json_file), ich suche eine Möglichkeit, dies ohne die temporäre Datei zu tun. Ich habe versucht, geopandas.GeoDataFrame.from_feature() zu verwenden, es funktioniert ziemlich gut für Feature Collection von einfachen Polygon, aber ich kann es nicht für Feature Collection von Polygons und MultiPolygons arbeiten, dachte ich über etwas wie unten, aber es funktioniert noch nicht.Wie eine Feature-Sammlung von einem

features_collection = [] 

for feature in json_data['features']: 
    tmp_properties = {'id': feature['properties']['id']} 

    if is_multipolygon (feature): 
     tmp = Feature(geometry=MultiPolygon((feature['geometry']['coordinates'])), properties=tmp_properties) 
    else: 
     Feature(geometry=Polygon((feature['geometry']['coordinates'])), properties=tmp_properties) 
    features_collection.append(tmp) 

collection = FeatureCollection(features_collection) 

return geopandas.GeoDataFrame.from_features(collection['features']) 

Die GeoJSON von einer API genommen wird, das Gebiet der Rückkehr (einige Gebiet werden von einem einzigen Polygon, andere durch eine Menge von Polygonen (formatiert als ein Multi) modelliert.

Die GeoJSON strukturiert sind wie folgt : http://pastebin.com/PPdMUGkY

ich bin von der Funktion über den folgenden Fehler erhalten: dieses

Traceback (most recent call last): 
    File "overlap.py", line 210, in <module> 
    print bdv_json_to_geodf(contours_bdv) 
    File "overlap.py", line 148, in json_to_geodf 
    return geopandas.GeoDataFrame.from_features(collection['features']) 
    File "/Library/Python/2.7/site-packages/geopandas/geodataframe.py", line 179, in from_features 
    d = {'geometry': shape(f['geometry'])} 
    File "/Library/Frameworks/GEOS.framework/Versions/3/Python/2.7/site-packages/shapely/geometry/geo.py", line 40, in shape 
    return MultiPolygon(ob["coordinates"], context_type='geojson') 
    File "/Library/Frameworks/GEOS.framework/Versions/3/Python/2.7/site-packages/shapely/geometry/multipolygon.py", line 64, in __init__ 
    self._geom, self._ndim = geos_multipolygon_from_py(polygons) 
    File "/Library/Frameworks/GEOS.framework/Versions/3/Python/2.7/site-packages/shapely/geometry/multipolygon.py", line 138, in geos_multipolygon_from_py 
    N = len(ob[0][0][0]) 
TypeError: object of type 'float' has no len() 
+0

Bitte geben Sie ein reproduzierbares Beispiel an (http://stackoverflow.com/help/mcve). Was ist 'json_data'? Wenn es bereits eine FeatureCollection ist, warum erstellen Sie eine FeatureCollection? Welchen Fehler bekommen Sie, wenn Sie das an 'GeoDataFrame.from_features' liefern? – joris

+0

Ja sicher, ich habe meinen Beitrag bearbeitet – kwn

Antwort

2

Für mich funktioniert, wenn ich die json nur füttern _data Funktionen GeoDataFrame.from_features:

In [17]: gdf = geopandas.GeoDataFrame.from_features(json_data['features']) 

In [18]: gdf.head() 
Out[18]: 
              geometry id 
0 (POLYGON ((-0.58570861816406 44.810461337462, ... 2 
1 (POLYGON ((-0.5851936340332 44.816550206151, -... 1 
2 POLYGON ((-0.58805465698242 44.824018340447, -... 5 
3 POLYGON ((-0.59412002563477 44.821664359038, -... 9 
4 (POLYGON ((-0.58502197265625 44.817159057661, ... 12 

Das resultierende GeoDataFrame hat eine Mischung von Polygonen und Multi wie in den Eingabedaten:

In [19]: gdf.geom_type.head() 
Out[19]: 
0 MultiPolygon 
1 MultiPolygon 
2   Polygon 
3   Polygon 
4 MultiPolygon 
dtype: object 

ich versuchte, diese mit GeoPandas 0,2, wohlgeformten 1.5.15, pandas 0,18. 1 unter Windows.