SlideShare una empresa de Scribd logo
1 de 34
GIS na GNU/Linux-u Darko Boto APIS IT d.o.o . GIS analitičar   CLI Geoproccesing GDAL/OGR
Sadržaj : ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
O radionici ,[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Povijest FLOSS GIS-a   ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
 
Što je GIS? ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Projekcije http://spatialreference.org/
Prostorni podaci Vektorski podaci:   Arc/Info Binary Coverage, Arc/Info .E00 (ASCII) Coverage, Atlas BNA, AutoCAD DXF, CSV, ESRI Personal GeoDatabase, ESRI Shapefile, GeoJSON, GeoRSS, GML, GMT, GPX.... Simple features (OGC- http://www.opengeospatial.org/standards/sfa)  POINT(6 10) LINESTRING(3 4,10 50,20 25) POLYGON((1 1,5 1,5 5,1 5,1 1),(2 2,2 3,3 3,3 2,2 2)) MULTIPOINT(3.5 5.6, 4.8 10.5) MULTILINESTRING((3 4,10 50,20 25),(-5 -8,-10 -8,-15 -4)) MULTIPOLYGON(((1 1,5 1,5 5,1 5,1 1),(2 2,2 3,3 3,3 2,2 2)),((6 3,9 2,9 4,6 3))) GEOMETRYCOLLECTION(POINT(4 6),LINESTRING(4 6,7 10)) POINT ZM (1 1 5 60) POINT M (1 1 80) Rasterski Podaci:  Arc/Info ASCII Grid, Arc/Info Binary Grid, Bathymetry Attributed Grid, ESRI .hdr, Erdas Imagine Raw, Generic Binary (.hdr), Oracle Spatial GeoRaster,Graphics Interchange Format (.gif), GMT Compatible netCDF, GRASS Rasters, TIFF/BigTIFF/GeoTIFF (.tif), JPEG JFIF, JPEG2000, NetCDF, PNG, Netpbm (.ppm,.pgm), Rasterlite - Rasters in SQLite DB, SRTM HGT Format, Terralib, USGS ASCII DEM, OGC Web Coverage Server, WKTRaster, OGC Web Map Server.... $wget  http://slobodniatlas.mapnix.org/~dboto/DORS_workshop.tar.gz  && tar -xvzf DORS_workshop.tar.gz
GDAL = GDAL+OGR ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
GDAL Utilities ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
gdalinfo $gdalinfo ASTGTM_N44E015_dem.tif Driver: GTiff/GeoTIFF Files: ASTGTM_N44E015_dem.tif Size is 3601, 3601 Coordinate System is: GEOGCS[&quot;WGS 84&quot;,     DATUM[&quot;WGS_1984&quot;,         SPHEROID[&quot;WGS 84&quot;,6378137,298.2572235629972,             AUTHORITY[&quot;EPSG&quot;,&quot;7030&quot;]],         AUTHORITY[&quot;EPSG&quot;,&quot;6326&quot;]],     PRIMEM[&quot;Greenwich&quot;,0],     UNIT[&quot;degree&quot;,0.0174532925199433],     AUTHORITY[&quot;EPSG&quot;,&quot;4326&quot;]] Origin = (14.999861111111111,45.000138888888891) Pixel Size = (0.000277777777778,-0.000277777777778) Metadata:   AREA_OR_POINT=Area   TIFFTAG_DOCUMENTNAME=created at   TIFFTAG_IMAGEDESCRIPTION=SILC TIFF   TIFFTAG_SOFTWARE=IDL 6.3, Research Systems, Inc.   TIFFTAG_DATETIME=2008:10:27 20:50:05   TIFFTAG_XRESOLUTION=100   TIFFTAG_YRESOLUTION=100   TIFFTAG_RESOLUTIONUNIT=2 (pixels/inch) Image Structure Metadata:   INTERLEAVE=BAND Corner Coordinates: Upper Left  (  14.9998611,  45.0001389) ( 14d59'59.50&quot;E, 45d 0'0.50&quot;N) Lower Left  (  14.9998611,  43.9998611) ( 14d59'59.50&quot;E, 43d59'59.50&quot;N) Upper Right (  16.0001389,  45.0001389) ( 16d 0'0.50&quot;E, 45d 0'0.50&quot;N) Lower Right (  16.0001389,  43.9998611) ( 16d 0'0.50&quot;E, 43d59'59.50&quot;N) Center      (  15.5000000,  44.5000000) ( 15d30'0.00&quot;E, 44d30'0.00&quot;N) Band 1 Block=3601x1 Type=Int16, ColorInterp=Gray   < format zapisa    < veličina   <projekcija <EPSG kod - $ less /usr/share/proj/epsg | grep 4326 < metapodaci                 < koordinate extenta < type Int16
gdal_translate $gdal_translate --formats   - mogućnost konverzije između 88 rasterskih formata sa kontrolom izlaznog formata (GeoTiff - defaultni izlazni format). ***Ukoliko fromat ne podržava pohranu metapodataka u samom formatu, metapodatke pohranjuje u xml datoteku. Konverzija TIFF-a u DEM $gdal_translate -of USGSDEM ASTGTM_N44E015_dem.tif translate_output1.dem   Konverzija sa kontrolom veličine outputa (resize) $gdal_translate -of USGSDEM -outsize 50% 50% ASTGTM_N44E015_dem.tif translate_output2.dem   Konverzija u 16bit-ni raster $gdal_translate -of PNG ot UInt16 ASTGTM_N44E015_dem.tif translate_output3.png   Konverzija u JPG + worldfile  $gdal_translate -of JPEG -scale -co worldfile=yes ASTGTM_N44E015_dem.tif translate_output3.jpg   Konverzija u tekstualnu xyz datoteku  $gdal2xyz.py ASTGTM_N44E015_dem.tif translate_output4.xyz   Jako velike mogućnosti pogledati: $man gdal_translate
gdalwarp Omogućava izradu mozaika, reprojiciranje i prilagođavanje slike  koordinatnom sustavu sa kontrolom outputa . $gdalwarp -of GTiff -co &quot;TILED=YES&quot; -srcnodata 32767 -dstnodata 32767 -t_srs &quot;+proj=tmerc +ellps=bessel +k_o=0.9999 +x_0=5500000 +lon_0=15 +units=m +towgs84=519.729316,72.371421,492.743986,5.63370593,-3.33002924,-3.10746598,10.07015476&quot; -rcs -order 3 -tr 15 15 -wt Float32 -ot Float32 -wo SAMPLE_STEPS=100 ASTGTM_N44E015_dem.tif gdalwarp_output1.tif -t_srs - ciljani prostorno referentni sustav, -tr - rezolucija, -wo SAMPLE_STEPS - gustoća uzimanja uzoraka Primjer resempliranja gdalwarp -ts 3601 3601 -r cubicspline ASTGTM_N44E015_dem.tif gdalwarp_output2.tif Primjer isijecanja dijela rastera korištenjem poligona (gdalwarp + gdal_translate) #!/bin/bash SHPFILE=$1 BASE=`basename $SHPFILE .shp` EXTENT=`ogrinfo -so $SHPFILE $BASE | grep Extent | sed 's/Extent: //g' | sed 's/(//g' | sed 's/)//g' | sed 's/ - /, /g'` EXTENT=`echo $EXTENT | awk -F ',' '{print $1 &quot; &quot; $4 &quot; &quot; $3 &quot; &quot; $2}'` echo &quot;Clipping to $EXTENT&quot; RASTERFILE=$2 gdal_translate  -projwin $EXTENT -of GTiff $RASTERFILE /tmp/`basename $RASTERFILE .tif`_bbclip.tif gdalwarp  -co COMPRESS=DEFLATE -co TILED=YES -of GTiff -r lanczos -cutline $SHPFILE /tmp/`basename $RASTERFILE .tif`_bbclip.tif /tmp/`basename $RASTERFILE .tif`_shpclip.tif
kolorizacija rastera #! /usr/bin/env python from osgeo import gdal import sys import numpy import os.path gdal.TermProgress = gdal.TermProgress_nocb src_file = sys.argv[1] dst_file = sys.argv[2] out_bands = 3 def MakeColor(z_value):    '''LUT for color ramp. Keys are pixel values, hash values are RGB triplets.'''   color_dict = {     0:[102,0,255],    25:[20,82,255],    50:[0,194,224],    75:[0,255,122],   100:[41,255,0],   125:[204,255,0],   150:[245,194,0],   175:[224,118,0],   200:[168,46,0],   225:[105,0,0],   250:[64,0,0],   255:[0,0,0]}   key_list = color_dict.keys()   key_list.sort()   while len(key_list) > 0:     last_val = key_list[-1]     if z_value >= last_val:       return color_dict[last_val]     else:       key_list.remove(last_val) # Print some info print &quot;Creating %s&quot; % (dst_file) # Open source file src_ds = gdal.Open( src_file ) src_band = src_ds.GetRasterBand(1) # create destination file dst_driver = gdal.GetDriverByName('GTiff') dst_ds = dst_driver.Create(dst_file, src_ds.RasterXSize, src_ds.RasterYSize, out_bands, gdal.GDT_Byte) # create output bands band1 = numpy.zeros([src_ds.RasterYSize, src_ds.RasterXSize]) band2 = numpy.zeros([src_ds.RasterYSize, src_ds.RasterXSize]) band3 = numpy.zeros([src_ds.RasterYSize, src_ds.RasterXSize]) # set the projection and georeferencing info dst_ds.SetProjection( src_ds.GetProjection() ) dst_ds.SetGeoTransform( src_ds.GetGeoTransform() ) # read the source file gdal.TermProgress( 0.0 ) for iY in range(src_ds.RasterYSize):   src_data = src_band.ReadAsArray(0,iY,src_ds.RasterXSize,1)   col_values = src_data[0]                          # array of z_values, one per row in source data   for iX in range(src_ds.RasterXSize):     z_value = col_values[iX]     # print z_value                                          # uncomment to see what value breaks color ramp     [R,G,B] = MakeColor(z_value)     band1[iY][iX] = R     band2[iY][iX] = G     band3[iY][iX] = B   gdal.TermProgress( (iY+1.0) / src_ds.RasterYSize ) # write each band out dst_ds.GetRasterBand(1).WriteArray(band1) dst_ds.GetRasterBand(2).WriteArray(band2) dst_ds.GetRasterBand(3).WriteArray(band3) dst_ds = None   #END script  Ili jednostavnije:   $gdaldem color-relief ASTGTM_N44E015_dem.tif ramp.txt color.tif   gdje je ramp.txt  3500 white 2500 235:220:175 50% 190 185 135 700 240 250 150 0 50 180 50 nv 0 0 0 0  100%  255 255 255 75%   235 220 175 50%   190 185 135 25%   240 250 150 0%    50  180  50 nv    0   0   0
gdal_contour Omogućava generiranje izo linija iz rastera sa visinskim podacima (SRTM, ASTER)   $gdal_contour -i 10 -snodata 32767 -a height ASTGTM_N44E015_dem.tif gdalwarp_output1.tif Primjer generiranja izo linija iz SRTM .hgt podataka shell skriptom: # povlacenje SRTM podataka (.hgt binary grid) sa NASA ftp servera. Lista za HR je u srtmhr_list.txt cat srtmhr_list.txt | sed 's!^!ftp://e0srp01u.ecs.nasa.gov/srtm/version2/SRTM3/Eurasia/!' | xargs -i wget -nc {} # apsolutna putanja do direktorija sa podacima DATA_PATH=&quot;&quot; # ulazi u direktorij sa podacima cd $DATA_PATH # za  svaki *hgt.zip izvrsava srtm_generate_hdr.sh  http://mpa.itc.it/rs/srtm/srtm_generate_hdr.sh for X in *.hgt.zip do # skripta od .hgt fileova generira BIL headere i priprema podatke za GDAL          # GDAL nakon toga radi konverziju u GeoTIFF te napravi .prj za shp     yes | $DATA_PATH/srtm_generate_hdr.sh $X done # generiranje izohipsi iz pripremljenih tifova for X in $DATA_PATH/*.tif do     echo $X # izvlacenje izohipsi po visinskim podacima za 10, 50 i 100 m i spremanje u ESRI shape file (.shp)      gdal_contour -i 10 -snodata 32767 -a height $X ${X%%.tif}c10.shp     gdal_contour -i 50 -snodata 32767 -a height $X ${X%%.tif}c50.shp     gdal_contour -i 100 -snodata 32767 -a height $X ${X%%.tif}c100.shp done
Još primjera korištenja GDAL utilitija ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Zadatak - primjer analize  ,[object Object],[object Object],[object Object]
Rješenje ,[object Object],[object Object]
 
OGR ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
ogrinfo $ogrinfo -al region_borders.shp | less $ogrinfo -ro region_borders.shp -sql &quot;SELECT * FROM region_borders WHERE CNTRYNAME='Croatia'&quot; | less INFO: Open of `region_borders.shp'       using driver `ESRI Shapefile' successful. Layer name: region_borders Geometry: Polygon Feature Count: 7 Extent: (6.625400, 35.491300) - (23.006200, 49.020300) Layer SRS WKT: GEOGCS[&quot;GCS_WGS_1984&quot;,     DATUM[&quot;WGS_1984&quot;,         SPHEROID[&quot;WGS_1984&quot;,6378137.0,298.257223563]],     PRIMEM[&quot;Greenwich&quot;,0.0],     UNIT[&quot;Degree&quot;,0.0174532925199433]] ObjectID: Integer (9.0) NATION: String (3.0) CNTRYNAME: String (25.0) CNTRYABB: String (3.0) SQKM: Real (19.5) COLORMAP: Real (11.0) OGRFeature(region_borders):0  < ime datoteke <tip geometrije < broj featurea < prostroni kvadrat (extent)  < projekcija < ime/tip atribunog polja
ogr2ogr $ogr2ogr --help   (Mala pomoć za pregled mogućnosti alata)   $ogr2ogr -f &quot;KML&quot; region_borders.kml region_borders.shp   (Konverzija .shp datoteke u google .kml)   $ogr2ogr -f &quot;PostgreSQL&quot; PG:&quot;host=localhost user=user dbname=user password=user&quot; region_borders.kml -nln region_borders   (Punjenje .kml datoteke u PostGIS baz)  $ogr2ogr -f &quot;ESRI Shapefile&quot; pg_export.shp PG:&quot;host=localhost user=user dbname=user password=user&quot; &quot;region_borders&quot;   (Export prostorne tablice u .shp datoteku)    $ogr2ogr -s_srs EPSG:4326 -t_srs EPSG:31275 rb_31275.shp region_borders.shp  (Konverzija prostornog referentnog sustava)   $ogr2ogr -update -append -f PostgreSQL PG:dbname=user region_borders.shp  (Update i append iz .shp datoteke u PostGIS bazu)
ogr2ogr - merge (sh vs py) Merge shp datoteka - Shell  #!/bin/bash mkdir merged find . -name 'contours*' -exec mv '{}' merged  for i in $(ls *.shp);       do ogr2ogr -f 'ESRI Shapefile' -update -append merged $i -nln contours_merge done   Merge shp datoteka - Python   import sys import glob from osgeo import ogr outfile = &quot;merge.shp&quot; file_list = glob.glob(&quot;*.shp&quot;) # CREATE OUTPUT FILE out_driver = ogr.GetDriverByName( 'ESRI Shapefile' ) out_ds = out_driver.CreateDataSource(outfile) out_srs = None out_layer = out_ds.CreateLayer(&quot;trans&quot;, out_srs, ogr.wkbLineString) fd = ogr.FieldDefn('name', ogr.OFTString) out_layer.CreateField(fd) fd = ogr.FieldDefn('height', ogr.OFTInteger) out_layer.CreateField(fd) # READ EACH INPUT FILE AND WRITE FIELDS TO NEW FILE for shapefile in file_list:   print shapefile   [filename, extension] = shapefile.split('.')   in_drv = ogr.GetDriverByName( 'ESRI Shapefile' )   in_ds = in_drv.Open(shapefile)   in_layer = in_ds.GetLayer(0)   in_feature = in_layer.GetNextFeature()   height_field = in_feature.GetFieldIndex('height')   counter = 1   print height_field while in_feature is not None and (height_field != -1):     name = filename + &quot;_&quot; + `counter`     #print kV_field     #print in_feature.GetField     height = in_feature.GetField(height_field)     out_feat = ogr.Feature(out_layer.GetLayerDefn())     out_feat.SetField('name', name)     out_feat.SetField('height', height)     out_feat.SetGeometry(in_feature.GetGeometryRef().Clone())     out_layer.CreateFeature(out_feat)     out_layer.SyncToDisk()     out_feat.Destroy()     in_feature.Destroy()     in_feature = in_layer.GetNextFeature()     counter += 1 out_ds.Destroy()
 
PostGIS ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Podešavanje baze Kreiranje baze i template-a (preporučujem) $ sudo su postgres $ createdb gistemplate Kreiranje PL/PSQL $ createlang plpgsql gistemplate Load funkcija i SRS tablice $ psql -d gistemplate -f /usr/share/postgresql-8.4-postgis/lwpostgis.sql $ psql -d gistemplate -f /usr/share/postgresql-8.4-postgis/spatial_ref_sys.sql Provjera $psql -d gistemplate -c &quot;SELECT postgis_full_version();&quot; Ulazimo u bazu  $ psql  postgres=# Kreiranje grupe, dodjela role i grant postgres=# CREATE ROLE gisgroup NOSUPERUSER NOINHERIT CREATEDB NOCREATEROLE; postgres=# CREATE ROLE gis LOGIN PASSWORD 'gisuser' NOINHERIT; postgres=# GRANT gisgroup TO gis; postgres=#
Dodjela ovlasti na prostorne tablice gis user-u i kreiranje sheme $ psql -d gistemplate gistemplate=# ALTER TABLE geometry_columns OWNER TO gis; gistemplate=# ALTER TABLE spatial_ref_sys OWNER TO gis; gistemplate=# CREATE SCHEMA gis_schema AUTHORIZATION gis; gistemplate=#  Kreiranje baze rv_gis i priprema za load podataka $ createdb -T gistemplate -O gis rv_gis Dodavanje google projekcije u postgis INSERT into spatial_ref_sys (srid, auth_name, auth_srid, srtext, proj4text) values (900913 ,'EPSG',900913,'GEOGCS[&quot;WGS 84&quot;, DATUM[&quot;World Geodetic System  1984&quot;, SPHEROID[&quot;WGS 84&quot;, 6378137.0, 298.257223563,AUTHORITY[&quot;EPSG&quot;,&quot;7030&quot;]], AUTHORITY[&quot;EPSG&quot;,&quot;6326&quot;]],PRIMEM[&quot;Greenwich&quot;, 0.0, AUTHORITY[&quot;EPSG&quot;,&quot;8901&quot;]], NIT[&quot;degree&quot;,0.017453292519943295], AXIS[&quot;Longitude&quot;, EAST], AXIS[&quot;Latitude&quot;, NORTH],AUTHORITY[&quot;EPSG&quot;,&quot;4326&quot;]], PROJECTION[&quot;Mercator_1SP&quot;],PARAMETER[&quot;semi_minor&quot;, 6378137.0],  PARAMETER[&quot;latitude_of_origin&quot;,0.0], PARAMETER[&quot;central_meridian&quot;, 0.0], PARAMETER[&quot;scale_factor&quot;,1.0], PARAMETER[&quot;false_easting&quot;, 0.0], PARAMETER[&quot;false_northing&quot;, 0.0],UNIT[&quot;m&quot;, 1.0], AXIS[&quot;x&quot;, EAST], AXIS[&quot;y&quot;, NORTH],AUTHORITY[&quot;EPSG&quot;,&quot;900913&quot;]] |','+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m  +nadgrids=@null +no_defs'); Primjer kreiranja view-a za MapServer CREATE VIEW gradovi AS SELECT * FROM mjesta WHERE type LIKE 'Small' ORDER BY gid;
Load shp2pgsql - data loader (može se koristiti i OGR sa podrškom za veći broj formata) $shp2pgsql  – s <epsgcode> <ShapeFileName> <TableName> <dbName> > <filename> $psql -d <dbname> -f <filename>   $pgsql2shp -f croatia -h localhot -u user -P user gis &quot;SELECT name, wkb_geometry FROM region_borders WHERE name = 'CROATIA'&quot; Primjer $shp2pgsql -W &quot;ISO-8859-1&quot; -I -s 4326 region_borders gis_schema.region_borders | psql -d gis Napomena: Za naše dijakritičke znakove -W &quot;WINDOWS-1250&quot; EPSG referenca: $ less /usr/share/proj/epsg | grep 31275 <31275> +proj=tmerc +lat_0=0 +lon_0=15 +k=0.999900 +x_0=5500000 +y_0=0 +ellps=bessel +towgs84=577.326,90.129,463.919,5.137,1.474,5.297,2.4232 +units=m +no_defs  <> Export podataka iz baze pgsql2shp -f imefilea imebaze gis_schema.region_borders Prostorni upit za extent SELECT EXTENT(the_geom) FROM region_borders; Kreiranje prostorog indexa GIST (auto vacum i analyze) CREATE INDEX <indexname> ON <tablename> USING GIST ( <geometrycolumn> GIST_GEOMETRY_OPS ); Promjena tipa podatka na stupcu  alter TABLE gis_schema.contours50 ALTER COLUMN height TYPE integer;
Primjeri:    Kreiranje prostorne kolone - AddGeometryColumn SELECT AddGeometryColumn('public', 'region_borders', 'geom', 4326, 'POINT', 2); Kreiranje prostornog indeksa - GIST CREATE INDEX idx_region_borders_geom ON region_borders USING gist(geom);  Izračunavanje površine - ST_Area SELECT neigh_name, ST_Area(geom) FROM region_borders ORDER BY ST_Area(geom) limit 1;   Pronađi sve objekte 100 meters od točke POINT(1000 1000) - ST_DWithin SELECT * FROM geotable WHERE ST_DWithin(geocolumn,  ’ POINT(1000 1000) ’ , 100.0); select zup_naziv from gis_schema.zupanija where ST_DWithin(the_geom, 'POINT(5538942 5007927)',100.0); Ukupna dužina ceste izražena u kilometrima - ST_Length SELECT sum(ST_Length(the_geom))/1000 AS km_roads FROM roads; Selecija poligona koje se nalaze unutar drugog poligona ST_Within CREATE TABLE gis_schema.corine_cro AS SELECT gis_schema.corine.* FROM gis_schema.corine, gis_schema.granice WHERE ST_WITHIN (gis_schema.corine.the_geom, gis_schema.granice.the_geom) AND gis_schema.granice.CNTRYNAME = 'Croatia';  Simplifikacija - ST_Simplify CREATE TABLE gis_schema.border_genSimplify AS SELECT ST_SIMPLIFY(gis_schema.border_gen.the_geom,2) FROM gis_schema.border_gen; ST funkcije
Pregled alata ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
WEB GIS ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
 
PITANJA? kontakt: [email_address] [email_address]

Más contenido relacionado

Destacado

Destacado (7)

GeoNode - Open Source Geospatial Content Management System
GeoNode - Open Source Geospatial Content Management SystemGeoNode - Open Source Geospatial Content Management System
GeoNode - Open Source Geospatial Content Management System
 
공간정보 거점대학 - OpenLayers의 고급 기능 이해 및 실습
 공간정보 거점대학 - OpenLayers의 고급 기능 이해 및 실습 공간정보 거점대학 - OpenLayers의 고급 기능 이해 및 실습
공간정보 거점대학 - OpenLayers의 고급 기능 이해 및 실습
 
[FOSS4G Korea 2016] Workshop - Advanced GeoServer
[FOSS4G Korea 2016] Workshop - Advanced GeoServer[FOSS4G Korea 2016] Workshop - Advanced GeoServer
[FOSS4G Korea 2016] Workshop - Advanced GeoServer
 
오픈소스GIS를 활용한 서버기반 공간분석과 시각화
오픈소스GIS를 활용한 서버기반 공간분석과 시각화오픈소스GIS를 활용한 서버기반 공간분석과 시각화
오픈소스GIS를 활용한 서버기반 공간분석과 시각화
 
Java 기반의 오픈 소스 GIS를 지원하는 국내 공간 DBMS 드라이버 개발
Java 기반의 오픈 소스 GIS를 지원하는 국내 공간 DBMS 드라이버 개발Java 기반의 오픈 소스 GIS를 지원하는 국내 공간 DBMS 드라이버 개발
Java 기반의 오픈 소스 GIS를 지원하는 국내 공간 DBMS 드라이버 개발
 
[제86회 Open Technet]OGC 표준 기반의 공간자료 분석과 시각화 기술 개발
[제86회 Open Technet]OGC 표준 기반의 공간자료 분석과 시각화 기술 개발[제86회 Open Technet]OGC 표준 기반의 공간자료 분석과 시각화 기술 개발
[제86회 Open Technet]OGC 표준 기반의 공간자료 분석과 시각화 기술 개발
 
오픈소스GIS 개론 과정 - OpenLayers 기초
오픈소스GIS 개론 과정 - OpenLayers 기초오픈소스GIS 개론 과정 - OpenLayers 기초
오픈소스GIS 개론 과정 - OpenLayers 기초
 

CLI Geoprocessing GDAL/OGR

  • 1. GIS na GNU/Linux-u Darko Boto APIS IT d.o.o . GIS analitičar CLI Geoproccesing GDAL/OGR
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.  
  • 7.
  • 9. Prostorni podaci Vektorski podaci:   Arc/Info Binary Coverage, Arc/Info .E00 (ASCII) Coverage, Atlas BNA, AutoCAD DXF, CSV, ESRI Personal GeoDatabase, ESRI Shapefile, GeoJSON, GeoRSS, GML, GMT, GPX.... Simple features (OGC- http://www.opengeospatial.org/standards/sfa) POINT(6 10) LINESTRING(3 4,10 50,20 25) POLYGON((1 1,5 1,5 5,1 5,1 1),(2 2,2 3,3 3,3 2,2 2)) MULTIPOINT(3.5 5.6, 4.8 10.5) MULTILINESTRING((3 4,10 50,20 25),(-5 -8,-10 -8,-15 -4)) MULTIPOLYGON(((1 1,5 1,5 5,1 5,1 1),(2 2,2 3,3 3,3 2,2 2)),((6 3,9 2,9 4,6 3))) GEOMETRYCOLLECTION(POINT(4 6),LINESTRING(4 6,7 10)) POINT ZM (1 1 5 60) POINT M (1 1 80) Rasterski Podaci: Arc/Info ASCII Grid, Arc/Info Binary Grid, Bathymetry Attributed Grid, ESRI .hdr, Erdas Imagine Raw, Generic Binary (.hdr), Oracle Spatial GeoRaster,Graphics Interchange Format (.gif), GMT Compatible netCDF, GRASS Rasters, TIFF/BigTIFF/GeoTIFF (.tif), JPEG JFIF, JPEG2000, NetCDF, PNG, Netpbm (.ppm,.pgm), Rasterlite - Rasters in SQLite DB, SRTM HGT Format, Terralib, USGS ASCII DEM, OGC Web Coverage Server, WKTRaster, OGC Web Map Server.... $wget http://slobodniatlas.mapnix.org/~dboto/DORS_workshop.tar.gz && tar -xvzf DORS_workshop.tar.gz
  • 10.
  • 11.
  • 12. gdalinfo $gdalinfo ASTGTM_N44E015_dem.tif Driver: GTiff/GeoTIFF Files: ASTGTM_N44E015_dem.tif Size is 3601, 3601 Coordinate System is: GEOGCS[&quot;WGS 84&quot;,     DATUM[&quot;WGS_1984&quot;,         SPHEROID[&quot;WGS 84&quot;,6378137,298.2572235629972,             AUTHORITY[&quot;EPSG&quot;,&quot;7030&quot;]],         AUTHORITY[&quot;EPSG&quot;,&quot;6326&quot;]],     PRIMEM[&quot;Greenwich&quot;,0],     UNIT[&quot;degree&quot;,0.0174532925199433],     AUTHORITY[&quot;EPSG&quot;,&quot;4326&quot;]] Origin = (14.999861111111111,45.000138888888891) Pixel Size = (0.000277777777778,-0.000277777777778) Metadata:   AREA_OR_POINT=Area   TIFFTAG_DOCUMENTNAME=created at   TIFFTAG_IMAGEDESCRIPTION=SILC TIFF   TIFFTAG_SOFTWARE=IDL 6.3, Research Systems, Inc.   TIFFTAG_DATETIME=2008:10:27 20:50:05   TIFFTAG_XRESOLUTION=100   TIFFTAG_YRESOLUTION=100   TIFFTAG_RESOLUTIONUNIT=2 (pixels/inch) Image Structure Metadata:   INTERLEAVE=BAND Corner Coordinates: Upper Left  (  14.9998611,  45.0001389) ( 14d59'59.50&quot;E, 45d 0'0.50&quot;N) Lower Left  (  14.9998611,  43.9998611) ( 14d59'59.50&quot;E, 43d59'59.50&quot;N) Upper Right (  16.0001389,  45.0001389) ( 16d 0'0.50&quot;E, 45d 0'0.50&quot;N) Lower Right (  16.0001389,  43.9998611) ( 16d 0'0.50&quot;E, 43d59'59.50&quot;N) Center      (  15.5000000,  44.5000000) ( 15d30'0.00&quot;E, 44d30'0.00&quot;N) Band 1 Block=3601x1 Type=Int16, ColorInterp=Gray   < format zapisa   < veličina   <projekcija <EPSG kod - $ less /usr/share/proj/epsg | grep 4326 < metapodaci                 < koordinate extenta < type Int16
  • 13. gdal_translate $gdal_translate --formats   - mogućnost konverzije između 88 rasterskih formata sa kontrolom izlaznog formata (GeoTiff - defaultni izlazni format). ***Ukoliko fromat ne podržava pohranu metapodataka u samom formatu, metapodatke pohranjuje u xml datoteku. Konverzija TIFF-a u DEM $gdal_translate -of USGSDEM ASTGTM_N44E015_dem.tif translate_output1.dem   Konverzija sa kontrolom veličine outputa (resize) $gdal_translate -of USGSDEM -outsize 50% 50% ASTGTM_N44E015_dem.tif translate_output2.dem   Konverzija u 16bit-ni raster $gdal_translate -of PNG ot UInt16 ASTGTM_N44E015_dem.tif translate_output3.png   Konverzija u JPG + worldfile $gdal_translate -of JPEG -scale -co worldfile=yes ASTGTM_N44E015_dem.tif translate_output3.jpg   Konverzija u tekstualnu xyz datoteku $gdal2xyz.py ASTGTM_N44E015_dem.tif translate_output4.xyz   Jako velike mogućnosti pogledati: $man gdal_translate
  • 14. gdalwarp Omogućava izradu mozaika, reprojiciranje i prilagođavanje slike  koordinatnom sustavu sa kontrolom outputa . $gdalwarp -of GTiff -co &quot;TILED=YES&quot; -srcnodata 32767 -dstnodata 32767 -t_srs &quot;+proj=tmerc +ellps=bessel +k_o=0.9999 +x_0=5500000 +lon_0=15 +units=m +towgs84=519.729316,72.371421,492.743986,5.63370593,-3.33002924,-3.10746598,10.07015476&quot; -rcs -order 3 -tr 15 15 -wt Float32 -ot Float32 -wo SAMPLE_STEPS=100 ASTGTM_N44E015_dem.tif gdalwarp_output1.tif -t_srs - ciljani prostorno referentni sustav, -tr - rezolucija, -wo SAMPLE_STEPS - gustoća uzimanja uzoraka Primjer resempliranja gdalwarp -ts 3601 3601 -r cubicspline ASTGTM_N44E015_dem.tif gdalwarp_output2.tif Primjer isijecanja dijela rastera korištenjem poligona (gdalwarp + gdal_translate) #!/bin/bash SHPFILE=$1 BASE=`basename $SHPFILE .shp` EXTENT=`ogrinfo -so $SHPFILE $BASE | grep Extent | sed 's/Extent: //g' | sed 's/(//g' | sed 's/)//g' | sed 's/ - /, /g'` EXTENT=`echo $EXTENT | awk -F ',' '{print $1 &quot; &quot; $4 &quot; &quot; $3 &quot; &quot; $2}'` echo &quot;Clipping to $EXTENT&quot; RASTERFILE=$2 gdal_translate -projwin $EXTENT -of GTiff $RASTERFILE /tmp/`basename $RASTERFILE .tif`_bbclip.tif gdalwarp -co COMPRESS=DEFLATE -co TILED=YES -of GTiff -r lanczos -cutline $SHPFILE /tmp/`basename $RASTERFILE .tif`_bbclip.tif /tmp/`basename $RASTERFILE .tif`_shpclip.tif
  • 15. kolorizacija rastera #! /usr/bin/env python from osgeo import gdal import sys import numpy import os.path gdal.TermProgress = gdal.TermProgress_nocb src_file = sys.argv[1] dst_file = sys.argv[2] out_bands = 3 def MakeColor(z_value):   '''LUT for color ramp. Keys are pixel values, hash values are RGB triplets.'''   color_dict = {     0:[102,0,255],    25:[20,82,255],    50:[0,194,224],    75:[0,255,122],   100:[41,255,0],   125:[204,255,0],   150:[245,194,0],   175:[224,118,0],   200:[168,46,0],   225:[105,0,0],   250:[64,0,0],   255:[0,0,0]}   key_list = color_dict.keys()   key_list.sort()   while len(key_list) > 0:     last_val = key_list[-1]     if z_value >= last_val:       return color_dict[last_val]     else:       key_list.remove(last_val) # Print some info print &quot;Creating %s&quot; % (dst_file) # Open source file src_ds = gdal.Open( src_file ) src_band = src_ds.GetRasterBand(1) # create destination file dst_driver = gdal.GetDriverByName('GTiff') dst_ds = dst_driver.Create(dst_file, src_ds.RasterXSize, src_ds.RasterYSize, out_bands, gdal.GDT_Byte) # create output bands band1 = numpy.zeros([src_ds.RasterYSize, src_ds.RasterXSize]) band2 = numpy.zeros([src_ds.RasterYSize, src_ds.RasterXSize]) band3 = numpy.zeros([src_ds.RasterYSize, src_ds.RasterXSize]) # set the projection and georeferencing info dst_ds.SetProjection( src_ds.GetProjection() ) dst_ds.SetGeoTransform( src_ds.GetGeoTransform() ) # read the source file gdal.TermProgress( 0.0 ) for iY in range(src_ds.RasterYSize):   src_data = src_band.ReadAsArray(0,iY,src_ds.RasterXSize,1)   col_values = src_data[0]                         # array of z_values, one per row in source data   for iX in range(src_ds.RasterXSize):     z_value = col_values[iX]     # print z_value                                         # uncomment to see what value breaks color ramp     [R,G,B] = MakeColor(z_value)     band1[iY][iX] = R     band2[iY][iX] = G     band3[iY][iX] = B   gdal.TermProgress( (iY+1.0) / src_ds.RasterYSize ) # write each band out dst_ds.GetRasterBand(1).WriteArray(band1) dst_ds.GetRasterBand(2).WriteArray(band2) dst_ds.GetRasterBand(3).WriteArray(band3) dst_ds = None   #END script Ili jednostavnije:   $gdaldem color-relief ASTGTM_N44E015_dem.tif ramp.txt color.tif   gdje je ramp.txt 3500 white 2500 235:220:175 50% 190 185 135 700 240 250 150 0 50 180 50 nv 0 0 0 0 100%  255 255 255 75%   235 220 175 50%   190 185 135 25%   240 250 150 0%    50  180  50 nv    0   0   0
  • 16. gdal_contour Omogućava generiranje izo linija iz rastera sa visinskim podacima (SRTM, ASTER)   $gdal_contour -i 10 -snodata 32767 -a height ASTGTM_N44E015_dem.tif gdalwarp_output1.tif Primjer generiranja izo linija iz SRTM .hgt podataka shell skriptom: # povlacenje SRTM podataka (.hgt binary grid) sa NASA ftp servera. Lista za HR je u srtmhr_list.txt cat srtmhr_list.txt | sed 's!^!ftp://e0srp01u.ecs.nasa.gov/srtm/version2/SRTM3/Eurasia/!' | xargs -i wget -nc {} # apsolutna putanja do direktorija sa podacima DATA_PATH=&quot;&quot; # ulazi u direktorij sa podacima cd $DATA_PATH # za  svaki *hgt.zip izvrsava srtm_generate_hdr.sh http://mpa.itc.it/rs/srtm/srtm_generate_hdr.sh for X in *.hgt.zip do # skripta od .hgt fileova generira BIL headere i priprema podatke za GDAL         # GDAL nakon toga radi konverziju u GeoTIFF te napravi .prj za shp     yes | $DATA_PATH/srtm_generate_hdr.sh $X done # generiranje izohipsi iz pripremljenih tifova for X in $DATA_PATH/*.tif do     echo $X # izvlacenje izohipsi po visinskim podacima za 10, 50 i 100 m i spremanje u ESRI shape file (.shp)     gdal_contour -i 10 -snodata 32767 -a height $X ${X%%.tif}c10.shp     gdal_contour -i 50 -snodata 32767 -a height $X ${X%%.tif}c50.shp     gdal_contour -i 100 -snodata 32767 -a height $X ${X%%.tif}c100.shp done
  • 17.
  • 18.
  • 19.
  • 20.  
  • 21.
  • 22. ogrinfo $ogrinfo -al region_borders.shp | less $ogrinfo -ro region_borders.shp -sql &quot;SELECT * FROM region_borders WHERE CNTRYNAME='Croatia'&quot; | less INFO: Open of `region_borders.shp'       using driver `ESRI Shapefile' successful. Layer name: region_borders Geometry: Polygon Feature Count: 7 Extent: (6.625400, 35.491300) - (23.006200, 49.020300) Layer SRS WKT: GEOGCS[&quot;GCS_WGS_1984&quot;,     DATUM[&quot;WGS_1984&quot;,         SPHEROID[&quot;WGS_1984&quot;,6378137.0,298.257223563]],     PRIMEM[&quot;Greenwich&quot;,0.0],     UNIT[&quot;Degree&quot;,0.0174532925199433]] ObjectID: Integer (9.0) NATION: String (3.0) CNTRYNAME: String (25.0) CNTRYABB: String (3.0) SQKM: Real (19.5) COLORMAP: Real (11.0) OGRFeature(region_borders):0 < ime datoteke <tip geometrije < broj featurea < prostroni kvadrat (extent) < projekcija < ime/tip atribunog polja
  • 23. ogr2ogr $ogr2ogr --help (Mala pomoć za pregled mogućnosti alata) $ogr2ogr -f &quot;KML&quot; region_borders.kml region_borders.shp (Konverzija .shp datoteke u google .kml)   $ogr2ogr -f &quot;PostgreSQL&quot; PG:&quot;host=localhost user=user dbname=user password=user&quot; region_borders.kml -nln region_borders (Punjenje .kml datoteke u PostGIS baz) $ogr2ogr -f &quot;ESRI Shapefile&quot; pg_export.shp PG:&quot;host=localhost user=user dbname=user password=user&quot; &quot;region_borders&quot; (Export prostorne tablice u .shp datoteku)   $ogr2ogr -s_srs EPSG:4326 -t_srs EPSG:31275 rb_31275.shp region_borders.shp (Konverzija prostornog referentnog sustava)   $ogr2ogr -update -append -f PostgreSQL PG:dbname=user region_borders.shp (Update i append iz .shp datoteke u PostGIS bazu)
  • 24. ogr2ogr - merge (sh vs py) Merge shp datoteka - Shell #!/bin/bash mkdir merged find . -name 'contours*' -exec mv '{}' merged for i in $(ls *.shp);       do ogr2ogr -f 'ESRI Shapefile' -update -append merged $i -nln contours_merge done   Merge shp datoteka - Python   import sys import glob from osgeo import ogr outfile = &quot;merge.shp&quot; file_list = glob.glob(&quot;*.shp&quot;) # CREATE OUTPUT FILE out_driver = ogr.GetDriverByName( 'ESRI Shapefile' ) out_ds = out_driver.CreateDataSource(outfile) out_srs = None out_layer = out_ds.CreateLayer(&quot;trans&quot;, out_srs, ogr.wkbLineString) fd = ogr.FieldDefn('name', ogr.OFTString) out_layer.CreateField(fd) fd = ogr.FieldDefn('height', ogr.OFTInteger) out_layer.CreateField(fd) # READ EACH INPUT FILE AND WRITE FIELDS TO NEW FILE for shapefile in file_list:   print shapefile   [filename, extension] = shapefile.split('.')   in_drv = ogr.GetDriverByName( 'ESRI Shapefile' )   in_ds = in_drv.Open(shapefile)   in_layer = in_ds.GetLayer(0)   in_feature = in_layer.GetNextFeature()   height_field = in_feature.GetFieldIndex('height')   counter = 1   print height_field while in_feature is not None and (height_field != -1):     name = filename + &quot;_&quot; + `counter`     #print kV_field     #print in_feature.GetField     height = in_feature.GetField(height_field)     out_feat = ogr.Feature(out_layer.GetLayerDefn())     out_feat.SetField('name', name)     out_feat.SetField('height', height)     out_feat.SetGeometry(in_feature.GetGeometryRef().Clone())     out_layer.CreateFeature(out_feat)     out_layer.SyncToDisk()     out_feat.Destroy()     in_feature.Destroy()     in_feature = in_layer.GetNextFeature()     counter += 1 out_ds.Destroy()
  • 25.  
  • 26.
  • 27. Podešavanje baze Kreiranje baze i template-a (preporučujem) $ sudo su postgres $ createdb gistemplate Kreiranje PL/PSQL $ createlang plpgsql gistemplate Load funkcija i SRS tablice $ psql -d gistemplate -f /usr/share/postgresql-8.4-postgis/lwpostgis.sql $ psql -d gistemplate -f /usr/share/postgresql-8.4-postgis/spatial_ref_sys.sql Provjera $psql -d gistemplate -c &quot;SELECT postgis_full_version();&quot; Ulazimo u bazu  $ psql postgres=# Kreiranje grupe, dodjela role i grant postgres=# CREATE ROLE gisgroup NOSUPERUSER NOINHERIT CREATEDB NOCREATEROLE; postgres=# CREATE ROLE gis LOGIN PASSWORD 'gisuser' NOINHERIT; postgres=# GRANT gisgroup TO gis; postgres=#
  • 28. Dodjela ovlasti na prostorne tablice gis user-u i kreiranje sheme $ psql -d gistemplate gistemplate=# ALTER TABLE geometry_columns OWNER TO gis; gistemplate=# ALTER TABLE spatial_ref_sys OWNER TO gis; gistemplate=# CREATE SCHEMA gis_schema AUTHORIZATION gis; gistemplate=# Kreiranje baze rv_gis i priprema za load podataka $ createdb -T gistemplate -O gis rv_gis Dodavanje google projekcije u postgis INSERT into spatial_ref_sys (srid, auth_name, auth_srid, srtext, proj4text) values (900913 ,'EPSG',900913,'GEOGCS[&quot;WGS 84&quot;, DATUM[&quot;World Geodetic System 1984&quot;, SPHEROID[&quot;WGS 84&quot;, 6378137.0, 298.257223563,AUTHORITY[&quot;EPSG&quot;,&quot;7030&quot;]], AUTHORITY[&quot;EPSG&quot;,&quot;6326&quot;]],PRIMEM[&quot;Greenwich&quot;, 0.0, AUTHORITY[&quot;EPSG&quot;,&quot;8901&quot;]], NIT[&quot;degree&quot;,0.017453292519943295], AXIS[&quot;Longitude&quot;, EAST], AXIS[&quot;Latitude&quot;, NORTH],AUTHORITY[&quot;EPSG&quot;,&quot;4326&quot;]], PROJECTION[&quot;Mercator_1SP&quot;],PARAMETER[&quot;semi_minor&quot;, 6378137.0], PARAMETER[&quot;latitude_of_origin&quot;,0.0], PARAMETER[&quot;central_meridian&quot;, 0.0], PARAMETER[&quot;scale_factor&quot;,1.0], PARAMETER[&quot;false_easting&quot;, 0.0], PARAMETER[&quot;false_northing&quot;, 0.0],UNIT[&quot;m&quot;, 1.0], AXIS[&quot;x&quot;, EAST], AXIS[&quot;y&quot;, NORTH],AUTHORITY[&quot;EPSG&quot;,&quot;900913&quot;]] |','+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +no_defs'); Primjer kreiranja view-a za MapServer CREATE VIEW gradovi AS SELECT * FROM mjesta WHERE type LIKE 'Small' ORDER BY gid;
  • 29. Load shp2pgsql - data loader (može se koristiti i OGR sa podrškom za veći broj formata) $shp2pgsql – s <epsgcode> <ShapeFileName> <TableName> <dbName> > <filename> $psql -d <dbname> -f <filename>   $pgsql2shp -f croatia -h localhot -u user -P user gis &quot;SELECT name, wkb_geometry FROM region_borders WHERE name = 'CROATIA'&quot; Primjer $shp2pgsql -W &quot;ISO-8859-1&quot; -I -s 4326 region_borders gis_schema.region_borders | psql -d gis Napomena: Za naše dijakritičke znakove -W &quot;WINDOWS-1250&quot; EPSG referenca: $ less /usr/share/proj/epsg | grep 31275 <31275> +proj=tmerc +lat_0=0 +lon_0=15 +k=0.999900 +x_0=5500000 +y_0=0 +ellps=bessel +towgs84=577.326,90.129,463.919,5.137,1.474,5.297,2.4232 +units=m +no_defs  <> Export podataka iz baze pgsql2shp -f imefilea imebaze gis_schema.region_borders Prostorni upit za extent SELECT EXTENT(the_geom) FROM region_borders; Kreiranje prostorog indexa GIST (auto vacum i analyze) CREATE INDEX <indexname> ON <tablename> USING GIST ( <geometrycolumn> GIST_GEOMETRY_OPS ); Promjena tipa podatka na stupcu alter TABLE gis_schema.contours50 ALTER COLUMN height TYPE integer;
  • 30. Primjeri:   Kreiranje prostorne kolone - AddGeometryColumn SELECT AddGeometryColumn('public', 'region_borders', 'geom', 4326, 'POINT', 2); Kreiranje prostornog indeksa - GIST CREATE INDEX idx_region_borders_geom ON region_borders USING gist(geom); Izračunavanje površine - ST_Area SELECT neigh_name, ST_Area(geom) FROM region_borders ORDER BY ST_Area(geom) limit 1;   Pronađi sve objekte 100 meters od točke POINT(1000 1000) - ST_DWithin SELECT * FROM geotable WHERE ST_DWithin(geocolumn, ’ POINT(1000 1000) ’ , 100.0); select zup_naziv from gis_schema.zupanija where ST_DWithin(the_geom, 'POINT(5538942 5007927)',100.0); Ukupna dužina ceste izražena u kilometrima - ST_Length SELECT sum(ST_Length(the_geom))/1000 AS km_roads FROM roads; Selecija poligona koje se nalaze unutar drugog poligona ST_Within CREATE TABLE gis_schema.corine_cro AS SELECT gis_schema.corine.* FROM gis_schema.corine, gis_schema.granice WHERE ST_WITHIN (gis_schema.corine.the_geom, gis_schema.granice.the_geom) AND gis_schema.granice.CNTRYNAME = 'Croatia'; Simplifikacija - ST_Simplify CREATE TABLE gis_schema.border_genSimplify AS SELECT ST_SIMPLIFY(gis_schema.border_gen.the_geom,2) FROM gis_schema.border_gen; ST funkcije
  • 31.
  • 32.
  • 33.