kwn*_*kwn 0 gis geojson geopandas
我有一个Feature Collection,Polygons我MultiPolygons必须首先将其写入临时文件中,然后使用 geopandas.GeoDataFrame.from_file(tmp_json_file) 加载它,我正在寻找一种无需临时文件即可完成此操作的方法。我尝试过使用geopandas.GeoDataFrame.from_feature(),它对于简单多边形的特征集合工作得很好,但我不能让它适用于Feature Collection和Polygons,MultiPolygons我正在考虑做类似下面的事情,但它还没有工作。
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'])
Run Code Online (Sandbox Code Playgroud)
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()
Run Code Online (Sandbox Code Playgroud)
对我来说,如果我只是将 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
Run Code Online (Sandbox Code Playgroud)
生成的 GeoDataFrame 混合了多边形和多多边形,如输入数据所示:
In [19]: gdf.geom_type.head()
Out[19]:
0 MultiPolygon
1 MultiPolygon
2 Polygon
3 Polygon
4 MultiPolygon
dtype: object
Run Code Online (Sandbox Code Playgroud)
我在 Windows 上使用 GeoPandas 0.2、shapely 1.5.15、pandas 0.18.1 进行了尝试。
| 归档时间: |
|
| 查看次数: |
2748 次 |
| 最近记录: |