abt 182 / hyd 182 environmental analysis using gis week 5-2 filters & analysis of vector data...

14
ABT 182 / HYD 182 Environmental Analysis using GIS Week 5-2 Filters & Analysis of vector data Functions & Modules

Upload: madeline-banks

Post on 12-Jan-2016

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: ABT 182 / HYD 182 Environmental Analysis using GIS Week 5-2 Filters & Analysis of vector data Functions & Modules

ABT 182 / HYD 182

Environmental Analysis using GIS

Week 5-2

Filters & Analysis of vector dataFunctions & Modules

Page 2: ABT 182 / HYD 182 Environmental Analysis using GIS Week 5-2 Filters & Analysis of vector data Functions & Modules

Driver ( definition of data structure )

| ------ Datasource (file / database)

|------ Layer (1 or more)

| ------ Field (several)

Feature (several)

| ------ Geometry

Field-values

OGR

Page 3: ABT 182 / HYD 182 Environmental Analysis using GIS Week 5-2 Filters & Analysis of vector data Functions & Modules

The Layer object has a method

SetAttributeFilter(<where_clause>)

>>> layer.GetFeatureCount()42>>> layer.SetAttributeFilter("cover = 'shrubs'")>>> layer.GetFeatureCount()6>>> layer.SetAttributeFilter(None)>>> layer.GetFeatureCount()42

Attribute filters

Page 4: ABT 182 / HYD 182 Environmental Analysis using GIS Week 5-2 Filters & Analysis of vector data Functions & Modules

• SetSpatialFilter(<geom>)

• SetSpatialFilterRect(<minx>, <miny>, <maxx>, <maxy>)

featAreas = layerAreas.GetNextFeature()poly = featAreas.GetGeometryRef()

layerSites.SetSpatialFilter(poly)

layerSites.SetSpatialFilterRect(460000, 4590000, 490000, 4600000)

layerSites.SetSpatialFilter(None)

See: http://www.gdal.org/ogr/classOGRLayer.html

Spatial filters

Page 5: ABT 182 / HYD 182 Environmental Analysis using GIS Week 5-2 Filters & Analysis of vector data Functions & Modules

q=("select * from sites where cover = 'grass' order by id desc")

layer = datasource.ExecuteSQL(q)layer.GetFeatureCount()layer.GetFeature(0).GetField(0)

feat = layer.GetNextFeature()while feat :

print feat.GetField('id')feat = layer.GetNextFeature()

datasource.ReleaseResultSet(layer)

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

SQL filters

Page 6: ABT 182 / HYD 182 Environmental Analysis using GIS Week 5-2 Filters & Analysis of vector data Functions & Modules

layer = ds.ExecuteSQL('select distinct cover from sites')

feat = layer.GetNextFeature()

while feat: q = "select count(*) from sites where cover = '" + coverFeat.GetField(0) + "'" count = ds.ExecuteSQL(q) print feat.GetField(0) + ' ' + cnt.GetFeature(0).GetFieldAsString(0) ds.ReleaseResultSet(count) feat = layer.GetNextFeature()

ds.ReleaseResultSet(coverLayer)

shrubs 6trees 11rocks 6grass 11bare 6water 2

Count of each unique value in a variable

Page 7: ABT 182 / HYD 182 Environmental Analysis using GIS Week 5-2 Filters & Analysis of vector data Functions & Modules

point1.Within(poly1)

line1.Intersect(line2)

line1.Touches(poly2)

Spatial queries (True / False)

Page 8: ABT 182 / HYD 182 Environmental Analysis using GIS Week 5-2 Filters & Analysis of vector data Functions & Modules

poly1.Intersection(poly2)

poly1.Union(poly2)

poly1.Difference(poly2)

poly1.SymmetricDifference(poly2)

poly1 poly2 Spatial queries

Page 9: ABT 182 / HYD 182 Environmental Analysis using GIS Week 5-2 Filters & Analysis of vector data Functions & Modules

Buffer a geometry

<geom>.Buffer(<distance>)

Distance between two geometries

<geom1>.Distance(<geom2>)

A geometry's extent (xminx, xmax, yminy, ymax)

<geom>.GetEnvelope()

Spatial queries

Page 10: ABT 182 / HYD 182 Environmental Analysis using GIS Week 5-2 Filters & Analysis of vector data Functions & Modules

a = 10b = 0

try: ds = driver.CreateDataSource(fn)finally: ds.Destroy

Try … finally

Page 11: ABT 182 / HYD 182 Environmental Analysis using GIS Week 5-2 Filters & Analysis of vector data Functions & Modules

import sys, traceback

a = 10b = 0

try: print a / bexcept 'e1': print a / (b * 2)except: print 'Exception type:', sys.exc_info()[0] print 'Exception value:', sys.exc_info()[1] traceback.print_exc()

finally: print 'done'

Try … except … except … finally

Page 12: ABT 182 / HYD 182 Environmental Analysis using GIS Week 5-2 Filters & Analysis of vector data Functions & Modules

def add(a, b): c = a + breturn c

print add(5, 1)

def add2(a, b=10): c = a + breturn c

print add2(5)

def distance(x1, y1, x2, y2): y = (y2 - y1)**2 x = (x2 - x1)**2 d = math.sqrt(x + y) return d

print distance(0,0,1,1)1.4142135623730951

Functions

Page 13: ABT 182 / HYD 182 Environmental Analysis using GIS Week 5-2 Filters & Analysis of vector data Functions & Modules

Put the function in a file e.g. mymod.py

import mymod

Python will look in:- the directory that the running script is in- then the PYTHONPATH environment variable- then possibly the current working directory- then in the standard library directories (site-packages)

import syssys.path.append('d:/temp')

If there is a function myFun inside:

mymod.myFun()

Import other modules at the top of your module

Putting function in a module

Page 14: ABT 182 / HYD 182 Environmental Analysis using GIS Week 5-2 Filters & Analysis of vector data Functions & Modules

* wildcard for multiple characters? wildcard for 1 character[] matches character ranges, like [0-9], [a-z], or [a,e,i,o,u]

Glob: Lists files that match a pattern

import globfiles = glob.glob('*.shp')

fn = files[0]newfn = fn.replace('.shp', '_new.shp')prjfn = newfn.replace('.shp', '.prj')

newfn = fn[:-4] + '_new.shp'prjfn = newfn[:-4] + '.prj'