Documentation ¶
Overview ¶
Package gdal provides Go bindings for GDAL.
Install `GCC` or `MinGW` (http://tdm-gcc.tdragon.net/download) at first, and then run these commands:
- `go get github.com/chai2010/gdal`
- `go run hello.go`
Example:
// Copyright 2015 <chaishushan{AT}gmail.com>. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. package main import ( "fmt" "log" "github.com/chai2010/gdal" ) func main() { fmt.Printf("GDAL %d.%d.%d\n", gdal.MajorVersion, gdal.MinorVersion, gdal.RevVersion) // load data m, err := gdal.Load("./testdata/lena512color.png") if err != nil { log.Fatal("gdal.Load:", err) } // save bmp err = gdal.Save("output.bmp", m, nil) if err != nil { log.Fatal("gdal.Save:", err) } // save tiff err = gdal.Save("output.tiff", m, nil) if err != nil { log.Fatal("gdal.Save:", err) } // save jpeg-tiff data err = gdal.Save("output.jpeg.tiff", m, &gdal.Options{ DriverName: "GTiff", ExtOptions: map[string]string{ "COMPRESS": "JPEG", "JPEG_QUALITY": "75", }, }) if err != nil { log.Fatal("gdal.Save:", err) } fmt.Println("Done.") }
Report bugs to <chaishushan@gmail.com>.
Thanks!
Index ¶
- Constants
- Variables
- func ChannelsOf(m image.Image) int
- func ColorModel(channels int, dataType reflect.Kind) color.Model
- func DepthOf(m image.Image) int
- func Load(filename string, buffer ...[]byte) (m image.Image, err error)
- func LoadConfig(filename string) (config image.Config, err error)
- func Save(filename string, m image.Image, opt *Options) (err error)
- func SizeofImage(m image.Image) int
- func SizeofKind(dataType reflect.Kind) int
- func SizeofPixel(channels int, dataType reflect.Kind) int
- type Access
- type ColorModelInterface
- type Dataset
- func CreateDataset(filename string, width, height, channels int, dataType reflect.Kind, ...) (p *Dataset, err error)
- func CreateDatasetCopy(filename string, src *Dataset, opt *Options) (p *Dataset, err error)
- func OpenDataset(filename string, flag Access) (p *Dataset, err error)
- func OpenDatasetWithOverviews(filename string, resampleType ResampleType, flag Access) (p *Dataset, err error)
- func (p *Dataset) BuildOverviews() error
- func (p *Dataset) BuildOverviewsIfNotExists() error
- func (p *Dataset) BuildOverviewsIsRunning() bool
- func (p *Dataset) Channels() int
- func (p *Dataset) Close() error
- func (p *Dataset) DataType() reflect.Kind
- func (p *Dataset) Flush() error
- func (p *Dataset) HasOverviews() bool
- func (p *Dataset) Height() int
- func (p *Dataset) Read(r image.Rectangle) (m image.Image, err error)
- func (p *Dataset) ReadOverview(idxOverview int, r image.Rectangle) (m image.Image, err error)
- func (p *Dataset) ReadToBuf(r image.Rectangle, data []byte, stride int) error
- func (p *Dataset) ReadToSize(r image.Rectangle, size image.Point) (m image.Image, err error)
- func (p *Dataset) SetGeoTransform(transform [6]float64) error
- func (p *Dataset) SetGeoTransformX0Y0DxDy(x0, y0, dx, dy float64) error
- func (p *Dataset) SetProjection(projName string) error
- func (p *Dataset) SetResampleType(resampleType ResampleType) error
- func (p *Dataset) Width() int
- func (p *Dataset) Write(r image.Rectangle, src image.Image) error
- func (p *Dataset) WriteFromBuf(r image.Rectangle, data []byte, stride int) error
- type MemP
- type MemPColor
- type MemPImage
- func (p *MemPImage) AsStdImage() (m image.Image, ok bool)
- func (p *MemPImage) At(x, y int) color.Color
- func (p *MemPImage) Bounds() image.Rectangle
- func (p *MemPImage) Channels() int
- func (p *MemPImage) Clone() *MemPImage
- func (p *MemPImage) ColorModel() color.Model
- func (p *MemPImage) DataType() reflect.Kind
- func (p *MemPImage) MemPMagic() string
- func (p *MemPImage) Pix() []byte
- func (p *MemPImage) PixOffset(x, y int) int
- func (p *MemPImage) PixelAt(x, y int) []byte
- func (p *MemPImage) Set(x, y int, c color.Color)
- func (p *MemPImage) SetPixel(x, y int, c []byte)
- func (p *MemPImage) StdImage() image.Image
- func (p *MemPImage) Stride() int
- func (p *MemPImage) SubImage(r image.Rectangle) image.Image
- type Options
- type PixSlice
- func (d PixSlice) Bytes() (v []byte)
- func (d PixSlice) Complex128s() (v []complex128)
- func (d PixSlice) Complex64s() (v []complex64)
- func (d PixSlice) Float32s() (v []float32)
- func (d PixSlice) Float64s() (v []float64)
- func (d PixSlice) Int16s() (v []int16)
- func (d PixSlice) Int32s() (v []int32)
- func (d PixSlice) Int64s() (v []int64)
- func (d PixSlice) Int8s() (v []int8)
- func (d PixSlice) SetValue(i int, dataType reflect.Kind, v float64)
- func (d PixSlice) Slice(newSliceType reflect.Type) interface{}
- func (d PixSlice) SwapEndian(dataType reflect.Kind)
- func (d PixSlice) Uint16s() (v []uint16)
- func (d PixSlice) Uint32s() (v []uint32)
- func (d PixSlice) Uint64s() (v []uint64)
- func (d PixSlice) Uint8s() []uint8
- func (d PixSlice) Value(i int, dataType reflect.Kind) float64
- type ResampleType
- type SizeofImager
Examples ¶
Constants ¶
const ( MajorVersion = int(C.GDAL_VERSION_MAJOR) MinorVersion = int(C.GDAL_VERSION_MINOR) RevVersion = int(C.GDAL_VERSION_REV) BuildVersion = int(C.GDAL_VERSION_BUILD) ReleaseDate = int(C.GDAL_RELEASE_DATE) ReleaseName = string(C.GDAL_RELEASE_NAME) )
const (
MemPMagic = "MemP" // See https://github.com/chai2010/image
)
Variables ¶
var DefaultDriverNameMap = map[string]string{
".blx": "BLX",
".xlb": "BLX",
".bmp": "BMP",
".kap": "BSB",
".bt": "BT",
".dim": "DIMAP",
".dog": "DOQ1",
".dt0": "DTED",
".dt1": "DTED",
".dt2": "DTED",
".hdr": "EHdr",
".ers": "ERS",
".n1": "ESAT",
".gif": "GIF",
".grb": "GRIB",
".gta": "GTA",
".tif": "GTiff",
".tiff": "GTiff",
".img": "HFA",
".mpr": "ILWIS",
".mpl": "ILWIS",
".jpg": "JPEG",
".jpeg": "JPEG",
".ntf": "NITF",
".nsf": "NITF",
".grc": "NWT_GRC",
".tab": "NWT_GRC",
".png": "PNG",
".ppm": "PNM",
".pgm": "PNM",
".rik": "RIK",
".rsw": "RMF",
".mtw": "RMF",
".ter": "TERRAGEN",
".dem": "USGSDEM",
".vrt": "VRT",
".xpm": "XPM",
}
Functions ¶
func ChannelsOf ¶
func ColorModel ¶
Example ¶
rgba := color.RGBA{R: 101, G: 102, B: 103, A: 104} c := ColorModel(4, reflect.Uint8).Convert(rgba).(MemPColor) fmt.Printf("c = %v\n", c)
Output: c = {4 uint8 [101 102 103 104]}
func Load ¶
Load reads a GDAL image from file and returns it as an image.Image.
Example ¶
m, err := Load("./testdata/video-001.tiff") if err != nil { log.Fatal(err) } fmt.Printf("Bounds = %v\n", m.Bounds())
Output: Bounds = (0,0)-(150,103)
func LoadConfig ¶
LoadConfig returns the color model and dimensions of a GDAL image without decoding the entire image.
Example ¶
cfg, err := LoadConfig("./testdata/video-001.tiff") if err != nil { log.Fatal(err) } fmt.Printf("Width = %d\n", cfg.Width) fmt.Printf("Height = %d\n", cfg.Height)
Output: Width = 150 Height = 103
func Save ¶
Encode writes the image m to w in GDAL format.
Example ¶
tmpname := "z_test_ExampleSave.tiff" defer os.Remove(tmpname) gray := NewMemPImage(image.Rect(0, 0, 400, 300), 1, reflect.Uint8) if err := Save(tmpname, gray, nil); err != nil { log.Fatal(err) }
Output:
func SizeofImage ¶
func SizeofKind ¶
Example ¶
fmt.Printf("%v = %v\n", reflect.Uint8, SizeofKind(reflect.Uint8)) fmt.Printf("%v = %v\n", reflect.Uint16, SizeofKind(reflect.Uint16)) fmt.Printf("%v = %v\n", reflect.Uint32, SizeofKind(reflect.Uint32)) fmt.Printf("%v = %v\n", reflect.Float32, SizeofKind(reflect.Float32)) fmt.Printf("%v = %v\n", reflect.Float64, SizeofKind(reflect.Float64))
Output: uint8 = 1 uint16 = 2 uint32 = 4 float32 = 4 float64 = 8
func SizeofPixel ¶
Example ¶
fmt.Printf("sizeof(gray) = %d\n", SizeofPixel(1, reflect.Uint8)) fmt.Printf("sizeof(gray16) = %d\n", SizeofPixel(1, reflect.Uint16)) fmt.Printf("sizeof(rgb) = %d\n", SizeofPixel(3, reflect.Uint8)) fmt.Printf("sizeof(rgb48) = %d\n", SizeofPixel(3, reflect.Uint16)) fmt.Printf("sizeof(rgba) = %d\n", SizeofPixel(4, reflect.Uint8)) fmt.Printf("sizeof(rgba64) = %d\n", SizeofPixel(4, reflect.Uint16)) fmt.Printf("sizeof(float32) = %d\n", SizeofPixel(1, reflect.Float32))
Output: sizeof(gray) = 1 sizeof(gray16) = 2 sizeof(rgb) = 3 sizeof(rgb48) = 6 sizeof(rgba) = 4 sizeof(rgba64) = 8 sizeof(float32) = 4
Types ¶
type ColorModelInterface ¶
type Dataset ¶
Example (SetTFW) ¶
package main import ( "fmt" "log" "github.com/chai2010/gdal" ) func main() { po, err := gdal.OpenDataset("some.tiff", gdal.GA_Update) if err != nil { log.Fatal(err) } defer po.Close() if err = po.SetGeoTransformX0Y0DxDy(0, 0, 1, 1); err != nil { log.Fatal(err) } fmt.Println("Done") }
Output:
func CreateDataset ¶
func CreateDatasetCopy ¶
func OpenDatasetWithOverviews ¶
func OpenDatasetWithOverviews(filename string, resampleType ResampleType, flag Access) (p *Dataset, err error)
func (*Dataset) BuildOverviews ¶
func (*Dataset) BuildOverviewsIfNotExists ¶
func (*Dataset) BuildOverviewsIsRunning ¶
func (*Dataset) HasOverviews ¶
func (*Dataset) ReadOverview ¶
func (*Dataset) ReadToSize ¶
func (*Dataset) SetGeoTransform ¶
func (*Dataset) SetGeoTransformX0Y0DxDy ¶
func (*Dataset) SetProjection ¶
func (*Dataset) SetResampleType ¶
func (p *Dataset) SetResampleType(resampleType ResampleType) error
type MemP ¶
type MemP interface { MemPMagic() string Bounds() image.Rectangle Channels() int DataType() reflect.Kind Pix() []byte // PixSlice type // Stride is the Pix stride (in bytes, must align with SizeofKind(p.DataType)) // between vertically adjacent pixels. Stride() int }
MemP Image Spec (Native Endian), see https://github.com/chai2010/image.
type MemPImage ¶
type MemPImage struct { XMemPMagic string // MemP XRect image.Rectangle XChannels int XDataType reflect.Kind XPix PixSlice XStride int }
func NewMemPImage ¶
func NewMemPImageFrom ¶
func (*MemPImage) ColorModel ¶
type Options ¶
type Options struct { DriverName string Projection string Transform [6]float64 ExtOptions map[string]string }
GDAL Raster Formats
See http://www.gdal.org/formats_list.html
Transform[0] /* top left x */ Transform[1] /* w-e pixel resolution */ Transform[2] /* 0 */ Transform[3] /* top left y */ Transform[4] /* 0 */ Transform[5] /* n-s pixel resolution (negative value) */
type PixSlice ¶
type PixSlice []byte
Example ¶
a := []int32{101, 102, 103} b := AsPixSlice(a) b.Int32s()[0] = 12345 b.SetValue(1, reflect.Int32, 1002) b.SetValue(2, reflect.Int32, 1003.5) fmt.Printf("len(b) = %d\n", len(b)) fmt.Printf("b.Int32s() = %v\n", b.Int32s())
Output: len(b) = 12 b.Int32s() = [12345 1002 1003]
func AsPixSlice ¶
func AsPixSlice(slice interface{}) (d PixSlice)
AsPixSlice convert a normal slice to byte slice.
Convert []X to []byte:
x := make([]X, xLen) y := AsPixSlice(x)
func (PixSlice) Complex128s ¶
func (d PixSlice) Complex128s() (v []complex128)
func (PixSlice) Complex64s ¶
func (PixSlice) Slice ¶
Slice convert a normal slice to new type slice.
Convert []byte to []Y:
x := make([]byte, xLen) y := PixSlice(x).Slice(reflect.TypeOf([]Y(nil))).([]Y)
func (PixSlice) SwapEndian ¶
Example ¶
rgba64 := image.NewRGBA64(image.Rect(0, 0, 1, 1)) rgba64.SetRGBA64(0, 0, color.RGBA64{ R: 0x0102, G: 0x0304, B: 0x0506, A: 0x0708, }) // pix is big-endian format fmt.Printf("big-endian: %v\n", rgba64.Pix) AsPixSlice(rgba64.Pix).SwapEndian(reflect.Uint16) fmt.Printf("little-endian: %v\n", rgba64.Pix)
Output: big-endian: [1 2 3 4 5 6 7 8] little-endian: [2 1 4 3 6 5 8 7]
type ResampleType ¶
type ResampleType int
const ( ResampleType_Nil ResampleType = iota // "NONE" ResampleType_Nearest // "NEAREST" ResampleType_Gauss // "GAUSS" ResampleType_Cubic // "CUBIC" ResampleType_Average // "AVERAGE" ResampleType_Mode // "MODE" ResampleType_AverageMagpase // "AVERAGE_MAGPHASE" )
func NewResampleType ¶
func NewResampleType(name string) ResampleType
func (ResampleType) Name ¶
func (p ResampleType) Name() string
type SizeofImager ¶
type SizeofImager interface {
SizeofImage() int
}