Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            //verify gdal dlls are available; see: http://trac.osgeo.org/gdal/wiki/GdalOgrCsharpUsage
            string GDAL_HOME = @";c:\Program Files (x86)\FWTools2.4.7\bin";
            string path = Environment.GetEnvironmentVariable("PATH");
            path += ";" + GDAL_HOME;
            SetEnvironmentVariable("PATH", path);

            // string tifDirectory = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
            string tifDirectory = @"C:\dev\quito\For_Geoportal\For_Geoportal\Climate\";
            string resultDirectory = tifDirectory + @"out\";

            if (args.Length == 1)
            {
                tifDirectory = args[0];
            }

            Gdal.AllRegister();
            OSGeo.GDAL.Driver srcDrv = Gdal.GetDriverByName("GTiff");

            //if app scales, or additional implementations made (e.g. database table), use DI for this dependency
            IColorRepository colorRepo = new ColorRepository();

            //see: http://sharpmap.codeplex.com/discussions/421752
            GeoAPI.GeometryServiceProvider.Instance = new NetTopologySuite.NtsGeometryServices();

            Console.WriteLine(tifDirectory);
            DirectoryInfo di = new DirectoryInfo(tifDirectory);
            DirectoryInfo resultDir = new DirectoryInfo(resultDirectory);
            if (!resultDir.Exists)
            {
                resultDir.Create();
            }
            Console.WriteLine("Processing {0} files matching '*.tif'");
            foreach (FileInfo fi in di.GetFiles("*.tif"))
            {
                Console.WriteLine("Processing " + fi.Name + "...");

                //read data from source raster
                Dataset src = Gdal.Open(fi.FullName, Access.GA_ReadOnly);
                Band band = src.GetRasterBand(1);
                float[] r = new float[band.XSize * band.YSize];
                byte[] red = new byte[band.XSize * band.YSize];
                byte[] green = new byte[band.XSize * band.YSize];
                byte[] blue = new byte[band.XSize * band.YSize];
                band.ReadRaster(0, 0, band.XSize, band.YSize, r, band.XSize, band.YSize, 0, 0);

                //assign values to rgb rasters to produce new raster with rgb bands matching color pattern
                for (int cell = 0; cell < r.Length; cell++)
                {
                    RGBColors colors = colorRepo.ColorsOfValueInFile(fi.Name, r[cell]);
                    red[cell] = (byte)colors.Red;
                    green[cell] = (byte)colors.Green;
                    blue[cell] = (byte)colors.Blue;
                }

                //write new output
                using (Dataset output = srcDrv.Create(resultDirectory + fi.Name, band.XSize, band.YSize, 3, DataType.GDT_Byte, null))
                {
                    if (output == null)
                    {
                        Console.WriteLine("Can't create " + args[0]);
                        System.Environment.Exit(-1);
                    }
                    //set metadata
                    output.SetProjection(src.GetProjection());
                    double[] geotransform = new double[0];
                    src.GetGeoTransform(geotransform);
                    output.SetGeoTransform(geotransform);

                    //prepare data for write
                    int[] colorData = new int[red.Length * 3];
                    red.CopyTo(colorData, 0);
                    green.CopyTo(colorData, red.Length);
                    blue.CopyTo(colorData, red.Length + green.Length);

                    //write data to disk
                    output.WriteRaster(0, 0, band.XSize,  band.YSize, colorData, band.XSize, band.YSize, 3, null, 0, 0, 0);
                    output.FlushCache();
                }
            }
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            //validate input
            /*if (args.Length != 2)
            {
                Console.WriteLine(string.Join(",", args));
                throw new Exception("Required arguments are: path/to/parent-directory-of-gis-data path/to/app-folder-of-quito-climate-study-project");
            }*/

            #if DEBUG

            string srcDir = @"C:\data\Geoportal_Test";// @"C:\dev\quito\For_Geoportal_WGS\For_Geoportal_WGS\";// args[0];// @"C:\dev\quito\For_Geoportal_WGS\For_Geoportal_WGS\";
            string resultDir = @"C:\data\result"; //  @"C:\dev\quito\buildtest"; //args[1];
            #else

            string srcDir = args[0].Trim(); // @"C:\dev\quito\For_Geoportal_WGS\For_Geoportal_WGS\";// args[0];// @"C:\dev\quito\For_Geoportal_WGS\For_Geoportal_WGS\";
            string resultDir = args[1].Trim(); //  @"C:\dev\quito\buildtest"; //args[1];
            #endif
            if (!srcDir.EndsWith("\\"))
            {
                srcDir += "\\";
            }
            if (!resultDir.EndsWith("\\"))
            {
                resultDir += "\\";
            }

            Console.WriteLine("\n");
            Console.WriteLine("Source:");
            Console.WriteLine(srcDir);
            Console.WriteLine("Destination:");
            Console.WriteLine(resultDir);

            //verify gdal dlls are available; see: http://trac.osgeo.org/gdal/wiki/GdalOgrCsharpUsage
            string GDAL_HOME = @";c:\Program Files (x86)\FWTools2.4.7\bin";
            string path = Environment.GetEnvironmentVariable("PATH");
            path += ";" + GDAL_HOME;
            SetEnvironmentVariable("PATH", path);

            //register gdal extensions
            Gdal.AllRegister();

            string copyToOut = @"";
            string rasterout = @"raster\";
            string vectorout = @"vector\";
            string legendout = @"legend\";

            string tifResultDir = resultDir + copyToOut + rasterout;
            string shpResultDir = resultDir + copyToOut + vectorout;
            DirectoryInfo copyTo = new DirectoryInfo(srcDir + copyToOut);
            if (!copyTo.Exists)
            {
                copyTo.Create();
            }

            DirectoryInfo legResultDir = new DirectoryInfo(resultDir + copyToOut + legendout);

            if (args.Length == 1)
            {
                srcDir = args[0];
            }

            IColorRepository colorRepo = new ColorRepository();
            LegendRepository legend = new LegendRepository();

            //see: http://sharpmap.codeplex.com/discussions/421752
            GeoAPI.GeometryServiceProvider.Instance = new NetTopologySuite.NtsGeometryServices();

            List<string> processedRasters = new List<string>();
            List<string> processedVectors = new List<string>();
            Stopwatch sw = new Stopwatch();
            sw.Start();

            Tuple<List<string>, List<string>> rasterResults = new Tuple<List<string>,List<string>>(new List<string>(), new List<string>());
            Tuple<List<string>, List<string>> gridResults = new Tuple<List<string>,List<string>>(new List<string>(), new List<string>());
            Tuple<List<string>, List<string>> vectorResults = new Tuple<List<string>,List<string>>(new List<string>(), new List<string>());
            //compute files to publish to web
            try
            {
                Console.WriteLine("Processing raster files...");
                rasterResults = ProcessDatasets(args, srcDir, tifResultDir, colorRepo, legend, ".tif", processedRasters);
                processedRasters = rasterResults.Item1;
            }
            catch (Exception rastEx)
            {
                Console.WriteLine("Exception processing raster files ");
                Console.WriteLine(rastEx.Message);
                Console.WriteLine(rastEx.InnerException.ToString());
            }

            try
            {
                Console.WriteLine("Processing grid files...");
                gridResults = ProcessDatasets(args, srcDir, tifResultDir, colorRepo, legend, "hdr.adf", processedRasters);
                processedRasters.AddRange(gridResults.Item1);
            }
            catch (Exception gridEx)
            {
                Console.WriteLine("Exception processing grid files ");
                Console.WriteLine(gridEx.Message);
                Console.WriteLine(gridEx.InnerException.ToString());
            }

            try
            {
                Console.WriteLine("Processing vector files...");
                vectorResults = ProcessDatasets(args, srcDir, shpResultDir, colorRepo, legend, ".shp", processedVectors);
                processedVectors = vectorResults.Item1;
            }
            catch (Exception vex)
            {
                Console.WriteLine("Exception processing vector files ");
                Console.WriteLine(vex.Message);
                Console.WriteLine(vex.InnerException.ToString());
            }

            //output legend file
            if (!legResultDir.Exists)
            {
                legResultDir.Create();
            }
            File.WriteAllText(legResultDir.FullName + "legend.json", JsonConvert.SerializeObject(legend));

            //reporting
            sw.Stop();
            Console.WriteLine("*********");
            Console.WriteLine("Unknown raster color maps in colormap.json, these files were not processed:");
            rasterResults.Item2.ForEach(a => Console.WriteLine("=> " + a));
            Console.WriteLine("Unknown vector color maps in colormap.json, these files were not processed:");
            vectorResults.Item2.ForEach(a => Console.WriteLine("=> " + a));
            Console.WriteLine("Finished processing {0} datasets in {1} seconds. Press any key to close.", processedRasters.Count() + processedVectors.Count(), sw.Elapsed.TotalSeconds);
            Console.ReadKey();
        }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            //verify gdal dlls are available; see: http://trac.osgeo.org/gdal/wiki/GdalOgrCsharpUsage
            string GDAL_HOME = @";c:\Program Files (x86)\FWTools2.4.7\bin";
            string path      = Environment.GetEnvironmentVariable("PATH");

            path += ";" + GDAL_HOME;
            SetEnvironmentVariable("PATH", path);

            // string tifDirectory = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
            string tifDirectory    = @"C:\dev\quito\For_Geoportal\For_Geoportal\Climate\";
            string resultDirectory = tifDirectory + @"out\";

            if (args.Length == 1)
            {
                tifDirectory = args[0];
            }

            Gdal.AllRegister();
            OSGeo.GDAL.Driver srcDrv = Gdal.GetDriverByName("GTiff");

            //if app scales, or additional implementations made (e.g. database table), use DI for this dependency
            IColorRepository colorRepo = new ColorRepository();

            //see: http://sharpmap.codeplex.com/discussions/421752
            GeoAPI.GeometryServiceProvider.Instance = new NetTopologySuite.NtsGeometryServices();

            Console.WriteLine(tifDirectory);
            DirectoryInfo di        = new DirectoryInfo(tifDirectory);
            DirectoryInfo resultDir = new DirectoryInfo(resultDirectory);

            if (!resultDir.Exists)
            {
                resultDir.Create();
            }
            Console.WriteLine("Processing {0} files matching '*.tif'");
            foreach (FileInfo fi in di.GetFiles("*.tif"))
            {
                Console.WriteLine("Processing " + fi.Name + "...");

                //read data from source raster
                Dataset src   = Gdal.Open(fi.FullName, Access.GA_ReadOnly);
                Band    band  = src.GetRasterBand(1);
                float[] r     = new float[band.XSize * band.YSize];
                byte[]  red   = new byte[band.XSize * band.YSize];
                byte[]  green = new byte[band.XSize * band.YSize];
                byte[]  blue  = new byte[band.XSize * band.YSize];
                band.ReadRaster(0, 0, band.XSize, band.YSize, r, band.XSize, band.YSize, 0, 0);

                //assign values to rgb rasters to produce new raster with rgb bands matching color pattern
                for (int cell = 0; cell < r.Length; cell++)
                {
                    RGBColors colors = colorRepo.ColorsOfValueInFile(fi.Name, r[cell]);
                    red[cell]   = (byte)colors.Red;
                    green[cell] = (byte)colors.Green;
                    blue[cell]  = (byte)colors.Blue;
                }

                //write new output
                using (Dataset output = srcDrv.Create(resultDirectory + fi.Name, band.XSize, band.YSize, 3, DataType.GDT_Byte, null))
                {
                    if (output == null)
                    {
                        Console.WriteLine("Can't create " + args[0]);
                        System.Environment.Exit(-1);
                    }
                    //set metadata
                    output.SetProjection(src.GetProjection());
                    double[] geotransform = new double[0];
                    src.GetGeoTransform(geotransform);
                    output.SetGeoTransform(geotransform);

                    //prepare data for write
                    int[] colorData = new int[red.Length * 3];
                    red.CopyTo(colorData, 0);
                    green.CopyTo(colorData, red.Length);
                    blue.CopyTo(colorData, red.Length + green.Length);

                    //write data to disk
                    output.WriteRaster(0, 0, band.XSize, band.YSize, colorData, band.XSize, band.YSize, 3, null, 0, 0, 0);
                    output.FlushCache();
                }
            }
        }