Beispiel #1
0
    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)
    sys.stdout.write("Resampling DEM to resolution %.2f x %.2f..." % (demResolutionX, demResolutionY) )
    sys.stdout.flush()
    resampleRaster(context.config, context.projectDir, tmpDEMFilepath, demFilename, \
                   s_srs=t_srs, t_srs=t_srs, \
                   trX=demResolutionX, trY=demResolutionY)
    sys.stdout.write('done\n')
else:
    shutil.move(tmpDEMFilepath, demFilepath)

# Write metadata
GenericMetadata.writeStudyAreaEntry(context, 'dem_res_x', demResolutionX)
GenericMetadata.writeStudyAreaEntry(context, 'dem_res_y', demResolutionY)
GenericMetadata.writeStudyAreaEntry(context, 'dem_srs', t_srs)

# Get rows and columns for DEM
(columns, rows) = getDimensionsForRaster(demFilepath)
GenericMetadata.writeStudyAreaEntry(context, 'dem_columns', columns)
GenericMetadata.writeStudyAreaEntry(context, 'dem_rows', rows)

# Write provenance
asset = AssetProvenance(GenericMetadata.MANIFEST_SECTION)
asset.name = 'dem'
asset.dcIdentifier = demFilename
asset.dcSource = demURL
asset.dcTitle = 'Digital Elevation Model'
asset.dcPublisher = 'GeoBrain'
asset.dcDescription = cmdline
asset.writeToMetadata(context)

# Write processing history
GenericMetadata.appendProcessingHistoryItem(context, cmdline)
                     (demResolutionX, demResolutionY))
    sys.stdout.flush()
    resampleRaster(context.config, context.projectDir, tmpDEMFilepath, demFilename, \
                   s_srs=t_srs, t_srs=t_srs, \
                   trX=demResolutionX, trY=demResolutionY)
    sys.stdout.write('done\n')
else:
    shutil.move(tmpDEMFilepath, demFilepath)

# Write metadata
GenericMetadata.writeStudyAreaEntry(context, 'dem_res_x', demResolutionX)
GenericMetadata.writeStudyAreaEntry(context, 'dem_res_y', demResolutionY)
GenericMetadata.writeStudyAreaEntry(context, 'dem_srs', t_srs)

# Get rows and columns for DEM
(columns, rows) = getDimensionsForRaster(demFilepath)
GenericMetadata.writeStudyAreaEntry(context, 'dem_columns', columns)
GenericMetadata.writeStudyAreaEntry(context, 'dem_rows', rows)

# Write provenance
asset = AssetProvenance(GenericMetadata.MANIFEST_SECTION)
asset.name = 'dem'
asset.dcIdentifier = demFilename
asset.dcSource = demURL
asset.dcTitle = demwcs.COVERAGE_DESC[args.demType]
asset.dcPublisher = 'Geoscience Australia'
asset.dcDescription = cmdline
asset.writeToMetadata(context)

# Write processing history
GenericMetadata.appendProcessingHistoryItem(context, cmdline)
Beispiel #4
0
    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)
Beispiel #5
0
        resampleRaster(context.config, context.projectDir, inRasterPath, rasterFilepath, \
                       s_srs=None, t_srs=srs, \
                       trX=demResolutionX, trY=demResolutionY, \
                       resampleMethod=args.resampleMethod)
    else:
        # Copy the raster in to the project directory
        processingNotes = "Importing %s raster from %s without resampling" % (
            args.type, inRasterPath)
        sys.stdout.write(textwrap.fill("%s..." % (processingNotes, )))
        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 (newRasterMetadata[0] != demColumns) or (newRasterMetadata[1] != demRows):
    if args.type == GenericMetadata.RASTER_TYPE_STREAM_BURNED_DEM:
        # Extents to not match, roll back and bail out
        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))
Beispiel #6
0
         demResolutionX, demResolutionY) 
    sys.stdout.write("%s..." % (processingNotes,) )
    resampleRaster(context.config, context.projectDir, inRasterPath, rasterFilepath, \
                   s_srs=rasterSrs, t_srs=srs, \
                   trX=demResolutionX, trY=demResolutionY, \
                   resampleMethod=args.resampleMethod)
else:
    # Copy the raster in to the project directory
    processingNotes = "Importing %s raster from %s without resampling" % (args.type, inRasterPath)
    sys.stdout.write("%s..." % (processingNotes,) )
    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,)