導航:首頁 > 源碼編譯 > gdal編譯過程

gdal編譯過程

發布時間:2023-01-12 16:06:51

『壹』 如何在CSharp中使用GDAL

問題解決方案,可以不考慮測試結果 將四個*_CSharp.dll在項目中「添加引用」添加進來,其餘gdal16.dll和另外四個編譯C#時生成的dll文件拷貝到項目的debug下。即可。 如果不把dll文件拷貝到debug下,將出現下面錯誤: 「OSGeo.OGR.Ogr」的類型初始值設定項引發異常這樣的問題。 這個問題是dll不全造成的,除了要引用的4個dll外,還有5個dll也要放到Debug目錄下。 在編譯C#下的gdal時,總共生成了9個dll,在編譯的本機上,程序是通過環境變數path找到另外的幾個dll的。 在沒有編譯過gdal的電腦上,反正就把這9個編譯後的dll放到debug下面就一切Ok了

『貳』 如何編譯mapserver在windows環境下

編譯mapserver的時候一定要選擇一個根目錄
推薦 C 盤
在C盤下面創建一個projects目錄
將你的所以用來支持mapserver編譯的支持庫文件都放到該目錄下面
註:由於mapserver等都是開源的軟體。但是都有一定的版權。所以他們不是集成到mapserver下面的,而是有各種支持庫文件通過編譯說明文件鏈接
所有文件鏈接的說明文件在nmake.opt文件中。這是使用vc編譯的說明文件。在編譯的時候具體選擇支持什麼庫文件都可以在這里說明。

下面是具體的編譯環境
gdwin32 的bgd.lib是第一次編譯後拷貝過來的運行makemsvcimport.bat
libpng 的libpng.lib,libpngd.lib是第一次編譯後拷貝過來的vc6
freetype 的freetype2110_D.lib在vc7下面編譯成功
zlib zlib.lib 編譯成功在zlib123\contrib\vstudio\vc7用vc7
proj proj.lib 在vc7環境下編譯成功
curl libcurl.lib 在vc6編譯下成功 C:\projects\curl\lib
gdal gdal.lib 用submake.bat批處理文件執行編譯成功
pdflib 用vc7編譯成功 C:\projects\PDFlibLite
fcgi 沒有成功 將fcgi_config_x86.h改為fcgi_config.h用vc6編譯成功
jpeg 沒有編譯成功將gdal里的拷貝過來
regex 沒有編譯成功
編譯支持庫的時候比較艱難
仔細細心的查看各個支持庫中的文檔目錄。一般都會有一個可以通過的編譯。
編譯的時候也要懂得使用技巧。比如他說找不到庫文件或著什麼頭文件或源文件你都可以從其他地方拷貝一個過來。

附加nmake.opt文件的配置
#
# nmake.opt - MapServer 4.x configuration for MSVC++
#
# This VC++ configuration is used in building MAPSERVER.LIB, MAPSERV.EXE,
# and the other MapServer command-line programs.
#
# To use the makefile:
# - Open a DOS prompt window
# - Run the VCVARS32.BAT script to initialize the VC++ environment variables
# - Start the build with: nmake /f makefile.vc
#
# $Id: nmake.opt,v 1.24 2005/12/08 19:14:48 hobu Exp $
#
# Contents:
# Section I: Mapserver Options (you may want to edit)
# Section II: Support Libraries (you must edit)
# Section III: Debug Flags (no need to edit)
# Section IV: Variable Setup (should not need to edit)
# Section V: UMN GIS System Setup (should not need to edit)
# Section VI: Collect compiler flags
#
########################################################################
# Section I: Mapserver Options
########################################################################
# Uncomment the following to link mapserv.exe withh dll
DLLBUILD=1
# Set the following to point to the current directory.
MS_BASE = C:\projects\mapserver
# Optmization and related compile flags.
# Optimized, with using MSVCRT.
OPTFLAGS = /nologo /MD $(WARNING_LEVEL) $(DEBUG)
#LDFLAGS = /NODEFAULTLIB:msvcrt /NODEFAULTLIB:libcd /DEBUG
# Debug with MSVCRT
#OPTFLAGS = /nologo /Zi /MD $(WARNING_LEVEL) $(DEBUG)
# Optimized, with LIBC.
#OPTFLAGS = /nologo $(WARNING_LEVEL) $(DEBUG)
# Input raster format options:
#
# The lite version of mapserver 4.x supports only GIF, PNG and JPEG data for
# input. If you wish to support many geospatial raster formats for input
# you will need the GDAL support library from http://www.gdal.org/.
# Once built, enable the GDAL flag, and point GDAL_DIR to the directory
# where GDAL was built.
#GDAL=-DUSE_GDAL
#GDAL_DIR=c:\projects\gdal
#
# Input vector format options
#
# The lite version of Mapserver 4.x only suports ESRI shapefiles for input.
#
# The OGR library (part of GDAL) supports a variety of geospatial vector
# formats including mapinfo, Arc/Info binary coverages, S-57, SDTS,
# Microstation DGN (pre-v7), TIGER, UK .NTF. It also include support for
# treating non-spatial tablular data from ODBC, CSV, mysql, Oracle Spatial,
# and PostgreSQL as spatial table with use of the VRT (virtual) driver.
#
# NOTE: Both -DUSE_OGR and -DUSE_GDAL need to be defined if you want to
# use GDAL/OGR for both raster and vector support, but GDAL_DIR needs only
# be defined once.
#
#OGR=-DUSE_OGR
#GDAL_DIR=c:\projects\gdal
# JPEG Input:
# JPEG input for raster layers is also available through GDAL, If you wish
# to build support for JPEG without GDAL, uncomment the following flag
# and provide the full path to the jpeg support library project directory.
# See http://www.ijg.org/ for support library.
JPEG=-DUSE_JPEG
JPEG_DIR=c:/projects/libjpeg
# Output format options:
# If you wish to allow JPEG output maps, uncomment the following flag.
# If not using a GD build with an internal of libjpeg, you will
# also need to uncomment JPEG_DIR and point to it; however, with BGD.DLL
# that is not necessary.
OUTPUT_JPEG=-DUSE_GD_JPEG
JPEG_DIR=c:/projects/libjpeg
# If you wish to allow PNG output maps, uncomment the following flag.
# If not using a GD build with an internal of libpng, you will
# also need to uncomment PNG_DIR and ZLIB_DIR and point to it; however, with
# BGD.DLL that is not necessary.
# See http://www.libpng.org/pub/png/libpng.html for support library.
# See http://www.gzip.org/zlib/ for support library.
OUTPUT_PNG=-DUSE_GD_PNG
PNG_DIR=c:/projects/libpng
ZLIB_DIR=c:/projects/zlib
#flag to indicate the use of zlib library. It is used intially in SVG
#output to compressed files.
ZLIB=-DUSE_ZLIB
# If you wish to allow Windows BMP output maps, uncomment the following flag.
OUTPUT_WBMP=-DUSE_GD_WBMP
# If you wish to have FLASH output, uncomment the following flag and provide
# the full path to the MING support library project directory.
# See http://ming.sourceforge.net/ for support library.
#MING=-DUSE_MING_FLASH
#MING_DIR=c:/projects/ming-0.3beta1

# If you wish to have PDF output, uncomment the following flag and provide the
# full path to the PDF support library project directory.
# See http://www.pdflib.com/ for support library.
PDF=-DUSE_PDF
PDF_DIR=c:/projects/PDFlibLite

# Annotation fonts.
#
# If you wish to annotate your maps with true type fonts unccomment the
# following flag. Provide the full path to the FreeType 2.x external
# support library, unless it is provided within your GD build as is the
# case with BGD.DLL.
# See http://www.freetype.org for support library.
ANNOTATION_FT=-DUSE_GD_FT
FT_DIR=c:/projects/freetype

# Direct connectivity to Postgresql PostGIS.
#
# To turn on direct connectivity to Postgresql PostGIS uncomment the following
# flag and set the full path name to the project directory for the
# Postgresql native Win32 client library.
# See http://www.postgresql.org for support library.
#POSTGIS =-DUSE_POSTGIS
#POSTGIS_DIR =c:/projects/libpq
#Orcale
ORACLE_DIR = c:\Oracle\Ora81
ORACLE=-DUSE_ORACLESPATIAL
# Direct connectivity to ArcSDE.
#
# To turn on direct connectivity to ArcSDE uncomment the following
# flag and set the full path name to the project directory for ArcSDE.
# Since ESRI includes the version number in the name of their libraries
# you may need to change that number in Section III of this configuration
# file.
# See http://www.esri.com/software/arcgis/arcinfo/arcsde/index.html for
# support library
#
#
#SDE_OPT=-DUSE_SDE -DWIN32
#SDE_DIR=c:/my_path_to/arcsde
# EPPL7 Support
#
# This activates ERDAS as well. It is included in the distribution.
# Probably the best raster alternative if
# you've got EPPL7 laying around. See http://www.lmic.state.mn.us/ for
# more information.
# Uncomment out the following flag and set the full path name to the
# epplib.obj file.
#EPPL=-DUSE_EPPL
#EPPL_OBJ=c:/my_path/epplib.obj
# If you want to ignore missing datafile errors uncomment the following
# line. This is especially useful with large tiled datasets that may not
# have complete data for each tile.
IGNORE_MISSING_DATA=-DIGNORE_MISSING_DATA
# If you want to use shape Z and M parameter this option must be set.
# It's OFF by default.
#USE_POINT_Z_M=-DUSE_POINT_Z_M
USE_POINT_Z_M=
#NEED_NONBLOCKING_STDERR=-DNEED_NONBLOCKING_STDERR
ENABLE_STDERR_DEBUG=-DENABLE_STDERR_DEBUG
# If you want antialiasing (note that It requires gd2)
USE_GD_ANTIALIAS=-DUSE_GD_ANTIALIAS
# Enable if you want thread safe locking, not needed for simple CGI.
#THREADS=-DUSE_THREAD
# Use this flag to compile with WMS Server support.
# To find out more about the OpenGIS Web Map Server Specification go to
# http://www.opengis.org/
WMS=-DUSE_WMS_SVR
# Use this flag to compile with WMS Client support. WMS Client support
# allows you to pull layers from other OGIS WMS servers on the interent and
# incorporate them into your map.
# To find out more about the OpenGIS Web Map Server Specification go to
# http://www.opengis.org/
# you need the libcurl library from http://curl.haxx.se/library/c/
# Set the full path to the curl project directory.
# You may also need to the full path to the windows socket library.
#WMSCLIENT= -DUSE_WMS_LYR
CURL_DIR=c:/projects/curl
#CURL_DIR=c:/projects/curl-7.10.7
WINSOCK_LIB = "WSOCK32.LIB"
WINSOCK_LIB = "C:\Program Files\Microsoft Visual Studio\VC98\Lib\WSOCK32.LIB"
# Use -DUSE_WFS_SVR to compile with WFS server support, requires OGR and PROJ4
#WFS=-DUSE_WFS_SVR
# Use -DUSE_WFS_LYR to compile with WFS client support, requires libcurl
#WFSCLIENT= -DUSE_WFS_LYR
# Use -DUSE_WCS_SVR to compile with WCS server support, requires GDAL.
#WCS=-DUSE_WCS_SVR
#libiconv support is used for to support double bytes (see bug 911).
#uncomment the following to build with libiconv support.
#ICONV=-DUSE_ICONV
#
# Reprojecting.
# If you would like mapserver to be able to reproject data from one
# geographic projection to another, uncomment the following flag
# Proj.4 distribution (cartographic projection routines). PROJ.4 is
# also required for all OGC services (WMS, WFS, and WCS).
#
# For PROJ_DIR use full path to Proj.4 distribution
PROJ=-DUSE_PROJ -DUSE_PROJ_API_H
PROJ_DIR=c:\projects\proj
# php Mapscript.
# If you plan to build PHP mapscript uncomment the following flag and
# set the full path to the PHP project directory
#PHP=1
#PHP_DIR=c:\projects\php-4.3.4
# Apparently these aren't as commonplace. Edit the
# following line to reflect the missing functions on your platform.
#
#STRINGS=-DNEED_STRCASECMP -DNEED_STRNCASECMP -DNEED_STRDUP
STRINGS=-DNEED_STRCASECMP -DNEED_STRNCASECMP -DNEED_STRLCAT

########################################################################
# Section II: External Support Libraries
########################################################################
# You will need to set the paths to various support library projects
# that you have compiled.

『叄』 如何將debug版本的so庫變成release版本

由於Smart_Gis安卓客戶端的需要,所以需要把gdal_2.3.1庫編譯成安卓平台Arm64_v8a版本的.so庫。由於要求是android NDK版本 12以上,Android API 21以上的支持。

所以在本次編譯過程中全部需要使用64位的操作系統,NDK及編譯器,編譯環境為 操作系統:Ubuntu 16.04 LTS,安卓NDK版本:android-ndk-r14b,geos, proj。

1,最近遇到了之前編譯的geos, proj, skia, gdal庫都為debug版本,本人也沒有特意的去驗證,但是由於項目上線發布的原因,需要將所有的依賴的庫發布成release版本的,一來可以增加庫的穩定性,二來可以減小體積。

2,由於 gdal 庫在編譯成release版本的過程中可以參考我的gdal庫安卓平台編譯這篇文章,並在Application.mk文件裡面增加 APP_OPTIM := release 然後 ndk-build命令進行編譯就行了,如何驗證生成的庫是不是debug版本的呢?

  在庫目錄下執行 readelf -S + 文件名 ,在列印出的信息中如果能查看到有debug字元即是debug版本。

3,到這一步我們需要找到我們打開我們編譯的工具鏈的位置,找到strip這個可執行程序,比如說 GCC存儲目錄或者本人的Android 交叉編譯工具鏈的位置。然後找到存儲.so庫的目錄下並復制當前目錄加上需要裁剪的庫名到工具鏈strip存放目錄下,

如下圖所示並回車。

4,再次查看SO庫並會發現該庫的體積縮小了很多,並使用readelf -S + 庫名列印出來的信息沒有debug字元。

『肆』 linux(centos8)系統安裝編譯GDAL 2.2.1

win10系統安裝gdal,看這篇: windows 安裝 GDAL - (jianshu.com)

GDAL庫是處理地理信息一個非常強大的庫,我這邊要用它做 cad 轉 geojson 的工作。測試是在windows上進行的,然後現在需要部署到linux上,遇到了安裝上的坎兒,記錄一下。

DownloadSource – GDAL (osgeo.org)
可以直接:

python36-devel 找不到的話 用:python-devel

在make編譯這一步,我報了個錯誤:
jpeg2000dataset.cpp:35:10: fatal error: jasper/jasper.h: No such file or directory

jasper安裝完之後,重新執行步驟4(4. 執行配置、編譯、安裝:)

在最後添加如下:

保存:

應用:

這步不執行會報錯:報錯:gdalinfo: error while loading shared libraries: libgdal.so.20: cannot open shared object file: No such file or directory
找不到共享庫,意思就是在 bashrc 中添加的沒生效。
解決:參考: https://..com/question/1929349307136401747.html

『伍』 使用nmake編譯Gdal源代碼(Win10+VS2022)

為了編譯Gdal-master的源碼,需要PROJ的支持,而PROJ又需要SQLite的支持。

SQLite、PROJ、Gdal-master的源碼及編譯可參見網頁:

https://www.toutiao.com/a7063703225425199623/?log_from=fe233e3892be3_1644664643652

也可使用nmake在命令行創建(參考網頁 https://trac.osgeo.org/gdal/wiki/BuildingOnWindows

使用命令行,進入VS2022開發者命令行窗口

cd D:ProgramsMicrosoft Visual

D:

vcvars64.bat


cd D:SourceCodesgdal-master

set BINDIR=d:programsgdalgdal-bin

set DATADIR=d:programsgdalgdal-data

set INCDIR=d:programsgdalgdal-include

set LIBDIR=d:programsgdalgdal-lib

修改文件「nmake.opt」的內容:

(1)將

#PROJ_INCLUDE = -Id:install-projlocalinclude

改為

PROJ_INCLUDE = -ID:My3rdPartyinclude

(2)將

#PROJ_LIBRARY = d:install-projlocallibproj_6_0.lib shell32.lib ole32.lib

改為

PROJ_LIBRARY = D:My3rdPartylibproj.lib shell32.lib ole32.lib

依次使用下列命令, 創建x64的Release版本

nmake -f makefile.vc MSVC_VER=1930 WIN64=1

nmake /f makefile.vc install MSVC_VER=1930 WIN64=yes

nmake /f makefile.vc devinstall MSVC_VER=1930 WIN64=yes

執行完畢後,目錄「d:programsgdal」中的內容如下(使用命令 「tree /f d:programsgdal」 列出)

D:PROGRAMSGDAL

gdal-bin

gdal303.dll

gdaladdo.exe

gdalbuildvrt.exe

gdaldem.exe

gdalenhance.exe

gdalinfo.exe

gdallocationinfo.exe

gdalmanage.exe

gdalmdiminfo.exe

gdalmdimtranslate.exe

gdalsrsinfo.exe

gdaltindex.exe

gdaltransform.exe

gdalwarp.exe

gdal_contour.exe

gdal_create.exe

gdal_grid.exe

gdal_rasterize.exe

gdal_translate.exe

gdal_viewshed.exe

gnmanalyse.exe

gnmmanage.exe

nearblack.exe

ogr2ogr.exe

ogrinfo.exe

ogrlineref.exe

ogrtindex.exe

gdalplugins

drivers.ini

gdal-data

bag_template.xml

cubewerx_extra.wkt

default.rsc

ecw_cs.wkt

eedaconf.json

epsg.wkt

esri_StatePlane_extra.wkt

gdalicon.png

GDALLogoBW.svg

GDALLogoColor.svg

GDALLogoGS.svg

gdalmdiminfo_output.schema.json

gdalvrt.xsd

gmlasconf.xml

gmlasconf.xsd

gml_registry.xml

grib2_center.csv

grib2_process.csv

grib2_subcenter.csv

grib2_table_4_2_0_0.csv

grib2_table_4_2_0_1.csv

grib2_table_4_2_0_13.csv

grib2_table_4_2_0_14.csv

grib2_table_4_2_0_15.csv

grib2_table_4_2_0_16.csv

grib2_table_4_2_0_17.csv

grib2_table_4_2_0_18.csv

grib2_table_4_2_0_19.csv

grib2_table_4_2_0_190.csv

grib2_table_4_2_0_191.csv

grib2_table_4_2_0_2.csv

grib2_table_4_2_0_20.csv

grib2_table_4_2_0_3.csv

grib2_table_4_2_0_4.csv

grib2_table_4_2_0_5.csv

grib2_table_4_2_0_6.csv

grib2_table_4_2_0_7.csv

grib2_table_4_2_10_0.csv

grib2_table_4_2_10_1.csv

grib2_table_4_2_10_191.csv

grib2_table_4_2_10_2.csv

grib2_table_4_2_10_3.csv

grib2_table_4_2_10_4.csv

grib2_table_4_2_1_0.csv

grib2_table_4_2_1_1.csv

grib2_table_4_2_1_2.csv

grib2_table_4_2_20_0.csv

grib2_table_4_2_20_1.csv

grib2_table_4_2_20_2.csv

grib2_table_4_2_2_0.csv

grib2_table_4_2_2_3.csv

grib2_table_4_2_2_4.csv

grib2_table_4_2_2_5.csv

grib2_table_4_2_3_0.csv

grib2_table_4_2_3_1.csv

grib2_table_4_2_3_2.csv

grib2_table_4_2_3_3.csv

grib2_table_4_2_3_4.csv

grib2_table_4_2_3_5.csv

grib2_table_4_2_3_6.csv

grib2_table_4_2_4_0.csv

grib2_table_4_2_4_1.csv

grib2_table_4_2_4_10.csv

grib2_table_4_2_4_2.csv

grib2_table_4_2_4_3.csv

grib2_table_4_2_4_4.csv

grib2_table_4_2_4_5.csv

grib2_table_4_2_4_6.csv

grib2_table_4_2_4_7.csv

grib2_table_4_2_4_8.csv

grib2_table_4_2_4_9.csv

grib2_table_4_2_local_Canada.csv

grib2_table_4_2_local_HPC.csv

grib2_table_4_2_local_index.csv

grib2_table_4_2_local_MRMS.csv

grib2_table_4_2_local_NCEP.csv

grib2_table_4_2_local_NDFD.csv

grib2_table_4_5.csv

grib2_table_versions.csv

gt_datum.csv

gt_ellips.csv

header.dxf

inspire_cp_BasicPropertyUnit.gfs

inspire_cp_CadastralBoundary.gfs

inspire_cp_CadastralParcel.gfs

inspire_cp_CadastralZoning.gfs

jpfgdgml_AdmArea.gfs

jpfgdgml_AdmBdry.gfs

jpfgdgml_AdmPt.gfs

jpfgdgml_BldA.gfs

jpfgdgml_BldL.gfs

jpfgdgml_Cntr.gfs

jpfgdgml_CommBdry.gfs

jpfgdgml_CommPt.gfs

jpfgdgml_Cstline.gfs

jpfgdgml_ElevPt.gfs

jpfgdgml_GCP.gfs

jpfgdgml_LeveeEdge.gfs

jpfgdgml_RailCL.gfs

jpfgdgml_RdArea.gfs

jpfgdgml_RdASL.gfs

jpfgdgml_RdCompt.gfs

jpfgdgml_RdEdg.gfs

jpfgdgml_RdMgtBdry.gfs

jpfgdgml_RdSgmtA.gfs

jpfgdgml_RvrMgtBdry.gfs

jpfgdgml_SBAPt.gfs

jpfgdgml_SBArea.gfs

jpfgdgml_SBBdry.gfs

jpfgdgml_WA.gfs

jpfgdgml_WL.gfs

jpfgdgml_WStrA.gfs

jpfgdgml_WStrL.gfs

LICENSE.TXT

netcdf_config.xsd

nitf_spec.xml

nitf_spec.xsd

ogrvrt.xsd

osmconf.ini

ozi_datum.csv

ozi_ellips.csv

pci_datum.txt

pci_ellips.txt

pdfcomposition.xsd

pds4_template.xml

plscenesconf.json

ruian_vf_ob_v1.gfs

ruian_vf_st_uvoh_v1.gfs

ruian_vf_st_v1.gfs

ruian_vf_v1.gfs

s57agencies.csv

s57attributes.csv

s57expectedinput.csv

s57objectclasses.csv

seed_2d.dgn

seed_3d.dgn

stateplane.csv

template_tiles.mapml

tms_LINZAntarticaMapTileGrid.json

tms_MapML_APSTILE.json

tms_MapML_CBMTILE.json

tms_NZTM2000.json

trailer.dxf

vdv452.xml

vdv452.xsd

vicar.json

gdal-include

armadillo_headers.h

cplkeywordparser.h

cpl_alibaba_oss.h

cpl_atomic_ops.h

cpl_auto_close.h

cpl_aws.h

cpl_azure.h

cpl_compressor.h

cpl_config.h

cpl_config_extras.h

cpl_conv.h

cpl_cpu_features.h

cpl_csv.h

cpl_curl_priv.h

cpl_error.h

cpl_error_internal.h

cpl_float.h

cpl_google_cloud.h

cpl_hash_set.h

cpl_http.h

cpl_json.h

cpl_json_header.h

cpl_json_streaming_parser.h

cpl_json_streaming_writer.h

cpl_list.h

cpl_md5.h

cpl_mem_cache.h

cpl_minixml.h

cpl_minizip_ioapi.h

cpl_minizip_unzip.h

cpl_minizip_zip.h

cpl_multiproc.h

cpl_odbc.h

cpl_port.h

cpl_progress.h

cpl_quad_tree.h

cpl_sha1.h

cpl_sha256.h

cpl_spawn.h

cpl_string.h

cpl_swift.h

cpl_time.h

cpl_userfaultfd.h

cpl_vax.h

cpl_virtualmem.h

cpl_vsi.h

cpl_vsil_curl_class.h

cpl_vsil_curl_priv.h

cpl_vsil_plugin.h

cpl_vsi_error.h

cpl_vsi_virtual.h

cpl_worker_thread_pool.h

cpl_zlib_header.h

gdal.h

gdalexif.h

gdalgeorefpamdataset.h

gdalgrid.h

gdalgrid_priv.h

gdaljp2abstractdataset.h

gdaljp2metadata.h

gdaljp2metadatagenerator.h

gdallinearsystem.h

gdalpansharpen.h

gdalpython.h

gdalsse_priv.h

gdalwarper.h

gdalwarpkernel_opencl.h

gdal_alg.h

gdal_alg_priv.h

gdal_csv.h

gdal_frmts.h

gdal_mdreader.h

gdal_pam.h

gdal_priv.h

gdal_proxy.h

gdal_rat.h

gdal_simplesurf.h

gdal_thread_pool.h

gdal_utils.h

gdal_version.h

gdal_vrt.h

gnm.h

gnmgraph.h

gnm_api.h

gvgcpfit.h

internal_qhull_headers.h

memdataset.h

nasakeywordhandler.h

ograpispy.h

ogrgeomediageometry.h

ogrpgeogeometry.h

ogrsf_frmts.h

ogr_api.h

ogr_attrind.h

ogr_core.h

ogr_expat.h

ogr_feature.h

ogr_featurestyle.h

ogr_geocoding.h

ogr_geometry.h

ogr_geos.h

ogr_geo_utils.h

ogr_libs.h

ogr_p.h

ogr_proj_p.h

ogr_sfcgal.h

ogr_spatialref.h

ogr_srs_api.h

ogr_srs_esri_names.h

ogr_swq.h

ogr_xerces.h

ogr_xerces_headers.h

rawdataset.h

thinplatespline.h

vrtdataset.h

gdal-lib

gdal_i.lib

『陸』 如何導入PostgreSQL資料庫數據

說起資料庫,大家耳熟能詳的商業資料庫產品當推Oracle、微軟的SqlServer和IBM的

DB2等,而開源資料庫中則有兩大產品MySQL和PostgreSQL。

PostgreSQL 是一種對象-關系型資料庫管理系統(ORDBMS),也是目前功能最強大、

特性最豐富和最復雜的自由軟體資料庫系統。它起源於伯克利(BSD)的資料庫研究計劃,

目前是最重要的開源資料庫產品開發項目之一,有著非常廣泛的用戶。

PostgreSQL 可以說是最富特色的自由資料庫管理系統,也有人認為可以是最強大的自由

軟體資料庫管理系統。PostgreSQL 是唯一支持事務、子查詢、多版本並行控制系統、數據完

整性檢查等特性的唯一的一種自由軟體的資料庫管理系統。能在多平台下---包括Linux、

FreeBSD和Windows等---運行,並且支持多語言的開發。

在兩大開源資料庫產品的對比中,一般認為MySQL速度更快,所以得到更為廣泛的使

用;而PostgreSQL性能更為先進,PostgreSQL 提供很多 MySQL 目前所不支持的特性,比

如觸發器、視圖、存儲過程等等,在記錄數超千萬之後性能表現尤其出色。

當前的最新版本是PostgreSQL 8.2.3,官方網站是:

www.postgresql.org

1.2 什麼是PostGIS

PostGIS在對象關系型資料庫PostgreSQL上增加了存儲管理空間數據的能力,相當於

Oracle的spatial部分。PostGIS最大的特點是符合並且實現了OpenGIS的一些規范,是最著

名的開源GIS資料庫。

當前的最新版本是PostGIS 1.2.1,官方網站是:

www.postgis.org

二 PostgreSQL和PostGIS的安裝

2.1 在windows下安裝。

新版本的PostgreSQL在其安裝程序中集成了PostGIS,只需要在安裝過程中選中

PostGIS和pgsql項就可以了。

2.2 在Debian下安裝PostGIS

# apt-get install postgresql-8.1 postgresql-8.1-postgis

當前Etch中的版本:PostgreSQL是8.1.7,而PostGIS是1.1.6,雖然不是最新的版本,

但已經足夠了。

還需要做的工作是:

1) 創建一個專門用於PostGIS的資料庫並為專用資料庫導入PostGIS支持:

# su – postgres

$ cd /usr/share/postgresql-8.1-postgis

$ createdb wen1

$ createlang plpgsql wen1

$ psql -d wen1 -f lwpostgis.sql

$ psql -d wen1 -f spatial_ref_sys.sql

2) 創建一個專門的用戶,並把相應的資料庫和數據表的屬主設置為該用戶:

# su – postgres

$ psql

$ create user wen1 password 『123456』;

$ alter database wen1 owner to wen1;

$ alter table spatial_ref_sys owner to wen1;

$ alter table geometry_columns owner to wen1 ;

3) 修改PostgreSQL配置文件以便可以遠程使用。

修改PostgreSQL的配置文件/etc/postgresql/8.1/main/Postgresql.conf,將其中的一句:

listen_address=』localhost』

前面的注釋去掉,並把』localhost』該為』*』。

修改Postgresql的配置文件/etc/postgresql/8.1/main/pg_hba.conf,在文件後面加一句:

host all all 192.168.0.0/24 password

這句的意思是:同網路中192.168.0.*的機器可以以密碼的形式使用所有的資料庫。更具

體的參數意義直接看該配置文件中的注釋就可以了。

這里一定要配置正確,否則無法在遠程連接PostgreSQL資料庫。

2.3 源碼安裝PostgreSQL和PostGIS

參閱以前寫的老文章。

三 PostGIS的使用

要使用PostGIS,需要兩方面的預備知識,一是要熟悉基本的SQL語法;二是熟悉

PostgreSQL資料庫的基本使用。

(一) 快速入門

我們使用前面創建的資料庫wen2,用戶為wen1,現在創建一個包含三個點的資料庫

test1:

首先在Etch下以wen1登陸,然後打開wen2資料庫:

$ psql -d wen2 ;

再在PostgreSQL的控制台下輸入以下命令:

wen2=> create table test1 (myID int4, pt geometry, myName
varchar );

wen2=> insert into test1 values (1, 'POINT(0 0)', 'beijing'
);

wen2=> insert into test1 values (2, 'POINT(31.5 60.87)',
'shanghai' );

wen2=> insert into test1 values (3, 'POINT(10.77 85.902)',
'tianjin' );

這樣我們利用PostGIS創建了一個包含三個點的GIS數據表。

為了能在QGIS中打開這一圖層,我們還需要為數據表創建一個主鍵:

wen2=>alter table test1 add primary key (myID);

可以看到PostGIS的使用其實就相當於使用一個經過擴展的SQL語法,上述語句熟悉

SQL語法的人一看都很熟悉,都是普通的SQL語句,不同的只不過是增加了PostGIS特殊的

geometry數據類型。

你可以再試試這些SQL語句:

select * from test1;

select myID,AsText(pt) from test1;

select Distance(pt, 'POINT(0 0)') from test1;

(二) PostGIS的Geometry數據類型

Geometry可以說是PostGIS最重要的一個概念,是「幾何體」的意思,由於PostGIS很

好地遵守OGC的」Simple Feature for Specification for
SQL」規范,目前支持的幾何體類型包

含其實例有:

POINT(1 1)

MULTIPOINT(1 1, 3 4, -1 3)

LINESTRING(1 1, 2 2, 3 4)

POLYGON((0 0, 0 1, 1 1, 1 0, 0 0))

MULTIPOLYGON((0 0, 0 1, 1 1, 1 0, 0 0), (5 5, 5 6, 6 6, 6 5, 5
5))

MULTILINESTRING((1 1, 2 2, 3 4),(2 2, 3 3, 4 5))

而geometry具體表現又有兩種形式,一種叫做WKT(Well Known Text)形式,如上面的

例子。或者使用如下SQL語句瀏覽:

select AsText(pt) from test1;

另一種叫做「Canonical Form」形式,看上去是一串古怪的數字,其實是一種增強的十六

進制編碼,使用如下SQL語句就可以瀏覽了:

select pt from test1;

(三) 讀寫PostGIS數據

建設好PostGIS資料庫之後,我們現在需要進行讀寫GIS數據了,包括把GIS寫入到

PostGIS資料庫中以及在應用程序中使用PostGIS資料庫的數據。讀寫PostGIS目前主要有以

下四種方式:

3.1 使用psql語言

Psql語言是PostgreSQL內嵌的一個命令行工具,其語法基本上和標準的SQL語法是一

致的,可以使用Psql工具,結合標准SQL語法和一些PostGIS的擴展對PostGIS資料庫進行

讀寫操作。

具體例子如上面「快速入門」中的例子。

這種方式功能強大,但全部需要手工操作,煩瑣且繁重。

3.2 使用一些小工具

有兩個很有用的小的轉換工具,一是shp2pg;一是ogr2ogr

3.2.1 shp2pgsql和pgsql2shp

shp2pgsql和pgsql2shp是PostGIS自身攜帶的一對在Shape文件和PostGIS資料庫之間進

行轉換的工具,在Debian中安裝好PostGIS之後,這兩個程序已經位於可執行文件的搜索路

徑上,因此可以在系統中任何位置使用。

假如當前目錄下有完整的china.shp文件(應該有三個同名不同擴展名的文件),需要把其

中數據輸入到上述的wen2資料庫中的數據表china_postgis中,具體操作為(操作用戶為

wen1):

$ shp2pgsql china china_postgis > tmp.sql

$ psql -d wen2 -f roads.sql

這個工具很好用,缺點在於使用范圍有限,只針對Shape文件格式。

3.2.2 ogr2ogr

PostGIS本身的shp2pg工具只把shape文件轉換到PostGIS 資料庫中,那如何把其他的

GIS數據加入呢?比如說MapInfo的mid格式,ESRI的e00格式呢?這就要使用ogr這個工

具了。

Ogr目前是gdal的一個組成部分,那什麼是gdal呢?其官方主頁(http://www.gdal.org)上

如此介紹:

GDAL is a translator library for raster geospatial data formats
that is released under an X/MIT

style Open Source license by the Open Source Geospatial
Foundation. As a library, it presents a

single abstract data model to the calling application for all
supported formats. It also comes with a

variety of useful commandline utilties for data translation and
processing.

簡單地說,Gdal是一個各種Gis數據格式的轉換軟體庫,ogr則是轉換矢量GIS數據的

軟體庫。

目前ogr能夠支持的數據格式包括:

Arc/Info Binary Coverage、DWG、ESRI Personal
GeoDatabase、ArcSDE、ESRI

Shapefile、GML、GRASS、Mapinfo File、Microstation DGN、ODBC、Oracle
Spatial和

PostgreSQL等。應該說,這就基本包括了我們平常用到的所有矢量型GIS文件格式了。

Gdal支持的柵格數據格式參閱http: //www.gdal.org/formats_list.html

下面我們從源碼編譯安裝gdal---因為我在Debian下使用apt方式安裝的gdal並不支持

postgresql資料庫,其實更為簡便的方法也許是去gdal.org下載一個Fwtools的工具包,可以

直接運行不用繁瑣的編譯,既有Windows的,也有Linux下用的,只不過這個包有些大。

1) 下載最新的gdal-1.3.2

2) 解壓

3) ./configure --prefix=/opt/gdal
--with-pg=/opt/pg/bin/pg_config --without-ogdi

這里我習慣把軟體安裝在/opt目錄下。

--with-pg參數很重要,使ogr可以支持PostGIS,後面的參數是我的PostGIS安裝的地方。

我在這里編譯很多次失敗,經過仔細查找,發現問題出在ogdi上,所以暫時我只好使

它不支持ogdi。

4) make

5) make install

下面是使用過程,假設現在我要把一個rai.mid文件導入到資料庫data1中:

$ ogr2ogr –f Postgresql PG:dbname=data1 rai.mid

注意,f參數後面跟的是導入的數據類型,最後那個文件才是要導出的文件。PG後面說

明的是資料庫的名字,需要是已經創建好的資料庫,而數據表則由程序自動創建。還要特別

注意許可權問題,以上命令我是由postgres用戶執行的。

還有一個要注意的是主鍵問題。一般情況下,你使用shp2pg或者 ogr2ogr向postgis中

增加了Gis數據後,在GIS客戶端添加postgis圖層的時候,常常會因為數據表沒有定義主鍵

而出錯,解決的辦法是,在伺服器上使用psql或者其他sql工具,使用命令:

alter table table_name add primary key (column_name);

另外我們發現ogr竟然是不支持最常見的GIS格式e00格式,好象是因為版權方面的原

因吧。解決的方法就是去sf.net查找相關工具。我在上面就找到一個e002pg工具,支持將

e00文件導入到postGIS資料庫中。

3.3 在其他GIS軟體中讀寫PostGIS數據

比如在QGIS中,能夠打開PostGIS圖層,還有SPIT插件可以把Shape文件輸入到

PostGIS資料庫中。

其他GIS軟體如uDig,Grass等,甚至連ArcInfo都支持或部分支持讀寫PostGIS數據。

3.4 利用介面在應用程序中讀寫PostGIS數據

廣大的開源GIS程序員幾乎為每一種程序設計語言設計好了讀寫PostGIS的介面,如利

用PostgreSQL的JDBC庫,可以使用Java語言在程序中讀寫PostGIS數據;利用libpq庫,

可以使用C語言讀寫PostGIS數據。

(四) 使用PostGIS函數

4.1 PostGIS函數

要能熟練使用PostGIS,掌握PostGIS的所有函數是關鍵。通過掌握這些PostGIS函數,

我們能夠高效、穩定地處理各種地理數據。由於PostGIS的函數設計時都遵守OpenGIS規范,

我們也可以把這些PostGIS叫做OpenGIS函數。

4.2 PostGIS函數的分類

PostGIS函數大致可以分為以下四類

1) 欄位處理函數

這類函數當前一共有3個,分別是:

AddGeometryColumn(var1,var2,var3,var4,var5,var6):為已有的數據表增加一個地理幾何

數據欄位。Var1代表數據表的模式(schema)的名字,一般是public,也可以省略,則使用當

前的預設模式;var2是已有的數據表的名字;var3是新的地理數據欄位的名字;var4是

SRID值,不確定的話就取-1吧;var5是地理數據的類型,可以是POINT等;var6是指該幾

何數據是二維還是三維數據。

前面的SQL語句 create table test1 (myID int4, pt geometry, myName
varchar )更規范的寫

法為:

create table test1 (myID int4, myName varchar );

select AddGeometryColumn('test1','pt',-1,'GEOMETRY',2);

DropGeometryColumn函數顯然是刪除一個地理數據欄位的;

SetSRID函數顯然是設置SRID值的。

2) 幾何關系函數

這類函數目前共有10個,分別是:

Distance Equals Disjoint Intersects Touches Crosses Within
Overlaps Contains Relate

3) 幾何分析函數

這類函數目前共有12個,分別是:Centroid Area Lenth PointOnSurface Boundary
Buffer

ConvexHull Intersection SymDifference Difference GeomUnion
MemGeomUnion

4) 讀寫函數

這類函數很多,主要是用於在各種數據類型之間的轉換,尤其是在於Geometry數據類

型與其他如字元型等數據類型之間的轉換,函數名如AsText、GeomFromText等,其作用是

顯然的。

4.3 PostGIS函數使用示例。

假設在我們的wen2資料庫中,有兩張表,一張為guojia,是從「國家基礎地理數據」

網站下載的國界數據表,數據類型為LINE;二為shengqu_polygon,也從同一個網站下載,

地理數據類型為多邊形。

1) 查詢我國邊境線的長度:

wen1=> select sum(length(the_geom)) as lenth from guojie;

2) 查詢我國面積最大的省區名字:

wen1=> select name area(the_geom) as myarea

from shengqu_polygon

order by myarea DESC

LIMIT 1;

(五) 使用PostGIS擴展函數

除了上述遵循OpenGIS的函數之外,PostGIS還自行擴展了一些當前OpenGIS規范之外

的函數,主要包括以下幾類:

5.1 管理類函數

擴展的管理類函數主要包括一些軟體版本查詢函數,如

postgis_version()、postgis_geos_version()、postgis_proj_version()函數等,分別查詢當前的

PostGIS的版本及其使用的Geos和Proj庫的版本。

5.2 數據類型的輸入輸出函數

除了OpenGIS定義的地理數據類型之外,PostGIS還對數據類型進行了擴展,這種擴展

主要是兩方面的擴展,一是把二維的數據向三維和四維擴展;二就是在WKT和WKB數據

類型基礎上擴展出EWKT和EWKB數據類型。

PostGIS提供了在這些地理數據類型和常用數據類型如字元型、浮點型數據之間進行轉

換的函數。

5.3 量算函數

如length3d函數是對length2d函數的擴展。

5.4 幾何操作函數

如addBBox(geometry)函數給所給的幾何體加上一個邊框。

如simplify(geometry,tolerance)函數可以對折線和多邊形利用Douglas-Peuker演算法進行一

些節點進行刪除,從而使表現的圖形更簡單而清晰,在網路傳輸數據時具有更高的效率。

5.5 操作符號

5.6 其他擴展函數

(六) 建立PostGIS索引

當資料庫的記錄增大的時候,如果沒有建立索引的話,操作的效率就顯著下降。

POstGIS建議當記錄數超過幾千的時候就應該建立索引,而GIS資料庫一般都是海量數據,

所以對PostGIS而言,索引就非常重要。

Shapfile文件為ESRI公司的文件存儲格式,並且得到了業界廣泛的支持。Shapfile格式是一種簡單的,用非拓樸關系的形式存儲幾何位置和地
理特徵的屬性信息的格式。雖然GeoServer採用Shapfile文件可以快速的創建網上地圖服務,但它的缺點確很明顯:

1、Shapefile只支持一個圖層,在實際中沒有意義。

2、直接保用SHP文件不安全,Shapfile文件很容易被病毒或其他原因誤刪除。

3、GeoServer中用Shapfile文件作數據源的效率是很低的。

4、Shapfile中的漢字GeoServer不能解析,會出現亂碼。

5、資料庫可以方便的對地理信息進行查詢。

用PostGIS管理空間數據

PostGIS支持GIST空間索引(附錄1)、規范窗體,能很大的提高處理效率。


OGC格式只提供二維的幾何體,且相關聯的SRID從未深入的用於輸入輸出請求,PostGIS支持OpenGIS組織"Simple
Features for
SQL"規范指定的所有GIS對象和函數,並進行了擴展,格式是EWKB、EWKT,其中增加了對3DZ,3DM和4D
坐標系的支持(當然三維、四維數據的OGC標准還未完全制定),深入引進了SRID信息。

空間數據表結構:PostGIS中存在兩個必需的元數據表格, SPATIAL_REF_SYS(空間參考表格) 和
GEOMETRY_COLUMNS(幾何體屬性列),兩個表用於存儲空間資料庫使用的坐標系統數字ID和文本描述。

PostGIS的shp2pgsql命令可以將Shapfile直接導入到資料庫中也可以導出為SQL文件,推薦先導出為SQL文件再將此文件在SQL運行窗口中執行可將數據導入資料庫。

Shapfile到SQL語句:

shp2pgsql 路徑shp數據文件名 新建的數據表名 >
路徑SQL文件名.sql

Shapfile直接入庫:

shp2pgsql -c 路徑shp數據文件名 新建的數據表名 資料庫名|psql -d 資料庫名

舉例說明:

如將一Shapfile文件「c:road.shp」導入到數據表「road」中,資料庫為「sjzmap」。

1、運行「命令提示符」。

2、切換至PostgreSQL資料庫安裝目錄中的bin目錄下。

3、執行此目錄下的shp2pgsql命令:「shp2pgsql c:road.shp road >
c:road.sql」。

4、如將此文件直接導入資料庫(不推薦):「shp2pgsql -c c:road.shp road
sjzmap | psql -d sjzmap」。

5、使用pgAdmin3
選擇資料庫,再導入表。

『柒』 GDAL在windows,c++環境如何編譯安裝支持PostGIS格式

check out gdal offical site

閱讀全文

與gdal編譯過程相關的資料

熱點內容
未來番禺程序員待遇 瀏覽:211
安卓安智部落沖突密碼怎麼改 瀏覽:648
http協議單片機 瀏覽:73
pdfdocument 瀏覽:556
gcc編譯vi文件 瀏覽:63
安卓連airpods怎麼找耳機 瀏覽:927
加密貨幣轉賬教程 瀏覽:229
程序員小灰hashmap 瀏覽:838
國語pdf版 瀏覽:184
少兒編程作品美麗的小房子 瀏覽:974
伺服器卡在網頁上怎麼辦 瀏覽:54
用python自製編譯器 瀏覽:951
android分享新浪微博客戶端 瀏覽:26
系統中伺服器在哪裡下載地址 瀏覽:1001
新a4安卓手機怎麼投屏 瀏覽:173
pdftoemf 瀏覽:886
java介面可以實現介面嗎 瀏覽:59
vb編程10個隨機函數 瀏覽:22
程序員個人簡介100 瀏覽:772
土木工程師演算法工程師 瀏覽:92