使用GDAL的ogr2ogr.py将shapefile插入到mysql中(一)

因为课程需要,需要将shapefile格式的数据储存进入mysql来进行查询等操作。

google百度之后发现GDAL的org2org命令行工具可以将shp文件储存进mysql。

编译gdal-2.1.3\swig\python文件夹下的setup.py文件:

	python setup.py build

出现错误:

running build
running build_py
running build_ext
building 'osgeo._gdal' extension
error: INCLUDE environment variable is empty

找了setup.py文件下的的含有“osgeo._gdal”代码,发现这段代码引用了目录下的文件夹extension中的gdal_wrap文件

gdal_module = Extension('osgeo._gdal',
                        sources=['extensions/gdal_wrap.cpp'],
                        extra_compile_args = extra_compile_args,
                        extra_link_args = extra_link_args)

不知道怎么办,于是下载了别人编译好的GDAL,直接安装在了我的python的Lib\site-packages文件夹下,但是再引用ogr2ogr.py文件时,还是报错:

Traceback (most recent call last):
  File "ogr2ogr.py", line 42, in <module>
    from osgeo import gdal
  File "C:\Users\Administrator\De
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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和栅格的范围可能不完全重合,因此上述代码的重叠判断是基于两者的边界框进行的。如果需要更精确的重叠判断,可以使用更复杂的方法,例如将栅格转换为矢量数据,然后进行空间分析。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值