【python】gdal 获取相关属性


from osgeo import ogr, osr
import re
driver = ogr.GetDriverByName('ESRI Shapefile')

daShapefile = r'E:\gitck\lineChick\Shapfile\point_test.shp'
dataSource = ogr.Open(daShapefile)
daLayer = dataSource.GetLayer(0)
layerDefinition = daLayer.GetLayerDefn()
layer = dataSource.GetLayer()

p = {}
# (点名, 坐标)
for i in range(layerDefinition.GetFieldCount()):
    fieldName = layerDefinition.GetFieldDefn(i).GetName()
    if fieldName == "wellname":
        for feature in layer:
            geom = feature.GetGeometryRef()
            name = feature.GetField("wellname")
            a = geom.Centroid().ExportToWkt()
            regex = "[-+]?([0-9]*\.[0-9]+|[0-9]+)"
            prog = re.compile(regex)
            result = prog.findall(a)
            print(a, type(a),result)
            # print(name, geom.Centroid().ExportToWkt())
            # p[name] = geom.Centroid().ExportToWkt()
print(p)
"""
# 获取字段名
for i in range(layerDefinition.GetFieldCount()):
    fieldName = layerDefinition.GetFieldDefn(i).GetName() # 字段名称
    fieldTypeCode = layerDefinition.GetFieldDefn(i).GetType() # 数据类型
    fieldType = layerDefinition.GetFieldDefn(i).GetFieldTypeName(fieldTypeCode) # 长度
    fieldWidth = layerDefinition.GetFieldDefn(i).GetWidth()#
    GetPrecision = layerDefinition.GetFieldDefn(i).GetPrecision()
    print(fieldName + " - " + fieldType + " " + str(fieldWidth) + " " + str(GetPrecision))

# 获取指定字段的所有值
layer = dataSource.GetLayer()
# for feature in layer:
#     print(feature.GetField("wellname"))
# layer.ResetReading()

# 获取坐标值WKT数据
for feature in layer:
    geom = feature.GetGeometryRef()
    print(geom.Centroid().ExportToWkt())
"""

转载自:https://blog.csdn.net/staHuri/article/details/80096133

You may also like...