Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/276.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 在使用basemap创建的加利福尼亚州地图上创建等距坐标_Python_Gis_Shapefile_Matplotlib Basemap_Shapely - Fatal编程技术网

Python 在使用basemap创建的加利福尼亚州地图上创建等距坐标

Python 在使用basemap创建的加利福尼亚州地图上创建等距坐标,python,gis,shapefile,matplotlib-basemap,shapely,Python,Gis,Shapefile,Matplotlib Basemap,Shapely,我用Basemap python库创建了一个加利福尼亚州地图 代码如下 from mpl_toolkits.basemap import Basemap import matplotlib.pyplot as plt fig, ax = plt.subplots(figsize=(20,20)) map = Basemap(llcrnrlon=-124.48,llcrnrlat=32.53,urcrnrlon=-114.13,urcrnrlat=42.01, reso

我用Basemap python库创建了一个加利福尼亚州地图

代码如下

from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt

fig, ax = plt.subplots(figsize=(20,20))
map = Basemap(llcrnrlon=-124.48,llcrnrlat=32.53,urcrnrlon=-114.13,urcrnrlat=42.01,
             resolution='c', projection='lcc', lat_0 =  36.778259, lon_0 = -119.417)
#westlimit=-124.48; southlimit=32.53; eastlimit=-114.13; northlimit=42.01
map.drawmapboundary(fill_color='aqua')
map.fillcontinents(color='#f2f2f2',lake_color='#46bcec')
map.drawcoastlines()
map.readshapefile('./CA_Counties/CA_Counties_TIGER', 'CA_Counties_TIGER')
plt.show()
输出

现在我接下来要做的是,只在加利福尼亚地图上绘制等距点,这将填充整个加利福尼亚地图(没有海洋和附近的州)。示例是下图所示的网格,因为网格中的所有点都填充了网格(几乎)

我认为可以这样做的方法是将加利福尼亚视为一个多边形或多边形,并在其中生成等距的经度和纬度。我正在看,但它并没有真正解决我的问题,因为当我运行下面的代码来生成点

import fiona
from shapely.geometry import shape
import random
from shapely.geometry import Point
from shapely.geometry import Polygon

def generate_random(number, polygon):
    list_of_points = []
    minx, miny, maxx, maxy = polygon.bounds
    counter = 0
    while counter < number:
        pnt = Point(random.uniform(minx, maxx), random.uniform(miny, maxy))
        if polygon.contains(pnt):
            list_of_points.append(pnt)
            counter += 1
    return list_of_points
all_points=[]
for pol in fiona.open('./CA_Counties/CA_Counties_TIGER.shp'):
    #l.append(pol['geometry'])
    all_points.append(generate_random(50, Polygon(pol['geometry']['coordinates'])))

此外,我甚至不确定上述代码是否有效,它是否会给我所有等间距的点(纬度和经度),这些点将填充整个加利福尼亚地图。是否有其他方法可以做到这一点,或者有人可以帮助我与上述代码。它必须生成lat和lon tho。因此,我可以使用basemap中的
map.plot()
函数来绘制它们。还希望获得数据结构中的所有点。例如,一个列表列表或数组数组或任何其他,如果它运行良好(可能是一个字典)

我不完全理解您的示例,尤其是为什么您要对点坐标使用随机生成。如果我理解正确,您可能希望沿常规栅格生成点,但只能在给定的形状文件中生成点

我的建议如下:

import numpy as np
from shapely.geometry import Point
from shapely.ops import cascaded_union
import geopandas as gpd

def generate_grid_in_polygon(spacing, polygon):
    ''' This Function generates evenly spaced points within the given GeoDataFrame.
        The parameter 'spacing' defines the distance between the points in coordinate units. '''
    
    # Convert the GeoDataFrame to a single polygon
    poly_in = cascaded_union([poly for poly in polygon.geometry])

    # Get the bounds of the polygon
    minx, miny, maxx, maxy = poly_in.bounds    
    
    # Now generate the entire grid
    x_coords = list(np.arange(np.floor(minx), int(np.ceil(maxx)), spacing))
    y_coords = list(np.arange(np.floor(miny), int(np.ceil(maxy)), spacing))
    
    grid = [Point(x) for x in zip(np.meshgrid(x_coords, y_coords)[0].flatten(), np.meshgrid(x_coords, y_coords)[1].flatten())]
    
    # Finally only keep the points within the polygon
    list_of_points = [point for point in grid if point.within(poly_in)]

    return list_of_points
shape_in = gpd.read_file('path/to/shapefile.shp')

points_in_poly = generate_grid_in_polygon(0.25, shape_in)
points_in_poly_gdf = gpd.GeoDataFrame(geometry=points_in_poly)

ax1 = shape_in.plot(facecolor='none', edgecolor='k')
ax1 = points_in_poly_gdf.plot(ax=ax1)
该函数的使用示例如下所示:

import numpy as np
from shapely.geometry import Point
from shapely.ops import cascaded_union
import geopandas as gpd

def generate_grid_in_polygon(spacing, polygon):
    ''' This Function generates evenly spaced points within the given GeoDataFrame.
        The parameter 'spacing' defines the distance between the points in coordinate units. '''
    
    # Convert the GeoDataFrame to a single polygon
    poly_in = cascaded_union([poly for poly in polygon.geometry])

    # Get the bounds of the polygon
    minx, miny, maxx, maxy = poly_in.bounds    
    
    # Now generate the entire grid
    x_coords = list(np.arange(np.floor(minx), int(np.ceil(maxx)), spacing))
    y_coords = list(np.arange(np.floor(miny), int(np.ceil(maxy)), spacing))
    
    grid = [Point(x) for x in zip(np.meshgrid(x_coords, y_coords)[0].flatten(), np.meshgrid(x_coords, y_coords)[1].flatten())]
    
    # Finally only keep the points within the polygon
    list_of_points = [point for point in grid if point.within(poly_in)]

    return list_of_points
shape_in = gpd.read_file('path/to/shapefile.shp')

points_in_poly = generate_grid_in_polygon(0.25, shape_in)
points_in_poly_gdf = gpd.GeoDataFrame(geometry=points_in_poly)

ax1 = shape_in.plot(facecolor='none', edgecolor='k')
ax1 = points_in_poly_gdf.plot(ax=ax1)
对于间距为0.25°的加利福尼亚州,可以如下所示:

随意调整。
根据选择的间距,这可能需要很长时间来处理。您可能希望使用空间索引来提高性能。

我不完全理解您的示例,尤其是为什么要对点坐标使用随机生成。如果我理解正确,您可能希望沿常规栅格生成点,但只能在给定的形状文件中生成点

我的建议如下:

import numpy as np
from shapely.geometry import Point
from shapely.ops import cascaded_union
import geopandas as gpd

def generate_grid_in_polygon(spacing, polygon):
    ''' This Function generates evenly spaced points within the given GeoDataFrame.
        The parameter 'spacing' defines the distance between the points in coordinate units. '''
    
    # Convert the GeoDataFrame to a single polygon
    poly_in = cascaded_union([poly for poly in polygon.geometry])

    # Get the bounds of the polygon
    minx, miny, maxx, maxy = poly_in.bounds    
    
    # Now generate the entire grid
    x_coords = list(np.arange(np.floor(minx), int(np.ceil(maxx)), spacing))
    y_coords = list(np.arange(np.floor(miny), int(np.ceil(maxy)), spacing))
    
    grid = [Point(x) for x in zip(np.meshgrid(x_coords, y_coords)[0].flatten(), np.meshgrid(x_coords, y_coords)[1].flatten())]
    
    # Finally only keep the points within the polygon
    list_of_points = [point for point in grid if point.within(poly_in)]

    return list_of_points
shape_in = gpd.read_file('path/to/shapefile.shp')

points_in_poly = generate_grid_in_polygon(0.25, shape_in)
points_in_poly_gdf = gpd.GeoDataFrame(geometry=points_in_poly)

ax1 = shape_in.plot(facecolor='none', edgecolor='k')
ax1 = points_in_poly_gdf.plot(ax=ax1)
该函数的使用示例如下所示:

import numpy as np
from shapely.geometry import Point
from shapely.ops import cascaded_union
import geopandas as gpd

def generate_grid_in_polygon(spacing, polygon):
    ''' This Function generates evenly spaced points within the given GeoDataFrame.
        The parameter 'spacing' defines the distance between the points in coordinate units. '''
    
    # Convert the GeoDataFrame to a single polygon
    poly_in = cascaded_union([poly for poly in polygon.geometry])

    # Get the bounds of the polygon
    minx, miny, maxx, maxy = poly_in.bounds    
    
    # Now generate the entire grid
    x_coords = list(np.arange(np.floor(minx), int(np.ceil(maxx)), spacing))
    y_coords = list(np.arange(np.floor(miny), int(np.ceil(maxy)), spacing))
    
    grid = [Point(x) for x in zip(np.meshgrid(x_coords, y_coords)[0].flatten(), np.meshgrid(x_coords, y_coords)[1].flatten())]
    
    # Finally only keep the points within the polygon
    list_of_points = [point for point in grid if point.within(poly_in)]

    return list_of_points
shape_in = gpd.read_file('path/to/shapefile.shp')

points_in_poly = generate_grid_in_polygon(0.25, shape_in)
points_in_poly_gdf = gpd.GeoDataFrame(geometry=points_in_poly)

ax1 = shape_in.plot(facecolor='none', edgecolor='k')
ax1 = points_in_poly_gdf.plot(ax=ax1)
对于间距为0.25°的加利福尼亚州,可以如下所示:

随意调整。
根据选择的间距,这可能需要很长时间来处理。您可能希望使用空间索引来提高性能。

请确定给出错误的具体行,这将帮助您了解问题所在。我不知道你的代码是如何导致错误的,但是,线性化问题看起来更像是多边形定义是SHP文件的问题。请确定导致错误的具体行,这将帮助你了解问题所在。我不明白你的代码是如何导致错误的,但是,线性化问题看起来更像是一个多边形定义的问题是SHP file.Genius!非常感谢,这正是我想要的!您还可以使用空间索引更新代码。那将是一个有用的天才!非常感谢,这正是我想要的!您还可以使用空间索引更新代码。那会有帮助的