Skip to content

Instantly share code, notes, and snippets.

@arthur-e
Created August 22, 2013 21:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arthur-e/6313249 to your computer and use it in GitHub Desktop.
Save arthur-e/6313249 to your computer and use it in GitHub Desktop.

Introduction

Drivers

These are the exact names that OGR recognizes; in parentheses are the file extensions, not to be included in the driver name:

  • ESRI Shapefile (*.shp)
  • KML (*.kml)
  • AVCE00 (*.e00)
  • AVCBin
  • CSV (*.csv)
  • GeoJSON (*.json)
  • GML (*.gml)

Importing

try:
    from osgeo import ogr
    
except ImportError:
    import ogr

Finding a Driver for a Given Format

driver = ogr.GetDriverByName(‘Driver Code’)

Opening a Data Source and/or Layers

os.chdir(‘/usr/bin/blah/’)
dataSource = driver.Open(filename, 0) # 0: read-only, 1: writeable
layer = dataSource.GetLayer(index) # index is usually 0 (for SHP)

Creating a Data Source, Layers, and Features

# The filename argument is a string
dataSource = driver.CreateDataSource(filename)
layer = dataSource.CreateLayer(‘name’, geom_type=ogr.wkbPoint)

# After adding fields, get the layer definition
featureDefn = layer.GetLayerDefn()

# Create a feature from scratch
feature = ogr.Feature(featureDefn)

# Set the geometry for the new feature
feature.SetGeometry(point)

# Populate the attribute table
feature.SetField(‘field_name’, value)

# Field edits need to be set each time made
layer.SetFeature(feature)

# Finally, feature must be written to the layer
layer.CreateFeature(feature)

# Close data source at the very end with Destroy() so that everything is written to disk
dataSource.Destroy()

Getting Information about a Layer

# Count number of features
numFeatures = layer.GetFeatureCount()

# Returns geographic extent as a tuple where i=0: upper; i=1: left; i=2: right; i=3: lower
extent = layer.GetExtent()
extent[i]

Getting Features, Attributes, Geometry

# Grab a feature by its FID
feature = layer.GetFeature(fid)

# Grabs next feature every time called
feature = layer.GetNextFeature()
while feature:
	feature = layer.GetNextFeature()
    
# Call to reset pointer and loop again
layer.ResetReading()

# The name argument is the attribute name as a string
value = feature.GetField(name)
value = feature.GetFieldAsString(name)
value = feature.GetFieldAsInteger(name)

# Return the feature’s geometry (point, polygon...)
geometry = feature.GetGeometryRef()

# Point objects have GetX() and GetY() methods
x = geometry.GetX()
y = geometry.GetY()

Memory and Object Management

feature.Destroy()
dataSource.Destroy()

# Check that a file exists, delete the object
import os
if os.path.exists(filename):
	driver.DeleteDataSource(filename)

Adding Attribute Fields

# Shapefiles must be empty to add fields; field names have a 12-character limit
fieldDefn = feature.GetFieldDefnRef(‘field_name’) # fid also works in place of name

fieldDefn = ogr.FieldDefn(‘field_name’, ogr.OFTString) # Also: ogr.OFTInteger
fieldDefn.SetWidth(x) # Where argument x is an integer

# Create a field with the field definition
layer.CreateField(fieldDefn)

# Close data source at the very end with Destroy() so that everything is written to disk
dataSource.Destroy()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment