OGR introduction

OGR is part of GDAL, a library for reading and processing GIS vector data. OGR provides read and write support for vector data formats. It supports common ESRI Shapefile, PostGIS, Oracle Spatial, Mapinfo mid/mif, Mapinfo TAB and other file formats or data sources. More file formats supported by OGR are listed in the table Table 3 .

Table 3 Most used files format that OGR supported

Format name

Code

Can it be written?

Whether Projection is Supported

ESRI ArcObjects

ArcObjects

No

Yes

Arc/Info Binary Coverage

AVCBin

No

Yes

Arc/Info.E00 (ASCII) Coverage

AVCE00

No

Yes

AutoCAD DXF

DXF

Yes

No

Comma Separated Value (.csv)

CSV

Yes

No

ESRI Shapefile

ESRI Shapefile

Yes

Yes

Geomedia .mdb

Geomedia

No

No

Google Fusion Tables

GFT

Yes

Yes

GML

GML

Yes

Yes

GMT

GMT

Yes

Yes

GRASS

GRASS

No

Yes

Idrisi Vector (.VCT)

Idrisi

No

Yes

KML

KML

Yes

Yes

Mapinfo File

MapInfo File

Yes

Yes

Memory

Memory

Yes

Yes

PostgreSQL/PostGIS

PostgreSQL/PostGIS

Yes

Yes

SQLite/SpatiaLite

SQLite

Yes

Yes

VRT - Virtual Data Source

VRT

No

Yes

OGR’s Command Line Tools

  • ogrinfo : Print vector layer information

  • ogr2ogr : Vector data format conversion

Common command line parameters

You can view the data types supported by the OGR and whether the OGR can be read and written with the following commands.

$ ogrinfo  --formats
Supported Formats:
  -> "GRASS" (readonly)
  -> "ESRI Shapefile" (read/write)
  ... ...
  -> "Geoconcept" (read/write)

Examples of ogr2ogr commands

Let’s take a look at an ogr2ogr actual command:

$ ogr2ogr -f "CSV" output.csv /gdata/hyd2_4l.shp

The above code outputs Shapefile’s spatial data into CSV-formatted table data.

Look again at a program that converts Shapefile into a SpatiaLite database. The [ch_spatialite] chapter introduces the use of SpatiaLite and will generate the case database with the following command. The suffix used here is .db, which can be recognized as a SpatiaLite spatial database in a newer version of ArcGIS Desktop if it is suffixed with .sqlite.

$ ogr2ogr -f SQLite -dsco SPATIALITE=YES spalite.db \
   /gdata/region_popu.shp -nlt multipolygon

OGR Basic Classes in Python

In Python, the following classes are defined in the ogr module (Class):

  • Geometry Objects: The class Geometryencapsulates the Vector Data Model of OpenGIS and provides some geometric operations, such as the conversion between WKB and WKT formats, and the spatial reference system (projection).

  • Elements: The class Feature encapsulates the definition of a complete elements, including a geometric object and its set of properties.

  • Element Definition: Class `FeatureDefn’encapsulates the attributes, types, names and default spatial reference systems of elements. An element definition object usually corresponds to a layer.

  • Layer: The class Layeris an abstract base class that represents a layer of features in the data source.

  • Data Source: The class DataSourceis an abstract base class that represents a file or database containing layer objects.

  • Driver: The class Driver corresponds to each vector file format. The class Driverneeds to be registered for use.