area_field = ogr.FieldDefn('area', ogr.OFTReal) #A area_field.SetWidth(30) #A area_field.SetPrecision(4) location_field = ogr.FieldDefn('location', ogr.OFTString) #A poly_lyr.CreateFields([id_field, area_field, location_field]) poly_row = ogr.Feature(poly_lyr.GetLayerDefn()) land_lyr = ds.GetLayerByName('land_lambert') #A land_row = next(land_lyr) #A land_poly = land_row.geometry().Buffer(100000) #A for tag_id in ch7funcs.get_unique(ds, 'albatross_lambert', 'tag_id'): print('Processing ' + tag_id) pt_lyr.SetAttributeFilter("tag_id = '{}'".format(tag_id)) pt_locations = ogr.Geometry(ogr.wkbMultiPoint) last_location = None for pt_row in pt_lyr: pt = pt_row.geometry().Clone() if not land_poly.Contains(pt): #B continue #B if pt.GetX() < -2800000: #C location = 'island' #C else: #C location = 'mainland' #C if location != last_location: #D if pt_locations.GetGeometryCount() > 2: homerange = pt_locations.ConvexHull()
# Calculate distance between adjacent points. from osgeo import ogr import ch7funcs # Open the layer and add a distance field. ds = ogr.Open(r'D:\osgeopy-2001\Galapagos', True) lyr = ds.GetLayerByName('albatross_lambert') lyr.CreateField(ogr.FieldDefn('distance', ogr.OFTReal)) # Get the unique tags. Notice this uses the ch7funcs module but the text # assumes the function is in the same script. tag_ids = ch7funcs.get_unique(ds, 'albatross_lambert', 'tag_id') # Loop through the IDs. for tag_id in tag_ids: print('Processing ' + tag_id) # Limit the GPS points to the ones with the current tag ID. lyr.SetAttributeFilter("tag_id ='{}'".format(tag_id)) # Get the point and timestamp for the first location. row = next(lyr) previous_pt = row.geometry().Clone() previous_time = row.GetField('timestamp') # Loop the rest of the locations for the current tag. for row in lyr: current_time = row.GetField('timestamp') if current_time < previous_time: raise Exception('Timestamps out of order')