如何将 shapefile 转换为经纬度点的完整列表
How to convert a shapefile to a complete list of latitude and longitude points
我正在尝试将 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
我想要一些函数 returns 可以绘制的点列表,看起来与 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]
我正在尝试将 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
我想要一些函数 returns 可以绘制的点列表,看起来与 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]