def cellbatchshapefile2statscsv(shapefile, vardir): ''' Extracts statistics on variables in the Worldclim tile for the cells in the shapefile via starspan. Arguments: shapefile - the shapefile containing the cells vardir - the path to the directory in which to store the Worldclim files ''' variables = [os.path.join(vardir, x) \ for x in os.listdir(vardir) \ if x.endswith('.bil')] variables = reduce(lambda x,y: '%s %s' % (x, y), variables) csvfile = shapefile.replace('.shp', '.csv') if os.path.exists(csvfile): return None # Call starspan requesting mean of variable, excluding nodata values (-9999 in the file is the same as 55537) # Example: starspan --vector 0-clipped.shp --raster tmean command = 'starspan --vector %s --raster %s --stats %s avg --nodata 55537' \ % (shapefile, variables, csvfile) # command = 'starspan --vector %s --raster %s --stats %s avg stdev min max --nodata 55537' \ # % (shapefile, variables, csvfile) args = shlex.split(command) try: subprocess.call(args) except: logging.info('command %s failed.' % command) return None return csvfile
def intersect(cls, shapefile, options): """Intesects features in a shapefile with variables via starspan.""" variables = [os.path.join(options.vardir, x) \ for x in os.listdir(options.vardir) \ if x.endswith('.bil')] variables = reduce(lambda x,y: '%s %s' % (x, y), variables) csvfile = shapefile.replace('.shp', '.csv') command = 'starspan --vector %s --raster %s --csv %s' \ % (shapefile, variables, csvfile) logging.info(command) args = shlex.split(command) subprocess.call(args) return csvfile
def intersect(cls, shapefile, options): """Intersects features in a shapefile with variables via starspan.""" t0 = time.time() logging.info("Beginning starspan statistics on %s." % (shapefile)) variables = [os.path.join(options.vardir, x) for x in os.listdir(options.vardir) if x.endswith(".bil")] variables = reduce(lambda x, y: "%s %s" % (x, y), variables) csvfile = shapefile.replace(".shp", ".csv") # Call starspan requesting mean of variable, exclusing nodata values (-9999 in the file is the same as 55537) command = "starspan --vector %s --raster %s --stats %s avg --nodata 55537" % (shapefile, variables, csvfile) # logging.info(command) args = shlex.split(command) subprocess.call(args) t1 = time.time() logging.info("starspan statistics finished in %s." % (t1 - t0)) return csvfile