Download presentation
Presentation is loading. Please wait.
Published byPercival Dickerson Modified over 8 years ago
1
OGR Simple Feature Library ABT 182 / HYD 182 Environmental Analysis using GIS Week 4-2
2
OGR Command line program Library (python, R, QGIS, ….) Supports many different vector formats Shapefiles, personal geodatabases, MapInfo, GRASS, KML Databases such as MySQL, PostgreSQL, Oracle Spatial try: from osgeo import ogr except: import ogr
3
http://www.gdal.org/ogr/
4
A driver knows a certain data type from osgeo import ogr driver = ogr.GetDriverByName('ESRI Shapefile') dataSource = driver.Open(fn, 0) layer = dataSource.GetLayer(0) print layer.GetFeatureCount() print layer.GetExtent() feature = layer.GetNextFeature() while feature: id = feature.GetField('id') geom = feature.GetGeometryRef() print geom.GetX() feature.Destroy() feature = layer.GetNextFeature() datasource.Destroy() 0 for read-only, 1 for writeable first layer (shp only has one) very similar to Arc scripts for point objects Reading a file with OGR
5
Writing a file with OGR dataSource = driver.Open(fn, 1) layer = dataSource.GetLayer(0) driver = ogr.GetDriverByName('ESRI Shapefile') if os.path.exists(fn): driver.DeleteDataSource(fn) dataSource = driver.CreateDataSource(fn) layer = dataSource.CreateLayer('test', geom_type=ogr.wkbPoint) Open an existing file Create a new file os.chdir('c:/abt182/lab8/') fn = 'test.shp'
6
fieldDefn = ogr.FieldDefn('id', ogr.OFTInteger) fieldDefn = ogr.FieldDefn('id', ogr.OFTString) fieldDefn.SetWidth(4) Defining new fields From an existing feature fieldDefn = feature.GetFieldDefnRef(0) fieldDefn = feature.GetFieldDefnRef('id') layer.CreateField(fieldDefn) From a new feature
7
featureDefn = layer.GetLayerDefn() feature = ogr.Feature(featureDefn) feature.SetGeometry( ) feature.SetField('id', 23) layer.CreateFeature(feature) Creating new features (after adding the fields)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.