ABT 182  / HYD 182
This presentation is the property of its rightful owner.
Sponsored Links
1 / 7

OGR Simple Feature Library PowerPoint PPT Presentation


  • 97 Views
  • Uploaded on
  • Presentation posted in: General

ABT 182 / HYD 182 Environmental Analysis using GIS Week 4-2. OGR Simple Feature Library. OGR Command line program Library (python, R, QGIS, ….) Supports many different vector formats Shapefiles, personal geodatabases, MapInfo, GRASS, KML

Download Presentation

OGR Simple Feature Library

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Ogr simple feature library

ABT 182 / HYD 182

Environmental Analysis using GIS

Week 4-2

OGR Simple Feature Library


Ogr simple feature library

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


Ogr simple feature library

http://www.gdal.org/ogr/


Ogr simple feature library

Reading a file with OGR

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()

A driver knows a certain data type

0 for read-only, 1 for writeable

first layer (shp only has one)

very similar to Arc scripts

for point objects


Ogr simple feature library

Writing a file with OGR

os.chdir('c:/abt182/lab8/')

fn = 'test.shp'

Open an existing file

dataSource = driver.Open(fn, 1)

layer = dataSource.GetLayer(0)

Create a new file

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)


Ogr simple feature library

Defining new fields

From an existing feature

fieldDefn = feature.GetFieldDefnRef(0)

fieldDefn = feature.GetFieldDefnRef('id')

From a new feature

fieldDefn = ogr.FieldDefn('id', ogr.OFTInteger)

fieldDefn = ogr.FieldDefn('id', ogr.OFTString)

fieldDefn.SetWidth(4)

layer.CreateField(fieldDefn)


Ogr simple feature library

Creating new features

(after adding the fields)

featureDefn = layer.GetLayerDefn()

feature = ogr.Feature(featureDefn)

feature.SetGeometry(<object>)

feature.SetField('id', 23)

layer.CreateFeature(feature)


  • Login