def run(self, *args, **kwargs): """ Run the command: Acquire USGS DEM data. Arguments: coverage -- string Source dataset from which DEM tile should be extracted. outfile -- string The name of the DEM file to be written. File extension ".tif" will be added. demResolution list<float>[2] Two floating point numbers representing the desired X and Y output resolution of soil property raster maps; unit: meters. srs -- string Target spatial reference system of output, in EPSG:num format. verbose -- boolean Produce verbose output. Default: False. overwrite -- boolean Overwrite existing output. Default: False. """ coverage = kwargs.get('coverage', self.DEFAULT_COVERAGE) outfile = kwargs.get('outfile', None) demResolution = kwargs.get('demResolution', None) srs = kwargs.get('srs', None) verbose = kwargs.get('verbose', False) overwrite = kwargs.get('overwrite', False) self.checkMetadata() bbox = bboxFromString(self.studyArea['bbox_wgs84']) if not outfile: outfile = 'DEM' demFilename = "%s.tif" % (outfile) demResolutionX = demResolutionY = None if demResolution: demResolutionX = demResolution[0] demResolutionY = demResolution[1] if srs: if not isValidSrs(srs): msg = "ERROR: '%s' is not a valid spatial reference. Spatial reference must be of the form 'EPSG:XXXX', e.g. 'EPSG:32617'. For more information, see: http://www.spatialreference.org/" % (srs,) raise RunException(msg) else: # Default for UTM (centerLon, centerLat) = calculateBoundingBoxCenter(bbox) (utmZone, isNorth) = getUTMZoneFromCoordinates(centerLon, centerLat) srs = getEPSGStringForUTMZone(utmZone, isNorth) try: (dataFetched, urlFetched) = ecohydrolib.usgs.demwcs.getDEMForBoundingBox(self.context.config, self.context.projectDir, demFilename, bbox, srs, coverage=coverage, resx=demResolutionX, resy=demResolutionY, scale=0.01, overwrite=overwrite, verbose=verbose, outfp=self.outfp) except Exception as e: traceback.print_exc(file=self.outfp) raise RunException(e) if not dataFetched: raise RunException("Failed to download DEM data from URL {0}".format(urlFetched)) # Write metadata entries cmdline = GenericMetadata.getCommandLine() # Write metadata demFilepath = os.path.join(self.context.projectDir, demFilename) demSrs = getSpatialReferenceForRaster(demFilepath) GenericMetadata.writeStudyAreaEntry(self.context, 'dem_res_x', demSrs[0]) GenericMetadata.writeStudyAreaEntry(self.context, 'dem_res_y', demSrs[1]) GenericMetadata.writeStudyAreaEntry(self.context, 'dem_srs', srs) # Get rows and columns for DEM (columns, rows) = getDimensionsForRaster(demFilepath) GenericMetadata.writeStudyAreaEntry(self.context, 'dem_columns', columns) GenericMetadata.writeStudyAreaEntry(self.context, 'dem_rows', rows) # Write provenance asset = AssetProvenance(GenericMetadata.MANIFEST_SECTION) asset.name = 'dem' asset.dcIdentifier = demFilename asset.dcSource = urlFetched asset.dcTitle = "Digital Elevation Model ({0})".format(coverage) asset.dcPublisher = 'U.S. Geological Survey' asset.dcDescription = cmdline asset.processingNotes = "Elevation values rescaled from centimeters to meters. " asset.processingNotes += "Spatial grid resampled to {srs} with X resolution {xres} and Y resolution {yres}." asset.processingNotes = asset.processingNotes.format(srs=srs, xres=demSrs[0], yres=demSrs[1]) asset.writeToMetadata(self.context) # Write processing history GenericMetadata.appendProcessingHistoryItem(self.context, cmdline)
os.unlink(rasterFilepath) sys.exit( textwrap.fill( "ERROR: Raster type %s must be the same extent as DEM" % (GenericMetadata.RASTER_TYPE_STREAM_BURNED_DEM, ))) if not force: # Extents to not match, roll back and bail out os.unlink(rasterFilepath) sys.exit( textwrap.fill( "ERROR: Extent of raster dataset %s does not match extent of DEM in project directory %s. Use --force to override." ) % (rasterFilename, context.projectDir)) # Write metadata if GenericMetadata.RASTER_TYPE_LC == args.type: GenericMetadata.writeStudyAreaEntry(context, "landcover_type", "custom") # Write provenance asset = AssetProvenance(GenericMetadata.MANIFEST_SECTION) asset.name = args.type asset.dcIdentifier = rasterFilename asset.dcSource = "file://%s" % (inRasterPath, ) asset.dcTitle = args.type asset.dcPublisher = publisher asset.dcDescription = cmdline asset.processingNotes = processingNotes asset.writeToMetadata(context) # Write processing history GenericMetadata.appendProcessingHistoryItem(context, cmdline)
sys.stdout.flush() copyRasterToGeoTIFF(context.config, context.projectDir, inRasterPath, rasterFilename) sys.stdout.write('done\n') # Make sure extent of resampled raster is the same as the extent of the DEM newRasterMetadata = getDimensionsForRaster(rasterFilepath) if (not force) and ( (newRasterMetadata[0] != demColumns) or (newRasterMetadata[1] != demRows) ): # Extents to not match, roll back and bail out os.unlink(rasterFilepath) sys.exit(textwrap.fill("ERROR: Extent of raster dataset %s does not match extent of DEM in project directory %s. Use --force to override.") % (rasterFilename, context.projectDir)) # Write metadata if GenericMetadata.RASTER_TYPE_LC == args.type: GenericMetadata.writeStudyAreaEntry(context, "landcover_type", "custom") # Write provenance asset = AssetProvenance(GenericMetadata.MANIFEST_SECTION) asset.name = args.type asset.dcIdentifier = rasterFilename asset.dcSource = "file://%s" % (inRasterPath,) asset.dcTitle = args.type asset.dcPublisher = publisher asset.dcDescription = cmdline asset.processingNotes = processingNotes asset.writeToMetadata(context) # Write processing history GenericMetadata.appendProcessingHistoryItem(context, cmdline)