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
Python gdal2tiles.py –p mercator –z 0-5 –w none image dir
上一篇: 用批处理实现禁止运行指定程序
下一篇: dubbo学习笔记--dubbo用法
推荐阅读
-
Python3.4学习笔记之常用操作符,条件分支和循环用法示例
-
PHP超级全局变量【$GLOBALS,$_SERVER,$_REQUEST等】用法实例分析
-
C#中String StringBuilder StringBuffer类的用法
-
Laravel框架Eloquent ORM新增数据、自定义时间戳及批量赋值用法详解
-
深入探讨:oracle中row_number() over()分析函数用法
-
oracle case when 语句的用法详解
-
ASP.NET C#中Application的用法教程
-
微信小程序 支付功能开发错误总结
-
SQL中distinct 和 row_number() over() 的区别及用法
-
Laravel5.1框架自带权限控制系统 ACL用法分析