def main(): if sys.argv[5] == 'N': clip = 'clip_N.shp' elif sys.argv[5] == 'S': clip = 'clip_S.shp' else: print 'Is it in the N(orth) or S(outh)?' exit with fiona.open(clip, 'r') as shp: coords = [feature['geometry'] for feature in shp] with rasterio.open(sys.argv[1]) as src: clipped1, out_transform = mask(src, coords, nodata=np.nan, crop=False) with rasterio.open(sys.argv[2]) as src: clipped2, out_transform = mask(src, coords, nodata=np.nan, crop=False) sigma_c = (np.nanmedian(clipped1) + np.nanmedian(clipped2)) / float( 2) # coregistration error if np.isnan(sigma_c): sigma_c = 0 #print 'sigma_c', sigma_c C = 0.4 # uncertainty of the tracking algorithm [p] delta_x = float(sys.argv[4]) # image resolution [m/p] delta_t = float(sys.argv[3]) # time interval between images [d] z = 2 # oversampling factor #### C and z values taken from Seehaus 2015 sigma_t = C * delta_x / float( z * delta_t ) * 365.2 # uncertainties in the velocity field due to the tracking algorithm #print 'sigma_t', sigma_t e_const = np.sqrt(sigma_t**2 + sigma_c**2) print e_const
def cropSlopeAndNDVI(self): # Open the myDataDownloader = DataHelper.DataDownloader() localfile = myDataDownloader.downloadFiles([config.settings['aoi']]) with fiona.open(localfile, "r") as aoi: geoms = [feature["geometry"] for feature in aoi] self.files['normalized_cropped_urban_slope'] = "output/classified-slope-urban-cropped.tiff" self.files['normalized_cropped_ag_slope'] = "output/classified-slope-AG-cropped.tiff" self.files['classified_cropped_ndvi_file']="output/classified-ndvi-cropped.tiff" print("Cropping Classified Slope and NDVI..") with rasterio.open('output/tmp/classified-slope-urban-normalized.tiff') as urb_src: urb_out_image, urb_out_transform = mask(urb_src, geoms, crop=True) urb_out_meta = urb_src.meta.copy() urb_out_meta.update({"driver": "GTiff", "height": urb_out_image.shape[1], "width": urb_out_image.shape[2], "transform": urb_out_transform}) with rasterio.open(self.files['normalized_cropped_urban_slope'], "w", **urb_out_meta) as urb_dest: urb_dest.write(urb_out_image) with rasterio.open('output/tmp/classified-slope-AG-normalized.tiff') as ag_src: ag_out_image, ag_out_transform = mask(ag_src, geoms, crop=True) ag_out_meta = ag_src.meta.copy() ag_out_meta.update({"driver": "GTiff", "height": ag_out_image.shape[1], "width": ag_out_image.shape[2], "transform": ag_out_transform}) with rasterio.open(self.files['normalized_cropped_ag_slope'], "w", **ag_out_meta) as ag_dest: ag_dest.write(ag_out_image) with rasterio.open('output/tmp/classified-ndvi.tiff') as ndvi_src: ndvi_out_image, ndvi_out_transform = mask(ndvi_src, geoms, crop=True) ndvi_out_meta = ndvi_src.meta.copy() ndvi_out_meta.update({"driver": "GTiff", "height": ndvi_out_image.shape[1], "width": ndvi_out_image.shape[2], "transform": ndvi_out_transform}) with rasterio.open(self.files['classified_cropped_ndvi_file'], "w", **ndvi_out_meta) as ndvi_dest: ndvi_dest.write(ndvi_out_image)
def clip_raster(): geoms = [{ "type": "Polygon", "coordinates": [[[79.78271484375, 25.997549919572112], [88.57177734375, 25.997549919572112], [88.57177734375, 30.713503990354965], [79.78271484375, 30.713503990354965], [79.78271484375, 25.997549919572112]]] }] file_input_dir = '/home/tethys/geotiff/' file_outut_dir = '/home/tethys/geotiff_clipped/' for file in os.listdir(file_input_dir): with rasterio.open(file_input_dir + file) as src: out_image, out_transform = mask(src, geoms, crop=True) out_meta = src.meta.copy() out_meta.update({ "driver": "GTiff", "height": out_image.shape[1], "width": out_image.shape[2], "transform": out_transform }) with rasterio.open(file_outut_dir + file, "w", **out_meta) as dest: dest.write(out_image)
def makeCounties(counties, saveName): geoms = [] if type(counties[0]) is str: for county in counties: geoms.append(countyHandler.get_county(county).geo) geoms.append(countyHandler.get_county(county).geo) else: for county in counties: geoms.append(county.geo) geoms.append(county.geo) # load the raster, mask it by the polygon and crop it with rasterio.open( "/home/jacob/python/SeiiData/gpw-v4-population-density_2015.tif" ) as src: out_image, out_transform = mask(src, geoms, crop=True) out_meta = src.meta.copy() # save the resulting raster out_meta.update({ "driver": "GTiff", "height": out_image.shape[1], "width": out_image.shape[2], "transform": out_transform }) with rasterio.open(saveName + ".tif", "w", **out_meta) as dest: dest.write(out_image)
def build_mask(raster_path, shp_path, dest_path): # Read the GeoTIFF with rasterio with rasterio.open(raster_path) as src: profile = src.profile crs = profile["crs"].to_string() # Read the shapefile with fiona with fiona.open(shp_path, "r") as shapefile: shp_crs = shapefile.crs geoms = [ transform_geom(shapefile.crs, crs, feature["geometry"]) for feature in shapefile ] # Mask with the list of geometries; crop to the geoms extent dest_image, dest_transform = mask(src, geoms, crop=True) # Update the profile profile["height"] = dest_image.shape[1] profile["width"] = dest_image.shape[2] profile["transform"] = dest_transform # Write the masked data in the destination raster with rasterio.open(dest_path, "w", **profile) as dest: dest.write(dest_image)
def AOIs_from_shp(image_path, shapefile_path, results_dir, overwrite=0): """From a tiff image (path: image_path), write a tiff (in results_dir) for each feature in a shapefile (path: shapefile_path).""" # Check if files already there if os.path.exists(os.path.join(results_dir, "tiff_AOIs", "AOI_1.tif")): print("tiff_AOIs already written") if overwrite: print("overwritting...") else: return # Read shapefile with fiona.open(shapefile_path, "r") as shapefile: # Walk through shapefile features for i, feature in enumerate(shapefile): AOI_path = os.path.join(results_dir, "tiff_AOIs", "AOI_" + str(i + 1) + ".tif") # Get feature geometry geoms = [feature["geometry"]] # Open image, mask with geometry with rasterio.open(image_path) as src: out_image, out_transform = mask(src, geoms, crop=True) out_meta = src.meta.copy() # Write the image (masked and cropped to the geometry) out_meta.update({ "driver": "GTiff", "height": out_image.shape[1], "width": out_image.shape[2], "transform": out_transform }) with rasterio.open(AOI_path, "w", **out_meta) as dest: dest.write(out_image)
def cutBox(centerX, centerY, radius, saveName): box = [{ 'type': 'Polygon', 'coordinates': [[(centerX - radius, centerY - radius), (centerX + radius, centerY - radius), (centerX + radius, centerY + radius), (centerX - radius, centerY + radius)]] }] # load the raster, mask it by the polygon and crop it with rasterio.open(saveName + ".tif") as src: out_image, out_transform = mask(src, box, crop=True) out_meta = src.meta.copy() # save the resulting raster out_meta.update({ "driver": "GTiff", "height": out_image.shape[1], "width": out_image.shape[2], "transform": out_transform }) with rasterio.open(saveName + ".tif", "w", **out_meta) as dest: dest.write(out_image)
def clip_raster(inraster, features, outraster): rasterio = import_rasterio() # check for rasterio from rasterio.tools.mask import mask geoms = _to_geojson(features) with rasterio.open(inraster) as src: print('clipping {}...'.format(inraster)) out_image, out_transform = mask(src, geoms, crop=True) out_meta = src.meta.copy() out_meta.update({"driver": "GTiff", "height": out_image.shape[1], "width": out_image.shape[2], "transform": out_transform}) with rasterio.open(outraster, "w", **out_meta) as dest: dest.write(out_image) print('wrote {}'.format(outraster))
def Crop(filename, long, lat, output): dataset = gdal.Open(filename, GA_ReadOnly) cols = dataset.RasterXSize rows = dataset.RasterYSize bands = dataset.RasterCount driver = dataset.GetDriver().LongName projection = dataset.GetProjection() geotransform = dataset.GetGeoTransform() originX = geotransform[0] # top left x originY = geotransform[3] # top left y pixelWidth = geotransform[1] pixelHeight = -geotransform[5] band = dataset.GetRasterBand(1) # want to find raster indexes of top points, p1=[minX,maxY], and p2=[maxX, minY] xmin = long - 0.001 ymax = lat + 0.001 xmax = long + 0.001 ymin = lat - 0.001 geoms = [{ 'type': 'Polygon', 'coordinates': [[(xmin, ymax), (xmax, ymax), (xmax, ymin), (xmin, ymin)]] }] with rasterio.open(filename) as src: out_image, out_transform = mask(src, geoms, crop=True) out_meta = src.meta.copy() out_meta.update({ "driver": "GTiff", "height": out_image.shape[1], "width": out_image.shape[2], "transform": out_transform }) # just set the directory to save cropped image os.chdir('C:\\Users\\Xiaoyan\\Desktop\\DDS Research\\Crop') with rasterio.open(output + ".tif", "w", **out_meta) as dest: dest.write(out_image)
def clip_world(): file_input_dir = '/home/tethys/geotiff_global/' file_outut_dir = '/home/tethys/geotiff_clipped_global/' with fiona.open( "/home/tethys/Downloads/world/TM_WORLD_BORDERS_SIMPL-0.3.shp", "r") as shpfile: features = [feature["geometry"] for feature in shpfile] for file in os.listdir(file_input_dir): with rasterio.open(file_input_dir + file) as src: out_image, out_transform = mask(src, features, crop=True) out_meta = src.meta.copy() out_meta.update({ "driver": "GTiff", "height": out_image.shape[1], "width": out_image.shape[2], "transform": out_transform }) with rasterio.open(file_outut_dir + file, "w", **out_meta) as dest: dest.write(out_image)
def crop_rgb(id, file, rgb_tile_dir, show=False): #select row row = file.loc[id] #create polygon from bounding box features = data2geojson(row) #crop and return image with rasterio.open(rgb_tile_dir + row.rgb_path) as src: out_image, out_transform = mask(src, [features], crop=True) #color channel should be last out_image = np.moveaxis(out_image, 0, -1) if show: #cv2 takes bgr order to_write = out_image[..., ::-1] cv2.imwrite("logs/images/" + id + ".png", to_write) #resize image and rescale image_resize = cv2.resize(out_image, (150, 150)) image_rescale = image_resize / 255.0 return (image_rescale)
def computeTransportNaturalBreaks(self, rawtransfile): print("Computing Natural breaks on Transport..") myDataDownloader = DataHelper.DataDownloader() localfile = myDataDownloader.downloadFiles([config.settings['aoi']]) with fiona.open(localfile, "r") as aoi: geoms = [feature["geometry"] for feature in aoi] classfiedtranstmppath = os.path.join(self.cwd,config.settings['outputdirectory'],'tmp','classified-transport.tiff') with rasterio.open(rawtransfile) as src: profile = src.profile bands = src.read() for band in bands: b = band[(band != np.array(None)) & (np.logical_not(np.isnan(band))) ] breaks = nb(b.ravel(),k=4,initial=1) bins = breaks.bins.tolist() # bins.insert(1,-1) # add -1 to the beginning of the breaks # print bins print("Writing new Transport with Natural break classes..") with rasterio.open(rawtransfile) as src: profile = src.profile bands = src.read(masked=True) for band in bands: for x in np.nditer(band, op_flags=['readwrite']): x[...] = np.digitize(x,bins) # Reproject and write each band with rasterio.open(classfiedtranstmppath, 'w', **profile) as dst: dst.write(bands) classfiedtranspath = os.path.join(self.cwd,config.settings['outputdirectory'],'classified-transport.tiff') print("Cropping Transport..") with rasterio.open(classfiedtranstmppath) as trans_src: trans_out_image, trans_out_transform = mask(trans_src, geoms, crop=True) trans_out_meta = trans_src.meta.copy() trans_out_meta.update({"driver": "GTiff", "height": trans_out_image.shape[1], "width": trans_out_image.shape[2], "transform": trans_out_transform}) with rasterio.open(classfiedtranspath, "w", **trans_out_meta) as trans_dest: trans_dest.write(trans_out_image) TransClassification = dict([(1,2),(2,3),(3,1),(4,1)]) print("Reclassing Transport file..") finaltransevalpath = os.path.join(self.cwd,config.settings['outputdirectory'],'evals','TRANS', 'TRANS.tiff') with rasterio.open(classfiedtranspath) as transnogdhsrc: classifiedprofile = transnogdhsrc.profile classifiedbands = transnogdhsrc.read() classifiedbands1 = np.vectorize(TransClassification.get)(classifiedbands) classifiedbands2 = classifiedbands1.astype(np.float32) with rasterio.open(finaltransevalpath, 'w', **classifiedprofile) as classifieddst: classifieddst.write(classifiedbands2) print("Reclassing completed") print("...")
import fiona import rasterio from rasterio.tools.mask import mask with fiona.open("tests/data/box.shp", "r") as shapefile: geoms = [feature["geometry"] for feature in shapefile] with rasterio.open("tests/data/RGB.byte.tif") as src: out_image, out_transform = mask(src, geoms, crop=True) out_meta = src.meta.copy() out_meta.update({"driver": "GTiff", "height": out_image.shape[1], "width": out_image.shape[2], "transform": out_transform}) with rasterio.open("/tmp/masked.tif", "w", **out_meta) as dest: dest.write(out_image)
#-------------------------------------------------- # 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') sys.exit(0) else: print('valid extent returned, exiting script') sys.exit(0) #-------------------------------------------------- # Read in tree cover vrt, subset by shape bounding box
'#', gdal.GDT_Float32, 'GTiff', dfra, geotrans=dfr[1], rasterproj=rwkt) # Get geometry from zone shapefile with fiona.open( idir + '/rp5k130k90m_' + j + '_' + i + '_' + k + '_ifl/cfinal/' + fluxdir + '/' + j + '_' + i + '_' + k + '_' + l + '.shp', "r") as shapefile: geoms = [feature["geometry"] for feature in shapefile] # Use zone boundary to mask forest cover layer with rasterio.open(otiff1) as src: out_image, out_transform = mask(src, geoms, crop=True, nodata=ndval) out_meta = src.meta.copy() # Delete rectangle cropped raster os.remove(otiff1) # Forest cover area fcasum = np.sum(out_image[out_image != ndval] / 100.0 * 8100.0) # Stop if there's no tree cover in the zone if (fcasum <= 0) or not (np.isfinite(fcasum)): print('invalid or zero tree cover in zone, exiting') sys.exit(0) # Average forest cover values for zone fcave = np.mean(out_image[out_image != ndval]) # Standard deviation of forest cover values for zone fcstd = np.std(out_image[out_image != ndval]) # One line dataframe to hold forest cover mean, std, and other info
def crop_rgb(filename,row): with rasterio.open(self.filename) as src: out_image, out_transform = mask(src, geoms, crop=True) out_meta = src.meta.copy()
shpCut = [()] #with fiona.open(iDir3+'AK_LCC_GCS_WGS84_b20k.shp') as shapefile: # shpExt = shapefile.extent #with rasterio.open(iDir + 'FRP_2003_2013_QD.nc','r', driver='NetCDF') as src: #with rasterio.open(iDir + 'dNBR_2001_2012_QD.nc','r', driver='NetCDF') as src: with rasterio.open(iDir + 'PctBurned_2001_2012_QD.nc', 'r', driver='NetCDF') as src: #with rasterio.open(iDir + 'VegDestructionIndex_2003_2012_QD.nc','r', driver='NetCDF') as src: #with rasterio.open(iDir + 'dLST_2003_2012_QD.nc','r', driver='NetCDF') as src: #with rasterio.open(iDir + 'PctBorealPixel_QD.nc','r', driver='NetCDF') as src: #with rasterio.open(iDir + 'PZI.flt','r', driver='EHdr') as src: profile = src.profile test_prof = src.profile test_img, test_trans = mask(src, shpGeom, crop=True) #test_img, test_trans = mask(src,shpBounds, crop=True) #test_img = src test_crs = src.crs test_meta = src.meta.copy() test_bound = src.bounds test_affine = src.affine test_crs = crs.CRS.from_epsg(4326) # Calculate the ideal dimensions and transformation in the new crs dst_affine, dst_width, dst_height = calculate_default_transform( test_crs, Crs, test_img.shape[2], test_img.shape[1], *shpBounds) # update the relevant parts of the profile profile.update({
"gdalbuildvrt", "-vrtnodata", "0", "-separate", "-input_file_list", iflist, avrt ]) # Sum flux files intersecting with polygon boundary csum = hfu.rastersum(avrt) # Write to file hfu.array2raster(otiff1, avrt, gdal.GDT_Float32, 'GTiff', csum) # Get geometry from zone shapefile with fiona.open( idir + '/rp5k130k90m_' + j + '_' + i + '_' + k + '_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() # Sum flux values to get total for the zone tcsum = np.sum(out_image) # Delete rectangle cropped raster os.remove(otiff1) # Save zone cropped raster out_meta.update({ "driver": "GTiff", "compress": "lzw", "dtype": rasterio.float32, "height": out_image.shape[1], "width": out_image.shape[2], "transform": out_transform }) with rasterio.open(otiff1, "w", **out_meta) as dest:
def crop_to_overlap(tif): # crop image to overlap region with rio.open(tif) as img: masked, new_affine = mask(img, [ol.__geo_interface__], crop=True) cropped = np.where(masked.mask, np.nan, masked.data)[0] return cropped, new_affine