用geojson制作多边形对象

发布于 2025-02-09 23:42:13 字数 518 浏览 2 评论 0原文

我有可以包含X多边形量的Geojson文件,因此我有一个循环浏览文件,并将每个多边形的坐标添加到一个数组中。现在,我在制作多边形对象时试图索引数组。

    def check_overlap():
    geom = []
    for j in range(int(get_polygon_count())):
        for i, feature in enumerate(data['features'][j+1:]):
            geom.append(feature['geometry']['coordinates'])
    value1 = geom[0]
    polygon1 = Polygon(value1)
    #polygon2 = Polygon(geom[1])
    print(polygon1)
    print(geom[1])
    #print(polygon1.intersects(polygon2))

我得到的错误是ValueRorr:亚麻环必须至少具有3个坐标元组

I have geojson files that can have x amount of polygons so I have a for loop that goes through the file and adds the coords of each polygon to an array. Now i'm trying to index the array when making Polygon objects.

    def check_overlap():
    geom = []
    for j in range(int(get_polygon_count())):
        for i, feature in enumerate(data['features'][j+1:]):
            geom.append(feature['geometry']['coordinates'])
    value1 = geom[0]
    polygon1 = Polygon(value1)
    #polygon2 = Polygon(geom[1])
    print(polygon1)
    print(geom[1])
    #print(polygon1.intersects(polygon2))

The error I get is ValueError: A LinenearRing must have at least 3 coordinate tuples

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

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

发布评论

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

评论(1

累赘 2025-02-16 23:42:13

这是一个猜测,但是GEOM列表被填写和以后的索引的方式polygon()在每个Geojson Polygon中失败,因为结构是不同的。

geojson polygon

多边形的坐标是线性环的阵列(请参阅部分
3.1.6)坐标数组。阵列中的第一个元素代表外环。任何后续元素代表
内环(或孔)。

如果GEOM [X]中的结构恰好是没有孔的Geojson Polygon,则看起来像这样:

[[[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0]]]

shapely polygon()

采用两个位置参数。第一个是一个有序的序列
(x,y [,z])指向元组,并完全像线性一样处理
案件。第二个是可选的无环类的序列
指定内部边界或“孔”的序列
功能。

polygon()期望的是Geojson Polygon的第一个元素,可选为孔:

geom = []
geom.append([[[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0]]])
geom[0]
# [[[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0]]]
Polygon(geom[0]).wkt
# ValueError: A LinearRing must have at least 3 coordinate tuples

geom[0][0]
# [[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0]]
Polygon(geom[0][0]).wkt
# 'POLYGON ((100 0, 101 0, 101 1, 100 1, 100 0))'

尽管我只是将整个东西加载到geopandas中。

It's bit of a guesswork, but the way that geom list gets filled and later indexed makes Polygon() to fail with every GeoJSON Polygon as structures are different.

GeoJSON polygon:

Coordinates of a Polygon are an array of linear ring (see Section
3.1.6) coordinate arrays. The first element in the array represents the exterior ring. Any subsequent elements represent
interior rings (or holes).

If structure in geom[x] happens to be GeoJSON Polygon with no holes, it looks something like this :

[[[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0]]]

Shapely Polygon():

takes two positional parameters. The first is an ordered sequence of
(x, y[, z]) point tuples and is treated exactly as in the LinearRing
case. The second is an optional unordered sequence of ring-like
sequences specifying the interior boundaries or “holes” of the
feature.

What Polygon() expects is the first element of GeoJSON Polygon, optionally followed by holes:

geom = []
geom.append([[[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0]]])
geom[0]
# [[[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0]]]
Polygon(geom[0]).wkt
# ValueError: A LinearRing must have at least 3 coordinate tuples

geom[0][0]
# [[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0]]
Polygon(geom[0][0]).wkt
# 'POLYGON ((100 0, 101 0, 101 1, 100 1, 100 0))'

Though I'd just load the whole thing into GeoPandas.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文