def getBoundingBoxForCatchmentsForGage(config, outputDir, reachcode, measure, deleteIntermediateFiles=True):
    """ Get bounding box coordinates (in WGS 84) for the drainage area associated with a given NHD 
        (National Hydrography Dataset) streamflow gage identified by a reach code and measure.
        
        @param config A Python ConfigParser containing the following sections and options:
            'GDAL/OGR' and option 'PATH_OF_OGR2OGR' (absolute path of ogr2ogr binary)
            'NHDPLUS2' and option 'PATH_OF_NHDPLUS2_DB' (absolute path to SQLite3 DB of NHDFlow data)
            'NHDPLUS2', 'PATH_OF_NHDPLUS2_CATCHMENT' (absolute path to NHD catchment SQLite3 spatial DB)
        @param outputDir String representing the absolute/relative path of the directory into which output rasters should be written
        @param reachcode String representing NHD streamflow gage 
        @param measure Float representing the measure along reach where Stream Gage is located 
            in percent from downstream end of the one or more NHDFlowline features that are 
            assigned to the ReachCode (see NHDPlusV21 GageLoc table)
        @param deleteIntermediateFiles A boolean, True if intermediate files generated from the analysis should be deleted
         
        @return A dictionary with keys: minX, minY, maxX, maxY, srs. The key srs is set to 'EPSG:4326' (WGS 84)
        
        @raise ConfigParser.NoSectionError
        @raise ConfigParser.NoOptionError
        @raise IOError(errno.ENOTDIR) if outputDir is not a directory
        @raise IOError(errno.EACCESS) if outputDir is not writable
    """
    nhddbPath = config.get('NHDPLUS2', 'PATH_OF_NHDPLUS2_DB')
    if not os.access(nhddbPath, os.R_OK):
        raise IOError(errno.EACCES, "The database at %s is not readable" %
                      nhddbPath)
    nhddbPath = os.path.abspath(nhddbPath)
        
    catchmentFeatureDBPath = config.get('NHDPLUS2', 'PATH_OF_NHDPLUS2_CATCHMENT')
    if not os.access(catchmentFeatureDBPath, os.R_OK):
        raise IOError(errno.EACCES, "The catchment feature DB at %s is not readable" %
                      catchmentFeatureDBPath)
    catchmentFeatureDBPath = os.path.abspath(catchmentFeatureDBPath)
    
    ogrCmdPath = config.get('GDAL/OGR', 'PATH_OF_OGR2OGR')
    if not os.access(ogrCmdPath, os.X_OK):
        raise IOError(errno.EACCES, "The ogr2ogr binary at %s is not executable" %
                      ogrCmdPath)
    ogrCmdPath = os.path.abspath(ogrCmdPath)
    
    if not os.path.isdir(outputDir):
        raise IOError(errno.ENOTDIR, "Output directory %s is not a directory" % (outputDir,))
    if not os.access(outputDir, os.W_OK):
        raise IOError(errno.EACCES, "Not allowed to write to output directory %s" % (outputDir,))
    outputDir = os.path.abspath(outputDir)
    
    # Connect to DB
    conn = sqlite3.connect(nhddbPath)
    
    comID = getComIdForStreamGage(conn, reachcode, measure)
    #sys.stderr.write("Gage with reachcode %s, measure %f has ComID %d" % (reachcode, measure, comID))
    
    # Get upstream reaches
    upstream_reaches = []
    getUpstreamReachesSQL(conn, comID, upstream_reaches)
    #sys.stderr.write("Upstream reaches: ")
    #sys.stderr.write(upstream_reaches)
    
    # Extract polygons for upstream catchments
    catchmentOut = os.path.join(outputDir, "catchment-%s.shp" % time.time())
    ogrCommand = "%s -s_srs EPSG:4326 -t_srs EPSG:4326  -f 'ESRI Shapefile' -sql 'SELECT * FROM catchment WHERE featureid=%s" % (ogrCmdPath, comID) # NHDPlusV2
    for reach in upstream_reaches:
        ogrCommand = ogrCommand + " OR featureid=%s" % reach # NHDPlusV2
    ogrCommand = ogrCommand +"' " + catchmentOut + " " + catchmentFeatureDBPath  
    #sys.stderr.write("ogr command: %s" % ogrCommand)
    os.system(ogrCommand)
    
    bbox = getBoundingBoxForShapefile(catchmentOut)
    
    # Clean-up temporary shapefile/ancillary files
    if deleteIntermediateFiles:
        deleteShapefile(catchmentOut)
    
    conn.close()

    return bbox
    publisher = 'SELF PUBLISHED'

if args.outfile:
    outfile = args.outfile
else:
    outfile = "gage"

# Get study area parameters
studyArea = GenericMetadata.readStudyAreaEntries(context)
bbox = bboxFromString(studyArea['bbox_wgs84'])

outFilename = "%s%sshp" % (outfile, os.extsep)
# Overwrite DEM if already present
outFilepath = os.path.join(context.projectDir, outFilename)
if os.path.exists(outFilepath):
    deleteShapefile(outFilepath)

sys.stdout.write('Importing streamflow gage...')
sys.stdout.flush()

gageIDs = [args.idValue]
coords = getCoordinatesOfPointsFromShapefile(inGagePath, args.layerName,
                                             args.idAttribute, gageIDs)
gage_lon = coords[0][0]
gage_lat = coords[0][1]
coordinates = (gage_lon, gage_lat)

# Ensure gage coordinates are within bounding box
if not isCoordinatePairInBoundingBox(bbox, coordinates):
    sys.exit("Gage coordinates %s, %s do not appear to lie within bounding box %s, %s, %s, %s" %
             ( str(gage_lon), str(gage_lat), str(bbox['minX']), str(bbox['minY']), str(bbox['maxX']), str(bbox['maxY']) ) )
    configFile = args.configfile

context = Context(args.projectDir, configFile) 

if not context.config.has_option('GDAL/OGR', 'PATH_OF_OGR2OGR'):
    sys.exit("Config file %s does not define option %s in section %s" & \
          (args.configfile, 'GDAL/OGR', 'PATH_OF_OGR2OGR'))

# Check if features shapefile already exists
manifest = GenericMetadata.readManifestEntries(context)
if 'soil_features' in manifest:
    if args.overwrite:
        sys.stdout.write('Deleting existing SSURGO features shapefile\n')
        sys.stdout.flush()
        shpFilepath = os.path.join( context.projectDir, manifest['soil_features'] )
        deleteShapefile(shpFilepath)
    else:
        sys.exit( textwrap.fill('SSURGO features already exist in project directory.  Use --overwrite option to overwrite.') )

# Get study area parameters
studyArea = GenericMetadata.readStudyAreaEntries(context)
bbox = bboxFromString(studyArea['bbox_wgs84'])

outputrasterresolutionX = studyArea['dem_res_x']
outputrasterresolutionY = studyArea['dem_res_y']
srs = studyArea['dem_srs']

sys.stdout.write('Downloading SSURGO features for study area from USDA Soil Data mart...\n')
sys.stdout.flush()
shpFilename = getMapunitFeaturesForBoundingBox(context.config, context.projectDir, bbox, t_srs=srs)[0]
Example #4
0
    configFile = args.configfile

context = Context(args.projectDir, configFile) 

if not context.config.has_option('GDAL/OGR', 'PATH_OF_OGR2OGR'):
    sys.exit("Config file %s does not define option %s in section %s" & \
          (args.configfile, 'GDAL/OGR', 'PATH_OF_OGR2OGR'))

# Check if features shapefile already exists
manifest = GenericMetadata.readManifestEntries(context)
if 'soil_features' in manifest:
    if args.overwrite:
        sys.stdout.write('Deleting existing SSURGO features shapefile\n')
        sys.stdout.flush()
        shpFilepath = os.path.join( context.projectDir, manifest['soil_features'] )
        deleteShapefile(shpFilepath)
    else:
        sys.exit( textwrap.fill('SSURGO features already exist in project directory.  Use --overwrite option to overwrite.') )

# Get study area parameters
studyArea = GenericMetadata.readStudyAreaEntries(context)
bbox = bboxFromString(studyArea['bbox_wgs84'])

outputrasterresolutionX = studyArea['dem_res_x']
outputrasterresolutionY = studyArea['dem_res_y']
srs = studyArea['dem_srs']

sys.stdout.write('Downloading SSURGO features for study area from USDA Soil Data mart...\n')
sys.stdout.flush()
shpFilename = getMapunitFeaturesForBoundingBox(context.config, context.projectDir, bbox, 
                                               tileBbox=args.tile, t_srs=srs, tileDivisor=args.tiledivisor,
Example #5
0
    publisher = 'SELF PUBLISHED'

if args.outfile:
    outfile = args.outfile
else:
    outfile = "gage"

# Get study area parameters
studyArea = GenericMetadata.readStudyAreaEntries(context)
bbox = bboxFromString(studyArea['bbox_wgs84'])

outFilename = "%s%sshp" % (outfile, os.extsep)
# Overwrite DEM if already present
outFilepath = os.path.join(context.projectDir, outFilename)
if os.path.exists(outFilepath):
    deleteShapefile(outFilepath)

sys.stdout.write('Importing streamflow gage...')
sys.stdout.flush()

gageIDs = [args.idValue]
coords = getCoordinatesOfPointsFromShapefile(inGagePath, args.layerName,
                                             args.idAttribute, gageIDs)
gage_lon = coords[0][0]
gage_lat = coords[0][1]
coordinates = (gage_lon, gage_lat)

# Ensure gage coordinates are within bounding box
if not isCoordinatePairInBoundingBox(bbox, coordinates):
    sys.exit(
        "Gage coordinates %s, %s do not appear to lie within bounding box %s, %s, %s, %s"
def getBoundingBoxForCatchmentsForGage(config,
                                       outputDir,
                                       reachcode,
                                       measure,
                                       deleteIntermediateFiles=True):
    """ Get bounding box coordinates (in WGS 84) for the drainage area associated with a given NHD 
        (National Hydrography Dataset) streamflow gage identified by a reach code and measure.
        
        @param config A Python ConfigParser containing the following sections and options:
            'GDAL/OGR' and option 'PATH_OF_OGR2OGR' (absolute path of ogr2ogr binary)
            'NHDPLUS2' and option 'PATH_OF_NHDPLUS2_DB' (absolute path to SQLite3 DB of NHDFlow data)
            'NHDPLUS2', 'PATH_OF_NHDPLUS2_CATCHMENT' (absolute path to NHD catchment SQLite3 spatial DB)
        @param outputDir String representing the absolute/relative path of the directory into which output rasters should be written
        @param reachcode String representing NHD streamflow gage 
        @param measure Float representing the measure along reach where Stream Gage is located 
            in percent from downstream end of the one or more NHDFlowline features that are 
            assigned to the ReachCode (see NHDPlusV21 GageLoc table)
        @param deleteIntermediateFiles A boolean, True if intermediate files generated from the analysis should be deleted
         
        @return A dictionary with keys: minX, minY, maxX, maxY, srs. The key srs is set to 'EPSG:4326' (WGS 84)
        
        @raise ConfigParser.NoSectionError
        @raise ConfigParser.NoOptionError
        @raise IOError(errno.ENOTDIR) if outputDir is not a directory
        @raise IOError(errno.EACCESS) if outputDir is not writable
    """
    nhddbPath = config.get('NHDPLUS2', 'PATH_OF_NHDPLUS2_DB')
    if not os.access(nhddbPath, os.R_OK):
        raise IOError(errno.EACCES,
                      "The database at %s is not readable" % nhddbPath)
    nhddbPath = os.path.abspath(nhddbPath)

    catchmentFeatureDBPath = config.get('NHDPLUS2',
                                        'PATH_OF_NHDPLUS2_CATCHMENT')
    if not os.access(catchmentFeatureDBPath, os.R_OK):
        raise IOError(
            errno.EACCES, "The catchment feature DB at %s is not readable" %
            catchmentFeatureDBPath)
    catchmentFeatureDBPath = os.path.abspath(catchmentFeatureDBPath)

    ogrCmdPath = config.get('GDAL/OGR', 'PATH_OF_OGR2OGR')
    if not os.access(ogrCmdPath, os.X_OK):
        raise IOError(
            errno.EACCES,
            "The ogr2ogr binary at %s is not executable" % ogrCmdPath)
    ogrCmdPath = os.path.abspath(ogrCmdPath)

    if not os.path.isdir(outputDir):
        raise IOError(errno.ENOTDIR,
                      "Output directory %s is not a directory" % (outputDir, ))
    if not os.access(outputDir, os.W_OK):
        raise IOError(
            errno.EACCES,
            "Not allowed to write to output directory %s" % (outputDir, ))
    outputDir = os.path.abspath(outputDir)

    # Connect to DB
    conn = sqlite3.connect(nhddbPath)

    comID = getComIdForStreamGage(conn, reachcode, measure)
    #sys.stderr.write("Gage with reachcode %s, measure %f has ComID %d" % (reachcode, measure, comID))

    # Get upstream reaches
    upstream_reaches = []
    getUpstreamReachesSQL(conn, comID, upstream_reaches)
    #sys.stderr.write("Upstream reaches: ")
    #sys.stderr.write(upstream_reaches)

    # Extract polygons for upstream catchments
    catchmentOut = os.path.join(outputDir, "catchment-%s.shp" % time.time())
    ogrCommand = "%s -s_srs EPSG:4326 -t_srs EPSG:4326  -f 'ESRI Shapefile' -sql 'SELECT * FROM catchment WHERE featureid=%s" % (
        ogrCmdPath, comID)  # NHDPlusV2
    for reach in upstream_reaches:
        ogrCommand = ogrCommand + " OR featureid=%s" % reach  # NHDPlusV2
    ogrCommand = ogrCommand + "' " + catchmentOut + " " + catchmentFeatureDBPath
    #sys.stderr.write("ogr command: %s" % ogrCommand)
    os.system(ogrCommand)

    bbox = getBoundingBoxForShapefile(catchmentOut)

    # Clean-up temporary shapefile/ancillary files
    if deleteIntermediateFiles:
        deleteShapefile(catchmentOut)

    conn.close()

    return bbox