資源簡介
資源為python3.7.2下調用gdal進行shapefile操作的庫文件及調用方法的源碼,找了好久才找到,使用方法:在命令行工具下輸入
pip install GDAL-3.1.2-cp37-cp37m-win_amd64.whl
回車,安裝完成。實踐的操作系統win10
代碼片段和文件信息
#!/usr/bin/python
#?-*-?coding:?gbk?-*-
‘‘‘
Created?on?2013-8-27
@author:?chenll
‘‘‘
import?os?sys
from?osgeo?import?gdal
from?osgeo?import?ogr
from?osgeo?import?osr
import?numpy
#?讀取shap文件
def?readShap():
????#?為了支持中文路徑,請添加下面這句代碼
????gdal.SetConfigOption(“GDAL_FILENAME_IS_UTF8“?“NO“)
????#?為了使屬性表字段支持中文,請添加下面這句
????gdal.SetConfigOption(“SHAPE_ENCODING“?““)
????#?注冊所有的驅動
????ogr.RegisterAll()
????#?數據格式的驅動
????driver?=?ogr.GetDriverByName(‘ESRI?Shapefile‘)
????ds?=?driver.Open(‘E:\\temp\\shp\\poi.shp‘);
????if?ds?is?None:
????????print
????????‘Could?not?open?1km地鐵站試驗2.shp‘
????????sys.exit(1)
????#?獲取第0個圖層
????layer0?=?ds.GetlayerByIndex(0);
????#?投影
????spatialRef?=?layer0.GetSpatialRef();
????#?輸出圖層中的要素個數
????print(‘要素個數=%d‘?layer0.GetFeatureCount(0))
????print(‘屬性表結構信息‘)
????defn?=?layer0.GetlayerDefn()
????iFieldCount?=?defn.GetFieldCount()
????for?index?in?range(iFieldCount):
????????oField?=?defn.GetFieldDefn(index)
????????print(‘%s:?%s(%d.%d)‘?%?(
????????oField.GetNameRef()?oField.GetFieldTypeName(oField.GetType())?oField.GetWidth()?oField.GetPrecision()))
????feature?=?layer0.GetNextFeature()
????#?下面開始遍歷圖層中的要素
????while?feature?is?not?None:
????????#?獲取要素中的屬性表內容
????????for?index?in?range(iFieldCount):
????????????oField?=?defn.GetFieldDefn(index)
????????????line?=?“?%s?(%s)?=?“?%?(oField.GetNameRef()?oField.GetFieldTypeName(oField.GetType()))
????????????if?feature.IsFieldSet(index):
????????????????line?=?line?+?“%s“?%?(feature.GetFieldAsString(index))
????????????else:
????????????????line?=?line?+?“(null)“
????????????print(line)
????????????#?獲取要素中的幾何體
????????geometry?=?feature.GetGeometryRef()
????????print
????????geometry
????????#?為了演示,只輸出一個要素信息
????????break
????feature.Destroy()
????ds.Destroy()
#創建shap文件
def?createShap():
????#為了支持中文路徑,請添加下面這句代碼
????gdal.SetConfigOption(“GDAL_FILENAME_IS_UTF8““NO“)
????#為了使屬性表字段支持中文,請添加下面這句
????gdal.SetConfigOption(“SHAPE_ENCODING“““)
????#注冊所有的驅動
????ogr.RegisterAll()
????#數據格式的驅動
????driver?=?ogr.GetDriverByName(‘ESRI?Shapefile‘)
????ds=driver.CreateDataSource(“E:\\temp\\shp“)
????shaplayer=ds.Createlayer(“poi“geom_type=ogr.wkbPoint);
????#添加字段
????fieldDefn?=?ogr.FieldDefn(‘id‘?ogr.OFTString)
????fieldDefn.SetWidth(4)
????shaplayer.CreateField(fieldDefn);
????#創建feature
????defn?=?shaplayer.GetlayerDefn()
????feature?=?ogr.Feature(defn)?;
????#添加屬性
????feature.SetField(“id““liu“)
????#添加坐標
????point?=?ogr.Geometry(ogr.wkbPoint)
????point.AddPoint(float(113.56647912)float(22.16128203))
????feature.SetGeometry(point);
????shaplayer.CreateFeature(feature)
????feature.Destroy()
????#指定投影
????sr?=?osr.SpatialReference();
????sr.ImportFromEPSG(32612);
????prjFile?=?open(“E:\\temp\\shp\\poi.prj“‘w‘);
????sr.MorphToESRI();
????prjFile.write(sr.ExportToWkt());
????prjFile.close();
????ds.Destroy()
def?main():
????readShap();
????print(‘readShap?finish‘)
????createShap();
??
評論
共有 條評論