gdal

package module
v0.0.0-...-f24acd4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 31, 2020 License: BSD-3-Clause Imports: 13 Imported by: 2

README

  • 赞助 BTC: 1Cbd6oGAUUyBi7X7MaR4np4nTmQZXVgkCW
  • 赞助 ETH: 0x623A3C3a72186A6336C79b18Ac1eD36e1c71A8a6
  • Go语言付费QQ群: 1055927514

Go bindings for GDAL

Build Status GoDoc

Notes: Need Go1.5+!

Install

Install GCC or MinGW (http://tdm-gcc.tdragon.net/download) at first, and then run these commands:

  1. go get github.com/chai2010/gdal
  2. go run hello.go

For windows-64bit user:

  1. run go generate github.com/chai2010/gdal copy the gdal-cgo-win64.dll to $(GOPATH)/bin

For windows-32bit user:

  1. Please build gdal-cgo-win32.dll and ./build-windows/lib/libgdal-cgo-386.a first!

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.")
}

BUGS

Report bugs to chaishushan@gmail.com.

Thanks!

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:

  1. `go get github.com/chai2010/gdal`
  2. `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

Examples

Constants

View Source
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)
)
View Source
const (
	MemPMagic = "MemP" // See https://github.com/chai2010/image
)

Variables

View Source
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",
}

See http://www.gdal.org/formats_list.html

Functions

func ChannelsOf

func ChannelsOf(m image.Image) int

func ColorModel

func ColorModel(channels int, dataType reflect.Kind) color.Model
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 DepthOf

func DepthOf(m image.Image) int

func Load

func Load(filename string, buffer ...[]byte) (m image.Image, err error)

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

func LoadConfig(filename string) (config image.Config, err error)

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

func Save(filename string, m image.Image, opt *Options) (err error)

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 SizeofImage(m image.Image) int

func SizeofKind

func SizeofKind(dataType reflect.Kind) int
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

func SizeofPixel(channels int, dataType reflect.Kind) int
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 Access

type Access int
const (
	GA_ReadOnly Access = iota
	GA_Update
)

type ColorModelInterface

type ColorModelInterface interface {
	Channels() int
	DataType() reflect.Kind
}

type Dataset

type Dataset struct {
	Filename string
	Opt      *Options
	// contains filtered or unexported fields
}
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 CreateDataset(filename string, width, height, channels int, dataType reflect.Kind, opt *Options) (p *Dataset, err error)

func CreateDatasetCopy

func CreateDatasetCopy(filename string, src *Dataset, opt *Options) (p *Dataset, err error)

func OpenDataset

func OpenDataset(filename string, flag Access) (p *Dataset, err error)

func OpenDatasetWithOverviews

func OpenDatasetWithOverviews(filename string, resampleType ResampleType, flag Access) (p *Dataset, err error)

func (*Dataset) BuildOverviews

func (p *Dataset) BuildOverviews() error

func (*Dataset) BuildOverviewsIfNotExists

func (p *Dataset) BuildOverviewsIfNotExists() error

func (*Dataset) BuildOverviewsIsRunning

func (p *Dataset) BuildOverviewsIsRunning() bool

func (*Dataset) Channels

func (p *Dataset) Channels() int

func (*Dataset) Close

func (p *Dataset) Close() error

func (*Dataset) DataType

func (p *Dataset) DataType() reflect.Kind

func (*Dataset) Flush

func (p *Dataset) Flush() error

func (*Dataset) HasOverviews

func (p *Dataset) HasOverviews() bool

func (*Dataset) Height

func (p *Dataset) Height() int

func (*Dataset) Read

func (p *Dataset) Read(r image.Rectangle) (m image.Image, err error)

func (*Dataset) ReadOverview

func (p *Dataset) ReadOverview(idxOverview int, r image.Rectangle) (m image.Image, err error)

func (*Dataset) ReadToBuf

func (p *Dataset) ReadToBuf(r image.Rectangle, data []byte, stride int) error

func (*Dataset) ReadToSize

func (p *Dataset) ReadToSize(r image.Rectangle, size image.Point) (m image.Image, err error)

func (*Dataset) SetGeoTransform

func (p *Dataset) SetGeoTransform(transform [6]float64) error

func (*Dataset) SetGeoTransformX0Y0DxDy

func (p *Dataset) SetGeoTransformX0Y0DxDy(x0, y0, dx, dy float64) error

func (*Dataset) SetProjection

func (p *Dataset) SetProjection(projName string) error

func (*Dataset) SetResampleType

func (p *Dataset) SetResampleType(resampleType ResampleType) error

func (*Dataset) Width

func (p *Dataset) Width() int

func (*Dataset) Write

func (p *Dataset) Write(r image.Rectangle, src image.Image) error

func (*Dataset) WriteFromBuf

func (p *Dataset) WriteFromBuf(r image.Rectangle, data []byte, stride int) 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 MemPColor

type MemPColor struct {
	Channels int
	DataType reflect.Kind
	Pix      PixSlice
}

func (MemPColor) RGBA

func (c MemPColor) RGBA() (r, g, b, a uint32)

type MemPImage

type MemPImage struct {
	XMemPMagic string // MemP
	XRect      image.Rectangle
	XChannels  int
	XDataType  reflect.Kind
	XPix       PixSlice
	XStride    int
}

func AsMemPImage

func AsMemPImage(m interface{}) (p *MemPImage, ok bool)

m is MemP or image.Image

func LoadImage

func LoadImage(filename string, buffer ...[]byte) (m *MemPImage, err error)

LoadImage reads a GDAL image from file and returns it as an Image.

func NewMemPImage

func NewMemPImage(r image.Rectangle, channels int, dataType reflect.Kind) *MemPImage

func NewMemPImageFrom

func NewMemPImageFrom(m image.Image) *MemPImage

func (*MemPImage) AsStdImage

func (p *MemPImage) AsStdImage() (m image.Image, ok bool)

func (*MemPImage) At

func (p *MemPImage) At(x, y int) color.Color

func (*MemPImage) Bounds

func (p *MemPImage) Bounds() image.Rectangle

func (*MemPImage) Channels

func (p *MemPImage) Channels() int

func (*MemPImage) Clone

func (p *MemPImage) Clone() *MemPImage

func (*MemPImage) ColorModel

func (p *MemPImage) ColorModel() color.Model

func (*MemPImage) DataType

func (p *MemPImage) DataType() reflect.Kind

func (*MemPImage) MemPMagic

func (p *MemPImage) MemPMagic() string

func (*MemPImage) Pix

func (p *MemPImage) Pix() []byte

func (*MemPImage) PixOffset

func (p *MemPImage) PixOffset(x, y int) int

func (*MemPImage) PixelAt

func (p *MemPImage) PixelAt(x, y int) []byte

func (*MemPImage) Set

func (p *MemPImage) Set(x, y int, c color.Color)

func (*MemPImage) SetPixel

func (p *MemPImage) SetPixel(x, y int, c []byte)

func (*MemPImage) StdImage

func (p *MemPImage) StdImage() image.Image

func (*MemPImage) Stride

func (p *MemPImage) Stride() int

func (*MemPImage) SubImage

func (p *MemPImage) SubImage(r image.Rectangle) image.Image

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) Bytes

func (d PixSlice) Bytes() (v []byte)

func (PixSlice) Complex128s

func (d PixSlice) Complex128s() (v []complex128)

func (PixSlice) Complex64s

func (d PixSlice) Complex64s() (v []complex64)

func (PixSlice) Float32s

func (d PixSlice) Float32s() (v []float32)

func (PixSlice) Float64s

func (d PixSlice) Float64s() (v []float64)

func (PixSlice) Int16s

func (d PixSlice) Int16s() (v []int16)

func (PixSlice) Int32s

func (d PixSlice) Int32s() (v []int32)

func (PixSlice) Int64s

func (d PixSlice) Int64s() (v []int64)

func (PixSlice) Int8s

func (d PixSlice) Int8s() (v []int8)

func (PixSlice) SetValue

func (d PixSlice) SetValue(i int, dataType reflect.Kind, v float64)

func (PixSlice) Slice

func (d PixSlice) Slice(newSliceType reflect.Type) interface{}

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

func (d PixSlice) SwapEndian(dataType reflect.Kind)
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]

func (PixSlice) Uint16s

func (d PixSlice) Uint16s() (v []uint16)

func (PixSlice) Uint32s

func (d PixSlice) Uint32s() (v []uint32)

func (PixSlice) Uint64s

func (d PixSlice) Uint64s() (v []uint64)

func (PixSlice) Uint8s

func (d PixSlice) Uint8s() []uint8

func (PixSlice) Value

func (d PixSlice) Value(i int, dataType reflect.Kind) float64

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
}

Directories

Path Synopsis
Package gdal_bench provides more benchmark for gdal.
Package gdal_bench provides more benchmark for gdal.
cmd
mkovr
Make GDAL Overviews file is not exists.
Make GDAL Overviews file is not exists.
Package tfw provides tfw reader.
Package tfw provides tfw reader.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL