studyArea = GenericMetadata.readStudyAreaEntries(context)
outputrasterresolutionX = studyArea['dem_res_x']
outputrasterresolutionY = studyArea['dem_res_y']
srs = studyArea['dem_srs']

if args.source == 'local':

    nlcdURL = 'http://gisdata.usgs.gov/TDDS/DownloadFile.php?TYPE=nlcd2006&FNAME=NLCD2006_landcover_4-20-11_se5.zip'
    nlcdRaster = context.config.get('NLCD', 'PATH_OF_NLCD2006')
    if not os.access(nlcdRaster, os.R_OK):
        raise IOError(errno.EACCES, "Not allowed to read NLCD raster %s" % (nlcdRaster,))
    nlcdRaster = os.path.abspath(nlcdRaster)
    
    sys.stdout.write('Extracting tile from local NLCD data...')
    sys.stdout.flush()
    extractTileFromRasterByRasterExtent(context.config, context.projectDir, demFilepath, nlcdRaster, tileFilename)
    sys.stdout.write('done\n')

else:
    # Download NLCD from WCS
    sys.stdout.write("Downloading NLCD via WCS from %s..." % (HOST,) )
    sys.stdout.flush()
    (returnCode, nlcdURL) = getNLCDForBoundingBox(context.config, context.projectDir, tileFilename, bbox=bbox, 
                                                  resx=outputrasterresolutionX, resy=outputrasterresolutionY, 
                                                  coverage='NLCD2006', srs=srs)
    assert(returnCode)
    sys.stdout.write('done\n')

# Write metadata
GenericMetadata.writeStudyAreaEntry(context, "landcover_type", "NLCD2006")
rasterMetadata = getSpatialReferenceForRaster(inRasterPath)
rasterSrs = rasterMetadata[5]
rasterX = float(rasterMetadata[0])
rasterY = float(rasterMetadata[1])
if (rasterSrs != srs):
    resample = True
elif (not args.noresample) and ( (rasterX != demResolutionX) or (rasterY != demResolutionY) ):
    resample = True

if args.clip:
    processingNotes = "Clipping %s raster %s to DEM extent" % (args.type, inRasterPath)
    manifest = GenericMetadata.readManifestEntries(context)
    demFilename = manifest['dem']
    demFilepath = os.path.join(context.projectDir, demFilename)
    demFilepath = os.path.abspath(demFilepath)
    extractTileFromRasterByRasterExtent(context.config, context.projectDir, demFilepath, inRasterPath, rasterFilepath, args.resampleMethod)
else:
    if resample:
        # Reproject raster, copying into project directory in the process
        processingNotes = "Resampling %s raster from %s to %s, spatial resolution (%.2f, %.2f) to (%.2f, %.2f)" % \
            (args.type, rasterSrs, srs, rasterX, rasterX,
             demResolutionX, demResolutionY) 
        sys.stdout.write( textwrap.fill("%s..." % (processingNotes,) ) )
        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,) ) )