欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

gdal用法总结

程序员文章站 2022-03-22 09:11:44
...

USAGE OF GDAL RASTER API

Import gdal
from osgeo import gdal

Open the file

Dataset=gdal.Open(filename)

Getting dataset information

Xsize=dataset.RasterXsize
Ysize=dataset.RasterYSize
Count=dataset.RasterCount
Geotransform=dataset.GetGeoTransform()
Pro=dataset.GetProjection()

Fetching a raster band

band=dataset.GetRasterBand(1)
type=gdal.GetDataTypeName(band.DataType)
min=band.GetMinimum()
max=band.GetMaximum()
if not min or not max:
    (min,max)=band.ComputeRasterMinMax(True)

Reading raster data

scanline=band.ReadRaster(xoff=0,yoff=0,xsize=band.XSize,ysize=1,buf_xsize=band.XSize,buf_ysize=1,buf_type=gdal.GDT_Float32)
import struct
tuple_of_floats=struct.unpack(‘f’*band.XSize,scanline)

CreatCopy

driver=src_ds.GetDriver()
dst_ds=driver.CreateCopy(dst_filename,src_ds,strict=0)

Create

dst_ds=driver.Create(dst_filename,xsize=512,ysize=512,bands=1,eType=gdal.GDT_Byte)

Set dataset transform and projection

from osgeo import osr
dst_ds.SetGeoTransform([444720,30,0,375130,0,-30])
srs=osr.SpatialReference()
srs.importFromEPSG(4326)
dst_ds.SetProjection(srs.ExportToWkt())

set dataset value

import numpy
raster=numpy.zeros((512,512),dtype=numpy.uint8)
dst_ds.GetRasterBand(1).WriteArray(raster)

Close the dataset

dst_ds=None

USAGE OF GDAL VECTOR API

Import ogr
from osgeo import ogr

Open the file

dataset=ogr.Open(filename)
driver=dataset.GetDriver()

Getting Layer

count=dataset.GetLayerCount()
lyr=dataset.GetLayer(i)
lyr2=dataset.GetLayer(‘layername’)

Getting layer information

extent=lyr.GetExtent()
geom_type=lyr.GetGeomType()
ref=lyr.GetSpatialRef()
lyrname=lyr.GetName()
feature_all=lyr.GetFeatureCount()
schema=lyr.schema
for field in schema:
    print(field.name,field.GetTypeName())

Getting feature

fea=lyr.GetFeature(index)

Getting feature information

geo=fea.geometry()
field_value=fea.GetFiled(filedname)
field_value=fea.GetFieldAsString(fieldname)
field_value=fea.GetFieldAsDouble(filedname)
field_count=fea.GetFieldCount()

Getting geometry information

point_count=line.GetPointCount()
x_0=line.GetX(0)
y_0=line.GetY(0)
ring=polygon.GetGeometryRef(0)

Create

drive=ogr.GetDriverByName(‘ESRI Shapefile’)
dataset=driver.CreateDataSource(filename)
layer=dataset.CreateLayer(name)
fieldDefn=ogr.FieldDefn(‘id’,ogr.OFTString)
fieldDefn.SetWidth(4)
layer.CreateField(fieldDefn)
featureDefn=layer.GetLayerDefn()
feature=ogr.Feature(featureDefn)
feature.SetGeometry(geometry)
feature.SetField(‘id’,23)
layer.CreateFeature(feature)

Setting geometry information

point=ogr.Geometry(ogr.wkbPoint)
point.AddPoint(10,20)
line=ogr.Geometry(ogr.wkbLineString)
line.AddPoint(10,20)
line.AddPoint(10,20)
line.SetPoint(1,50,50)
ring=ogr.Geometry(ogr.wkbLineRing)
ring.AddPoint(10,10)
ring.AddPoint(50,0)
ring.AddPoint(50,50)
ring.CloseRings()
polygon=ogr.Geometry(ogr.wkbPolygon)
polygon.AddGeometry(outring)
polygon.AddGeometry(inring)

Delete

Driver.DeleteDataSource(filename)

USAGE OF GDAL SCRIPTS
gdallocationinfo.exe

gdallocationinfo.exe N27E088.hgt -wgs84 88.2123 27.1115

gdalinfo.exe

gdalinfo.exe –proj4 rec.tif

gdalwarp.exe

gdalwarp.exe [-s_srs srs_def] [-t_srs srs_def] [-ot Byte/Int16/...] [-srcnodata "value [value...]"] [-dstnodata "value [value...]"] [-of format] [-co "NAME=VALUE"] srcfile dstfile

gdal_translate.exe

gdal_translate.exe rec.tif my.tif –b 1 –outsize 300 300

gdal_merge.exe

gdal_merge.exe -o output.tif srtm_56_07.tif srtm_57_07.tif

gdal_contour.exe

gdal_contour.exe –a elev dem.tif countor.shp –i 10.0

gdal2tiles.py

Python gdal2tiles.py –p mercator –z 0-5 –w none image dir
相关标签: gdal用法