【问题标题】:How to convert a shapefile to a complete list of latitude and longitude points如何将 shapefile 转换为完整的纬度和经度点列表
【发布时间】:2018-10-02 20:37:17
【问题描述】:

我正在尝试将 shapefile 转换为表示 shapefile 定义的每个点的纬度和经度点列表。使用geopandas 读取文件并使用.plot() 函数将这些点显示为图形,但我想要原始点。我试图遍历geopandas.geometry 中的多边形并将所有点存储在多边形内。我绘制了这些点以测试它们是否准确地表示了该区域,但它们没有。我使用以下代码完成了所有这些操作:

import re
import geopandas as gpd
import matplotlib.pyplot as plt

def geoToList(geodataframe):
    points = []
    for s in geodataframe.geometry:iq
        s = str(s)
        s = re.sub('[^0-9., ]+', '', s).split(',')
        s = map(lambda x: x.strip(), s)
        s = map(lambda x: (float(x.split()[0]), float(x.split()[1])), s)
        points.extend(list(s))   
    return points

habitat = gpd.read_file('desktop/species_19377/species_19377.shp')
#borough = borough.to_crs(epsg=4326)

points = geoToList(habitat)
x = [point[0] for point in points]
y = [point[1] for point in points]

plt.scatter(x, y)
plt.show() #representation of the points in all polygons
habitat.plot() #representtation of the points I want

我想要一些函数,它返回可以绘制的点列表,并且看起来与habitat.plot() 的输出相同

我的下一个想法是将图形存储为图像并根据图形的比例分配像素值纬度和经度值,但我确信这比它需要的更复杂。

任何帮助将不胜感激!

【问题讨论】:

    标签: python shapefile geopandas


    【解决方案1】:

    要从一组多边形/多多边形中提取所有点,您可以执行以下操作:

    from shapely.geometry import MultiPolygon
    
    def points_from_polygons(polygons):
        points = []
        for mpoly in polygons:
            if isinstance(mpoly, MultiPolygon):
                polys = list(mpoly)
            else:
                polys = [mpoly]
            for polygon in polys:
                for point in polygon.exterior.coords:
                    points.append(point)
                for interior in polygon.interiors:
                    for point in interior.coords:
                        points.append(point)
        return points
    
    points = points_from_polygons(habitat.geometry)
    x = [point.x for point in points]
    y = [point.y for point in points]
    

    【讨论】:

    • 感谢您的回答!第一个解决方案给了我一个值错误:x 属性访问只为点几何提供。第二个给我一个属性错误:多边形对象没有属性x。我有 0.3.0 版。是不是我做错了什么?
    • 啊,我不知何故假设你得到的几何图形是点。更新了答案
    • 我一直在玩这个解决方案,它似乎生成的输出与我上面发布的函数非常相似,它给出了一些定义外部的点,但没有一个定义内饰。我当然可以使用它来获取内部点,但是我想到的解决方案会很混乱并且计算量很大。有没有更简单的方法来获取形状内的所有点?
    • 是的,我想添加一条评论,说明这没有处理内部,但添加它也很容易。查看更新后的函数以迭代内部结构
    猜你喜欢
    • 1970-01-01
    • 2015-09-08
    • 1970-01-01
    • 1970-01-01
    • 2012-06-10
    • 2017-09-30
    • 2013-09-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多