github.com/cellofellow/gopkg@v0.0.0-20140722061823-eec0544a62ad/image/gdal/gdal.go (about)

     1  // Copyright 2011 go-gdal. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package gdal
     6  
     7  /*
     8  #include "go_gdal.h"
     9  
    10  #cgo linux  pkg-config: gdal
    11  #cgo darwin pkg-config: gdal
    12  #cgo windows LDFLAGS: -lgdal.dll
    13  */
    14  import "C"
    15  import (
    16  	_ "fmt"
    17  	_ "runtime"
    18  	"unsafe"
    19  )
    20  
    21  func init() {
    22  	gdalAllRegister()
    23  }
    24  
    25  /* -------------------------------------------------------------------- */
    26  /*      Significant constants.                                          */
    27  /* -------------------------------------------------------------------- */
    28  
    29  // Pixel data types
    30  type GDALDataType int
    31  
    32  const (
    33  	// Unknown or unspecified type
    34  	GDT_Unknown = GDALDataType(C.GDT_Unknown)
    35  	// Eight bit unsigned integer
    36  	GDT_Byte = GDALDataType(C.GDT_Byte)
    37  	// Sixteen bit unsigned integer
    38  	GDT_UInt16 = GDALDataType(C.GDT_UInt16)
    39  	// Sixteen bit signed integer
    40  	GDT_Int16 = GDALDataType(C.GDT_Int16)
    41  	// Thirty two bit unsigned integer
    42  	GDT_UInt32 = GDALDataType(C.GDT_UInt32)
    43  	// Thirty two bit signed integer
    44  	GDT_Int32 = GDALDataType(C.GDT_Int32)
    45  	// Thirty two bit floating point
    46  	GDT_Float32 = GDALDataType(C.GDT_Float32)
    47  	// Sixty four bit floating point
    48  	GDT_Float64 = GDALDataType(C.GDT_Float64)
    49  	// Complex Int16
    50  	GDT_CInt16 = GDALDataType(C.GDT_CInt16)
    51  	// Complex Int32
    52  	GDT_CInt32 = GDALDataType(C.GDT_CInt32)
    53  	// Complex Float32
    54  	GDT_CFloat32 = GDALDataType(C.GDT_CFloat32)
    55  	// Complex Float64
    56  	GDT_CFloat64 = GDALDataType(C.GDT_CFloat64)
    57  	// maximum type # + 1
    58  	GDT_TypeCount = GDALDataType(C.GDT_TypeCount)
    59  )
    60  
    61  // Get data type size in bits.
    62  //
    63  // Returns the size of a a GDT_* type in bits, not bytes!
    64  func GDALGetDataTypeSize(dataType GDALDataType) int {
    65  	return int(C.GDALGetDataTypeSize(C.GDALDataType(dataType)))
    66  }
    67  func GDALDataTypeIsComplex(dataType GDALDataType) int {
    68  	return int(C.GDALDataTypeIsComplex(C.GDALDataType(dataType)))
    69  }
    70  func GDALGetDataTypeName(dataType GDALDataType) string {
    71  	return C.GoString(C.GDALGetDataTypeName(C.GDALDataType(dataType)))
    72  }
    73  func GDALGetDataTypeByName(dataTypeName string) GDALDataType {
    74  	name := C.CString(dataTypeName)
    75  	defer C.free(unsafe.Pointer(name))
    76  	return GDALDataType(C.GDALGetDataTypeByName(name))
    77  }
    78  func GDALDataTypeUnion(dataTypeA, dataTypeB GDALDataType) GDALDataType {
    79  	return GDALDataType(
    80  		C.GDALDataTypeUnion(C.GDALDataType(dataTypeA), C.GDALDataType(dataTypeB)),
    81  	)
    82  }
    83  
    84  // status of the asynchronous stream
    85  type GDALAsyncStatusType int
    86  
    87  const (
    88  	GARIO_PENDING   = GDALAsyncStatusType(C.GARIO_PENDING)
    89  	GARIO_UPDATE    = GDALAsyncStatusType(C.GARIO_UPDATE)
    90  	GARIO_ERROR     = GDALAsyncStatusType(C.GARIO_ERROR)
    91  	GARIO_COMPLETE  = GDALAsyncStatusType(C.GARIO_COMPLETE)
    92  	GARIO_TypeCount = GDALAsyncStatusType(C.GARIO_TypeCount)
    93  )
    94  
    95  func GDALGetAsyncStatusTypeName(statusType GDALAsyncStatusType) string {
    96  	return C.GoString(C.GDALGetAsyncStatusTypeName(C.GDALAsyncStatusType(statusType)))
    97  }
    98  func GDALGetAsyncStatusTypeByName(statusTypeName string) GDALAsyncStatusType {
    99  	name := C.CString(statusTypeName)
   100  	defer C.free(unsafe.Pointer(name))
   101  	return GDALAsyncStatusType(C.GDALGetAsyncStatusTypeByName(name))
   102  }
   103  
   104  // Flag indicating read/write, or read-only access to data.
   105  type GDALAccess int
   106  
   107  const (
   108  	// Read only (no update) access
   109  	GA_ReadOnly = GDALAccess(C.GA_ReadOnly)
   110  	// Read/write access.
   111  	GA_Update = GDALAccess(C.GA_Update)
   112  )
   113  
   114  // Read/Write flag for RasterIO() method
   115  type GDALRWFlag int
   116  
   117  const (
   118  	// Read data
   119  	GF_Read = GDALRWFlag(C.GF_Read)
   120  	// Write data
   121  	GF_Write = GDALRWFlag(C.GF_Write)
   122  )
   123  
   124  // Types of color interpretation for raster bands.
   125  type GDALColorInterp int
   126  
   127  const (
   128  	GCI_Undefined = GDALColorInterp(C.GCI_Undefined)
   129  
   130  	// Greyscale
   131  	GCI_GrayIndex = GDALColorInterp(C.GCI_GrayIndex)
   132  	// Paletted (see associated color table)
   133  	GCI_PaletteIndex = GDALColorInterp(C.GCI_PaletteIndex)
   134  	// Red band of RGBA image
   135  	GCI_RedBand = GDALColorInterp(C.GCI_RedBand)
   136  	// Green band of RGBA image
   137  	GCI_GreenBand = GDALColorInterp(C.GCI_GreenBand)
   138  	// Blue band of RGBA image
   139  	GCI_BlueBand = GDALColorInterp(C.GCI_BlueBand)
   140  	// Alpha (0=transparent, 255=opaque)
   141  	GCI_AlphaBand = GDALColorInterp(C.GCI_AlphaBand)
   142  	// Hue band of HLS image
   143  	GCI_HueBand = GDALColorInterp(C.GCI_HueBand)
   144  	// Saturation band of HLS image
   145  	GCI_SaturationBand = GDALColorInterp(C.GCI_SaturationBand)
   146  	// Lightness band of HLS image
   147  	GCI_LightnessBand = GDALColorInterp(C.GCI_LightnessBand)
   148  	// Cyan band of CMYK image
   149  	GCI_CyanBand = GDALColorInterp(C.GCI_CyanBand)
   150  	// Magenta band of CMYK image
   151  	GCI_MagentaBand = GDALColorInterp(C.GCI_MagentaBand)
   152  	// Yellow band of CMYK image
   153  	GCI_YellowBand = GDALColorInterp(C.GCI_YellowBand)
   154  	// Black band of CMLY image
   155  	GCI_BlackBand = GDALColorInterp(C.GCI_BlackBand)
   156  	// Y Luminance
   157  	GCI_YCbCr_YBand = GDALColorInterp(C.GCI_YCbCr_YBand)
   158  	// Cb Chroma
   159  	GCI_YCbCr_CbBand = GDALColorInterp(C.GCI_YCbCr_CbBand)
   160  	// Cr Chroma
   161  	GCI_YCbCr_CrBand = GDALColorInterp(C.GCI_YCbCr_CrBand)
   162  	// Max current value
   163  	GCI_Max = GDALColorInterp(C.GCI_Max)
   164  )
   165  
   166  func GDALGetColorInterpretationName(colorInterp GDALColorInterp) string {
   167  	return C.GoString(C.GDALGetColorInterpretationName(C.GDALColorInterp(colorInterp)))
   168  }
   169  func GDALGetColorInterpretationByName(pszName string) GDALColorInterp {
   170  	name := C.CString(pszName)
   171  	defer C.free(unsafe.Pointer(name))
   172  	return GDALColorInterp(C.GDALGetColorInterpretationByName(name))
   173  }
   174  
   175  // Types of color interpretations for a GDALColorTable.
   176  type GDALPaletteInterp int
   177  
   178  const (
   179  	// Grayscale (in GDALColorEntry.c1)
   180  	GPI_Gray = GDALPaletteInterp(C.GPI_Gray)
   181  	// Red, Green, Blue and Alpha in (in c1, c2, c3 and c4)
   182  	GPI_RGB = GDALPaletteInterp(C.GPI_RGB)
   183  	// Cyan, Magenta, Yellow and Black (in c1, c2, c3 and c4)
   184  	GPI_CMYK = GDALPaletteInterp(C.GPI_CMYK)
   185  	// Hue, Lightness and Saturation (in c1, c2, and c3)
   186  	GPI_HLS = GDALPaletteInterp(C.GPI_HLS)
   187  )
   188  
   189  func GDALGetPaletteInterpretationName(paletteInterp GDALPaletteInterp) string {
   190  	return C.GoString(
   191  		C.GDALGetPaletteInterpretationName(C.GDALPaletteInterp(paletteInterp)),
   192  	)
   193  }
   194  
   195  // "well known" metadata items.
   196  const (
   197  	GDALMD_AREA_OR_POINT = string(C.GDALMD_AREA_OR_POINT)
   198  	GDALMD_AOP_AREA      = string(C.GDALMD_AOP_AREA)
   199  	GDALMD_AOP_POINT     = string(C.GDALMD_AOP_POINT)
   200  )
   201  
   202  /* -------------------------------------------------------------------- */
   203  /*      GDAL Specific error codes.                                      */
   204  /*                                                                      */
   205  /*      error codes 100 to 299 reserved for GDAL.                       */
   206  /* -------------------------------------------------------------------- */
   207  const CPLE_WrongFormat = int(C.CPLE_WrongFormat)
   208  
   209  /* -------------------------------------------------------------------- */
   210  /*      Define handle types related to various internal classes.        */
   211  /* -------------------------------------------------------------------- */
   212  
   213  // Opaque type used for the C bindings of the C++ GDALMajorObject class
   214  type GDALMajorObjectH C.GDALMajorObjectH
   215  
   216  // Opaque type used for the C bindings of the C++ GDALDataset class
   217  type GDALDatasetH C.GDALDatasetH
   218  
   219  // Opaque type used for the C bindings of the C++ GDALRasterBand class
   220  type GDALRasterBandH C.GDALRasterBandH
   221  
   222  // Opaque type used for the C bindings of the C++ GDALDriver class
   223  type GDALDriverH C.GDALDriverH
   224  
   225  // Opaque type used for the C bindings of the C++ GDALColorTable class
   226  type GDALColorTableH C.GDALColorTableH
   227  
   228  // Opaque type used for the C bindings of the C++ GDALRasterAttributeTable class
   229  type GDALRasterAttributeTableH C.GDALRasterAttributeTableH
   230  
   231  // Opaque type used for the C bindings of the C++ GDALAsyncReader class
   232  type GDALAsyncReaderH C.GDALAsyncReaderH
   233  
   234  /* -------------------------------------------------------------------- */
   235  /*      Callback "progress" function.                                   */
   236  /* -------------------------------------------------------------------- */
   237  
   238  type GDALProgressFunc func(dfComplete float64, pszMessage string, pProgressArg interface{}) int
   239  
   240  func GDALDummyProgress(dfComplete float64, pszMessage string, pData interface{}) int {
   241  	msg := C.CString(pszMessage)
   242  	defer C.free(unsafe.Pointer(msg))
   243  
   244  	rv := C.GDALDummyProgress(C.double(dfComplete), msg, unsafe.Pointer(nil))
   245  	return int(rv)
   246  }
   247  func GDALTermProgress(dfComplete float64, pszMessage string, pData interface{}) int {
   248  	msg := C.CString(pszMessage)
   249  	defer C.free(unsafe.Pointer(msg))
   250  
   251  	rv := C.GDALTermProgress(C.double(dfComplete), msg, unsafe.Pointer(nil))
   252  	return int(rv)
   253  }
   254  func GDALScaledProgress(dfComplete float64, pszMessage string, pData interface{}) int {
   255  	msg := C.CString(pszMessage)
   256  	defer C.free(unsafe.Pointer(msg))
   257  
   258  	rv := C.GDALScaledProgress(C.double(dfComplete), msg, unsafe.Pointer(nil))
   259  	return int(rv)
   260  }
   261  
   262  func GDALCreateScaledProgress(dfMin, dfMax float64, pfnProgress GDALProgressFunc, pData unsafe.Pointer) unsafe.Pointer {
   263  	panic("not impl")
   264  }
   265  
   266  func GDALDestroyScaledProgress(pData unsafe.Pointer) {
   267  	C.GDALDestroyScaledProgress(pData)
   268  }
   269  
   270  // -----------------------------------------------------------------------
   271  
   272  type goGDALProgressFuncProxyArgs struct {
   273  	progresssFunc GDALProgressFunc
   274  	pData         interface{}
   275  }
   276  
   277  //export goGDALProgressFuncProxyA
   278  func goGDALProgressFuncProxyA(dfComplete C.double, pszMessage *C.char, pData *interface{}) int {
   279  	if arg, ok := (*pData).(goGDALProgressFuncProxyArgs); ok {
   280  		return arg.progresssFunc(
   281  			float64(dfComplete), C.GoString(pszMessage), arg.pData,
   282  		)
   283  	}
   284  	return 0
   285  }
   286  
   287  /* ==================================================================== */
   288  /*      Registration/driver related.                                    */
   289  /* ==================================================================== */
   290  
   291  const (
   292  	GDAL_DMD_LONGNAME           = string(C.GDAL_DMD_LONGNAME)
   293  	GDAL_DMD_HELPTOPIC          = string(C.GDAL_DMD_HELPTOPIC)
   294  	GDAL_DMD_MIMETYPE           = string(C.GDAL_DMD_MIMETYPE)
   295  	GDAL_DMD_EXTENSION          = string(C.GDAL_DMD_EXTENSION)
   296  	GDAL_DMD_CREATIONOPTIONLIST = string(C.GDAL_DMD_CREATIONOPTIONLIST)
   297  	GDAL_DMD_CREATIONDATATYPES  = string(C.GDAL_DMD_CREATIONDATATYPES)
   298  
   299  	GDAL_DCAP_CREATE     = string(C.GDAL_DCAP_CREATE)
   300  	GDAL_DCAP_CREATECOPY = string(C.GDAL_DCAP_CREATECOPY)
   301  	GDAL_DCAP_VIRTUALIO  = string(C.GDAL_DCAP_VIRTUALIO)
   302  )
   303  
   304  func gdalAllRegister() {
   305  	C.GDALAllRegister()
   306  }
   307  
   308  // Create a new dataset with this driver.
   309  func GDALCreate(hDriver GDALDriverH,
   310  	pszFilename string,
   311  	nXSize, nYSize, nBands int,
   312  	dataType GDALDataType,
   313  	papszOptions []string,
   314  ) GDALDatasetH {
   315  	name := C.CString(pszFilename)
   316  	defer C.free(unsafe.Pointer(name))
   317  
   318  	opts := make([]*C.char, len(papszOptions)+1)
   319  	for i := 0; i < len(papszOptions); i++ {
   320  		opts[i] = C.CString(papszOptions[i])
   321  		defer C.free(unsafe.Pointer(opts[i]))
   322  	}
   323  	opts[len(opts)-1] = (*C.char)(unsafe.Pointer(nil))
   324  
   325  	h := C.GDALCreate(
   326  		C.GDALDriverH(hDriver),
   327  		name,
   328  		C.int(nXSize), C.int(nYSize), C.int(nBands),
   329  		C.GDALDataType(dataType),
   330  		(**C.char)(unsafe.Pointer(&opts[0])),
   331  	)
   332  	return GDALDatasetH(h)
   333  }
   334  
   335  // Create a copy of a dataset.
   336  func GDALCreateCopy(
   337  	hDriver GDALDriverH, pszFilename string,
   338  	hSrcDS GDALDatasetH,
   339  	bStrict int, papszOptions []string,
   340  	pfnProgress GDALProgressFunc, pProgressData interface{},
   341  ) GDALDatasetH {
   342  	name := C.CString(pszFilename)
   343  	defer C.free(unsafe.Pointer(name))
   344  
   345  	opts := make([]*C.char, len(papszOptions)+1)
   346  	for i := 0; i < len(papszOptions); i++ {
   347  		opts[i] = C.CString(papszOptions[i])
   348  		defer C.free(unsafe.Pointer(opts[i]))
   349  	}
   350  	opts[len(opts)-1] = (*C.char)(unsafe.Pointer(nil))
   351  
   352  	arg := &goGDALProgressFuncProxyArgs{
   353  		pfnProgress, pProgressData,
   354  	}
   355  
   356  	h := C.GDALCreateCopy(
   357  		C.GDALDriverH(hDriver), name,
   358  		C.GDALDatasetH(hSrcDS),
   359  		C.int(bStrict), (**C.char)(unsafe.Pointer(&opts[0])),
   360  		C.goGDALProgressFuncProxyB(),
   361  		unsafe.Pointer(arg),
   362  	)
   363  	return GDALDatasetH(h)
   364  }
   365  
   366  /* ==================================================================== */
   367  /*      GDAL_GCP                                                        */
   368  /* ==================================================================== */
   369  
   370  /* ==================================================================== */
   371  /*      major objects (dataset, and, driver, drivermanager).            */
   372  /* ==================================================================== */
   373  
   374  /* ==================================================================== */
   375  /*      GDALDataset class ... normally this represents one file.        */
   376  /* ==================================================================== */
   377  
   378  /* ==================================================================== */
   379  /*      GDALRasterBand ... one band/channel in a dataset.               */
   380  /* ==================================================================== */
   381  
   382  /* ==================================================================== */
   383  /*     GDALAsyncReader                                                  */
   384  /* ==================================================================== */
   385  
   386  /* ==================================================================== */
   387  /*      Color tables.                                                   */
   388  /* ==================================================================== */
   389  
   390  /* ==================================================================== */
   391  /*      Raster Attribute Table						*/
   392  /* ==================================================================== */
   393  
   394  /* ==================================================================== */
   395  /*      GDAL Cache Management                                           */
   396  /* ==================================================================== */