wiki:Cookbook gdal

Version 28 (modified by cedric, 15 years ago) (diff)

--

source:EDA/data/Docs/trac/gdal.jpg

Using GDAL - Geospatial Data Abstraction Library and rgdal


back to Recipes for EDA CookBook Eda

gdal


gdal url:http://www.gdal.org/
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 utilities for data translation and processing.
Cédric => What I've found at this site is not really helpfull, it is only python and c++ code.
url:http://www.bostongis.com/?content_name=ogr_cheatsheet => Cedric: very good site !
url:http://trac.osgeo.org/gdal/ Cedric : not really usefull

Below we suppose that you have the Qgis tool installed, so your path is C:\OSGeo4W Qgis has gdal included. Gdal comes with two main tools (executable files), the files for gdal are in

C:\OSGeo4W\share\gdal

call >cmd

C:\OSGeo4W\bin\ogr2ogr

or

C:\OSGeo4W\bin\ogrinfo

Translating .mdb into shapes outside ESRI


C:\OSGeo4W\bin\ogr2ogr -f "ESRI Shapefile" C:\base\basesig\Czhyd.shp C:\base\basesig\Czhyd.mdb

Cedric : this one works but warning there should be no space in the path (ex : no "document and settings/"...)

Conversion from PostGIS to ESRI Shape

adapted from url: http://www.bostongis.com/?content_name=ogr_cheatsheet The pgsql2shp and shp2pgsql are usually the best tools for converting back and forth between PostGIS and ESRI for 2 main reasons.

  • It has fewer idiosyncracies when converting data.
  • It has a lot fewer dependencies so can fit on your floppy.

If you really want to use Ogr2Ogr for this kind of conversion, below is the standard way to do it Here the example should export the table riversegments
Cedric

C:\OSGeo4W\bin\ogr2ogr -f "ESRI Shapefile" C:\base\basesig\essai.shp PG:"host=localhost user=postgres dbname=CCM password=postgres port=5433" "riversegments"

Celine (pwd to be precised)

C:\OSGeo4W\bin\ogr2ogr -f "ESRI Shapefile" C:\base\basesig\essai.shp PG:"host=localhost user=postgres dbname=CCM password=PWD" "riversegments"

Cedric : celui ci marche mais j'ai eu du mal, ne marche pas une deuxième fois si les shape sont déjà crées dans le répertoire

C:\OSGeo4W\bin\ogr2ogr -f "ESRI Shapefile" C:\base\basesig\essai PG:"host=localhost user=postgres dbname=CCM password=postgres port=5433" "riversegments"

Cedric : OK marche de nouveau
Selecting specific fields, sets of data and Geometry
Sometimes you have more than one geometry field in a table, and ESRI shape can only support one geometry field per shape. Also you may only want a subset of data. In these cases, you will need to select the geometry field to use. The most flexible way to do this is to use the -sql command which will take any sql statement.

C:\OSGeo4W\bin\ogr2ogr -f "ESRI Shapefile" C:\base\basesig\essai1 PG:"host=locahost user=postgres dbname=CCM password=postgres port=5433" -sql "SELECT gid, the_geom FROM riversegments"

Cedric : ne marche pas, j'obtiens un message du type Unable to open datasource with following drivers, celui là on l'a très très souvent, j'ai essayé plusieurs trucs je ne comprends pas, la requete marche dans pgadmin

One way in which ogr2ogr excels above using the pgsql2shp tool is that ogr2ogr can export multiple tables at once. This is pretty handy for sharing your postgis data with others who do not have a postgis database.

The code below will export all your postgis tables out into a folder called mydatadump in ESRI shape (shp) format.

C:\OSGeo4W\bin\ogr2ogr -f "ESRI Shapefile" essai2 PG:"host=locahost user=postgres dbname=CCM password=postgres port=5433"

Now most of the time you probably only want to output a subset of your postgis tables rather than all your tables. This code exports only the riversegments and rivernodes tables to a folder called c:/base/basesig/mydatadump in ESRI shapefile format

C:\OSGeo4W\bin\ogr2ogr -f "ESRI Shapefile" c:/base/basesig/mydatadump PG:"host=localhost user=postgres dbname=CCM password=postgres port=5433" riversegments rivernodes

Cedric Celui là marche même si il n'y a pas de guillemet"

Connecting PostGIS with R using Rgdal


The rddal is made of functions which point out to the c code functions of gdal, it seems to have been developped for intamap eu project which is also an R library and the most interesting page can be found at the following location url: http://wiki.intamap.org/index.php/PostGIS et bien sûr ça marche pas ...
A nice example script follows which I translated into the following code

library(rgdal)
meuse = readOGR("PG:dbname=CCM", "riversegments")

but it doesn't work So after a quite long time spent in gdal, I can translate into the more complex dsn

require(rgdal)
readOGR(dsn=PG:"host=locahost user=postgres dbname=CCM password=postgres port=5433",layer="riversegments")

However the R invariably returns an absolutely not helpful code

> require(rgdal)
Le chargement a nécessité le package : rgdal
Le chargement a nécessité le package : sp
Geospatial Data Abstraction Library extensions to R successfully loaded
Loaded GDAL runtime: GDAL 1.6.2, released 2009/07/31
Path to GDAL shared files: C:/Program Files/R/R-2.10.0/library/rgdal/gdal
Loaded PROJ.4 runtime: Rel. 4.6.1, 21 August 2008
Path to PROJ.4 shared files: C:/Program Files/R/R-2.10.0/library/rgdal/proj
Message d'avis :
le package 'sp' a été compilé avec la version R 2.10.1 
> readOGR(dsn="PG:host=localhost dbname=CCM user=postgres passwd=postgres port=5433",layer="riversegments")
Erreur dans ogrInfo(dsn = dsn, layer = layer, input_field_name_encoding = input_field_name_encoding) : 
  Cannot open file

Interestingly it is the pointer to ogrinfo() which fails so we will try this with ogrinfo (which is found at the same place than ogr2ogr

C:\OSGeo4W\bin\ogrinfo -f "PostgreSQL" PG:"host=locahost user=postgres dbname=CCM password=postgres port=5433"

which returns

C:\base\basesig>C:\OSGeo4W\bin\ogrinfo -f "PostgreSQL" PG:"host=locahost user=po
stgres dbname=CCM password=postgres port=5433"
Usage: ogrinfo [--help-general] [-ro] [-q] [-where restricted_where]
               [-spat xmin ymin xmax ymax] [-fid fid]
               [-sql statement] [-al] [-so] [--formats]
               datasource_name [layer [layer ...]]

There does not seem to be any problem and also nothing is returned ... oh fuck

Similar content in rgal library in R but with less support
help for rgdal package
url:file:///C:/Program%20Files/R/R-2.10.0/library/rgdal/html/00Index.html

Céline : "J'ai recherché dans mes bouquin et sur le Applied Spatial Data Analysis with R de Roger Bivand dont tu peux retrouver (une partie) des codes sur ce site" : url:http://www.asdar-book.org/ Plus précisément sur cette page url:http://www.asdar-book.org/book/die.R ou à url:http://www.asdar-book.org/book/die_mod.R