GDAL——命令使用專題——ogrinfo命令
GDAL——命令使用專題——ogrinfo命令
前言
GDAL(Geospatial Data Abstraction Library)是一個在X/MIT許可協議下的開源柵格空間資料轉換庫。它利用抽象資料模型來表達所支援的各種檔案格式。它還有一系列命令列工具來進行資料轉換和處理。
OGR是GDAL專案的一個分支,功能與GDAL類似,只不過它提供對向量資料的支援。
這一篇就講講常見的CDAL相關命令操作,使用好GDAL的相關命令,能夠讓你事半功倍。有人會說,現在都視覺化操作了,還用啥命令。但是當你的機器效能比較低,或者說你在無介面華的作業系統下工作,那麼命令就非常有用了,如:Linux等。
主要針對: ogrinfo 命令 、 gdalinfo 命令 、 gdalsrsinfo 命令 、 gdallocationinfo 命令 、 gdal_translate 命令 、 gdaltransform 命令 等。這一節討論 ogrinfo 命令。
軟體環境:在上次安裝MapServer中,定義環境變數安裝。詳見《 MapServer Configuring with IIS 》。
GDAL通用、常規、多用途命令
在cmd中輸入:ogrinfo --help-general 或者 gdalinfo --help-general
Generic GDAL utility command options: --version: report version of GDAL in use. --license: report GDAL license info. --formats: report all configured format drivers. --format [format]: details of one format. --optfile filename: expand an option file into the argument list. --config key value: set system configuration option. --debug [on/off/value]: set debug level. --pause: wait for user input, time to attach debugger --locale [locale]: install locale for debugging (i.e. en_US.UTF-8) --help-general: report detailed help on general options.
以上列表中的相關命令為通用命令。
通用命令的部分詳細使用見 ogrinfo命令 中的相關部分。
ogrinfo命令
用途:列出OGR所支援的資料資訊。
官網: https://gdal.org/ogrinfo.html
資料型別:OGR支援的資料型別
在cmd中輸入:ogrinfo --formats
資料應用型別 | 資料型別 | 操作許可權 | 說明 |
netCDF | -raster,vector- | (rw+s) | Network Common Data Format |
AmigoCloud | -vector- | (rw+) | AmigoCloud |
OCI | -vector- | (rw+) | Oracle Spatial |
PCIDSK | -raster,vector- | (rw+v) | PCIDSK Database File |
JP2OpenJPEG | -raster,vector- | (rwv) | JPEG-2000 driver based on OpenJPEG library |
-raster,vector- | (rw+vs) | Geospatial PDF | |
MBTiles | -raster,vector- | (rw+v) | MBTiles |
DB2ODBC | -raster,vector- | (rw+) | IBM DB2 Spatial Database |
ESRI Shapefile | -vector- | (rw+v) | ESRI Shapefile |
MapInfo File | -vector- | (rw+v) | MapInfo File |
UK .NTF | -vector- | (rov) | UK .NTF |
OGR_SDTS | -vector- | (rov) | SDTS |
S57 | -vector- | (rw+v) | IHO S-57 (ENC) |
DGN | -vector- | (rw+v) | Microstation DGN |
OGR_VRT | -vector- | (rov) | VRT - Virtual Datasource |
REC | -vector- | (ro) | EPIInfo .REC |
Memory | -vector- | (rw+) | Memory |
BNA | -vector- | (rw+v) | Atlas BNA |
CSV | -vector- | (rw+v) | Comma Separated Value (.csv) |
NAS | -vector- | (rov) | NAS-ALKIS |
GML | -vector- | (rw+v) | Geography Markup Language (GML) |
GPX | -vector- | (rw+v) | GPX |
LIBKML | -vector- | (rw+v) | Keyhole Markup Language (LIBKML) |
KML | -vector- | (rw+v) | Keyhole Markup Language (KML) |
GeoJSON | -vector- | (rw+v) | GeoJSON |
ESRIJSON | -vector- | (rov) | ESRIJSON |
TopoJSON | -vector- | (rov) | TopoJSON |
Interlis 1 | -vector- | (rw+v) | Interlis 1 |
Interlis 2 | -vector- | (rw+v) | Interlis 2 |
OGR_GMT | -vector- | (rw+v) | GMT ASCII Vectors (.gmt) |
GPKG | -raster,vector- | (rw+vs) | GeoPackage |
SQLite | -vector- | (rw+v) | SQLite / Spatialite |
ODBC | -vector- | (rw+) | ODBC |
WAsP | -vector- | (rw+v) | WAsP .map format |
PGeo | -vector- | (ro) | ESRI Personal GeoDatabase |
MSSQLSpatial | -vector- | (rw+) | Microsoft SQL Server Spatial Database |
PostgreSQL | -vector- | (rw+) | PostgreSQL/PostGIS |
MySQL | -vector- | (rw+) | MySQL |
OpenFileGDB | -vector- | (rov) | ESRI FileGDB |
XPlane | -vector- | (rov) | X-Plane/Flightgear aeronautical data |
DXF | -vector- | (rw+v) | AutoCAD DXF |
CAD | -raster,vector- | (rovs) | AutoCAD Driver |
Geoconcept | -vector- | (rw+v) | Geoconcept |
GeoRSS | -vector- | (rw+v) | GeoRSS |
GPSTrackMaker | -vector- | (rw+v) | GPSTrackMaker |
VFK | -vector- | (ro) | Czech Cadastral Exchange Data Format |
PGDUMP | -vector- | (w+v) | PostgreSQL SQL dump |
OSM | -vector- | (rov) | OpenStreetMap XML and PBF |
GPSBabel | -vector- | (rw+) | GPSBabel |
SUA | -vector- | (rov) | Tim Newport-Peace's Special Use Airspace Format |
OpenAir | -vector- | (rov) | OpenAir |
OGR_PDS | -vector- | (rov) | Planetary Data Systems TABLE |
WFS | -vector- | (rov) | OGC WFS (Web Feature Service) |
WFS3 | -vector- | (ro) | OGC WFS 3 client (Web Feature Service) |
HTF | -vector- | (rov) | Hydrographic Transfer Vector |
AeronavFAA | -vector- | (rov) | Aeronav FAA |
Geomedia | -vector- | (ro) | Geomedia .mdb |
EDIGEO | -vector- | (rov) | French EDIGEO exchange format |
GFT | -vector- | (rw+) | Google Fusion Tables |
SVG | -vector- | (rov) | Scalable Vector Graphics |
CouchDB | -vector- | (rw+) | CouchDB / GeoCouch |
Cloudant | -vector- | (rw+) | Cloudant / CouchDB |
Idrisi | -vector- | (rov) | Idrisi Vector (.vct) |
ARCGEN | -vector- | (rov) | Arc/Info Generate |
SEGUKOOA | -vector- | (rov) | SEG-P1 / UKOOA P1/90 |
SEGY | -vector- | (rov) | SEG-Y |
ODS | -vector- | (rw+v) | Open Document/ LibreOffice / OpenOffice Spreadsheet |
XLSX | -vector- | (rw+v) | MS Office Open XML spreadsheet |
ElasticSearch | -vector- | (rw+) | Elastic Search |
Walk | -vector- | (ro) | Walk |
Carto | -vector- | (rw+) | Carto |
SXF | -vector- | (rov) | Storage and eXchange Format |
Selafin | -vector- | (rw+v) | Selafin |
JML | -vector- | (rw+v) | OpenJUMP JML |
PLSCENES | -raster,vector- | (ro) | Planet Labs Scenes API |
CSW | -vector- | (ro) | OGC CSW (Catalog - Service for the Web) |
VDV | -vector- | (rw+v) | VDV-451/VDV-452/INTREST Data Format |
GMLAS | -vector- | (rwv) | Geography Markup Language (GML) driven by application schemas |
MVT | -vector- | (rw+v) | Mapbox Vector Tiles |
TIGER | -vector- | (rw+v) | U.S. Census TIGER/Line |
AVCBin | -vector- | (rov) | Arc/Info Binary Coverage |
AVCE00 | -vector- | (rov) | Arc/Info E00 (ASCII) Coverage |
HTTP | -raster,vector- | (ro) | HTTP Fetching Wrapper |
說明:
-
- raster:表示柵格資料
- vector:表示向量資料
- ro:表示只讀('ro' is read-only driver)
- rw:讀、寫以及複製(read or write (i.e. supports CreateCopy))
- rw+:讀、寫以及更新(read, write and update (i.e. supports Create)read, write and update (i.e. supports Create))
- v:虛擬讀寫(appended for formats supporting virtual IO (/vsimem, /vsigzip, /vsizip, etc))
- s:資料子集(appended for formats supporting subdatasets)
用法:
ogrinfo [--help-general] [-ro] [-q] [-where restricted_where|\@filename] [-spat xmin ymin xmax ymax] [-geomfield field] [-fid fid] [-sql statement|\@filename] [-dialect dialect] [-al] [-rl] [-so] [-fields={YES/NO}] [-geom={YES/NO/SUMMARY/WKT/ISO_WKT}] [--formats] [[-oo NAME=VALUE] ...] [-nomd] [-listmdd] [-mdd domain|`all`]* [-nocount] [-noextent] datasource_name [layer [layer ...]]
ogrinfo --help-general
cmd輸入:ogrinfo --help-general
用於獲取CDAL相關命令的通用支援
C:\WINDOWS\system32>ogrinfo --help-general #CDAL多用途命令,通用命令 Generic GDAL utility command options: --version: report version of GDAL in use. #檢視版本資訊 --license: report GDAL license info. #檢視許可資訊 --formats: report all configured format drivers. #檢視支援的驅動應用,詳情可檢視:https://gdal.org/ogr_formats.html --format [format]: details of one format. #檢視某一格式支援的詳細資訊 ,如:ogrinfo --format "ESRI Shapefile"。注意,在ogrinfo --formats列表中的檔名稱,如果名稱含空格,則必須用雙引號查詢 。 --optfile filename: expand an option file into the argument list. #執行操作檔案中的命令,想見:--optfile --config key value: set system configuration option. #全域性變數配置 --debug [on/off/value]: set debug level. #設定代開,關閉DEBUG,或者DEBUG的級別 --pause: wait for user input, time to attach debugger #在進行除錯的時候,等待使用者輸入 --locale [locale]: install locale for debugging (i.e. en_US.UTF-8)#除錯本地化安裝 --help-general: report detailed help on general options.
ogrinfo --optfile操作
在你的shape檔案所在資料夾下面新建“states_ugl.opt(名字可以任意取,字尾名也可以是txt)”檔案。
states_ugl.opt檔案中的內容為:-ro -al -so -rl
在cmd中輸入:cd /d E:\SvnWorkspace\LY_WEB_GIS\branches\Documents\ms4w-mapserver-for-wimdows\release-1911-x64-gdal-2-3-3-mapserver-7-2-1\apps\Example1.1\data 定位到你的shape檔案實際路徑。
在cmd中輸入:ogrinfo --optfile states_ugl.opt states_ugl.shp
通常這樣做事為了執行一些比較連貫和複雜的操作。
--config
GDAL的執行時配置選項,與構建時配置指令碼的選項不同。執行時配置選項適用於所有平臺,並在執行時進行評估。它們可以通過程式設計、命令列開關或使用者在環境中設定。
配置選項通常用於更改gdal和ogr驅動程式的預設行為,在某些情況下還用於更改gdal和ogr核心。它們本質上是使用者可以設定的全域性變數。
--config命令格式為: --config key value 來設定的。詳見: https://trac.osgeo.org/gdal/wiki/ConfigOptions
下面以SHAPE_RESTORE_SHX來講解如何使用。
首先開啟你存放shape檔案目錄,把shx檔案刪除掉,如下圖:
然後再cmd中輸入:cd /d E:\SvnWorkspace\LY_WEB_GIS\branches\Documents\ms4w-mapserver-for-wimdows\release-1911-x64-gdal-2-3-3-mapserver-7-2-1\apps\Example1.1\data
在cmd中輸入:ogrinfo --config SHAPE_RESTORE_SHX YES states_ugl.shp
檔案建立成功。
ogrinfo --config SHAPE_RESTORE_SHX YES states_ugl.shp 表示:在開啟shp檔案時,如果沒有shx檔案,則嘗試建立。
其他操作不一一討論,明白格式以及在實際需求中會使用就好。
--debug
設定debug級別,值為on、off、0或1
如:ogrinfo db.sqlite --debug on
--pause
在進行除錯的時候,等待使用者輸入。
--locale
除錯本地化安裝,如:en_US.UTF-8
以上是GDAL的一些通用命令使用方法,下面針對ogrinfo特有命令做相關介紹。
ogrinfo常用命令
-ro:read only,以只讀形式開啟檔案
-al:all layer,獲取所有layer的特徵資訊。如在cmd中輸入: ogrinfo -ro -al states_ugl.shp ,可看到下圖:
其所有特徵欄位資訊資料均會展現出來。
-rl:random layer,在GDAL2.2版本開始使用,表示啟用隨機讀取模式,即按在資料集中找到的順序迭代特性,而不是按每層迭代特性。對於像OSM、GMLAS等格式檔案讀取速度回快很多。
-so:summary only,僅顯示摘要資訊:顯示部分欄位型別資訊,僅顯示投影、架構、功能計數和範圍等摘要資訊。如在cmd中輸入: ogrinfo -ro -al -so states_ugl.shp
-q:quite,顯示主要資訊,包括座標系、層模式、範圍和功能計數(但是個人覺得沒這麼多,只有:圖層數量,名稱,層模式,可能是我的shp檔案問題)。在cmd中輸入:ogrinfo -q states_ugl.shp
#輸入 ogrinfo -ro-q states_ugl.shp #輸出 1: states_ugl (Polygon)
-where:類似sql語句查詢,只對shape檔案的屬性進行匹配查詢。如在cmd中輸入:ogrinfo -ro -al -so -where CLASS='land' states_ugl.shp
或者通過匯入檔案的方式執行where查詢(GDAL2.1版本以後才支援)
在資料來源資料夾中建立一個where.txt檔案,內容為CLASS='water',然後在cmd中執行:ogrinfo -ro -al -so -where @where.txt shates_ugl.shp
-sql:類似sql語句查詢,同時支援單獨指定sql檔案(.txt、.sql都可以)。
如在cmd中輸入:ogrinfo -ro -al -so -sql "SELECT CLASS,STATE FROM states_ugl" E:\SvnWorkspace\LY_WEB_GIS\branches\Documents\ms4w-mapserver-for-wimdows\release-1911-x64-gdal-2-3-3-mapserver-7-2-1\apps\tutorial\data
或者在cmd中輸入:ogrinfo -ro -al -where @E:\SvnWorkspace\LY_WEB_GIS\branches\Documents\ms4w-mapserver-for-wimdows\release-1911-x64-gdal-2-3-3-mapserver-7-2-1\apps\Example1.1\data\where.txt -sql @E:\SvnWorkspace\LY_WEB_GIS\branches\Documents\ms4w-mapserver-for-wimdows\release-1911-x64-gdal-2-3-3-mapserver-7-2-1\apps\Example1.1\data\sql.txt E:\SvnWorkspace\LY_WEB_GIS\branches\Documents\ms4w-mapserver-for-wimdows\release-1911-x64-gdal-2-3-3-mapserver-7-2-1\apps\tutorial\data
這個語句有上面的基礎很好理解。檔案路徑是絕對路徑,除非你的cmd命令定位的資料來源資料夾中存在sql過濾檔案和where過濾檔案。
-dialect:ogr版本sql語句。不大瞭解,標準sql語句也能使用,而且官網說效能地下。同時,sqlite支援的sql語句都能使用,所以使用通用sql語句就好,不做進一步的嘗試與講解。
-spat xmin ymin xmax ymax:根據資料範圍過濾。
在cmd中輸入:ogrinfo -ro -al -so states_ugl.shp 可以檢視其資料範圍為“ (-97.238976, 41.619778) - (-82.122902, 49.385620) ” ,上圖有。
在cmd中輸入:ogrinfo -ro -al -so -spat -90 42 -82 45 states_ugl.shp
-geomfield field:
-fid:OGR支援的一個特殊過濾欄位,fid。可以在 -where 中使用,如: -where "fid in (200,201,202)"
在cmd中輸入:ogrinfo -ro -al -where "fid in (200,201,202)" states_ugl.shp
-fields={YES/NO}:設定是否顯示欄位資料,如果是YES,顯示欄位資料資訊,如果為NO,不顯示。預設YES。
在cmd中輸入:ogrinfo -ro -al -fields=NO states_ugl.shp
-geom={YES/NO/SUMMARY/WKT/ISO_WKT}:用於控制欄位資料、幾何圖形的 POLYGON 資料顯示以及輸出(GDAL2.1版本的預設格式ISO_WKT)。
YES:預設值,全部顯示,顯示格式為OGC WKT格式輸出。
NO:不顯示 POLYGON 資料
如在cmd中輸入:ogrinfo -ro -al -gemo=NO states_ugl.shp
SUMMARY:顯示簡要資料,但是多了點的個數
設定為WKT、ISO_WKT效果通YES。WKT處其實顯示的是座標資訊。
-oo NAME=VALUE:資料集開啟操作項。
有點類似 --config KEY VALUE。只是-00 NAME=VALUE 是 名稱=值。表達方式不一樣。
#data.json的定義 { "type": "FeatureCollection", "features": [ { "type": "Feature", "geometry": { "type": "Point", "coordinates": [ 2, 49 ] }, "properties": { "a_property": "foo", "some_object": { "a_property": 1, "another_property": 2 } } } ] }
然後在cmd中輸入:
ogrinfo data.json -al -oo FLATTEN_NESTED_ATTRIBUTES=yes
可以看到如下轉換:
INFO: Open of `data.json' using driver `GeoJSON' successful. Layer name: data Geometry: Point Feature Count: 1 Extent: (2.000000, 49.000000) - (2.000000, 49.000000) Layer SRS WKT: GEOGCS["WGS 84", DATUM["WGS_1984", SPHEROID["WGS 84",6378137,298.257223563, AUTHORITY["EPSG","7030"]], AUTHORITY["EPSG","6326"]], PRIMEM["Greenwich",0, AUTHORITY["EPSG","8901"]], UNIT["degree",0.0174532925199433, AUTHORITY["EPSG","9122"]], AUTHORITY["EPSG","4326"]] a_property: String (0.0) some_object_a_property: Integer (0.0) some_object_another_property: Integer (0.0) OGRFeature(data):0 a_property (String) = foo some_object_a_property (Integer) = 1 some_object_another_property (Integer) = 2 POINT (2 49)
-nomd:阻止元資料列印。有些資料集可能包含許多元資料字串。
-listmdd:列出所有的元資料。
-nomd 和 -listmdd 都是針對元資料的。
#metadata 元資料 Metadata: DBF_DATE_LAST_UPDATE=2002-03-07 #在states_ugl.shp 檔案檢視的時候,你們會發現有這了資料。 #-nomd不顯示這條 #預設顯示, -listmdd顯示有所的。
-mdd domain:從指定的域顯示元資料。all可以顯示所有域的元資料。
-nocount:沒有Feature數量,即圖形數量 Feature Count: 204
-noextent:不輸出空間範圍。即沒有 Extent: (-97.238976, 41.619778) - (-82.122902, 49.385620)
--formats:列出OGR支援的資料格式。上面講過。
datasource_name:表示資料來源、或者多個數據原始檔所在目錄、或者其他定義的全域性虛擬名稱。檢視: https://gdal.org/ogr_formats.html 此網頁,瞭解OGR支援的資料格式。
如:在cmd中知己輸入(不定位到資料所在資料夾):ogrinfo -ro -al -so E:\SvnWorkspace\LY_WEB_GIS\branches\Documents\ms4w-mapserver-for-wimdows\release-1911-x64-gdal-2-3-3-mapserver-7-2-1\apps\tutorial\data
會羅列出當前檔案下下面所有可以開啟的資料檔案的資訊。
layer:獲取請求圖層資料資訊。
如在cmd中輸入:ogrinfo -ro -al -so E:\SvnWorkspace\LY_WEB_GIS\branches\Documents\ms4w-mapserver-for-wimdows\release-1911-x64-gdal-2-3-3-mapserver-7-2-1\apps\tutorial\data roads_ugl
則是顯示圖層名稱為roads_ugl的資料資訊
或者輸入:ogrinfo -ro -al -so E:\SvnWorkspace\LY_WEB_GIS\branches\Documents\ms4w-mapserver-for-wimdows\release-1911-x64-gdal-2-3-3-mapserver-7-2-1\apps\tutorial\data roads_ugl states_ugl
其他命令官網
如果想學習而不等待部落格更新,可以先上官網看看。
gdalinfo命令
官網: https://gdal.org/gdalinfo.html
dalsrsinfo命令
官網: https://gdal.org/gdalsrsinfo.html
gdallocationinfo命令
官網: https://gdal.org/gdallocationinfo.html
gdal_translate命令
官網: https://gdal.org/gdal_translate.html
gdaltransform命令
官網: https://gdal.org/gdaltransform.html
後記
寫得很累,每個命令的作用,效果,一步一步的操作基本都寫得非常詳細。但是這是個學習的過程。行者無疆,讓前面的人累點吧!畢竟也沒人指導,百度下的資料也是很稀缺的。要麼就是個大綱一樣。要麼就是讓人不知所云。
加油各位。