使用geopandas将属性导出到.geojson文件中

发布于 2025-01-24 15:03:04 字数 651 浏览 1 评论 0原文

我有一个脚本,它可以使用Shapely创建许多多边形,然后将它们导出为.geojson文件。 但是请参见下面的玩具示例

from shapely.geometry import Polygon
import geopandas

roi = Polygon([(0,0), (0,1), (1,0), (1,1)])
rois = [roi, roi]

geopandas.GeoSeries(rois).to_file("detection_data.geojson", driver='GeoJSON')

,但是我也有一个数字列表,每个数字都与一个ploygon关联。有没有办法用属性内的geojson文件导出此?

例如,如果我有一个列表:

detection_prob = [0.8, 0.9]

在.geojson文件中,我希望第一个多边形的属性部分

“ properties”:{“ dentection_prob”:0.8}

和第二个多边形

“ properties”:{“ dentection_prob”:0.9}

etc等...在输出的geojson文件中。

I have a script that creates a lot of Polygons using Shapely and then exports them as .geojson files. See toy example below

from shapely.geometry import Polygon
import geopandas

roi = Polygon([(0,0), (0,1), (1,0), (1,1)])
rois = [roi, roi]

geopandas.GeoSeries(rois).to_file("detection_data.geojson", driver='GeoJSON')

However, I also have a list of numbers, each number is associated with one ploygon. Is there a way to export this with the GeoJSON file inside properties?

For example, if I have a list:

detection_prob = [0.8, 0.9]

In the .geojson file I would like the properties section for the first polygon to read

"properties":{"detection_prob":0.8}

and for the second polygon

"properties":{"detection_prob":0.9}

etc etc etc... in the outputted GeoJSON file.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

神爱温柔 2025-01-31 15:03:04

如果您在数据框架上而不是系列上调用to_file,则可以将额外的属性添加为列:

import geopandas as gpd
import shapely.geometry as g

df = gpd.GeoDataFrame({
    'geometry': [g.Point(0, 0), g.Point(1,1)],
    'name': ['foo', 'bar']
})
df.to_file('out.json', driver='GeoJSON')

If you call to_file on a dataframe instead of a series, you can add extra attributes as columns:

import geopandas as gpd
import shapely.geometry as g

df = gpd.GeoDataFrame({
    'geometry': [g.Point(0, 0), g.Point(1,1)],
    'name': ['foo', 'bar']
})
df.to_file('out.json', driver='GeoJSON')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文