2016-06-10 20 views
0

我有Feature CollectionPolygonsMultiPolygons,我必須先將它寫入臨時文件,然後使用geopandas.GeoDataFrame.from_file(tmp_json_file)加載它,我在尋找一種沒有臨時文件的方法。我嘗試過使用geopandas.GeoDataFrame.from_feature(),它對於簡單多邊形的特徵集合非常有效,但是我無法使它適用於PolygonsMultiPolygonsFeature Collection,我正在考慮做類似下面的操作,但它還沒有工作。如何從GeoJSON創建要素集

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']) 

以GeoJSON是從API截取,返回領土(一些領土由單個多邊形由一組多邊形(格式化爲一個的MultiPolygon)模型化,其它。

以GeoJSON被構造爲遵循:http://pastebin.com/PPdMUGkY

我從上面的功能得到以下錯誤:

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

請提供可重現的示例(http://stackoverflow.com/help/mcve)。什麼是'json_data'?如果它已經是FeatureCollection,那麼爲什麼要製作一個FeatureCollection?如果你將它提供給'GeoDataFrame.from_features',你會得到什麼錯誤? – joris

+0

是的,我編輯了我的帖子 – kwn

回答

2

對於我這個作品,如果我只給了JSON _data功能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 

所得GeoDataFrame具有多邊形和MultiPolygons的混合物象在輸入數據:

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

我試圖與GeoPandas 0.2,勻稱1.5.15,熊貓0.18。 1在Windows上。