Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将形状文件转换为纬度和经度点的完整列表_Python_Shapefile_Geopandas - Fatal编程技术网

Python 如何将形状文件转换为纬度和经度点的完整列表

Python 如何将形状文件转换为纬度和经度点的完整列表,python,shapefile,geopandas,Python,Shapefile,Geopandas,我正在尝试将一个shapefile转换为一个纬度和经度点列表,这些点表示shapefile定义的每个点。使用geopandas读取文件并使用.plot()函数将这些点显示为图形,但我想要原始点。我尝试在geopandas.geometry中迭代多边形,并存储多边形中的所有点。我绘制了这些点来测试它们是否给出了该区域的准确表示,但它们没有。我使用以下代码完成了所有这些: import re import geopandas as gpd import matplotlib.pyplot as pl

我正在尝试将一个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()的输出相同。

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


任何帮助都将不胜感激

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

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。有什么我做错了吗?啊,我不知怎么地认为你得到的几何图形是点。更新了答案我一直在使用这个解决方案,它似乎生成了一个与我上面发布的函数非常相似的输出,它给出了一些定义外部的点,但没有一个定义内部。我当然可以用它来获得内部点,但我想到的解决方案会很混乱,计算成本也很高。有没有一种更简单的方法来获取形状中的所有点?是的,我想添加一条注释,说明这没有处理内部,但添加它也很容易。请参阅更新的函数,以在内部进行迭代