Changes between Initial Version and Version 1 of UsersWikiOGRExamples


Ignore:
Timestamp:
Apr 13, 2009, 2:08:59 PM (15 years ago)
Author:
pierre
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • UsersWikiOGRExamples

    v1 v1  
     1
     2= OGR Examples =
     3
     4
     5The OGR portion of GDAL/OGR supports PostGIS for read/write access.  OGR
     6tools like ogr2ogr can be used to load and extract from PostGIS.  The [http://www.gdal.org/ogr/drv_pg.html OGR PostgreSQL Driver] page describes details of the OGR PostGIS driver.  The [http://fwtools.maptools.org/ FWTools] builds include PostGIS enabled ogr2ogr binaries.
     7
     8'''Loading Data with ogr2ogr'''
     9
     10Smple translation of a shapefile into PostgreSQL. The table 'abc' will be created with the features from abc.shp and attributes from abc.dbf. The database instance (warmerda) must already exist, and the table abc must not already exist.
     11
     12{{{
     13 % ogr2ogr -f PostgreSQL PG:dbname=warmerda abc.shp
     14}}}
     15This second example loads a political boundaries layer from VPF (via the OGDI driver), and renames the layer from the cryptic OGDI layer name to something more sensible. If an existing table of the desired name exists it is overwritten.
     16
     17{{{
     18 % ogr2ogr -f PostgreSQL PG:dbname=warmerda \
     19        gltp:/vrf/usr4/mpp1/v0eur/vmaplv0/eurnasia \
     20        -lco OVERWRITE=yes -nln polbndl_bnd 'polbndl@bnd(*)_line'
     21}}}
     22
     23In this example we merge tiger line data from two different directories of tiger files into one table. Note that the second invocation uses -append and no OVERWRITE=yes.
     24
     25{{{
     26 % ogr2ogr -f PostgreSQL PG:dbname=warmerda tiger_michigan \
     27     -lco OVERWRITE=yes CompleteChain
     28 % ogr2ogr -update -append -f PostgreSQL PG:dbname=warmerda tiger_ohio \
     29     CompleteChain
     30}}}
     31
     32'''Queries with ogrinfo'''
     33
     34This example shows using ogrinfo to evaluate an SQL query statement within PostgreSQL. More sophisticated PostGIS specific queries may also be used via the -sql commandline switch to ogrinfo.
     35
     36{{{
     37 ogrinfo -ro PG:dbname=warmerda -sql "SELECT pop_1994 from canada where province_name = 'Alberta'"
     38}}}
     39
     40This example shows using ogrinfo to list PostgreSQL/PostGIS layers on a different host.
     41
     42{{{
     43 ogrinfo -ro PG:'host=myserver.velocet.ca user=postgres dbname=warmerda'
     44}}}
     45
     46'''Extracts with ogr2ogr'''
     47
     48Convert a spatial table into a GML 2.x file.
     49
     50{{{
     51  ogr2ogr -f GML abc.gml PG:dbname=warmerda abc
     52}}}
     53
     54Extract only the province of Alberta to a shapefile, the default output format of ogr2ogr.
     55
     56{{{
     57  ogr2ogr alberta.shp PG:dbname=warmerda canada -where "province_name = 'Alberta'"
     58}}}
     59
     60This example shows a small piece of python code that reads all data inside a postgis database (by Mauricio Carvalho):
     61
     62{{{
     63import ogr
     64
     65def shptopg(filename):
     66    conn=ogr.Open("PG: host=localhost dbname="+filename+" user=postgres")
     67    #print myfile.GetDriver().GetName()
     68    for layer in conn:
     69        j=0
     70        geom=layer.GetFeature(j)
     71        while (geom):
     72            geom=layer.GetFeature(j)
     73            geom.DumpReadable()
     74            j+=1
     75    conn.Destroy()
     76
     77}}}
     78
     79This second example shows how two tables can be joined.  PLease note the naming convention for the returned geometry column allows the Python OGR module to detect what column to use for geometry. If OGR cannot detect a geometry column the Feature will still be populated but the GetGeometryRef() will return "None".  (by Aaran Stent)
     80
     81{{{
     82def getfeatures():
     83    sql = "select AsBinary(the_geom) as wkb_geometry from river AS r, state AS s
     84        WHERE intersects(r.the_geom, s.the_geom)"
     85    conn = ogr.Open("PG: host=localhost dbname='rivers' user='postgres'")
     86    layer = conn.ExecuteSQL(sql)
     87    print layer.GetFeatureCount()
     88    feature = layer.GetNextFeature()
     89    geography = feature.GetGeometryRef()
     90    print geography
     91
     92}}}