【python】gdal创建普通点线面


import os

try:
    from osgeo import ogr, gdal
    from osgeo import osr
except:
    import ogr
# 属性
reader = [
    {
        'Name': "a",
        'Region': "我是",
        'Latitude': "86478.950188794",
        'Longitude': "74905.3874084952",
        'Elev': 10,

    }, {
        'Name': "b",
        'Region': "b",
        'Latitude': "86480",
        'Longitude': "74915",
        'Elev': 20,

    }
]
gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES")  # 路径中文
gdal.SetConfigOption("SHAPE_ENCODING", "GBK")  # 属性中文
"""
POINT(6 10)
LINESTRING(3 4,10 50,20 25)
POLYGON((1 1,5 1,5 5,1 5,1 1),(2 2,2 3,3 3,3 2,2 2))
MULTIPOINT(3.5 5.6, 4.8 10.5)
MULTILINESTRING((3 4,10 50,20 25),(-5 -8,-10 -8,-15 -4))
MULTIPOLYGON(((1 1,5 1,5 5,1 5,1 1),(2 2,2 3,3 3,3 2,2 2)),((6 3,9 2,9 4,6 3)))
GEOMETRYCOLLECTION(POINT(4 6),LINESTRING(4 6,7 10))
POINT ZM (1 1 5 60)
POINT M (1 1 80)
POINT EMPTY
MULTIPOLYGON EMPTY"""


def createPoint(reader):
    # 设置shapefile驱动
    driver = ogr.GetDriverByName("ESRI Shapefile")

    # 创建数据源
    data_source = driver.CreateDataSource("point.shp")

    # 创建空间参考,WGS84
    srs = osr.SpatialReference()
    srs.ImportFromEPSG(4326)

    # 创建图层
    # layer = data_source.CreateLayer("point", srs,ogr.wkbPoint) # 带坐标系文件

    layer = data_source.CreateLayer("point", geom_type=ogr.wkbPoint)  # 无坐标系文件

    # 设置字段
    field_name = ogr.FieldDefn("名称", ogr.OFTString)  # ogr.OFTString -> shapefile String
    field_name.SetWidth(24)  # 设置长度
    layer.CreateField(field_name)  # 创建字段
    field_region = ogr.FieldDefn("Region", ogr.OFTString)
    field_region.SetWidth(24)
    layer.CreateField(field_region)  # 创建字段
    layer.CreateField(
        ogr.FieldDefn("Latitude", ogr.OFTReal))  # 创建 名为Latitude 的字段数据类型为 Double 23/15 . ogr.OFTReal->shapefile Double
    layer.CreateField(ogr.FieldDefn("Longitude", ogr.OFTReal))
    layer.CreateField(
        ogr.FieldDefn("Elevation", ogr.OFTInteger))  # 创建 名为 Elevation 的字段数据类型为 long 9 ogr.OFTInteger ->shapefile Long

    for row in reader:
        # 创建特征
        feature = ogr.Feature(layer.GetLayerDefn())
        # 和设置字段内容进行关联  ,从数据源中写入数据
        feature.SetField("名称", row['Name'])
        feature.SetField("Region", row['Region'])
        feature.SetField("Latitude", row['Latitude'])
        feature.SetField("Longitude", row['Longitude'])
        feature.SetField("Elevation", row['Elev'])

        # 创建WKT 文本点
        wkt = "POINT(%f %f)" % (
            float(row['Longitude']), float(row['Latitude']))

        # 生成实体点
        point = ogr.CreateGeometryFromWkt(wkt)

        # 使用点
        feature.SetGeometry(point)
        # 添加点
        layer.CreateFeature(feature)
        # 关闭 特征
        feature = None
    # 关闭数据
    data_source = None


def createLine(reader):
    # 设置shapefile驱动
    driver = ogr.GetDriverByName("ESRI Shapefile")

    # 创建数据源
    data_source = driver.CreateDataSource("line.shp")

    # 创建空间参考,WGS84
    srs = osr.SpatialReference()
    srs.ImportFromEPSG(4326)

    # 创建图层
    layer = data_source.CreateLayer("line", srs, ogr.wkbLineString)

    # 设置字段
    field_name = ogr.FieldDefn("名称", ogr.OFTString)  # ogr.OFTString -> shapefile String
    field_name.SetWidth(24)  # 设置长度
    layer.CreateField(field_name)  # 创建字段
    field_region = ogr.FieldDefn("Region", ogr.OFTString)
    field_region.SetWidth(24)
    layer.CreateField(field_region)  # 创建字段
    layer.CreateField(
        ogr.FieldDefn("Latitude", ogr.OFTReal))  # 创建 名为Latitude 的字段数据类型为 Double 23/15 . ogr.OFTReal->shapefile Double
    layer.CreateField(ogr.FieldDefn("Longitude", ogr.OFTReal))
    layer.CreateField(
        ogr.FieldDefn("Elevation", ogr.OFTInteger))  # 创建 名为 Elevation 的字段数据类型为 long 9 ogr.OFTInteger ->shapefile Long

    for row in reader:
        # 创建特征
        feature = ogr.Feature(layer.GetLayerDefn())
        # 和设置字段内容进行关联  ,从数据源中写入数据
        feature.SetField("名称", row['Name'])
        feature.SetField("Region", row['Region'])
        feature.SetField("Latitude", row['Latitude'])
        feature.SetField("Longitude", row['Longitude'])
        feature.SetField("Elevation", row['Elev'])

        # 创建WKT 文本点
        wkt = "LINESTRING(%f %f,%f %f)" % (
            float(row['Longitude']), float(row['Latitude']), float(row['Latitude']), float(row['Latitude']))

        # 生成实体点
        point = ogr.CreateGeometryFromWkt(wkt)

        # 使用点
        feature.SetGeometry(point)
        # 添加点
        layer.CreateFeature(feature)
        # 关闭 特征
        feature = None
    # 关闭数据
    data_source = None


def createPolygon(reader):
    # 设置shapefile驱动
    driver = ogr.GetDriverByName("ESRI Shapefile")

    # 创建数据源
    data_source = driver.CreateDataSource("polygon.shp")

    # 创建空间参考,WGS84
    srs = osr.SpatialReference()
    srs.ImportFromEPSG(4326)

    # 创建图层
    layer = data_source.CreateLayer("polygon", srs, ogr.wkbPolygon)

    # 设置字段
    field_name = ogr.FieldDefn("名称", ogr.OFTString)  # ogr.OFTString -> shapefile String
    field_name.SetWidth(24)  # 设置长度
    layer.CreateField(field_name)  # 创建字段
    field_region = ogr.FieldDefn("Region", ogr.OFTString)
    field_region.SetWidth(24)
    layer.CreateField(field_region)  # 创建字段
    layer.CreateField(
        ogr.FieldDefn("Latitude", ogr.OFTReal))  # 创建 名为Latitude 的字段数据类型为 Double 23/15 . ogr.OFTReal->shapefile Double
    layer.CreateField(ogr.FieldDefn("Longitude", ogr.OFTReal))
    layer.CreateField(
        ogr.FieldDefn("Elevation", ogr.OFTInteger))  # 创建 名为 Elevation 的字段数据类型为 long 9 ogr.OFTInteger ->shapefile Long

    for row in reader:
        # 创建特征
        feature = ogr.Feature(layer.GetLayerDefn())
        # 和设置字段内容进行关联  ,从数据源中写入数据
        feature.SetField("名称", row['Name'])
        feature.SetField("Region", row['Region'])
        feature.SetField("Latitude", row['Latitude'])
        feature.SetField("Longitude", row['Longitude'])
        feature.SetField("Elevation", row['Elev'])

        # 创建WKT 文本面
        wkt = "POLYGON((120 30,30 120 ,120 120,120 30))"

        # 生成实体点
        point = ogr.CreateGeometryFromWkt(wkt)

        # 使用点
        feature.SetGeometry(point)
        # 添加点
        layer.CreateFeature(feature)
        # 关闭 特征
        feature = None
    # 关闭数据
    data_source = None


if __name__ == '__main__':
    # createLine(reader)
    createPoint(reader)
    # createPolygon(reader)
    pass

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

You may also like...