Python-shp转wkt

将shpfile文件转为wkt文本形式。可获取字段名称及几何坐标

#shp转wkt
from osgeo import ogr
    shapefile = r"D:\test.shp"

driver = ogr.GetDriverByName("ESRI Shapefile")
dataSource = driver.Open(shapefile, 0)
layer = dataSource.GetLayer()

layerDefinition = layer.GetLayerDefn()
#获取字段名称列表
#fields = []
#for i in range(layerDefinition.GetFieldCount()):
    #fields.append(layerDefinition.GetFieldDefn(i).GetName())
#print(fields)
#获取字段值及几何
for feature in layer:
    #classes = feature.GetField("Classes")
    geom = feature.GetGeometryRef()
    geomwkt = geom.ExportToWkt()
    print("%s;%s\n"%(geom,geomwkt))
posted @ 2023-02-05 09:53  Xiangze  阅读(515)  评论(0编辑  收藏  举报