140 likes | 249 Views
ABT 182 / HYD 182 Environmental Analysis using GIS Week 5-2. Filters & Analysis of vector data Functions & Modules. OGR. Driver ( definition of data structure ) | ------ Datasource (file / database) | ------ Layer (1 or more) | ------ Field (several)
E N D
ABT 182 / HYD 182 Environmental Analysis using GIS Week 5-2 Filters & Analysis of vector data Functions & Modules
OGR Driver( definition of data structure ) | ------ Datasource(file / database) | ------ Layer(1 or more) | ------ Field(several) Feature (several) | ------ Geometry Field-values
Attribute filters 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
Spatial filters • 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
SQL filters 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
Count of each unique value in a variable 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
Spatial queries (True / False) point1.Within(poly1) line1.Intersect(line2) line1.Touches(poly2)
poly1 poly2 Spatial queries poly1.Intersection(poly2) poly1.Union(poly2) poly1.Difference(poly2) poly1.SymmetricDifference(poly2)
Spatial queries Buffer a geometry <geom>.Buffer(<distance>) Distance between two geometries <geom1>.Distance(<geom2>) A geometry's extent (xminx, xmax, yminy, ymax) <geom>.GetEnvelope()
Try … finally a = 10 b = 0 try: ds = driver.CreateDataSource(fn) finally: ds.Destroy
Try … except … except … finally 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'
defadd(a, b): c = a + b return c print add(5, 1) defadd2(a, b=10): c = a + b return c print add2(5) defdistance(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
Putting function in a module 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
Glob: Lists files that match a pattern * wildcard for multiple characters ? wildcard for 1 character [] matches character ranges, like [0-9], [a-z], or [a,e,i,o,u] 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'