說明
最近要用go寫一個應用gdal庫的程式,go的gdal庫文檔資料很少,記錄一下,自己摸索的過程。
需要用到cgo 要編譯gdal
windows的環境 一直編譯這塊處理很麻煩 一直出現問題 最後換成到linux
有解決的可以分享出來 :)
環境 Ubuntu18.4
下載下傳安裝GDAL
通過
apt-get
下載下傳安裝
sudo apt-get install libgdal-dev
執行上面的指令,用于安裝 GDAL 的開發環境
可通過
gdal-config
指令來檢視 GDAL 的相關配置
頭檔案路徑預設安裝在:
/usr/include/gdal
共享庫及靜态庫預設安裝在:
/usr/lib
庫名字:
libgdal.a
和
libgdal.so
pkg-config
pkg-config
用來檢索系統中安裝庫檔案的資訊,典型的是用作庫的編譯和連接配接。
友善我們後續編譯連接配接
sudo apt install pkg-config
在預設情況下,每個支援
pkg-config
的庫對應的.pc檔案在安裝後都位于安裝目錄中的lib/pkgconfig目錄下.新軟體一般都會安裝.pc檔案,沒有可以自己建立,并且設定環境變量PKG_CONFIG_PATH設定擦汗尋.pc檔案路徑。使用
pkg-config
工具提取庫的編譯和連接配接參數有兩個基本的前提:
庫本身在安裝的時候必須提供一個相應的.pc檔案。不這樣做的庫說明不支援
pkg-config
工具的使用。
pkg-config
必須知道要到哪裡去尋找此.pc 檔案。
gdal.pc檔案
編寫一下
gdal.pc
檔案
name=gdal
prefix=/usr
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir=${exec_prefix}/include
datadir=${prefix}/share/${name}
Name: lib${name}
Description: Geospatial Data Abstraction Library
Version: 1.9.1
Libs: -L${libdir} -l${name}
Cflags: -I${includedir}/${name}
把
gdal.pc
檔案放到
/usr/lib/pkgconfig
路徑下
然後拉取go的gdal庫
在linux下通過
pkg-config
自動擷取編譯連接配接路徑,可以正常運作了
go調用gdal的例子
package main
import (
"fmt"
"github.com/lukeroth/gdal"
)
func main() {
readfilename := "./out.tif"
outfilename := "aout.tif"
if readfilename == "" {
fmt.Printf("Usage: tiff [filename]\n")
return
}
fmt.Printf("Filename: %s\n", readfilename)
dataset,_ := gdal.Open(readfilename,gdal.ReadOnly)
x := dataset.RasterXSize()
y := dataset.RasterYSize()
fmt.Println(x,y)
buffer := make([]int8,x*y)
fmt.Printf("Getting raster band\n")
raster := dataset.RasterBand(1)
fmt.Printf("Writing to raster band\n")
raster.IO(gdal.Read, 0, 0, x, y, buffer, x, y, 0, 0)
fmt.Println(buffer)
fmt.Printf("Loading driver\n")
driver, err := gdal.GetDriverByName("GTiff")
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Printf("Creating dataset\n")
outdataset := driver.Create(outfilename, 256, 256, 1, gdal.Byte, nil)
defer outdataset.Close()
}
關于go的gdal的一些API
go的gdal的函數跟c類似差不多,由于我也是剛接觸,也有很多函數不是很了解,
可以多看看c函數的一些用法,在檢視庫的源代碼進行測試。