说明
最近要用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函数的一些用法,在查看库的源代码进行测试。