def GetImageSubset(subsetparameters, data, dimensions): #------------------------------------------------------------------ # Create square image subset from predefined corner points #------------------------------------------------------------------ Point = subsetparameters.Point # get and scale image subset BoxRange = dimensions.BoxRange BoxRange = BoxRange.astype(int) image = data.image[Point[1] - BoxRange[0]:Point[1] + BoxRange[1], Point[0] - BoxRange[0]:Point[0] + BoxRange[1]] image = IP.ScaleImage(image, 8) # get and store corresponding coordinates easting = data.coordinates.easting[Point[1] - BoxRange[0]:Point[1] + BoxRange[1], Point[0] - BoxRange[0]:Point[0] + BoxRange[1]] northing = data.coordinates.northing[Point[1] - BoxRange[0]:Point[1] + BoxRange[1], Point[0] - BoxRange[0]:Point[0] + BoxRange[1]] coordinates = CL.Coordinates(northing, easting) #store subset data FlagFlip = IP.CheckImageOrientation(coordinates) subset = CL.Subset(Point, image, coordinates, data.resolution, FlagFlip) return subset
def PointsList2Coordinates(points): #----------------------------------------- # Convert points list in Coordinates list #------------------------------------------ easting, northing = [], [] for point in points: easting.append(point.easting) northing.append(point.northing) easting = np.asarray(easting) northing = np.asarray(northing) coordinates = CL.Coordinates(northing, easting) return coordinates
def ReadGtiffBathymetry(parameters): #----------------------------------------- # READ EMODNET BATHYMETRY #----------------------------------------- # Read bathymetry (Gtiff format) coordinate, depth, _ = IP.ReadSARImg(parameters) # process bathymetry # depth to bathymetry bathymetry = -depth # Land Mask bathymetry[bathymetry > 0] = np.nan # create vector coordinate (instead of ) east = coordinate.easting[0, :] north = coordinate.northing[:, 0] coordinates = CL.Coordinates(north, east) return coordinates, bathymetry
def ReadTopography(parameters): #----------------------------------------------------- # Read Topography file to extract #----------------------------------------------------- parameter = parameters.ProcessingParameters.LandMaskParameters filename = parameter.LandMaskFileName; path_input = parameter.LandMaskFilePath; path_output=parameters.File_path_name.path_output pattern ='/' if sys.platform=='win32': pattern = '\\' # get projection information EPSG_in = GetDataProjectionSystem(path_input+filename) # reproject topography in the selected output reference coordinate system EPSG_out = GetEPSG(parameters.SpatialReferenceSystem.EPSG_Flag_Out) if EPSG_in != EPSG_out: file_aux = filename[:-4].split(pattern)[-1]+"_projected_EPSG"+str(EPSG_out)+".tif"; flin = path_input + filename; fout = path_output + file_aux; if not os.path.isfile(fout): os.system("gdalwarp -overwrite -srcnodata 0 -dstnodata 0 -r average -t_srs EPSG:"+str(EPSG_out)+" "+flin+" "+fout) else: print file_aux," Already Exists..." flin = file_aux path = path_output else: path = path_input # read raster filename= path + flin f, _, topography = ReadGtiffRaster(filename,'float32') f = None # process exception topography[np.isnan(topography)] = 0 # get Coordinates northing, easting = GetNorthingEasting(filename) coordinates = CL.Coordinates(northing[:,0], easting[0,:]) return coordinates, topography
def RemoveBathymetryException(parameters, Points, Bathymetry): #------------------------------------------------------------------------------------------------------------------- # # Remove Grid points impacted with bathymetry exception # #-------------------------------------------------------------------------------------------------------------------- # remove NaN bathymetry values point, bathymetry, northing, easting = RemoveNanBathymetry( Points, Bathymetry) # remove land-sea interpolation artefacts #point, bathymetry, northing, easting = RemoveBathymetryAnomalies(parameters, point, bathymetry, northing, easting) coordinates = CL.Coordinates(northing, easting) BathymetryData = CL.BathymetryData(coordinates, bathymetry) """ fig, ax = plt.subplots(1) for i in range(bathymetry.shape[0]): ax.plot(i, bathymetry[i],'or') """ return point, BathymetryData
def GetFFTBoxes(subsetparameters, data, dimension): #--------------------------------------------------------------------------------------------------------------- # Create list of overlapping subset images to estimate spectrum at a given grid point (center of main subset) #--------------------------------------------------------------------------------------------------------------- # initialisation #images, northings, eastings= [],[],[] Subsets = [] # Get the various subset images centers (box defined with an offset from the main subset, the overlapping is controlled by the offset) offset = subsetparameters.Shift * dimension.BoxRange subset_centers = GetSubsetCenters(subsetparameters.Point, offset, subsetparameters.BoxNb) # get information on subsets (coordinates, image footprint) for i in subset_centers: # get subset subsetparameters.Point = i subset = GetImageSubset(subsetparameters, data, dimension) # scale image image = IP.ScaleImage(subset.image, 8) coordinates = CL.Coordinates(subset.coordinates.northing, subset.coordinates.easting) FlagFlip = IP.CheckImageOrientation(coordinates) subsets = CL.Subset(i, image, coordinates, data.resolution, FlagFlip) Subsets.append(subsets) """ # concatenate data images.append(image); eastings.append(subset.coordinates.easting); northings.append(subset.coordinates.northing) #store data coordinates = Coordinates(northings,eastings) subsets = Subset(subset_centers, images, coordinates, data.resolution) """ return Subsets
def CollocatedData(Coordinates, coord, data): #------------------------------------------------------------------------------------------------------------------- # ESTIMATE COLLOCATED COORDINATES AND data # # Remark: data (bathymetry/topography) domain should be bigger than image domain if grid points are different #-------------------------------------------------------------------------------------------------------------------- east = coord.easting north = coord.northing East = Coordinates.easting North = Coordinates.northing # check if image was flipped flagE = False FlagN = False if east[0] > east[-1]: flagE = True if north[0] > north[-1]: flagN = True # Image limit (Coordinate-wise, (W, E, S, N)) W = np.min(East) E = np.max(East) S = np.min(North) N = np.max(North) # find nearest collocated coordinates iW = np.argmin(abs(W - east)) iE = np.argmin(abs(E - east)) iS = np.argmin(abs(S - north)) iN = np.argmin(abs(N - north)) # verify that bathymetry domain is bigger if east[iW] > W: if flagE: iW = iW + 1 else: iW = iW - 1 if east[iE] < E: if flagE: iE = iE - 1 else: iE = iE + 1 if north[iS] > S: if flagN: iS = iS + 1 else: iS = iS - 1 if north[iN] < N: if flagN: iN = iN - 1 else: iN = iN + 1 # coordinates easting = east[iW:iE + 1] northing = north[iN:iS + 1] coordinates = CL.Coordinates(northing, easting) # bathymetry Data = data[iN:iS + 1, iW:iE + 1] return coordinates, Data
def CreateLandMask(filein, coordinates_image, parameters): #----------------------------------------------------- # create Land Mask using selected topography file #----------------------------------------------------- #******************************************************* # 0) get information on the output system of reference #******************************************************* EPSG_out = GetEPSG(parameters.SpatialReferenceSystem.EPSG_Flag_Out) kernel = np.ones((5,5),np.uint8) #************************** # A) band mask from image #************************** mask = CreateBandMask(filein) indexS = (mask>0); indexM = (mask==0) mask[indexS] = 0; mask[indexM] = 255 # create band mask northing, easting = GetNorthingEasting(filein) # retrieve coordinates coordinates_image = CL.Coordinates(northing[:,0], easting[0,:]) #process / dilate mask edge if parameters.ProcessingParameters.LandMaskParameters.FilterFlag: mask = cv2.dilate(mask, kernel,iterations = 2) # save raster to temporary file fileout_image = 'ImageMask.tif'; array2raster(mask, coordinates_image, EPSG_out, filein, fileout_image) #****************************** # B) band mask for topography #****************************** # read topography file Coordinates, Topography = ReadTopography(parameters) #read topography # collocated topography coordinates, topography = ROI.CollocatedData(coordinates_image, Coordinates, Topography) # find collocated topography (reference: image coordinates) indexS = (topography==0); indexL = (topography!=0) # distinguish between topography[indexS] = 0; topography[indexL] = 255 # create mask #process / dilate mask edge if parameters.ProcessingParameters.LandMaskParameters.FilterFlag: topography = cv2.dilate(topography, kernel,iterations = 2) # create mask raster fileout_mask='TopographyMask.tif'; array2raster(topography, coordinates, EPSG_out, filein, fileout_mask) #********************* # C) merge both masks #********************* filename = fileout_image # the mask is pasted into the image mask file (mandatory for gdal_merge to keep dimensions) os.system("gdal_merge.py -n 0. -o "+fileout_image+" "+fileout_mask+" "+ filename) # merge mask image using gdal_merge f, _, finalmask = ReadGtiffRaster(filename,'float32'); f = None; # read mask os.system("rm"+" "+fileout_mask+" "+fileout_image) # clean directory """ #-------- # figure #-------- fig, (ax1,ax2,ax3) = plt.subplots(3) # image mask E = coordinates_image.easting; N = coordinates_image.northing; ax1.imshow(mask, cmap=plt.cm.jet, interpolation=None, aspect='auto', origin='upper', extent = [np.min(E), np.max(E), np.min(N), np.max(N)]) # topography mask E = coordinates.easting; N = coordinates.northing; ax2.imshow(topography, cmap=plt.cm.jet, interpolation=None, aspect='auto', origin='upper', extent = [np.min(E), np.max(E), np.min(N), np.max(N)]) #merge mask E = coordinates_image.easting; N = coordinates_image.northing; ax3.imshow(finalmask, cmap=plt.cm.jet, interpolation=None, aspect='auto', origin='upper', extent = [np.min(E), np.max(E), np.min(N), np.max(N)]) plt.show() """ return finalmask
def ReadSARImg(parameters): #------------------------------------------------------------ # Read/process SAR images and get related information #------------------------------------------------------------ # define EPSG code according to frame of reference # (WGS84/4326: Geodesic, WGS84/32629: UTM zone 29N, ETRS89/3763: portugal projection) EPSG_in = GetEPSG(parameters.SpatialReferenceSystem.EPSG_Flag_In) EPSG_out = GetEPSG(parameters.SpatialReferenceSystem.EPSG_Flag_Out) # Input/Output path and files path_input = parameters.File_path_name.path_input; fname_input = parameters.File_path_name.fname_input; path_output = parameters.File_path_name.path_output; #path delimiter pattern ='/' if sys.platform=='win32': pattern = '\\' #************************** # A) Raw image #************************** #------------------------------------------------------------------------------------- # PROCESS IMAGE # Get image specific format and dimension filein = fname_input; flin = path_input + filein; ImgType, ImgSize, ImgRes = GetImgType(flin), GetImgSize(flin), GetImRes(flin) f, RasterBand, img = ReadGtiffRaster(flin,parameters.ProcessingParameters.DataType) # slant range correction if parameters.ProcessingParameters.SlantRangeCorrection_Flag: if (ImgType["ENVISAT"] or ImgType["ERS1/2"] or ImgType["GeoTIFF"]): fileout_slant = fname_input[:-4]+"_Slant.tif"; fout = path_input + fileout_slant SlantRangeGTiFF(flin,fout, EPSG_in) filein = fileout_slant # Scale image if parameters.ProcessingParameters.ScaleFactor!=1.: file_aux = filein[:-4].split(pattern)[-1]+"_scaled.tif"; flin = path_input + filein; fout = path_input + file_aux; dim = (int(ImgSize[1]*ScaleFactor), int(ImgSize[0]*ScaleFactor)) if not os.path.isfile(fout): print "\nCreating an "+EPSG_flag+" image file..." os.system("gdalwarp -overwrite -srcnodata 0 -dstnodata 0 -r average -ts "+str(dim[0])+" "+str(dim[1])+" -t_srs EPSG:"+str(EPSG_in)+" "+flin+" "+fout) else: print file_aux," Already Exists..." filein = file_aux #------------------------------------------------------------------------------------- #************************** # B) Projected image #************************** # IMAGE PROJECTION if EPSG_in != EPSG_out: file_aux = filein[:-4].split(pattern)[-1]+"_projected_EPSG"+str(EPSG_out)+".tif"; flin = path_input + filein; fout = path_output + file_aux; if not os.path.isfile(fout): os.system("gdalwarp -overwrite -srcnodata 0 -dstnodata 0 -r average -t_srs EPSG:"+str(EPSG_out)+" "+flin+" "+fout) else: print file_aux," Projected image Already Exists..." filein = file_aux path = path_output else: path = path_input # get Image type and dimensions flin = path + filein ImgType = GetImgType(flin); ImgSize = GetImgSize(flin); #------------------------------------------------------------------------------------- # GET IMAGE INFORMATION # get northing/easting and pixel resolution (m) from projected image northing, easting, res = GetProjImgInfo(flin) # gather all coordinates coordinates = CL.Coordinates(northing,easting) # read projected image and get raster image f, _, img = ReadGtiffRaster(flin, parameters.ProcessingParameters.DataType) # close raster f = None #------------------------------------------------------------------------------------- # PROCESS IMAGE # stretch contrast (CS is the reference image) if parameters.ProcessingParameters.ContrastStretch_Flag: img = ImageContrastStretch(coordinates, img, flin, EPSG_out) else: flin = path + filein; fout = path_output + filein[:-4].split(pattern)[-1] + "_CS.tif" os.system("cp " + flin + " " + fout) #create land mask if parameters.ProcessingParameters.LandMaskParameters.LandMaskFlag: filename = filein[:-4].split(pattern)[-1]+"_Masked.tif" if not os.path.isfile(path+filename): # create image mask mask = CreateLandMask(flin, coordinates, parameters) ind = (mask>0) # save masked image array2raster(img, coordinates, EPSG_out, flin, path+filename) else: print filename," Masked image Already Exists..." f, _, mask = ReadGtiffRaster(path+filename); f = None; ind = (mask==0) # superpose mask on image img[ind] = 0 else: LMaskFile = flin mask = img """ fig, ax = plt.subplots(1) E=coordinates.easting; N=coordinates.northing; ax.imshow(img, cmap=plt.cm.gray, interpolation=None, aspect='auto', origin='upper', extent=[np.min(E), np.max(E), np.min(N), np.max(N)]) plt.show() """ return coordinates, img, res