为什么在某些情况下无法变换形状多边形?

时间:2020-07-22 12:50:01

标签: python gis polygon geojson shapely

我正在尝试使用shapely计算地理坐标多边形的大小,这似乎需要转换为合适的投影以产生以平方米为单位的结果。我在网上找到了一些示例,但无法在示例多边形中使用。

因此,我尝试使用与我发现的代码片段相同的示例多边形,并且我发现它对某些整体不起作用。要重现结果,这是最少的示例代码:

import json
import pyproj

from shapely.ops import transform
from shapely.geometry import Polygon, mapping
from functools import partial

coords1 = [(-97.59238135821987, 43.47456565304017),
           (-97.59244690469288, 43.47962399877412),
           (-97.59191951546768, 43.47962728271748),
           (-97.59185396090983, 43.47456565304017),
           (-97.59238135821987, 43.47456565304017)]

coords1 = reversed(coords1) # Not sure if important, but https://geojsonlint.com says it's wrong handedness
                            # Doesn't seem to affect the error message though

coords2 = [(13.65374516425911, 52.38533382814119),
           (13.65239769133293, 52.38675829106993),
           (13.64970274383571, 52.38675829106993),
           (13.64835527090953, 52.38533382814119),
           (13.64970274383571, 52.38390931824483),
           (13.65239769133293, 52.38390931824483),
           (13.65374516425911, 52.38533382814119)]

coords = coords1 # DOES NOT WORK
#coords = coords2 # WORKS

polygon = Polygon(coords)

# Print GeoJON to check on https://geojsonlint.com
print(json.dumps(mapping(polygon)))

projection = partial(pyproj.transform, 
                     pyproj.Proj('epsg:4326'), 
                     pyproj.Proj('esri:54009'))

transform(projection, polygon)

coords1coords2都是从应该起作用的代码片段复制而来的。但是,只有coords2对我有用。我已经使用https://geojsonlint.com来查看两个多边形之间是否存在差异,并且该多边形的惯用性/方向似乎不是有效的GeoJSON。我不知道shapely是否在乎,但颠倒顺序-https://geojsonlint.com说这是有效的GeoJSON,并且它在地图上显示了多边形-不会更改错误。

因此,它可以与coords2一起使用,但是当我使用coords1时出现以下错误:

~/env/anaconda3/envs/py36/lib/python3.6/site-packages/shapely/geometry/base.py in _repr_svg_(self)
    398             if xmin == xmax and ymin == ymax:
    399                 # This is a point; buffer using an arbitrary size
--> 400                 xmin, ymin, xmax, ymax = self.buffer(1).bounds
    401             else:
    402                 # Expand bounds by a fraction of the data ranges

ValueError: not enough values to unpack (expected 4, got 0)

我认为coords1(以及我自己的数据中的示例多边形)有一些不同之处会导致此问题,但我无法确定与coords2相比有什么不同。

简而言之,coords1coords2有什么区别,一个工作而另一个却没有?

更新:我通过在投影的定义中添加always_xy=True使它起作用。连同shapely提供的较新语法,避免使用partial,有效的代码段如下所示:

    project = pyproj.Transformer.from_proj(
        pyproj.Proj('epsg:4326'), # source coordinate system
        pyproj.Proj('epsg:3857'),
        always_xy=True
    ) # destination coordinate system
 
    transform(project.transform, polygon)

说实话,即使阅读了文档,我也不知道always_xy在做什么。因此,我不想提供答案。

1 个答案:

答案 0 :(得分:0)

我认为你做得很好,只是反向不会创建新的数据集。

尝试使用该函数创建倒序列表:

def rev_slice(mylist):
    '''
    return a revered list
    mylist: is a list
    '''
    a = mylist[::-1]
    return a

像这样执行函数:

coords = rev_slice(coords1)