Пример #1
0
# Threshold type
gtorlt = p1.gtorlt

# Reclass loss year name
rclyname = p1.rclyname

# List loss year files
fl = os.listdir(p1.tld + p1.fcd)
fl = [i for i in fl if 'lossyear' in i and i.endswith('.tif')]

# Get file name to work on
fnamely = p1.tld + p1.fcd + '/' + fl[j]

# Loss year
lyear = hfu.raster2array(fnamely)
lya = lyear[0]
del lyear

if gtorlt == 'gt':
    # Reclass loss year > 12 to 1, else 0
    lya = np.where(lya > lyv, 1, 0)
    lya = lya.astype('int8')
else:
    # Reclass loss year <= 12 to 1, else 0
    lya = np.where((lya > 0) & (lya <= lyv), 1, 0)
    lya = lya.astype('int8')

# Output reclassed loss file name
ofn = os.path.dirname(fnamely) + os.sep + os.path.basename(fnamely).replace(
    'lossyear', rclyname)
Пример #2
0
        # Get flux extent
        fluxext = hfu.raster_corners(flname)
        # Get tree cover extent
        tcext = hfu.raster_corners(tcname)
        # Get tree cover loss extent
        tclext = hfu.raster_corners(idir + emtd + '/' + tclosslayers[0])

        # Get intersection of extents
        minx = np.max([minx, fluxext[0], tcext[0], tclext[0]])
        miny = np.max([miny, fluxext[1], tcext[1], tclext[1]])
        maxx = np.min([maxx, fluxext[2], tcext[2], tclext[2]])
        maxy = np.min([maxy, fluxext[3], tcext[3], tclext[3]])

        #--------------------------------------------------
        # Read in flux raster, subset by shape bounding box
        dfr = hfu.raster2array(flname, clip=[minx, miny, maxx, maxy])
        # If raster2array returns a valid output, keep processing
        if dfr != False:
            # Write clipped raster to file
            hfu.array2raster(otiff1,
                             '#',
                             gdal.GDT_Float32,
                             'GTiff',
                             dfr[0],
                             geotrans=dfr[1],
                             rasterproj=rwkt)
            # Use zone boundary to mask flux sum layer
            with rasterio.open(otiff1) as src:
                out_image1, out_transform = mask(src, geoms, crop=True)
                out_meta = src.meta.copy()
            # Delete rectangle cropped raster
 bext[2] = newymin
 bext[3] = newymax
 # Save extent to a new Shapefile
 boundingbox = box(bext[0], bext[2], bext[1], bext[3])
 #%%
 # Create resistance vrt tile
 csrast = tdir + vrtp + str(gc1) + '_' + str(ptid1) + '_' + str(
     gc2) + '_' + str(ptid2) + '.vrt'
 subprocess.call([
     "gdalbuildvrt", "-te",
     str(bext[0]),
     str(bext[2]),
     str(bext[1]),
     str(bext[3]), "-input_file_list", forfileindex, csrast
 ])
 cso = hfu.raster2array(csrast)
 cs = cso[0]
 cs[cs == 255] = 1000  # set no go areas to higher value
 # Give x and y coordinates. Returns row and column indices of coordinates
 locs1 = hfu.coord2pixelOffset(csrast, [cx1], [cy1])
 locs1 = [i[0] for i in locs1]  # drop list level
 locs1 = [(locs1[0], locs1[1])]  # convert to a list of lists...
 # Get distance from point in first layer
 # Create graph object
 go = graph.MCP_Geometric(cs,
                          offsets=None,
                          sampling=(xint, yint),
                          fully_connected=True)
 cc1, tb = go.find_costs(locs1)
 del tb  # delete traceback
 locs2 = hfu.coord2pixelOffset(csrast, [cx2], [cy2])
Пример #4
0
            '_' + l + '.shp')
 # Get shapefile extent in regional sinusoidal coordinates
 minx, miny, maxx, maxy = cx.geometry.total_bounds
 # Convert to bounding box
 bboxcx = gpd.GeoDataFrame({
     'id': [1],
     'ucode': [l]
 },
                           geometry=[box(minx, miny, maxx, maxy)])
 # Get intersection between raster bounding box and shapefile bounding box in sinusoidal
 ix = gpd.overlay(bbox, bboxcx, how='intersection', use_sindex=True)
 # Get clip coordinates from intersection
 minx, miny, maxx, maxy = ix.geometry.total_bounds
 # Read in forest cover raster, subset by intersection between polygon
 # bounding box and raster.
 dfr = hfu.raster2array(fcname, clip=[minx, miny, maxx, maxy])
 # If raster2array returns a valid output, keep processing
 if dfr != False:
     # Rasterized ifl name
     rifl = '/scratch/pj276/ifl_corridors/ifl/ifl_2000_tropics_ssu_' + i + '_' + j + '.tif'
     # Read in rasterized ifl, subset by shape bounding box
     rifl = hfu.raster2array(rifl, clip=[minx, miny, maxx, maxy])[0]
     # Mask out forest cover that overlaps ifls
     dfr[0][rifl >= 1] = ndval
     dfra = dfr[0]
     # Write to file
     rwkt = gdal.Open(fcname)
     rwkt = rwkt.GetProjectionRef()
     hfu.array2raster(otiff1,
                      '#',
                      gdal.GDT_Float32,
Пример #5
0
 # Get tree cover extent
 tcext = hfu.raster_corners(tcname)
 # Get tree cover loss extent
 tclext = hfu.raster_corners(idir + emtd + '/' + tclosslayers[0])
 # Get ifl extent
 riflext = hfu.raster_corners(riflname)
 
 # Get intersection of extents
 minx = np.max([minx, fluxext[0], tcext[0], tclext[0], riflext[0]])
 miny = np.max([miny, fluxext[1], tcext[1], tclext[1], riflext[1]])
 maxx = np.min([maxx, fluxext[2], tcext[2], tclext[2], riflext[2]])
 maxy = np.min([maxy, fluxext[3], tcext[3], tclext[3], riflext[3]])
         
 #--------------------------------------------------
 # Read in flux raster, subset by shape bounding box
 dfr = hfu.raster2array(flname, clip=[minx, miny, maxx, maxy])
 # If raster2array returns a valid output, keep processing
 if dfr != False:
     # Write clipped raster to file
     hfu.array2raster(otiff1,'#',gdal.GDT_Float32,'GTiff',dfr[0],geotrans=dfr[1],rasterproj=rwkt)
     # Use zone boundary to mask flux sum layer
     with rasterio.open(otiff1) as src:
         out_image1, out_transform = mask(src, geoms, crop=True)
         out_meta = src.meta.copy()
     # Delete rectangle cropped raster
     os.remove(otiff1)
     # Sum flux values to get total for the zone
     fluxsum = np.sum(out_image1)
     # Break out of script if there's no flux
     if fluxsum == 0:
         print('no flux, exiting script')
    # Get flux extent
    fluxext = hfu.raster_corners(flname)
    # Get tree cover extent
    tcext = hfu.raster_corners(tcname)
    # Get tree cover loss extent
    tclext = hfu.raster_corners(idir + emtd + '/' + tclosslayers[0])

    # Get intersection of extents
    minx = np.max([minx, fluxext[0], tcext[0], tclext[0]])
    miny = np.max([miny, fluxext[1], tcext[1], tclext[1]])
    maxx = np.min([maxx, fluxext[2], tcext[2], tclext[2]])
    maxy = np.min([maxy, fluxext[3], tcext[3], tclext[3]])

    # Read in zone raster subset
    zrast = hfu.raster2array(zr, clip=[minx, miny, maxx, maxy])

    # Read in euclidean zone raster subset
    ezrast = hfu.raster2array(ezr, clip=[minx, miny, maxx, maxy])

    # Read in flux raster, subset by zone bounding box
    frast = hfu.raster2array(flname, clip=[minx, miny, maxx, maxy])
    # If raster2array returns a valid output, keep processing
    if frast != False:
        zrast = zrast[0]
        frast = frast[0]
        # Sum flux values to get total for the zone
        fluxsum = np.sum(frast[(zrast == lorig) & (ezrast > 0)])
        # Break out of script if there's no flux
        if fluxsum == 0:
            print('no flux, exiting script')
# Job array index
j = int(sys.argv[1]) - 1  # subtract for python zero indexing

# Loss year by tree cover 2000 name
lybytc2000name = p1.lybytc2000name

# List loss year files
fl = os.listdir(p1.tld + p1.pfdd)
fl = [i for i in fl if 'lossyear' in i and i.endswith('.tif')]
fl.sort()
# Get file name to work on
fnamely = p1.tld + p1.pfdd + '/' + fl[j]

# Loss year
lyear = hfu.raster2array(fnamely)
lya = lyear[0]
del lyear

# List tree cover files
y2k = os.listdir(p1.tld + p1.pfdd)
y2k = [i for i in y2k if 'treecover2000' in i and i.endswith('.tif')]
y2k.sort()
# Get file name to work on
y2knamely = p1.tld + p1.pfdd + '/' + y2k[j]
# Year 2000 tree cover
y2k = hfu.raster2array(y2knamely)[0]

# Loop through years
for lyv in range(0, 19, 1):
    # Reclass loss year
Пример #8
0
            # Build a vrt from a file list
            acmd = [
                "gdalbuildvrt", "-te",
                str(newbb[0]),
                str(newbb[1]),
                str(newbb[2]),
                str(newbb[3]), ovrt
            ]
            for myfile in flist:
                acmd.append(myfile)
            subprocess.call(acmd)

            # If aggregating datamask files, reclassify water values (2) to 0
            if 'datamask' in p:
                # Read vrt to array
                dm = hfu.raster2array(ovrt)[0]
                # Reclassify
                dm = np.where(dm == 2, 0, dm)
                # Name for temporary tiff
                ttiff = aggodir + '/ttiff_' + p + '_' + str(k) + '.tif'
                # Write to tiff
                hfu.array2raster(ttiff, ovrt, gdal.GDT_Byte, 'GTiff', dm)
                # Aggregate to new resolution using average operator
                ofile = aggodir + '/temp_90m_' + p + '_' + str(k) + '.tif'
                subprocess.call([
                    "gdalwarp", "-multi", "-wo", "NUM_THREADS=ALL_CPUS", "-tr",
                    aggdim, aggdim, "-r", "average", "-srcnodata", ndval,
                    "-ot", "Float32", ttiff, ofile
                ])
                # Create vrt from aggregated file, clipping off the collar
                aggvrt = aggodir + '/agg_temp_' + p + str(k) + '.vrt'
 d = {'id': [1], 'ucode': [l]}
 dd = pd.DataFrame(data=d)
 bboxcx = gpd.GeoDataFrame(dd, geometry=b)
 bboxcx.crs = cx.crs
 ix = gpd.overlay(bboxgeog, bboxcx, how='intersection', use_sindex=True)
 if ix.shape[0] >= 1:
     # Repro shapefile
     cx = cx.to_crs(rproj4)
     # Save shapefile if needed
     if not os.path.exists(idir + '/rp5k130k90m_' + j + '_' + i + '_' + kk + '_ifl/cfinal/' + fluxdir + '/' + j + '_' + i + '_' + k + '_' + l + '.shp'):
         cx.to_file(idir + '/rp5k130k90m_' + j + '_' + i + '_' + kk + '_ifl/cfinal/' + fluxdir + '/' + j + '_' + i + '_' + k + '_' + l + '.shp')              
         sflist.append(idir + '/rp5k130k90m_' + j + '_' + i + '_' + kk + '_ifl/cfinal/' + fluxdir + '/' + j + '_' + i + '_' + k + '_' + l + '.shp')
     # Get shapefile extent in regional sinusoidal coordinates
     minx, miny, maxx, maxy = cx.geometry.total_bounds
     # Read in direct flux raster, subset by shape bounding box
     dfr = hfu.raster2array(dfrname, clip=[minx, miny, maxx, maxy])
     # If raster2array returns a valid output, keep processing
     if dfr != False:
         # Write to file
         rwkt = gdal.Open(dfrname)
         rwkt = rwkt.GetProjectionRef()
         hfu.array2raster(otiff1,'#',gdal.GDT_Float32,'GTiff',dfr[0],geotrans=dfr[1],rasterproj=rwkt)
         # Get geometry from zone shapefile
         with fiona.open(idir + '/rp5k130k90m_' + j + '_' + i + '_' + kk + '_ifl/cfinal/' + fluxdir + '/' + j + '_' + i + '_' + k + '_' + l + '.shp', "r") as shapefile:
             geoms = [feature["geometry"] for feature in shapefile]
         # Use zone boundary to mask flux sum layer
         with rasterio.open(otiff1) as src:
             out_image, out_transform = mask(src, geoms, crop=True)
             out_meta = src.meta.copy()
         # Delete rectangle cropped raster
         os.remove(otiff1)
Пример #10
0
else:
    fpat = '_'
# Switch for using morphology or not
usemorph = p1.usemorph  #'no'

#----
if usemorph == 'yes':
    # Check for forest cover tile and make sure output doesn't exist
    if (os.path.exists(idir + '/' + reg + '_rtile_treecover' + str(y) + fpat +
                       str(j) + '.tif')
        ) and not os.path.exists(odir + '/' + reg + '_rtile_cs' + str(y) +
                                 spname + '_' + str(j) + '.tif'):
        # Get forest cover tile
        fortile = idir + '/' + reg + '_rtile_treecover' + str(y) + fpat + str(
            j) + '.tif'
        fco = hfu.raster2array(fortile)
        fc = fco[0]
        # Burn in MSPA classes
        mspatile = mdir + '/mspa_' + str(y) + '_' + mnbo + '_' + str(
            j) + '.tif'
        mspa = hfu.raster2array(mspatile)[0]
        # Codes: core-17; perforation-5,37,69; edge-3,35,67; bridge-33; loop-65; branch-1, patch-9
        minusarray = np.zeros(mspa.shape)
        minusarray[(mspa == 5) | (mspa == 37) | (mspa == 69)] = 5
        minusarray[(mspa == 3) | (mspa == 35) | (mspa == 67)] = 10
        minusarray[(mspa == 33) | (mspa == 65) | (mspa == 1)] = 15
        minusarray[mspa == 9] = 20
        fc = fc - minusarray
        fc[fc < 0] == 0  # Recode any negative values
        mspa = None
        minusarray = None
Пример #11
0
# Update year
uyear = p1.uyear  #'2012'

# Loss year value
lyv = p1.lyv  #12

# List year 2000 tree cover files
fl = os.listdir(p1.tld + p1.fcd)
fl = [i for i in fl if 'treecover2000' in i and i.endswith('.tif')]

# Get file name to work on
fname2000 = p1.tld + p1.fcd + '/' + fl[j]

# Use to build file names for forest operations and read to array
# Tree cover in 2000
treecov = hfu.raster2array(fname2000)
tca = treecov[0]

# Build loss year file name from year 2000 forest cover name
fnameloss = os.path.dirname(fname2000) + '/' + os.path.basename(
    fname2000).replace('treecover2000', 'lossyear')

# Loss year
lyear = hfu.raster2array(fnameloss)
lya = lyear[0]
del lyear

if usegain == True:
    # Build gain file name from year 2000 forest cover name
    fnamegain = os.path.dirname(fname2000) + '/' + os.path.basename(
        fname2000).replace('treecover2000', 'gain')