python判断某个点是否在shapefile所示区域中

参考:Finding if latitude and longitude in shapefile region
问题:比如我现在有一些点的数据(具有经纬度坐标),然后有shapefile数据,现在我想知道哪些点是在shapefile中的,那些点不是。
以深圳为例,如下图有所示矩形的点,这些点有些在深圳范围内,有一些则不在其中,现在想要去除不在其中的点怎么办呢?
在这里插入图片描述
以下是相应的实现代码:

import shapefile # 这个
import shapely.geometry as geometry
import pandas as pd
from mpl_toolkits.basemap import Basemap
import matplotlib
import numpy as np
import pylab as plt

if __name__ == '__main__':
    # 取得深圳的shape
    sz_shp = shapefile.Reader(
        r'G:\Shenzhen-wind\深圳地形文件\shapefile\gadm36_CHN_shp\gadm36_CHN_2')
    for city_rcd in sz_shp.shapeRecords():  # 遍历每一条shaperecord
        if city_rcd.record[6] == 'Shenzhen':  # 遍历时,record字段是地区的信息(由字符串表示)
            sz_shp = city_rcd.shape  # 遍历时,shape字段是shape——形状(由点组成)

    m = Basemap(llcrnrlon=113.7, llcrnrlat=22.35, urcrnrlon=114.7, urcrnrlat=22.9,
                rsphere=(6378137.00, 6356752.3142
  • 18
    点赞
  • 79
    收藏
    觉得还不错? 一键收藏
  • 30
    评论
Python,可以使用OGR/GDAL库来确定shapefile和栅格是否重叠。具体步骤如下: 1. 导入必要的库 ```python from osgeo import ogr, gdal ``` 2. 打开shapefile,并获取其空间参考 ```python shapefile = ogr.Open("path/to/shapefile.shp") layer = shapefile.GetLayer() spatialRef = layer.GetSpatialRef() ``` 3. 打开栅格文件,并获取其空间参考和变换矩阵 ```python rasterfile = gdal.Open("path/to/rasterfile.tif") geotransform = rasterfile.GetGeoTransform() spatialRef = rasterfile.GetProjection() ``` 4. 将shapefile的空间参考和栅格的空间参考进行比较 ```python if spatialRef == layer.GetSpatialRef().ExportToWkt(): print("The shapefile and raster have the same spatial reference.") else: print("The shapefile and raster have different spatial references.") ``` 5. 获取shapefile范围,并将其转换为栅格坐标系范围 ```python # 获取shapefile范围 extent = layer.GetExtent() # 将shapefile范围转换为栅格坐标系范围 ulx, uly = gdal.ApplyGeoTransform(geotransform, extent[0], extent[3]) lrx, lry = gdal.ApplyGeoTransform(geotransform, extent[1], extent[2]) ``` 6. 获取栅格的范围 ```python xsize = rasterfile.RasterXSize ysize = rasterfile.RasterYSize x_min = geotransform[0] y_max = geotransform[3] x_max = x_min + geotransform[1] * xsize y_min = y_max + geotransform[5] * ysize ``` 7. 判断shapefile和栅格是否重叠 ```python if ulx < x_max and lrx > x_min and uly > y_min and lry < y_max: print("The shapefile and raster overlap.") else: print("The shapefile and raster do not overlap.") ``` 注意,上述代码shapefile和rasterfile的路径需要根据实际情况进行修改。另外,由于shapefile和栅格的范围可能不完全重合,因此上述代码的重叠判断是基于两者的边界框进行的。如果需要更精确的重叠判断,可以使用更复杂的方法,例如将栅格转换为矢量数据,然后进行空间分析。
评论 30
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值