def validate_zones(region_shapefile, identifier, pop_field, emp_field, zones_shapefile): print 'validating zone values against statistics' with fiona.open(zones_shapefile) as zs: values = defaultdict(list) zone_values = [(z['properties']['AGS'],z['properties']['Pop+Emp']) for z in zs] for k,v in zone_values: values[k].append(v) with fiona.open(region_shapefile) as rs: results = [] for r in rs: ags = r['properties'][identifier] total = r['properties'][pop_field] + r['properties'][emp_field] #print ags, total, sum(values[ags]), values[ags] results.append((total, sum(values[ags]))) check_and_display_results(results)
def check_raster_output(region_shapefile, stats_raster, fields): zs = rasterstats.zonal_stats(region_shapefile, stats_raster, stats=['sum']) with fiona.open(region_shapefile) as regions: results = [] for (region, stat) in zip(regions, zs): try: actual=sum([float(region['properties'][f]) for f in fields]) calculated = (float(stat['sum'])) results.append((actual, calculated)) except TypeError: #print "no value for ", region['properties']['AGS_Int'] results.append((0,0)) print "results for", stats_raster,"..." print results check_and_display_results(results)