使用OGR模块操作矢量数据
导入OGR模块
from osgeo import ogr
打开shp数据
from osgeo import ogr
if __name__ == '__main__':
shp_filename = 'shp/province_1.shp'
shp_dataset = ogr.Open(shp_filename)
driver = shp_dataset.GetDriver()
print(driver.name) # ESRI Shapefile
这种方式和gdal.Open一样的操作,但是对于shp文件而言,这个是缺省的打开方式。
在实际的编程过程中,最好是先声明打开文件类型的Driver,之后再通过Driver将文件打开。
from osgeo import ogr
if __name__ == '__main__':
shp_filename = 'shp/province_1.shp'
driver = ogr.GetDriverByName('ESRI Shapefile')
shp = driver.Open(shp_filename)
两个Open函数之间有点小小的不同
其实参数都是一样的,第一个参数表示传入的路径,第二个表示读取方式0表示只读,1表示可写。
# driver.Open
Open(*args, **kwargs) method of osgeo.ogr.Driver instance
Open(Driver self, char const * utf8_path, int update=0) -> DataSource
# ogr.Open
Open(*args, **kwargs)
Open(char const * utf8_path, int update=0) -> DataSource
该博客介绍了如何在Python中利用osgeo.ogr模块打开和操作ESRI Shapefile矢量数据。首先,从osgeo导入ogr模块,然后通过 ogr.GetDriverByName('ESRIShapefile') 获取Shapefile驱动,接着使用Driver打开指定路径的shp文件。博客强调了在编程实践中,应先声明驱动类型再进行文件打开,以确保正确操作。示例代码展示了两种打开Shapefile的方法,它们的主要区别在于直接调用ogr.Open()与先声明Driver再Open()。
3238

被折叠的 条评论
为什么被折叠?



