def runtest(): # input_raster = "./data/input_raster.tif" input_raster = "./data/h10_1005_3-2/h10_1005_3-2.tif" out_csv = "/tmp/out_xyz_2.csv" rtxyz = Raster2xyz() rtxyz.translate(input_raster, out_csv)
def raster_to_xyz(raster, csv): input_raster = raster out_csv = csv rtxyz = Raster2xyz() rtxyz.translate(input_raster, out_csv)
def main(): input_raster = "input.tif" out_csv = "output.csv" rtxyz = Raster2xyz() rtxyz.translate(input_raster, out_csv) df = pd.read_csv(out_csv) print(df.head(5)) print(df.columns) df.loc[df['z'] < 3, 'z'] = 0 df.loc[df['z'] > 200, 'z'] = 0 print(df.head(20)) minx = min(df.x) print(minx) miny = min(df.y) df['x'] = df['x'] - minx df['y'] = df['y'] - miny print(df.head(20)) df.to_csv(out_csv, index=False, header=False)
# -*- coding: utf-8 -*- """ Created on Mon Nov 16 14:30:24 2020 @author: vincenzomadaghiele """ # geoTiff libraries import rasterio as rs from rasterio.plot import show from raster2xyz.raster2xyz import Raster2xyz # general purpose import glob # Instanciate Raster2xyz() rtxyz = Raster2xyz() # convert to csv all the geoTiff files in NDVI300 source_path = 'data/copernicus_land/NDVI300/*.tiff' csv_path = 'data/copernicus_land/NDVI300/' source_files = glob.glob(source_path) for i in range(len(source_files)): file_name = source_files[i][29:-5] out_path = csv_path + file_name + '.csv' print('----------------') print('Converting: ' + file_name) print('----------------') rtxyz.translate(source_files[i], out_path) # plot dataset dataset = rs.open(source_files[i])
def get_SwissAlti3D(DEM_dir, file, gdf, dhm_ind, check_distance): """Read SwissAlti*d for glacier, reproject, convert to xyz, find closest value, reads out elevation Params: DEM_dir: dircetory containing SwissAlti3D glacier wide tiles file: filename of SA3D tile that contains glacier (based on Gl-ID) gdf: geopandas dataframe containing all point measurememts for this m_type and glacier dhm_ind: list of index of all points in gdf for which we need to check elevation check_distance: if true, check if current z-val is more than 150 m away from SWA3D, if not, just replace all NaN z-values """ dst_crs = 'EPSG:21781' # Read file, reproject to LV03 with rasterio.open(os.path.join(DEM_dir, "swissAlti3D", file)) as src: transform, width, height = calculate_default_transform( src.crs, dst_crs, src.width, src.height, *src.bounds) kwargs = src.meta.copy() kwargs.update({ 'crs': dst_crs, 'transform': transform, 'width': width, 'height': height }) with rasterio.open('RGB.tif', 'w', **kwargs) as dst: for i in range(1, src.count + 1): reproject(source=rasterio.band(src, i), destination=rasterio.band(dst, i), src_transform=src.transform, src_crs=src.crs, dst_transform=transform, dst_crs=dst_crs, resampling=Resampling.nearest) # Read file, convert to xyz # with rasterio.open('RGB.tif') as src: # image = src.read() # # transform image # bands, rows, cols = np.shape(image) # image1 = image.reshape(rows * cols, bands) # print(image1.shape) # # bounding box of image # l, b, r, t = src.bounds # # resolution of image # res = src.res # res = src.res # if (res[0]< 10): # res = list(res) # res[0]=10 # # meshgrid of X and Y # x = np.arange(l, r, res[0]) # y = np.arange(t, b, -res[0]) # X, Y = np.meshgrid(x, y) # # flatten X and Y # newX = np.array(X.flatten('C')) # newY = np.array(Y.flatten('C')) # print(newX.shape, newY.shape) # # join XY and Z information # try: # dem = np.column_stack((newX, newY, image1)) # except ValueError: # print("What is wrong here?") rtxyz = Raster2xyz() rtxyz.translate("RGB.tif", "out_xyz.csv") dem = pd.read_csv("out_xyz.csv", delimiter=",", engine="python") dem = dem[dem["z"] > 0] dem = dem.fillna(0).to_numpy() A = dem[:, :2] for f in dhm_ind: # find index of point in dem that is closest to our point pt for which we need to # add the z-component: pt = [gdf["x-pos"][f], gdf["y-pos"][f]] print(f) # distance and index of clostest point: distance, ind = spatial.KDTree(A).query(pt) # get z-pos of closest point: z_val = dem[ind, 2] if check_distance: # Check if z_val and original z_val are more than 150 m apart - if so, replace, if not, leave dis = abs(z_val - gdf["z-pos"][f]) if dis > 150: if dis > 10000000000000: print("Discrepancy bigger only because of NaN val ") else: print("Discrepancy bigger than 150 meter, ", z_val, gdf["z-pos"][f]) gdf.loc[[f], ['z-pos']] = z_val print("Z-Val and index to be replaced: ", z_val, gdf["x-pos"][f], f) # Find index of all gdf entries with same position (x-pos) and replace # z-pos there as well: for ix in gdf[gdf["x-pos"] == gdf["x-pos"][f] and gdf["y-pos"] == gdf["y-pos"] [f]].index.tolist(): if len(gdf[gdf["x-pos"] == gdf["x-pos"] [f]].index.tolist()) > 1: gdf.loc[[ix], ['z-pos']] = z_val print("Also adjusted z-val for index ", z_val, gdf["x-pos"][ix], ix) else: # write to gdf: print("Replacing ", z_val, gdf["z-pos"][f]) gdf.loc[[f], ['z-pos']] = z_val return gdf
def convert_tif_to_raster(tif, out='temp.csv'): print('Convert to csv: ', tif) rtxyz = Raster2xyz() rtxyz.translate(tif, out) return out