繁体   English   中英

Geopandas几何到geopandas dataframe中的geojson

[英]Geopandas geometry to geojson in geopandas dataframe

我有一个 geopandas object,我想将几何转换为字符串。 Normaly几何如下

file = geopandas.read_file("test.TAB")
# its content is 

ID                                                            ***
MI_PRINX                                                      ***9.0

geometry                POLYGON ((4496597.53 4537154.3691, 4496022.574... 
Name: 0, dtype: object

我有点模糊数据,因为它是私有的。 正如所见,几何形状是 shapely.Polygon 的 object,我想用它的 geojson 格式更改它。

Polygon(...)

如果我写如下

file.iloc[0].geometry = geopandas.GeoSeries(file.iloc[0]).geometry.to_json()

我收到了这个错误:

SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
  file.loc[0].geometry = "something"

我想要这样的数据:


{"type": "FeatureCollection", "features": [{"id": "0", "type": "Feature", "properties": {}, "geometry": {"type": "Polygon", "coordinates": [[[4496597.53, 4537154.3691], [4496022.574, 4537169.5574], [4495738.723, 4537227.9468], [4495722.012, 4537167.8742], [4495576.619, 4536541.8022], [4495762.527, 4536487.3604], [4495791.462, 4536284.4807], [4495800.719, 4536212.8866], [4495810.617, 4536200.223], [4495837.107, 4536191.6469], [4495838.469, 4536186.0163], [4495795.469, 4536178.2817], [4495750.385, 4536174.0539], [4495724.456, 4536165.7583], [4495684.381, 4536143.9576], [4495678.931, 4536140.9919], [4495702.776, 4536145.22], [4495729.345, 4536153.7961], [4495771.905, 4536163.3739], [4495803.284, 4536165.8987], [4495842.678, 4536165.3376], [4495891.008, 4536160.1279], [4495943.867, 4536151.6919], [4496009.109, 4536152.2532], [4496096.593, 4536148.5863], [4496453.701, 4536103.3015], [4496597.53, 4537154.3691]]]}, "bbox": [4495576.619, 4536103.3015, 4496597.53, 4537227.9468]}], "bbox": [4495576.619, 4536103.3015, 4496597.53, 4537227.9468]}

我不想创建新的 dataframe 因为它的尺寸太大了。 有什么办法吗?

除了 to_geojson,您可以使用geopandas.GeoDataFrame.to_wkt将所有几何列转换为众所周知的文本:

file = file.to_wkt()

geo_interface方法可能会对您有所帮助。 它以字典的形式返回几何图形。

file = file.__geo_interface__

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM