点和Python中的MultiPolygon Geoseries对象之间的距离

时间:2019-03-15 04:55:11

标签: python polygon distance geopandas shapely

我想计算与使用GeoPandas的cascaded_union函数创建的Multiploygon地理系列对象的距离。以下是我的代码:

import numpy as np
import pandas as pd
import geopandas as gpd
from shapely.ops import cascaded_union
from matplotlib import pyplot as plt
from shapely.geometry import LineString, Point, MultiPoint

fp = "\\TM_WORLD_BORDERS-0.3\\TM_WORLD_BORDERS-0.3.shp"
data = gpd.read_file(fp)

mycountries = ['Canada','Russia']

req_data = data[data.NAME.isin(mycountries)]
req_data = req_data.reset_index()
req_data = req_data.drop('index',axis=1)

polygons = [req_data['geometry'][0],req_data['geometry'][1]]
boundary = gpd.GeoSeries(cascaded_union(polygons))
boundary.plot(color = 'black')
plt.show()

我要求在计算点与多边形之间的距离时,应考虑最接近的国家/地区,并且距离应以英里/公里为单位。对此的任何帮助将不胜感激。

我尝试使用距离函数,但是得到的结果不正确。而且,如果我考虑在印度洋的某个地方,它给出的结果为0,这又不是我所需要的。

可以从 http://thematicmapping.org/downloads/world_borders.php

上的 TM_WORLD_BORDERS-0.3.zip 文件中下载

Shape文件。

1 个答案:

答案 0 :(得分:0)

这里是使用Point.distance的一种解决方案:

from shapely.geometry import Point
point = Point(34.23, 86.44)
dist={}
for index, row in req_data.iterrows():
    if row.geometry.geom_type == 'MultiPolygon':
        for  r in row.geometry:
            print(row.NAME,'  dist', point.distance(r))
            if row.NAME in dist:
                l = dist[row.NAME];l.append(point.distance(r))
                dist[row.NAME] = l
            else:
                dist[row.NAME] = [point.distance(r)]
    else: #Polygon
        print(row.NAME,'  dist', point.distance(row.geometry))
        dist[row.NAME] = [point.distance(row.geometry)]


for x in dist:
    print(x, min(dist[x]))

输出:每个国家/地区的最小距离

 Canada 95.0619464401356
 Russia 12.118509615115519