【geopandas】利用geopandas库提取不规则图形内的坐标点

对于一个不规则的shape文件,如何提取该shape文件中的坐标点/网格点?


主要思路:利用geopandas库读取shape文件,判断点是否在区域内。

import numpy as np
import geopandas as gpd
from shapely.geometry import Point, MultiPoint, Polygon


def get_xy_in_region(shp_path, origin_xy):
	
	'''
	input
	shp_path: 不规则shp文件的路径, 要求为单个多边形Polygon
	origin_xy:原有的坐标点信息,numpy.array; (lon,lat) dims=(station_number,2)

	output:
	region_xy: shp文件的坐标点信息 numpy.array; (lon,lat) dims=(station_number,2)
	'''
	
    shape = gpd.read_file(shp_path)
    region_shape = shape['geometry'][0]
    in_shape_xy = []
    for xy in origin_xy:
        if region_shape.contains(Point(xy)):
            in_shape_xy.append(xy)
    region_xy = np.array(in_shape_xy)

    return region_xy

一个案例:

# input
shp_path = r'E:/ComparsionPP/data/GIS/ylj2.shp'
china_grid = pd.read_csv(r'E:/ComparsionPP/data/GIS/china05grid_3825.csv')
origin_xy = china_grid.values  #shape=(3825,2)
# output
region_xy = get_xy_in_region(shp_path, origin_xy) #shape=(49,2)

你可能感兴趣的:(python)