Paragon Corpoation PostGIS Spatial Database Engine OSGeo.org The Open Source Geospatial Foundation UMN Mapserver Boston Geographic Information Systems       PostGreSQL Object Relational Database Management System
Home   GIS Books   About Boston GIS   Consulting Services  Boston GIS Blog  Postgres OnLine Journal  Planet PostGIS

Purpose of BostonGIS

BostonGIS is a testbed for GIS and Web Mapping solutions utilizing open source, freely available and/or open gis technologies. We will be using mostly Boston, Massachusetts data to provide mapping and spatial database examples.

If you have some thoughts or comments on what you would like to see covered on this site, drop us a line on our Feed Back page.


PostGIS in Action
check out book and download the first chapter and SQL Primer for free.

Tips and Tricks for PostGIS

PostgreSQL: Up and Running
PostgreSQL: Up and RunningIt covers PostgreSQL 8.4-9.2 with special focus on features introduced in PostgreSQL 9.1-9.2

GIS Tutorials on Opensource and OpenGIS technologies Tutorials
GIS Article comments Article and Tutorial Comments
Boston GIS BLog Rss FeedBoston GIS blog

PDF HTML All BostonGIS tutorials packaged together in an E-Book.


Boston GIS Store

Loading


Tutorial and Tip Sites
Desktop GIS
External Data
GIS Events and Groups
GIS SDKs and Frameworks
External Resources
Glossary
GIS Blogs Around Boston
External GIS Blogs
External Papers Articles
GIS Quick Guides and References
OpenStreetMap and OpenLayers Tutorials
PostGIS, pgRouting, and PostgreSQL Tutorials
pgRouting: Loading OpenStreetMap with Osm2Po and route querying more ...
Part 1: Getting Started With PostGIS: An almost Idiot's Guide (PostGIS 2.0) more ...
Part 1: Getting Started With PostGIS: An almost Idiot's Guide (PostGIS 1.5) more ...
OSCON 2009: Tips and Tricks for Writing PostGIS Spatial Queries more ...
PGCon2009: PostGIS 1.4, PostgreSQL 8.4 Spatial Analysis Queries, Building Geometries, Open Jump more ...
PLR Part 3: PL/R and Geospatial Data Abstraction Library (GDAL) RGDAL more ...
PostGIS Nearest Neighbor: A Generic Solution - Much Faster than Previous Solution more ...
Solving the Nearest Neighbor Problem in PostGIS more ...
PLR Part 2: PL/R and PostGIS more ...
PLR Part 1: Up and Running with PL/R (PLR) in PostgreSQL: An almost Idiot's Guide more ...
Part 2 - PostGIS and SharpMap in ASP.NET 2.0 using VB.NET: Displaying the Maps more ... download
Part 1 - PostGIS and SharpMap in ASP.NET 2.0 using VB.NET: Compiling SharpMap with PostGIS more ...
Part 3: PostGIS Loading Data from Non-Spatial Sources more ...
Part 2: Introduction to Spatial Queries and SFSQL with PostGIS more ...
Miscellaneous Tutorials/Cheatsheets/Examples
OGR2OGR Cheatsheet

Printer Friendly

OGR Tools

The OGR toolkit is a subkit of the FW Tools Toolkit. The FW Tools Toolkit is a toolkit available in both Linux and Windows executable form as well as source code form. It has several command line tools. The ones we find most useful are:

  • OgrInfo - inspects a GIS datasource and spits out summary data or detailed information about the layers, kinds of geometries found in the file.
  • Ogr2Ogr - this is a command line tool that converts one Ogr defined data source to another Ogr data source. Ogr supports multiple data formats: ESRI Shapefile, MapInfo Tab file, TIGER, s57, DGN, CSV, DBF, GML, KML, Interlis, SQLite, ODBC, ESRI GeoDatabase (MDB format), PostGIS/PostgreSQL, MySQL .

These 2 command line tools can be found in the bin folder of your FWTools install. To start using these tools

  1. Install the FW Tools tool kit.
  2. Launch the FW Tools Shell - in windows this is found under Start->Programs->FW Tools ..
  3. From the shell - cd into your directory that has the data you want to convert
  4. Getting more Help

    If you want a comprehensive listing of options offered by ogr2ogr or ogrinfo, run the following at the FW Tools Shell.

    ogr2ogr --help
    ogrinfo --help
    
    

    Conversions from MapInfo to Other formats

    Conversion from MapInfo to ESRI Shape

    ogr2ogr -f "ESRI Shapefile" mydata.shp mydata.tab

    Conversion from MapInfo to PostGIS

    ogr2ogr -f "PostgreSQL" PG:"host=myhost user=myloginname dbname=mydbname password=mypassword" mytabfile.tab

    Note: for the above, you can leave out the host if its localhost and user and password if you have your authentication set to trust.

    Importing as a different table name

    In the below example, we don't want OGR to create a table called mytable. We instead want to call the table something different like newtablename. To do so we use the nln option.

    ogr2ogr -f "PostgreSQL" PG:"host=myhost user=myloginname dbname=mydbname password=mypassword" mytabfile.tab -nln newtablename

    When OGR guesses wrong or fails to guess

    Sometimes OGR does not output the right projection, particularly with Units of Feet or data that has no projection info or the projection information can't be easily translated to your system. Sometimes OGR can't match the projection to one in your spatial_ref_sys table so creates a new entry in that table. In these cases you have to tell OGR what the output projection is. You do this with the -a_srs flag.

    ogr2ogr -f "PostgreSQL" -a_srs "EPSG:2249" PG:"host=myhost user=myloginname dbname=mydbname password=mypassword" mytabfile.tab

    In the above example I told OGR2OGR to assume the source/output projection is in Massachusetts Mainland US Ft. Note: All Spatial Ref Systems can be found in the spatial_ref_sys table of PostGIS or the Data/gcs.csv file of your FW Tools install.

    Conversions from PostGIS to Other formats

    Conversion from PostGIS to ESRI Shape

    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


    ogr2ogr -f "ESRI Shapefile" mydata.shp PG:"host=myhost user=myloginname dbname=mydbname password=mypassword" "mytable"

    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.


    ogr2ogr -f "ESRI Shapefile" mydata.shp PG:"host=myhost user=myloginname dbname=mydbname password=mypassword" -sql "SELECT name, the_geom FROM neighborhoods"

    Example snippet converting from PostGIS to KML

    ogr2ogr -f "KML" neighborhoods.kml PG:"host=myhost user=myloginname dbname=mydbname password=mypassword" -sql "select gid, name, the_geom from neighborhoods"
    The below outputs the neighborhood table to KML and sets the KML description field to name of neighborhood
    ogr2ogr -f "KML" neighborhoods.kml PG:"host=myhost user=myloginname dbname=mydbname password=mypassword" neighborhoods -dsco NameField=name

    Exporting multiple tables from PostGIS using Ogr2Ogr

    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.

    ogr2ogr -f "ESRI Shapefile" mydatadump PG:"host=myhost user=myloginname dbname=mydbname password=mypassword"

    The code below will export all your postgis tables out into a folder called mydatadump in MapInfo .tab format.

    ogr2ogr -f "MapInfo File" mydatadump PG:"host=myhost user=myloginname dbname=mydbname password=mypassword"

    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 neighborhoods and parcels tables to a folder called mydatadump in ESRI shapefile format

    ogr2ogr -f "ESRI Shapefile" mydatadump PG:"host=myhost user=myloginname dbname=mydbname password=mypassword" neighborhood parcels

    Conversion from TIGER to other formats

    Topologically Integrated Geographic Encoding and Referencing system (TIGER) is the US Census Bureaus proprietary format for exporting US Census geographic and statistical data. Starting in 2007, they will be using ESRI Shapefile (SHP) as there official export format. So this section may be a bit obsolete for the upcoming versions.

    To get the files for your location - you can browse their archive at http://www.census.gov/geo/www/tiger/index.html

    Reading the meta data using ogrinfo


    ogrinfo TGR25025.RTI

    Conversion from Tiger to ESRI shape

    Give me all layers in TGR25025

    The tiger files contain a set of layers, so unlike the other outputs we have done, we will specify a folder to dump all the layers into

    ogr2ogr -f "ESRI Shapefile" masuffolk TGR25025.RTI

    Note: The above outputs all the tiger layers in the TGR25025 set into a folder called masuffolk that resides within our data folder that we have cded to.

    Just One Layer

    ogr2ogr -f "ESRI Shapefile" sufcomp.shp TGR25025.RT1 layer CompleteChain

    In the above, we are asking for just the CompleteChain layer and to output to a new file called sufcomp.shp. Note it will output shp and the corresponding shx, and prj files.

    Conversion from TIGER to MapInfo

    The conversion follows a similar path to ESRI Shape

    All Layers - Each Layer as a single file

    The below will create a folder masuf and output all the layers into that folder and give each a tab file extension


    ogr2ogr -f "MapInfo File" masuf TGR25025.RT1

    Single Layer - Single File

    ogr2ogr -f "MapInfo File" sufcomp.tab TGR25025.RT1 layer CompleteChain

    Conversion from Tiger to PostGIS

    ogr2ogr -update -append -f "PostGreSQL" PG:"host=myserver user=myusername dbname=mydbname password=mypassword" TGR25025.RT1 layer CompleteChain -nln masuf -a_srs "EPSG:4269"

    Note in the above we needed to put the -update -append option because OGR2OGR will try to create a folder if given a file with no extension, which translates to creating a new database.

    We also put in the -nln masuf to prevent OGR2OGR from creating the table name as CompleteChain

    Lastly we put in EPSG:4269 to tell OGR to just assume Tiger is in NAD 83 long lat. Without this it creates an entry in the spatial_ref_sys table which is equivalent to the already existing well known 4269.

    ESRI GeoDatabase (MDB format)

    FWTools at least for windows, has the ESRI Geodatabase driver (referred to as PGeo for Personal Geodatabase) baked into the binary. This makes it relatively simple to export data out of the Personal Geodatabase format into other formats such as PostgreSQL or ESRI Shape

    If you want a sample geo database to play with, try the MassGIS annotation personal geo database http://www.mass.gov/mgis/ftpgnm.htm

    See list of Feature Classes

    ogrinfo C:\GISData\Geonames.mdb
    INFO: Open of `c:\GISData\Geonames.mdb'
          using driver `PGeo' successful.
    1: GEONAMES_ANNO_PLACES
    2: GEONAMES_ANNO_HYDRO
    3: GEONAMES_ANNO_HYPSO
    

    Import Personal Geodatabase to PostGIS

    To import all feature classes and assign a particular spatial ref

    ogr2ogr -f "PostgreSQL" PG:"host=localhost user=someuser dbname=somedb password=somepassword port=5432" C:\GISData\Geonames.mdb -a_srs EPSG:26986

    Import 1 feature class and reproject and rename geometry column. This example brings in a feature class that is of projection NAD 83 MA meters and transforms to NAD 83 longlat and also renames the feature class ma_hydro

    ogr2ogr -f "PostgreSQL" PG:"host=localhost user=someuser dbname=somedb" C:\Data\Geonames.mdb GEONAMES_ANNO_HYDRO -a_srs EPSG: 26986 -t_srs EPSG:4269 -nln ma_hydro -lco GEOMETRY_NAME=the_geom_4269

    Importing from ESRI Shape to MySQL

    MySQL 4 and 5 support spatial objects to a limited extent. The functionality is not as rich as PostGIS, but for basic mapping work and limited spatial analysis, and if you already have MySQL installed, it may suit your needs just fine. Below is a snippet of code that will import a shape file called world_adm0.shp into a MySQL database and will rename it world. NOTE: that if you have a virgin MySQL database with no geometry_columns or spatial_ref_sys table, then those will automatically be created.

    ogr2ogr -f "MySQL" MYSQL:"mydb,host=myhost,user=mylogin,password=mypassword,port=3306" -nln "world" -a_srs "EPSG:4326" path/to/world_adm0.shp

    Using OGR to Import Non-spatial Data

    While OGR was designed primarily to transform data between different spatial datasources, it is a little known fact that it can be used as well for importing non-spatial datasources such as Dbase files and CSV files.

    Importing Dbase file into PostgreSQL using Ogr2Ogr

    In the below example, we will demonstrate importing a Dbase file into PostgreSQL using OGR2OGR

    ogr2ogr -f "PostgreSQL" PG:"host=myserver user=myusername dbname=mydbname password=mypassword" sometable.dbf -nln "sometable"

    download

    Post Comments About OGR2OGR Cheatsheet
SpatiaLite Tutorials
Boston External Map Examples
SQL Server 2008 Tutorials
UMN Mapserver Tutorials
General Commentary
 CommenterComment
8/26/2012 7:48:01 PMReginaI don't have any familiarity with the Oracle OGR2OGR driver, but perhaps you can set the PGCLIENTENCODING environment variable
So if you are using a batch or sh, you would do something like
-- change latin1 to your encoding
PGCLIENTENCODING=latin1
ogr2ogr restofyourcommand

not sure if you need to do something similar for Oracle, though I don't see that variable documented if there is one.


8/7/2012 5:49:19 AMCorne@Stefsi
Do you have a solution already?
We have the same problem from Oracle to PostGIS
3/15/2012 4:26:12 AMStefsiWe're using ogr2ogr for exporting from PostGIS to Oracle Spatial. Export works fine, but there are problems with the national charactersets (we got german, french and arabian). The are all fine in PostGIS but after using ogr2ogr they are not displayed in Oracle (also the right characterset ist specified in Oracle).
Any tipps to solve the problem?
Thank you.
2/20/2012 8:34:45 AMIan PackhamCan the OGR2OGR tool preserve relationships between tables? In particular I have an ESRI mdb file that contains a number of "Has" relationships, but after throwing the whole database into PostGIS, I can't see the relationships, at least not where I would expect as a constraint. I also cannot see an option to preserve constraints in OGR2OGR.
11/16/2011 6:14:14 PMHeidi OchisAny ideas on when or if the ESRI GeoDatabase(gdb)format will be included in the OGR2OGR conversion list?
7/6/2011 4:55:58 AMRobHello,
I am trying to convert a .shp file to a .tab file whilst also only selecting records that have a date field that equals within the last 30 days.

However I am struggling to filter the data at all on the date field. I have tried using "-where" & "-sql" and I cannot filter the data even on a particular date. I think I must be using the wrong format for dates, but I've tried every combination I can think of.

Does anyone have any examples of working "-where" or "-sql" clauses on date fields?

Thanks
5/18/2011 12:29:32 PMbespinI'm running 2008 SP1, not R2. I did find that if you include the table name as part of the conn string it works. It won't let me use -sql though so it's a bit less flexible.

This works : http://pastebin.com/kTDuYg1a
5/18/2011 3:24:46 AMReginaSorry -- no haven't tried. Are you running MSSQL 2008 plain or MSSQL 2008 R2. I recall people having issues with another loader I forget which when using against R2. So I presume there must have been changes between the 2 releases.
5/16/2011 2:55:07 PMbespinHave you tried using the latest ogr2ogr (I'm running it from OSGeo4W) with MSSQL 2008 geometery? I'm having problems doing a simple dump to shp. The syntax is similar to what you have for PG.

Every row returns the error "ERROR 1: GetNextRawFeature(): Corrupt data" The shp comes out with attributes and no geometry.
4/28/2011 8:07:54 AMReginaGarret,

I think you have your arguments confused. a_srs is the assumed projection and t_srs is what you want to transform it to.

If it still doesn't work make sure OGR2OGR can find you epsg file. As I recall I think the environment variable is GDAL_DATA though install should have put that in.
4/27/2011 3:31:48 PMgarrethi, great software. However, I cannot get -a_srs to work properly. I've specifed EPSG:29902 but when I add the PostGIS layer in QGIS, the projection is always geographic WGS84... so I have to manually specify the projection.
4/25/2011 11:11:12 AMReginaEriovaldo,

You need to set the encoding.

Try setting the environment variable
something like this.

export PGCLIENTENCODING=latin1

On windows 7 set it in system -> Control Panel -> System -> Advanced System Settings -> Environment variables
4/21/2011 3:03:57 PMEriovaldoI am using ogr2ogr and I got in trouble with the tool running on DOS - Windows 7.
The command does not work fine when using special characters.

For example:

Command: INSERT INTO "spatial_treina"."table" ("id", "name")
VALUES (5470, '231260', 'SÒo LuÝs do Curu')
RETURNING ogc_fid
ERROR 1: INSERT command for new feature failed.
ERRO: sequ+¬ncia de bytes +® inv+ílida para codifica+º+úo "UTF8": 0xe36f20

the correct should be: São Luís do Curu.

How can I configure the DOS in order do not generate this kind of character.
1/21/2010 10:07:18 PMChris McCarthyThe code suggestion for converting MapInfo to PostGIS didn't work for me because a quotation mark was in the wrong place. It should read:

ogr2ogr -f "PostgreSQL" "PG:host=myhost user=myloginname dbname=mydbname password=mypassword" mytabfile.tab

Note the double quote after PG: should be before PG:.

This dogged me for a whole day.
7/17/2009 5:32:28 PMReginaI'm not sure its possible with plain OGR2OGR. I know its doable in UMN Mapserver using the OGR2OGR driver so I assume it must be possible. I'll experiment with this and see what I come up with.

Is output the folder where your shape and dbf files reside? I suspect for this to work, you need to specifiy the folder name not the shapefile name as you would normally.

Here is an example of how its done in UMN Mapserver

http://www.mail-archive.com/mapserver-users@lists.umn.edu/msg13477.html
7/10/2009 7:32:07 PMKRDI would like to see the syntax for an OGR2OGR join involving a shapefile and a dbf lookup table. The following syntax should work, but does not:

SELECT * FROM table1, table2 WHERE table1.field1=table2.fieldA

Neither JOIN nor LEFT JOIN syntax works either.

An example of what I really want to do is:

ogr2ogr -sql "SELECT * FROM soils LEFT JOIN master ON soils.MUKEY = master.MUKEY" output soils.shp

If I can get the join working, then I can move on to enhance the operation to select only certain field and records (shapes)

Thanks,
Locations of visitors to BostonGIS
Boston GIS      Copyright 2014      Paragon Corporation