public string GetSpatialReferenceFromPath(string filepath) { string spatialref = null; Dataset ds = Gdal.Open(filepath, Access.GA_ReadOnly); if (ds == null) { Console.WriteLine(" [-] Could not create dataset from file."); return(spatialref); } spatialref = ds.GetProjectionRef(); ds.Dispose(); return(spatialref); }
public void StartToProcess() { GdalConfiguration.ConfigureGdal(); /* -------------------------------------------------------------------- */ /* Register driver(s). */ /* -------------------------------------------------------------------- */ Gdal.AllRegister(); /* -------------------------------------------------------------------- */ /* Open dataset. */ /* -------------------------------------------------------------------- */ List <string> tiffFiles = GetGeoTiffFiles(); foreach (string servertiffFile in tiffFiles) { string localTiffFile = GetLocalTiffFileName(Path.GetFileNameWithoutExtension(servertiffFile)); string localPngFile = GetLocalPngFileName(Path.GetFileNameWithoutExtension(servertiffFile)); string serverPngFile = servertiffFile.Replace(".tif", ".png"); DownloadGeoTiffFile(servertiffFile, localTiffFile); Dataset ds = Gdal.Open(localTiffFile, Access.GA_Update); if (ds == null) { Console.WriteLine("Can't open " + localTiffFile); throw new Exception(); } Console.WriteLine("Raster dataset parameters:"); Console.WriteLine(" Projection: " + ds.GetProjectionRef()); Console.WriteLine(" RasterCount: " + ds.RasterCount); Console.WriteLine(" RasterSize (" + ds.RasterXSize + "," + ds.RasterYSize + ")"); /* -------------------------------------------------------------------- */ /* Get driver */ /* -------------------------------------------------------------------- */ Driver drv = ds.GetDriver(); if (drv == null) { Console.WriteLine("Can't get driver."); System.Environment.Exit(-1); } Console.WriteLine("Using driver " + drv.LongName); /* -------------------------------------------------------------------- */ /* Get raster band */ /* -------------------------------------------------------------------- */ for (int iBand = 1; iBand <= ds.RasterCount; iBand++) { Band band = ds.GetRasterBand(iBand); Console.WriteLine("Band " + iBand + " :"); Console.WriteLine(" DataType: " + band.DataType); Console.WriteLine(" Size (" + band.XSize + "," + band.YSize + ")"); Console.WriteLine(" PaletteInterp: " + band.GetRasterColorInterpretation().ToString()); for (int iOver = 0; iOver < band.GetOverviewCount(); iOver++) { Band over = band.GetOverview(iOver); Console.WriteLine(" OverView " + iOver + " :"); Console.WriteLine(" DataType: " + over.DataType); Console.WriteLine(" Size (" + over.XSize + "," + over.YSize + ")"); Console.WriteLine(" PaletteInterp: " + over.GetRasterColorInterpretation().ToString()); } } /* -------------------------------------------------------------------- */ /* Processing the raster */ /* -------------------------------------------------------------------- */ //SaveBitmapBuffered(ds, localPngFile, -1); SaveBitmapGrayBuffered(ds, localPngFile, -1); UploadKmzFile(localPngFile, serverPngFile); } //string fileName = "cspp-viirs-flood-globally_20180815_080000.p153435116453720"; //string geoTiffFileName = string.Format(@"D:\Repos\GmuGeo-bak\Resources\VIIRS_floodmap_Aug15_2018\{0}.tif", fileName); //string pngFileName = string.Format(@"D:\Repos\GmuGeo\Examples\png\{0}.png", fileName); }
public void FindNoDataXYCoords(string filepath, string outputdir) { //returns coordinates of points below a certain value, but only if N adjacent //nodes also fall below that value. the goal is to only identify anomalous clusters //of near-0 points ('data voids') Dataset ds = Gdal.Open(filepath, Access.GA_ReadOnly); //Read raster if (ds == null) { Console.WriteLine(" [-] Could not create raster dataset from file."); Environment.Exit(1); } if (ds.RasterCount < 3) { Console.WriteLine(" [-] Need at least a three band raster image."); Environment.Exit(1); ds.Dispose(); Environment.Exit(1); } List <string> OutputCSV = new List <string>(); double[] gt = new double[6]; ds.GetGeoTransform(gt); //Read geo transform info into array int Rows = ds.RasterYSize; int Cols = ds.RasterXSize; Band redband = ds.GetRasterBand(1); Band greenband = ds.GetRasterBand(2); Band blueband = ds.GetRasterBand(3); double startX = gt[0]; //Upper left lon double startY = gt[3]; //Upper left lat double interval = gt[1]; //Cell size double x, y; //Current lon and lat string filename = Path.GetFileNameWithoutExtension(filepath); int adjacencycount = 0; int adjacencythreshold = 25; bool previousrow = false; bool isinvalid = false; int pointsoverflow = 0; for (int k = 0; k < Rows; k++) //read one line { adjacencycount = 0; y = startY - k * interval; //current lat int[][] buf = new int[3][]; buf[0] = new int[Cols]; buf[1] = new int[Cols]; buf[2] = new int[Cols]; int[] previouspixel = new int[3]; //ReadRaster parameters are StartCol, StartRow, ColumnsToRead, RowsToRead, BufferToStoreInto, BufferColumns, BufferRows, 0, 0 try { redband.ReadRaster(0, k, Cols, 1, buf[0], Cols, 1, 0, 0); greenband.ReadRaster(0, k, Cols, 1, buf[1], Cols, 1, 0, 0); blueband.ReadRaster(0, k, Cols, 1, buf[2], Cols, 1, 0, 0); } catch { if (isinvalid == false) { Console.WriteLine("Invalid compression result. Please rerun ImageChecker"); isinvalid = true; } continue; } List <List <double> > pixlists = new List <List <double> >(); bool previous = false; bool hasblackpx = false; bool existingsequence = false; int firstinrow = 1; int sequencenumber = 0; //iterate each item in one line for (int r = 0; r < Cols; r++) { bool isidentical = false; bool isdarkpixel = false; if (buf[0][r] == previouspixel[0] && buf[1][r] == previouspixel[1] && buf[2][r] == previouspixel[2]) { isidentical = true; } if (buf[0][r] <= 15 && buf[1][r] <= 15 && buf[2][r] <= 15) { isdarkpixel = true; } //if we have reached this point then no additional coordinates will be found, so might as well avoid array accesses if (r >= (Cols - adjacencythreshold) && adjacencycount < adjacencythreshold) { break; } if ((isdarkpixel || isidentical) && previous == true) { //only add pixels if they're directly adjacent //this way, you avoid all the errant little shadows that aren't actual data voids //needs reworking x = startX + r * interval; //current lon List <double> potentialresult = new List <double>(); double ydistance = Convert.ToDouble(k); double xdistance = Convert.ToDouble(r); if (previousrow == true) { potentialresult.Add(x); potentialresult.Add(y); potentialresult.Add(ydistance); potentialresult.Add(xdistance); pixlists.Add(potentialresult); } ++adjacencycount; } else { existingsequence = false; adjacencycount = 0; previous = false; if (isdarkpixel || isidentical) { previous = true; } pixlists.Clear(); } if (adjacencycount == 0) { existingsequence = false; pixlists.Clear(); } if (adjacencycount == adjacencythreshold) { hasblackpx = true; if (previousrow == true) //cuts down the # of false positives while preserving "actual" voids { List <double> firstresult = pixlists.First(); List <double> lastresult = pixlists.Last(); double firstx = firstresult[0]; double firsty = firstresult[1]; double firstrow = firstresult[2]; double firstcolumn = firstresult[3]; double lastx = lastresult[0]; double lasty = lastresult[1]; double lastrow = lastresult[2]; double lastcolumn = lastresult[3]; if (existingsequence == true) //save array space by only preserving first/last points in a contiguous sequence { //wish C# had something like pop_back()... OutputCSV.RemoveAt(OutputCSV.Count - 1); } //just in case, don't want to hit OOM if (OutputCSV.Count > 500000) { Console.WriteLine("[-] Found a lot of valid BlackPx. Please consider using -m to mask out shoreline tiles"); string overflowfile = outputdir + "\\" + filename + "_" + pointsoverflow.ToString() + ".csv"; WritePointData(overflowfile, OutputCSV); ResultCoords.csvfiles.Add(overflowfile); pointsoverflow += 1; OutputCSV.Clear(); } //edge case if (sequencenumber == Int32.MaxValue) { Console.WriteLine("[-] Found too many unique patterns. Please consider using -m to mask out shoreline tiles"); Console.WriteLine(" Reducing results size, may skew results"); ReduceXYList(OutputCSV); } if (existingsequence == false) //another optimization { sequencenumber += 1; string firstline = string.Format("{0},{1},{2},{3},{4},{5},{6}{7}", firstx, firsty, firstrow, firstcolumn, sequencenumber, firstinrow, filename, Environment.NewLine); OutputCSV.Add(firstline); } if (existingsequence == false && firstinrow == 1) { firstinrow = 0; } string lastline = string.Format("{0},{1},{2},{3},{4},{5},{6}{7}", lastx, lasty, lastrow, lastcolumn, sequencenumber, firstinrow, filename, Environment.NewLine); OutputCSV.Add(lastline); } pixlists.Clear(); adjacencycount = 0; existingsequence = true; } previouspixel[0] = buf[0][r]; previouspixel[1] = buf[1][r]; previouspixel[2] = buf[2][r]; } if (hasblackpx == true) { previousrow = true; } else { previousrow = false; } } if (OutputCSV.Any()) { string outputfile = outputdir + "\\" + filename + ".csv"; WritePointData(outputfile, OutputCSV); ResultCoords.csvfiles.Add(outputfile); } ds.Dispose(); }