1 / 14

ABT 182 / HYD 182 Environmental Analysis using GIS Week 5-2

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)

brygid
Download Presentation

ABT 182 / HYD 182 Environmental Analysis using GIS Week 5-2

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

  3. 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

  4. 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

  5. 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

  6. 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

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

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

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

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

  11. 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'

  12. 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

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

  14. 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'

More Related