Python 在贴图投影后不连续时修复形状多边形对象

Python 在贴图投影后不连续时修复形状多边形对象,python,maps,geospatial,shapely,Python,Maps,Geospatial,Shapely,这个演示程序(打算在IPython笔记本电脑中运行;您需要matplotlib,mpl\u工具包。basemap,pyproj,和shapely)可以在地球表面绘制越来越大的圆圈。只要圆不越过其中一个极点,它就可以正常工作。如果发生这种情况,那么在地图上绘制的结果完全是胡说八道(参见下面的单元格2) 如果我将它们绘制在“空白处”而不是地图上(参见下面的单元格3),结果是正确的,因为如果删除了从+180到-180经度的水平线,那么曲线的其余部分将确实划定所需圆的内部和外部之间的边界。但是,它们是错

这个演示程序(打算在IPython笔记本电脑中运行;您需要
matplotlib
mpl\u工具包。basemap
pyproj
,和
shapely
)可以在地球表面绘制越来越大的圆圈。只要圆不越过其中一个极点,它就可以正常工作。如果发生这种情况,那么在地图上绘制的结果完全是胡说八道(参见下面的单元格2)

如果我将它们绘制在“空白处”而不是地图上(参见下面的单元格3),结果是正确的,因为如果删除了从+180到-180经度的水平线,那么曲线的其余部分将确实划定所需圆的内部和外部之间的边界。但是,它们是错误的,因为多边形无效(
.is\u valid
为False),而且更重要的是,多边形内部的非零卷绕数没有包围贴图的正确区域

我相信这是因为
shapely.ops.transform
对+180=-180经度处的坐标奇异性视而不见。问题是,如何检测问题并修复多边形,使其包含地图的正确区域?在这种情况下,适当的修正是将(X,+180)-(X,-180)的水平段替换为三行,(X,+180)-(90,+180)-(90,+180)-(90,-180)-(X,-180);但请注意,如果圆已经越过南极,则修正线将需要向南移动。如果圆越过了两极,我们会得到一个有效的多边形,但它的内部是它应该是的补充。我需要检测所有这些案例并正确处理它们。此外,我不知道如何“编辑”一个形状优美的几何体对象

可下载的笔记本电脑:


(图中所示的阳光照射区域是一个例子,说明了当投影到等分矩形地图上时,穿过极点的圆应该是什么样子,只要它今天不是等分点。)

手动修复投影多边形结果没有那么糟糕。 有两个步骤:首先,找到在经度±180处穿过的多边形的所有线段,并将它们替换为到北极或南极的偏移,以最近的为准;其次,如果生成的多边形不包含原点,请将其反转。请注意,无论shapely是否认为投影多边形“无效”,都必须执行这两个步骤;根据起点的位置,它可能会穿过一个或两个极点,而不会无效

这可能不是最有效的方法,但它确实有效

import pyproj
from shapely.geometry import Point, Polygon, box as Box
from shapely.ops import transform as sh_transform
from functools import partial

wgs84_globe = pyproj.Proj(proj='latlong', ellps='WGS84')

def disk_on_globe(lat, lon, radius):
    """Generate a shapely.Polygon object representing a disk on the
    surface of the Earth, containing all points within RADIUS meters
    of latitude/longitude LAT/LON."""

    aeqd = pyproj.Proj(proj='aeqd', ellps='WGS84', datum='WGS84',
                       lat_0=lat, lon_0=lon)
    disk = sh_transform(
        partial(pyproj.transform, aeqd, wgs84_globe),
        Point(0, 0).buffer(radius)
    )

    # Fix up segments that cross the coordinate singularity at longitude ±180.
    # We do this unconditionally because it may or may not create a non-simple
    # polygon, depending on where the initial point was.
    boundary = np.array(disk.boundary)
    i = 0
    while i < boundary.shape[0] - 1:
        if abs(boundary[i+1,0] - boundary[i,0]) > 180:
            assert (boundary[i,1] > 0) == (boundary[i,1] > 0)
            vsign = -1 if boundary[i,1] < 0 else 1
            hsign = -1 if boundary[i,0] < 0 else 1
            boundary = np.insert(boundary, i+1, [
                [hsign*179, boundary[i,1]],
                [hsign*179, vsign*89],
                [-hsign*179, vsign*89],
                [-hsign*179, boundary[i+1,1]]
            ], axis=0)
            i += 5
        else:
            i += 1
    disk = Polygon(boundary)

    # If the fixed-up polygon doesn't contain the origin point, invert it.
    if not disk.contains(Point(lon, lat)):
        disk = Box(-180, -90, 180, 90).difference(disk)

    assert disk.is_valid
    assert disk.boundary.is_simple
    assert disk.contains(Point(lon, lat))
    return disk

这不是一个琐碎的问题,但你似乎已经差不多明白了。有时候我只希望地球是平的,哈哈哈。。您需要通过检查方位等距投影中的缓冲区(点(0,0)。缓冲区(半径)多边形)是否接触其中一个极点,自行处理模糊性。通过了解这一点,您可以在尝试打印之前,在WGS84投影中对新多边形实施建议的修复。关于使用地图时绘图的差异,我不知道,但我想如果只处理固定的多边形,问题就会消失。
## cell 2
def plot_poly_on_map(map_, pol):
    if isinstance(pol, Polygon):
        map_.plot(*(pol.exterior.xy), '-', latlon=True)
    else:
        assert isinstance(pol, MultiPolygon)
        for p in pol:
            map_.plot(*(p.exterior.xy), '-', latlon=True)

plt.figure(figsize=(14, 12))
map_ = Basemap(projection='cyl', resolution='c')
map_.drawcoastlines(linewidth=0.25)

for rad in range(1,10):
    plot_poly_on_map(
        map_,
        disk_on_globe(40.439, -79.976, rad * 1000 * 1000)
)
plt.show()
## cell 3
def plot_poly_in_void(pol):
    if isinstance(pol, Polygon):
        plt.plot(*(pol.exterior.xy), '-')
    else:
        assert isinstance(pol, MultiPolygon)
        for p in pol:
            plt.plot(*(p.exterior.xy), '-', latlon=True)

plt.figure()
for rad in range(1,10):
    plot_poly_in_void(
        disk_on_globe(40.439, -79.976, rad * 1000 * 1000)
)
plt.show()
import pyproj
from shapely.geometry import Point, Polygon, box as Box
from shapely.ops import transform as sh_transform
from functools import partial

wgs84_globe = pyproj.Proj(proj='latlong', ellps='WGS84')

def disk_on_globe(lat, lon, radius):
    """Generate a shapely.Polygon object representing a disk on the
    surface of the Earth, containing all points within RADIUS meters
    of latitude/longitude LAT/LON."""

    aeqd = pyproj.Proj(proj='aeqd', ellps='WGS84', datum='WGS84',
                       lat_0=lat, lon_0=lon)
    disk = sh_transform(
        partial(pyproj.transform, aeqd, wgs84_globe),
        Point(0, 0).buffer(radius)
    )

    # Fix up segments that cross the coordinate singularity at longitude ±180.
    # We do this unconditionally because it may or may not create a non-simple
    # polygon, depending on where the initial point was.
    boundary = np.array(disk.boundary)
    i = 0
    while i < boundary.shape[0] - 1:
        if abs(boundary[i+1,0] - boundary[i,0]) > 180:
            assert (boundary[i,1] > 0) == (boundary[i,1] > 0)
            vsign = -1 if boundary[i,1] < 0 else 1
            hsign = -1 if boundary[i,0] < 0 else 1
            boundary = np.insert(boundary, i+1, [
                [hsign*179, boundary[i,1]],
                [hsign*179, vsign*89],
                [-hsign*179, vsign*89],
                [-hsign*179, boundary[i+1,1]]
            ], axis=0)
            i += 5
        else:
            i += 1
    disk = Polygon(boundary)

    # If the fixed-up polygon doesn't contain the origin point, invert it.
    if not disk.contains(Point(lon, lat)):
        disk = Box(-180, -90, 180, 90).difference(disk)

    assert disk.is_valid
    assert disk.boundary.is_simple
    assert disk.contains(Point(lon, lat))
    return disk
%matplotlib inline
from matplotlib import pyplot as plt
from mpl_toolkits.basemap import Basemap
from descartes import PolygonPatch

plt.figure(figsize=(14, 12))
map_ = Basemap(projection='cea', resolution='c')
map_.drawcoastlines(linewidth=0.25)

for rad in range(3,19,2):
    plt.gca().add_patch(PolygonPatch(
        sh_transform(map_,
            disk_on_globe(40.439, -79.976, rad * 1000 * 1000)),
        alpha=0.1))    
plt.show()