Reading and Writing Vector Data With OGR Data With OGR: Open Source RS/GIS Python Week 1
Reading and Writing Vector Data With OGR Data With OGR: Open Source RS/GIS Python Week 1
Cons
Doesnt have the built in geoprocessor Smaller user community
OS Python week 1: Reading & writing vector data [2]
Related modules
Numeric
Sophisticated array manipulation (extremely useful for raster data!) This is the one well be using in class
NumPy
Next generation of Numeric Some of you might use this one if you work at home
Other modules
http://www.gispython.org/ hosts Python Cartographic Library looks like great stuff, but I haven't used it
Development environments
FWTools
Includes Python, Numeric, GDAL and OGR modules, along with other fun tools Just a suite of tools, not an IDE I like to use Crimson Editor, but this means no debugging tools
PythonWin
Have to install Numeric, GDAL and OGR individually
OS Python week 1: Reading & writing vector data [6]
Documentation
Python: http://www.python.org/doc/ GDAL: http://www.gdal.org/, gdal.py, gdalconst.py (in the fwtools/pymod folder) OGR: http://www.gdal.org/ogr/, ogr.py Numeric: http://numpy.scipy.org/#older_array NumPy: http://numpy.scipy.org/
OGR
Supports many different vector formats ESRI formats such as shapefiles, personal geodatabases and ArcSDE Other software such as MapInfo, GRASS, Microstation Open formats such as TIGER/Line, SDTS, GML, KML Databases such as MySQL, PostgreSQL, Oracle Spatial, Informix, ODBC
From http://www.gdal.org/ogr/ogr_formats.html
Format Name Code Arc/Info Binary Coverage AVCBin Arc/Info .E00 (ASCII) Coverage AVCE00 Atlas BNA BNA Comma Separated Value (.csv) CSV DODS/OPeNDAP DODS ESRI Personal GeoDatabase PGeo ESRI ArcSDE SDE ESRI Shapefile ESRI Shapefile FMEObjects Gateway FMEObjects Gateway GeoJSON GeoJSON Goconcept Export Geoconcept GeoRSS GeoRSS GML GML GMT GMT GPX GPX GRASS GRASS Informix DataBlade IDB INTERLIS Interlis 1 and "Interlis 2" INGRES INGRES KML KML Mapinfo File MapInfo File Microstation DGN DGN Memory Memory MySQL MySQL Oracle Spatial OCI ODBC ODBC OGDI Vectors OGDI PostgreSQL PostgreSQL S-57 (ENC) S57 SDTS SDTS SQLite SQLite UK .NTF UK. NTF U.S. Census TIGER/Line TIGER VRT - Virtual Datasource VRT X-Plane/Flighgear aeronautical data XPLANE Creation No No Yes Yes No No No Yes No No Yes Yes Yes Yes Yes No Yes Yes Yes Yes Yes Yes Yes No Yes No No Yes No No Yes No No No No Georeferencing Yes Yes No No Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes No No Yes No Yes No Yes Yes Yes Yes Yes Yes No Yes Yes Yes Yes Compiled by default Yes Yes Yes Yes No, needs libdap No, needs ODBC library No, needs ESRI SDE Yes No, needs FME Yes Yes Yes (read support needs libexpat) Yes (read support needs Xerces) Yes Yes (read support needs libexpat) No, needs libgrass No, needs Informix DataBlade Yes (INTERLIS model reading needs ili2c.jar) No, needs INGRESS Yes (read support needs libexpat) Yes Yes Yes No, needs MySQL library No, needs OCI library No, needs ODBC library No, needs OGDI library No, needs PostgreSQL library Yes Yes No, needs libsqlite3 Yes Yes Yes Yes
Available formats
The version we use in class doesnt support everything on the previous slide To see available formats use this command from the FWTools shell: ogrinfo --formats
Same syntax if using a shell other than FWTools and the gdal & ogr utilities are in your path otherwise provide the full path to ogrinfo
OS Python week 1: Reading & writing vector data [10]
Importing OGR
With FWTools:
import ogr
Might as well grab the driver for read operations so it is available for writing
1. Import the OGR module 2. Use ogr.GetDriverByName(<driver_code>)
import ogr driver = ogr.GetDriverByName('ESRI Shapefile')
Opening a DataSource
The Driver Open() method returns a DataSource object
Open(<filename>, <update>) where <update> is 0 for read-only, 1 for writeable
fn = 'f:/data/classes/python/data/sites.shp' dataSource = driver.Open(fn, 0) if dataSource is None: print 'Could not open ' + fn sys.exit(1) #exit with an error code
Getting features
If we know the FID (offset) of a feature, we can use GetFeature(<index>) on the Layer
feature = layer.GetFeature(0)
Destroying objects
For memory management purposes we need to make sure that we get rid of things such as features when done with them
feature.Destroy()
# script to count features # import modules import ogr, os, sys # set the working directory os.chdir('f:/data/classes/python/data') # get the driver driver = ogr.GetDriverByName('ESRI Shapefile') # open the data source datasource = driver.Open('sites.shp', 0) if datasource is None: print 'Could not open file' sys.exit(1) # get the data layer layer = datasource.GetLayer() # loop through the features and count them cnt = 0 feature = layer.GetNextFeature() while feature: cnt = cnt + 1 feature.Destroy() feature = layer.GetNextFeature() print 'There are ' + str(cnt) + ' features' # close the data source datasource.Destroy()
OS Python week 1: Reading & writing vector data [23]
To write a line to a file, where the string ends with a newline character:
file.write('This is my line.\n')
Assignment 1a
Read coordinates and attributes from a shapefile
Loop through the points in sites.shp
Write out id, x & y coordinates, and cover type for each point to a text file, one point per line
Hint: The two attribute fields in the shapefile are called "id" and "cover" Turn in your code and the output text file
Writing data
1. 2. 3. 4. 5. 6. Get or create a writeable layer Add fields if necessary Create a feature Populate the feature Add the feature to the layer Close the layer
on a DataSource object
ds = driver.CreateDataSource('test.shp') layer = ds.CreateLayer('test', geom_type=ogr.wkbPoint)
Adding fields
Cannot add fields to non-empty shapefiles Shapefiles need at least one attribute field Need a FieldDefn object first
Copy one from an existing feature with GetFieldDefnRef(<field_index>) or
GetFieldDefnRef(<field_name>)
Now create a field on the layer using the FieldDefn object and
CreateField(<FieldDefn>)
layer.CreateField(fieldDefn)
Make sure to close the DataSource with Destroy() at the end so things get written
OS Python week 1: Reading & writing vector data [35]
# script to copy first 10 points in a shapefile # import modules, set the working directory, and get the driver import ogr, os, sys os.chdir('f:/data/classes/python/data') driver = ogr.GetDriverByName('ESRI Shapefile') # open the input data source and get the layer inDS = driver.Open('sites.shp', 0) if inDS is None: print 'Could not open file' sys.exit(1) inLayer = inDS.GetLayer() # create a new data source and layer if os.path.exists('test.shp'): driver.DeleteDataSource('test.shp') outDS = driver.CreateDataSource('test.shp') if outDS is None: print 'Could not create file' sys.exit(1) outLayer = outDS.CreateLayer('test', geom_type=ogr.wkbPoint) # use the input FieldDefn to add a field to the output fieldDefn = inLayer.GetFeature(0).GetFieldDefnRef('id') outLayer.CreateField(fieldDefn)
OS Python week 1: Reading & writing vector data [36]
# get the FeatureDefn for the output layer featureDefn = outLayer.GetLayerDefn() # loop through the input features cnt = 0 inFeature = inLayer.GetNextFeature() while inFeature: # create a new feature outFeature = ogr.Feature(featureDefn) outFeature.SetGeometry(inFeature.GetGeometryRef()) outFeature.SetField('id', inFeature.GetField('id')) # add the feature to the output layer outLayer.CreateFeature(outFeature) # destroy the features inFeature.Destroy() outFeature.Destroy() # increment cnt and if we have to do more then keep looping cnt = cnt + 1 if cnt < 10: inFeature = inLayer.GetNextFeature() else: break # close the data sources inDS.Destroy() outDS.Destroy()
OS Python week 1: Reading & writing vector data [37]
Assignment 1b
Copy selected features from one shapefile to another
Create a new point shapefile and add an ID field Loop through the points in sites.shp
If the cover attribute for a point is trees then write that point out to the new shapefile
Turn in your code and a screenshot of the new shapefile being displayed
OS Python week 1: Reading & writing vector data [38]