การใช้งาน gdal และ ogr เบ้ืองตน้ -...
TRANSCRIPT
การใชงาน GDAL และ OGR เบองตน
สวรนทร ฤกษอยสข
http://sawarino.blogspot.com
GDAL - Geospatial Data Abstraction Library
Sawarin Lerk-u-suke ([email protected]) May 2010
2
ความรเบองตนเกยวกบการใช command line
• องคประกอบเบองตนของค าสงประกอบดวย– ชอค าสง (Command)เชน gdalinfo หรอ gdal_translate
– ตวเลอก (Option) เชน –t_srs หรอ -of
– ไฟล (ทประมวลผล)• ไฟลตนฉบบ(ไฟลทใชประมวลผล) หรอ Source File
• ไฟลผลลพธ (ไฟลทไดหลงจากประมวลผลแลว) หรอ Destination file
• ตองมการเวนวรรค (space) ระหวาง ชอค ำสง ตวเลอก ไฟล เสมอ• คาหรอพารามเตอรของตวเลอกตองมการเวนววรคดวยเสมอ เชน
-s_srs epsg:32647 หรอ –of GTiff เปนตน
Sawarin Lerk-u-suke ([email protected]) May 2010
5
ระบบอางองทางต าแหนงทนยมใชส าหรบประเทศไทย
ระบบอางอง(Reference System)
โซน(Zone)
พนหลกฐาน(Datum)
รหส EPSG(Code)
Geographic - Indian 1975 4240Geographic - WGS 1984 4326UTM 47 Indian 1975 24047UTM 48 Indian 1975 24048UTM 47 WGS 1984 32647UTM 48 WGS 1984 32648
Sawarin Lerk-u-suke ([email protected]) May 2010
7
ชนดของการจดเกบขอมลเชงเลข (Data Type)
ชนดขอมล ขนาด คาทจดเกบ จ านวนระดบ
Boolean 1 bit 0 หรอ 1 2
Signed byte (8 bits) 1 Byte -128 0 127 256
Unsigned byte (8 bits) 1 Byte 0 - 255 256
Signed 16 bits 2 Bytes - 32768 0 32767 65536
Unsigned 16 bits 2 Bytes 0 - 65535 65536
Signed 32 bits 4 Bytes - 2,147,483,648 0 2,147,483,647 4,294,967,296
Unsigned 32 bits 4 Bytes 0 - 4,294,967,295 4,294,967,296
Sawarin Lerk-u-suke ([email protected]) May 2010
8
Supported Data Type (วธการระบ option)
• BYTE (8 Bit or 256 levels)
• Int16 (16 Bit or 65536 levels)
• UInt16
• UInt32
• Int32
• Float32
• Float64
• CInt16
• CInt32
• CFloat32
• CFloat64
Sawarin Lerk-u-suke ([email protected]) May 2010
9
องคประกอบของขอมลภาพและขอมลกรด
• จดภาพ (Picture Elements - PIXELS)• ค าอธบายขอมล (Metadata)• ขอมลการปรบยดเชงต าแหนง (Spatial registration information)
• วธการหอหม (Encapsulation) และการเขารหส (Coding)
Sawarin Lerk-u-suke ([email protected]) May 2010
10
gdalinfo
• การใชงาน>gdalinfo ไฟลทตองการดรายละเอยด• ตวอยางการใชงาน
>gdalinfo imagery32647.tif
•ควรเปลยนไดเรกทอรไปยงทจดเกบขอมลเสมอ เพอความสะดวกในการท างาน เชน จาก C:\Program Files\FWTools2.1.0>
ใหเปน D:\imagery>
Sawarin Lerk-u-suke ([email protected]) May 2010
11
gdalwarp
• กำรใชงำน>gdalwarp ตวเลอก ไฟลตนฉบบ ไฟลผลลพธ• ตวเลอกทส ำคญ-s_srs ระบระบบพกดของไฟลตนฉบบ เชน –s_srs epsg:32647
-t_srs ก าหนดระบบพกดของไฟลผลลพธ เชน –t_srs epsg:4326
-tr ขนาด GSD ของไฟลผลลพธ เชน –tr 30 30
(ตามหนวยของระบบอางองทางต าแหนง)• ตวอยำงกำรใชงำน>gdawarp –t_srs epsg:32647 imagery4326.tif
imagery32647.tif
Sawarin Lerk-u-suke ([email protected]) May 2010
12
gdal_translate
• กำรใชงำน
>gdal_translate ตวเลอก ไฟลตนฉบบ ไฟลผลลพธ• ตวเลอกทส ำคญ
-of รปแบบขอมลผลลพธทตองการ (รายละเอยดเพมเตมศกษาไดจาก GDAL raster format support)
-ot ลกษณะขอมลทจดเกบ เชน Int16 หรอ Float32
-co quality=?? คณภาพของผลลพธทตองการ เชน –co quality=5
หมายถงขนาดของไฟลผลลพธลดรายละเอยดลง 5:100 หรอ 1:20
>>gdal_translate –of jp2kak –co quality=5 imagery.tif imagery.jp2
Sawarin Lerk-u-suke ([email protected]) May 2010
13
gdaladdo
• การสรางพรามดขอมลภาพ (ชวยใหการแสดงผลขอมลภาพขนาดใหญรวดเรวขน)
• ขนาด(หนวยความจ า)จะเพมขน แตไมเกน 33%
• ขนาดภาพแตละชนจะลดลงทละครงหนง(ตอดาน) เชน 1000 x 1000
500 x 500 250 x 250 125 x 125 pixel
• ชนพรามดทสรางขนจะถกบนทกทบลงบนไฟลเดม (ไมมการเปลยนแปลงเชงรงสหรอระบบอางองทางต าแหนง แตขนาดไฟลจะใหญขน)
>>gdaladdo -r average input.tif 2 4 8 16 32
Sawarin Lerk-u-suke ([email protected]) May 2010
14
gdal_contour
• กำรใชงำน
>gdal_contour ตวเลอก ไฟลตนฉบบ ไฟลผลลพธ• ตวเลอกทส ำคญ
-a ชอฟลดทระบคาความสง เชน –a “height”
-i ชวงความสงของเสนชนความสง เชน –i 20
(หนวยตวามระบบอางองทางต าแหนง)• ตวอยำงกำรใชงำน>gdal_contour –a “height” –i 20 dem.tif contour.shp
Sawarin Lerk-u-suke ([email protected]) May 2010
15
gdaltindex
• กำรใชงำน
>gdaltindex -tileindex ชอฟลดส าหรบเกบทอยของไฟลขอมลภาพ -write_absolute_path (ระบเมอตองการบอก path แบบ
absolute)
-skip_different_projection (ระบเมอตองการขามขอมลภาพทมระบบพกดทแตกตางกน)
ชอไฟลผลลพธ (*.shp) ไฟลภาพทงหมด (เชน *.tif)
Sawarin Lerk-u-suke ([email protected]) May 2010
16
gdaltindex (ตอ)
• ตวอยางการใชงาน
> gdaltindex –tileindex img_path idx.shp *.tif
Sawarin Lerk-u-suke ([email protected]) May 2010
17
gdalenhance
• Histogram equalization enhancements.
• ตวอยางการใชงาน
>>gdalenhance –equalize –of BYTE –ot GTiff
Abc.tif abc_enh.tif
Sawarin Lerk-u-suke ([email protected]) May 2010
18
Transparent Pixel: gdalwarp
• Convert no data pixel to transparent pixel.
• ตวอยางการใชงาน
>>gdalwarp –srcnodata 0 –dstalpha imgB.tif
imgT.tif
• การแปลงระบบอางองทางต าแหนงพรอมกบการสรางจดภาพโปรงใส (เพอเรยกดขอมลภาพดวย Google Earth – epsg:4326)
>> gdalwarp –srcnodata 0 –dstalpha –t_srs
epsg:4326 imgB.tif imgT.tif
Sawarin Lerk-u-suke ([email protected]) May 2010
19
gdal2tiles
• Create Tile structure and KML for GE.
• SRS must be Geographic Coordinate System (EPSG:4326)
• ตวอยางการใชงาน
>>gdal2tiles –title “myTMS” Abc.tif d:\ge_tms\
Sawarin Lerk-u-suke ([email protected]) May 2010
20
Color Composition: gdal_merge
• Color Composition & At least 3 Bands
• ตวอยางการใชงาน
>>gdal_merge –o output.tif –separate
input_b1.tif input_b2.tif input_b3.tif
Sawarin Lerk-u-suke ([email protected]) May 2010
21
GDAL Batch processing.
• DOS batch programming
• ตวอยางการใชงาน
• บนทกไฟลใหเปนนามสกล .bat (เพอแยกความแตกตางจากไฟลอน)
FOR %i IN (D:\img_zone47\*.tif) DO gdal_translate -of jp2kak -co "quality=5" %i .\%~ni.jp2
*กรณตองการบนทกไฟลใหอยรป *.bat นนตองเพม % อก เชน จาก %i เปน %%i
Sawarin Lerk-u-suke ([email protected]) May 2010
22
GDAL support format
AAIGrid Arc/Info ASCII Grid JP2KAK JPEG-2000 (based on Kakadu)
ADRG ARC Digitized Raster Graphics JPEG JPEG JFIF
BMP MS Windows Device Independent Bitmap Leveller Leveller heightfield
BT VTP .bt (Binary Terrain) 1.3 Format MEM In Memory Raster
DTED DTED Elevation Raster MFF Vexcel MFF Raster
ECW ERMapper Compressed Wavelets MFF2 Vexcel MFF2 (HKV) Raster
EHdr ESRI .hdr Labelled netCDF Network Common Data Format
ELAS ELAS NITF National Imagery Transmission Format
ENVI ENVI .hdr Labelled PAux PCI .aux Labelled
ERS ERMapper .ers Labelled PCIDSK PCIDSK Database File
FIT FIT Image PCRaster PCRaster Raster File
GIF Graphics Interchange Format (.gif) PNG Portable Network Graphics
GMT GMT NetCDF Grid Format PNM Portable Pixmap Format (netpbm)
GSAG Golden Software ASCII Grid (.grd) RMF Raster Matrix Format
GSBG Golden Software Binary Grid (.grd) RST Idrisi Raster A.1
GTiff GeoTIFF SGI SGI Image File Format 1.0
HDF4Image HDF4 Dataset SRTMHGT SRTMHGT File Format
HFA Erdas Imagine Images (.img) Terragen Terragen heightfield
IDA Image Data and Analysis USGSDEM USGS Optional ASCII DEM (and CDED)
ILWIS ILWIS Raster Map VRT Virtual Raster
INGR Intergraph Raster XPM X11 PixMap Format
Sawarin Lerk-u-suke ([email protected]) May 2010
23
ogrinfo
กำรใชงำน
>ogrinfo –so ชอไฟล ชอไฟล(ไมมนามสกล)ตวอยำงกำรใชงำน
>ogrinfo –so contour.shp contour
Sawarin Lerk-u-suke ([email protected]) May 2010
24
ogr2ogr
กำรใชงำน
>ogr2ogr ตวเลอก ไฟลผลลพธ ไฟลตนฉบบ ตวเลอกทส ำคญ
-f รปแบบขอมลผลลพธ (ดเพมเตมทชนดขอมลทรองรบ)
ตวอยำงกำรใชงำน
>ogr2ogr -f "KML" province4326.kml
province4326.shp
Sawarin Lerk-u-suke ([email protected]) May 2010
25
Ogr2ogr(ส าหรบการแปลงระบบอางองทางต าแหนงของขอมลแบบเวคเตอร)
ตวเลอกทส ำคญ
-s_srs ระบระบบอางองฯของขอมลตนฉบบ-t_srs ก าหนดระบบอางองฯของขอมลผลลพธตวอยำงกำรใชงำน
>ogr2ogr –s_srs epsg:32647 –t_srs
epsg:4326 province_utm. shp
province_geog.shp
Sawarin Lerk-u-suke ([email protected]) May 2010
26
OGR support format (วธการระบ option)
• "ESRI Shapefile"• "MapInfo File"• "TIGER"• "S57"• "DGN"• "Memory"• "BNA"• "CSV"• "GML"• "GPX"
• "GeoJSON"• "Interlis 1"• "Interlis 2"• "GMT"• "SQLite"• "ODBC"• "PostgreSQL"• "MySQL"• "Geoconcept“• "KML"