我有Feature Collection
的Polygons
和MultiPolygons
,我必須先將它寫入臨時文件,然後使用geopandas.GeoDataFrame.from_file(tmp_json_file)加載它,我在尋找一種沒有臨時文件的方法。我嘗試過使用geopandas.GeoDataFrame.from_feature()
,它對於簡單多邊形的特徵集合非常有效,但是我無法使它適用於Polygons
和MultiPolygons
的Feature 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()
請提供可重現的示例(http://stackoverflow.com/help/mcve)。什麼是'json_data'?如果它已經是FeatureCollection,那麼爲什麼要製作一個FeatureCollection?如果你將它提供給'GeoDataFrame.from_features',你會得到什麼錯誤? – joris
是的,我編輯了我的帖子 – kwn