Changes between Initial Version and Version 1 of UsersWikiOGR


Ignore:
Timestamp:
Apr 14, 2009, 11:05:20 AM (15 years ago)
Author:
pierre
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • UsersWikiOGR

    v1 v1  
     1= OGR Examples =
     2
     3
     4The OGR portion of GDAL/OGR supports PostGIS for read/write access.  OGR
     5tools 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.
     6
     7'''Loading Data with ogr2ogr'''
     8
     9Smple 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.
     10
     11{{{
     12 % ogr2ogr -f PostgreSQL PG:dbname=warmerda abc.shp
     13
     14}}}
     15
     16This 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.
     17
     18
     19{{{
     20 % ogr2ogr -f PostgreSQL PG:dbname=warmerda \
     21        gltp:/vrf/usr4/mpp1/v0eur/vmaplv0/eurnasia \
     22        -lco OVERWRITE=yes -nln polbndl_bnd 'polbndl@bnd(*)_line'
     23
     24}}}
     25
     26
     27In 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.
     28
     29
     30{{{
     31 % ogr2ogr -f PostgreSQL PG:dbname=warmerda tiger_michigan \
     32     -lco OVERWRITE=yes CompleteChain
     33 % ogr2ogr -update -append -f PostgreSQL PG:dbname=warmerda tiger_ohio \
     34     CompleteChain
     35
     36}}}
     37
     38
     39'''Queries with ogrinfo'''
     40
     41This 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.
     42
     43
     44{{{
     45 ogrinfo -ro PG:dbname=warmerda -sql "SELECT pop_1994 from canada where province_name = 'Alberta'"
     46
     47}}}
     48
     49
     50This example shows using ogrinfo to list PostgreSQL/PostGIS layers on a different host.
     51
     52{{{
     53 ogrinfo -ro PG:'host=myserver.velocet.ca user=postgres dbname=warmerda'
     54
     55}}}
     56
     57'''Extracts with ogr2ogr'''
     58
     59Convert a spatial table into a GML 2.x file.
     60
     61{{{
     62  ogr2ogr -f GML abc.gml PG:dbname=warmerda abc
     63
     64}}}
     65
     66
     67Extract only the province of Alberta to a shapefile, the default output format of
     68ogr2ogr.
     69
     70{{{
     71  ogr2ogr alberta.shp PG:dbname=warmerda canada -where "province_name = 'Alberta'"
     72
     73}}}
     74
     75
     76This example shows a small piece of python code that reads all data inside a postgis database (by Mauricio Carvalho):
     77
     78
     79{{{
     80import ogr
     81
     82def shptopg(filename):
     83    conn=ogr.Open("PG: host=localhost dbname="+filename+" user=postgres")
     84    #print myfile.GetDriver().GetName()
     85    for layer in conn:
     86        j=0
     87        geom=layer.GetFeature(j)
     88        while (geom):
     89            geom=layer.GetFeature(j)
     90            geom.DumpReadable()
     91            j+=1
     92    conn.Destroy()
     93
     94}}}
     95
     96This 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)
     97
     98
     99{{{
     100def getfeatures():
     101    sql = "select AsBinary(the_geom) as wkb_geometry from river AS r, state AS s
     102        WHERE intersects(r.the_geom, s.the_geom)"
     103    conn = ogr.Open("PG: host=localhost dbname='rivers' user='postgres'")
     104    layer = conn.ExecuteSQL(sql)
     105    print layer.GetFeatureCount()
     106    feature = layer.GetNextFeature()
     107    geography = feature.GetGeometryRef()
     108    print geography
     109
     110}}}