>> layer.GetFeatureCount() 6 >>> layer.SetAttributeFilter(None) >>> layer.GetFeatureCount() 42 Attribute filters"> >> layer.GetFeatureCount() 6 >>> layer.SetAttributeFilter(None) >>> layer.GetFeatureCount() 42 Attribute filters">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "ABT 182 / HYD 182 Environmental Analysis using GIS Week 5-2 Filters & Analysis of vector data Functions & Modules."— Presentation transcript:

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

2 Driver ( definition of data structure ) | ------ Datasource (file / database) | ------ Layer (1 or more) | ------ Field (several) Feature (several) | ------ Geometry Field-values OGR

3 The Layer object has a method SetAttributeFilter( ) >>> layer.GetFeatureCount() 42 >>> layer.SetAttributeFilter("cover = 'shrubs'") >>> layer.GetFeatureCount() 6 >>> layer.SetAttributeFilter(None) >>> layer.GetFeatureCount() 42 Attribute filters

4 SetSpatialFilter( ) SetSpatialFilterRect(,,, ) 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.htmlhttp://www.gdal.org/ogr/classOGRLayer.html Spatial filters

5 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

6 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 6 trees 11 rocks 6 grass 11 bare 6 water 2 Count of each unique value in a variable

7 point1.Within(poly1) line1.Intersect(line2) line1.Touches(poly2) Spatial queries (True / False)

8 poly1.Intersection(poly2) poly1.Union(poly2) poly1.Difference(poly2) poly1.SymmetricDifference(poly2) poly1poly2 Spatial queries

9 Buffer a geometry.Buffer( ) Distance between two geometries.Distance( ) A geometry's extent (xminx, xmax, yminy, ymax).GetEnvelope() Spatial queries

10 a = 10 b = 0 try: ds = driver.CreateDataSource(fn) finally: ds.Destroy Try … finally

11 import sys, traceback a = 10 b = 0 try: print a / b except '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

12 def add(a, b): c = a + b return c print add(5, 1) def add2(a, b=10): c = a + b return 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

13 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 sys sys.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

14 * 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 glob files = 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'


Download ppt "ABT 182 / HYD 182 Environmental Analysis using GIS Week 5-2 Filters & Analysis of vector data Functions & Modules."

Similar presentations


Ads by Google