private void ShowGridLayerInMap(GridLayer grid, int intervals, double cutoff, IGridLayerBitmapOptions options, string filename = null)
        {
            this.InvokeIfRequired(() => {
                var prefix = grid.Name;
                FileInfo f = new FileInfo(grid.Name);
                if (f.Exists)
                {
                    prefix = f.Name.Substring(0, f.Name.LastIndexOf("."));
                }

                var map = PluginManager.Instance.GetMap();

                if (filename == null)
                {
                    filename = LayerImageGenerator.GenerateTemporaryImageFile(grid, prefix, options.LowColor, options.HighColor, options.NoValueColor, cutoff, intervals);
                }
                else
                {
                    map.RemoveRasterLayer(filename);
                    LayerImageGenerator.CreateImageFileFromGrid(grid, filename, options.LowColor, options.HighColor, options.NoValueColor, cutoff, intervals);
                }


                map.Show();
                map.AddRasterLayer(filename);
            });
        }
        private void ShowGridLayerImage(string filename)
        {
            var layer = new GridLayer(filename);
            var image = filename = LayerImageGenerator.GenerateTemporaryImageFile(layer, "layer_", _richnessOptions.LowColor, _richnessOptions.HighColor, _richnessOptions.NoValueColor, 0, 255);

            SystemUtils.ShellExecute(image);
        }
        public bool MatchesResolution(GridLayer other)
        {
            if (this.Width != other.Width)
            {
                return(false);
            }
            if (Height != other.Height)
            {
                return(false);
            }
            if (Latitude0 != other.Latitude0)
            {
                return(false);
            }
            if (Longitude0 != other.Longitude0)
            {
                return(false);
            }
            if (DeltaLatitude != other.DeltaLatitude)
            {
                return(false);
            }
            if (DeltaLongitude != other.DeltaLongitude)
            {
                return(false);
            }

            return(true);
        }
        private void ShowGridLayerProperties(GridLayer layer)
        {
            var frm = new GridLayerProperties(layer);

            frm.Owner = this.FindParentWindow();
            frm.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            frm.ShowDialog();
        }
        public static string GenerateTemporaryImageFile(GridLayer layer, string filenamePrefix, Color lowcolor, Color highcolor, Color novaluecolor, double cutoff = 0, int intervals = 256)
        {
            var filename = TempFileManager.NewTempFilename("bmp", filenamePrefix);

            CreateImageFileFromGrid(layer, filename, lowcolor, highcolor, novaluecolor, cutoff, intervals);
            var image = LayerImageGenerator.GetImageForLayer(layer, lowcolor, highcolor, novaluecolor, cutoff, intervals);

            TempFileManager.Attach(filename + "w");
            return(filename);
        }
        public static BitmapSource GetImageForLayer(GridLayer layer, Color lowcolor, Color highcolor, Color novaluecolor, double cutoff = 0, int intervals = 256)
        {
            var palette = CreateGradientPalette(lowcolor, highcolor, novaluecolor, intervals);
            var bmp     = new WriteableBitmap(layer.Width, layer.Height, 96, 96, PixelFormats.Indexed8, palette);

            var range = layer.GetRange();

            if (cutoff == 0 && range != null && range.Range > 0)
            {
                cutoff = range.Min;
            }

            double max = 0;

            if (range != null)
            {
                max = range.Max;
            }

            double dx = Math.Abs(max - cutoff) / (intervals - 1);

            byte[] array = new byte[layer.Width * layer.Height];
            byte   index = 0;

            for (int y = 0; y < layer.Height; y++)
            {
                for (int x = 0; x < layer.Width; x++)
                {
                    var value = layer.GetCellValue(x, (layer.Height - 1) - y);
                    if (value == layer.NoValueMarker)
                    {
                        index = 0;
                    }
                    else
                    {
                        if (value >= cutoff || cutoff == 0)
                        {
                            index = (byte)(((value - cutoff) / dx) + 1);
                        }
                        else
                        {
                            index = 0;
                        }
                    }
                    array[(y * layer.Width) + x] = index;
                }
            }

            Int32Rect r = new Int32Rect(0, 0, layer.Width, layer.Height);

            bmp.WritePixels(r, array, layer.Width, 0, 0);

            return(bmp);
        }
        private void SaveAsASCIIFile(GridLayer grid)
        {
            if (grid == null)
            {
                return;
            }

            var dlg = new SaveFileDialog();

            dlg.Filter = "ASC Files (*.asc)|*.asc|All files (*.*)|*.*";
            if (dlg.ShowDialog(this.FindParentWindow()) == true)
            {
                grid.SaveToASCIIFile(dlg.FileName);
            }
        }
        public static void CreateWorldFile(GridLayer layer, string filename)
        {
            double lat0;

            lat0 = layer.Latitude0 + (layer.DeltaLatitude * layer.Height);

            using (FileStream fs = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.Write)) {
                using (var writer = new StreamWriter(fs)) {
                    writer.WriteLine("{0}", layer.DeltaLongitude);
                    writer.WriteLine("0");
                    writer.WriteLine("0");
                    writer.WriteLine("{0}", -layer.DeltaLatitude);
                    writer.WriteLine("{0}", layer.Longitude0);
                    writer.WriteLine("{0}", lat0);
                }
            }
        }
Ejemplo n.º 9
0
        public static void CreateImageFileFromGrid(GridLayer layer, string filename, Color lowcolor, Color highcolor, Color novaluecolor, double cutoff = 0, int intervals = 256)
        {
            var image = LayerImageGenerator.GetImageForLayer(layer, lowcolor, highcolor, novaluecolor, cutoff, intervals);

            BitmapEncoder encoder = new BmpBitmapEncoder();
            encoder.Frames.Add(BitmapFrame.Create(image));
            if (File.Exists(filename)) {
                try {
                    File.Delete(filename);
                } catch (Exception) {
                    // ignore
                }

            }

            using (FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.Write)) {
                encoder.Save(fs);
            }

            var worldFilename = filename + "w";
            CreateWorldFile(layer, worldFilename);
        }
        public GridLayerProperties(GridLayer layer)
        {
            InitializeComponent();
            this.Layer = layer;
            _model     = new List <KeyValuePair <string, object> >();

            _model.Add(new KeyValuePair <string, object>("Width", layer.Width));
            _model.Add(new KeyValuePair <string, object>("Height", layer.Height));
            _model.Add(new KeyValuePair <string, object>("Top (Latitude)", layer.Latitude0));
            _model.Add(new KeyValuePair <string, object>("Left (Longitude)", layer.Longitude0));
            _model.Add(new KeyValuePair <string, object>("Cell size X", layer.DeltaLongitude));
            _model.Add(new KeyValuePair <string, object>("Cell size Y", layer.DeltaLatitude));

            var range = layer.GetRange();

            _model.Add(new KeyValuePair <string, object>("Minimum value", range.Min));
            _model.Add(new KeyValuePair <string, object>("Maximum value", range.Max));
            _model.Add(new KeyValuePair <string, object>("Range", range.Range));
            _model.Add(new KeyValuePair <string, object>("'No Value' value", layer.NoValueMarker));

            lvw.ItemsSource = _model;
        }
Ejemplo n.º 11
0
        public GridLayerProperties(GridLayer layer)
        {
            InitializeComponent();
            this.Layer = layer;
            _model = new List<KeyValuePair<string, object>>();

            _model.Add(new KeyValuePair<string, object>("Width", layer.Width));
            _model.Add(new KeyValuePair<string, object>("Height", layer.Height));
            _model.Add(new KeyValuePair<string, object>("Top (Latitude)", layer.Latitude0));
            _model.Add(new KeyValuePair<string, object>("Left (Longitude)", layer.Longitude0));
            _model.Add(new KeyValuePair<string, object>("Cell size X", layer.DeltaLongitude));
            _model.Add(new KeyValuePair<string, object>("Cell size Y", layer.DeltaLatitude));

            var range = layer.GetRange();

            _model.Add(new KeyValuePair<string, object>("Minimum value", range.Min));
            _model.Add(new KeyValuePair<string, object>("Maximum value", range.Max));
            _model.Add(new KeyValuePair<string, object>("Range", range.Range));
            _model.Add(new KeyValuePair<string, object>("'No Value' value", layer.NoValueMarker));

            lvw.ItemsSource = _model;
        }
        public static void CreateImageFileFromGrid(GridLayer layer, string filename, Color lowcolor, Color highcolor, Color novaluecolor, double cutoff = 0, int intervals = 256)
        {
            var image = LayerImageGenerator.GetImageForLayer(layer, lowcolor, highcolor, novaluecolor, cutoff, intervals);

            BitmapEncoder encoder = new BmpBitmapEncoder();

            encoder.Frames.Add(BitmapFrame.Create(image));
            if (File.Exists(filename))
            {
                try {
                    File.Delete(filename);
                } catch (Exception) {
                    // ignore
                }
            }

            using (FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.Write)) {
                encoder.Save(fs);
            }

            var worldFilename = filename + "w";

            CreateWorldFile(layer, worldFilename);
        }
Ejemplo n.º 13
0
 protected abstract void RunModelImpl(GridLayer targetLayer, IEnumerable <GridLayer> layers, ModelPointSet points);
        protected override void RunModelImpl(GridLayer targetLayer, IEnumerable <GridLayer> layers, ModelPointSet points)
        {
            double fVal, fDestNoVal;
            bool   bTestFailed;

            // Work out ranges for the varying percentile bands across all layers...
            if (layers.Count() <= 0)
            {
                return;
            }

            List <EnvironmentalLayerRange[]> Range = new List <EnvironmentalLayerRange[]>();

            foreach (GridLayer layer in layers)
            {
                var array = new EnvironmentalLayerRange[4];
                array[0] = layer.GetRangeForPoints(points.Points, 0.0);
                array[1] = layer.GetRangeForPoints(points.Points, 0.05);
                array[2] = layer.GetRangeForPoints(points.Points, 0.1);
                array[3] = layer.GetRangeForPoints(points.Points, 0.25);
                Range.Add(array);
            }

            // For each cell for each layer...
            var firstLayer = layers.ElementAt(0);

            fDestNoVal = firstLayer.NoValueMarker;

            targetLayer.SetAllCells(fDestNoVal);

            for (int y = 0; y < firstLayer.Height; ++y)
            {
                for (int x = 0; x < firstLayer.Width; ++x)
                {
                    // First test %0 - %100
                    bTestFailed = false;
                    for (int i = 0; i < layers.Count(); ++i)
                    {
                        var layer = layers.ElementAt(i);
                        fVal = layer.GetCellValue(x, y);
                        if (fVal == layer.NoValueMarker)
                        {
                            bTestFailed = true;
                            break;
                        }
                        else
                        {
                            if ((fVal < Range[i][0].Min) || (fVal > Range[i][0].Max))   // %0-%100
                            {
                                bTestFailed = true;
                                targetLayer.SetCellValue(x, y, fDestNoVal);
                                break;
                            }
                        }
                    }

                    if (!bTestFailed)
                    {
                        targetLayer.SetCellValue(x, y, 1);
                        // Second test %5 - %95
                        for (int i = 0; i < layers.Count(); ++i)
                        {
                            var layer = layers.ElementAt(i);
                            fVal = layer.GetCellValue(x, y);
                            if ((fVal < Range[i][1].Min) || (fVal > Range[i][1].Max))   // %5-%95
                            {
                                bTestFailed = true;
                                break;
                            }
                        }

                        if (!bTestFailed)
                        {
                            targetLayer.SetCellValue(x, y, 2);
                            for (int i = 0; i < layers.Count(); ++i)
                            {
                                var layer = layers.ElementAt(i);
                                fVal = layer.GetCellValue(x, y);
                                if ((fVal < Range[i][2].Min) || (fVal > Range[i][2].Max))   // %10-%90
                                {
                                    bTestFailed = true;
                                    break;
                                }
                            }

                            if (!bTestFailed)
                            {
                                targetLayer.SetCellValue(x, y, 3);
                                for (int i = 0; i < layers.Count(); ++i)
                                {
                                    var layer = layers.ElementAt(i);
                                    fVal = layer.GetCellValue(x, y);
                                    if ((fVal < Range[i][3].Min) || (fVal > Range[i][3].Max))   // %25-%75
                                    {
                                        bTestFailed = true;
                                        break;
                                    }
                                }

                                if (!bTestFailed)
                                {
                                    targetLayer.SetCellValue(x, y, 4);
                                }
                            }
                        }
                    }
                }

                if ((y % 20) == 0)
                {
                    int percent = (int)(((float)y / (float)targetLayer.Height) * (float)100);
                    if (ProgressObserver != null)
                    {
                        ProgressObserver.ProgressMessage("Running BOXCAR Model...", percent);
                    }

                    if (IsCancelled)
                    {
                        return;
                    }
                }
            }

            if (ProgressObserver != null)
            {
                ProgressObserver.ProgressMessage("Running BOXCAR Model...", 100);
            }
        }
        public GridLayer RunSpeciesRichness()
        {
            // First load each of the layers...

            IsCancelled = false;

            var layers = new List <GridLayer>();

            foreach (string filename in LayerFiles)
            {
                ProgressMessage("Loading environmental layer {0}...", filename);
                layers.Add(new GridLayer(filename));
            }

            // Now for each point set, run the model...
            var resultLayers = new List <GridLayer>();

            Model.ProgressObserver = this;

            if (ProgressObserver != null)
            {
                ProgressObserver.ProgressMessage("Running models...");
            }

            var first = layers[0];

            int setIndex = 0;

            foreach (SpeciesRichnessPointSetViewModel pointset in MapPointSets)
            {
                _currentPointSet = pointset.Model;

                var percent = ((double)setIndex / (double)MapPointSets.Count()) * 100.0;

                if (ProgressObserver != null)
                {
                    ProgressObserver.ProgressMessage("Running model on pointset " + pointset.Name, percent);
                }

                FireStartPointSet(pointset.Model);

                var list = new List <MapPointSet>();
                list.Add(pointset.Model);
                var modelLayer = Model.RunModel(layers, list);
                if (modelLayer != null)
                {
                    if (RetainLayers)
                    {
                        pointset.Filename = string.Format("{0}{1}_{2}.grd", TempFileManager.GetTemporaryFolder(), SystemUtils.StripIllegalFilenameChars(pointset.Name), setIndex);
                        modelLayer.SaveToGRDFile(pointset.Filename);
                    }

                    resultLayers.Add(modelLayer);
                }

                if (IsCancelled)
                {
                    return(null);
                }

                FireEndPointSet(pointset.Model);

                setIndex++;
            }

            var target = new GridLayer(first.Width, first.Height)
            {
                DeltaLatitude = first.DeltaLatitude, DeltaLongitude = first.DeltaLongitude, Flags = first.Flags, Latitude0 = first.Latitude0, Longitude0 = first.Longitude0, NoValueMarker = 0
            };

            target.SetAllCells(0);
            foreach (GridLayer result in resultLayers)
            {
                for (int y = 0; y < target.Height; ++y)
                {
                    var lat = target.Latitude0 + (y * target.DeltaLatitude);            // Work out Lat. of this cell.
                    for (int x = 0; x < target.Width; ++x)
                    {
                        var lon  = target.Longitude0 + (x * target.DeltaLongitude); // Work out Long. of this cell.
                        var fVal = result.GetValueAt(lat, lon, result.NoValueMarker);
                        if (fVal == result.NoValueMarker)
                        {
                            target.SetCellValue(x, y, target.NoValueMarker);
                        }
                        else
                        {
                            if (fVal > CutOff)
                            {
                                var currentVal = target.GetCellValue(x, y);
                                target.SetCellValue(x, y, currentVal + 1);
                            }
                        }
                    }
                }
            }

            return(target);
        }
Ejemplo n.º 16
0
        public GridLayer RunModel(IEnumerable <GridLayer> layers, IEnumerable <MapPointSet> pointSets)
        {
            IsCancelled = false;

            GridLayer target = null;
            var       list   = new List <GridLayer>();

            if (layers.Count() > 0)
            {
                ProgressMessage("Initializing Model...");
                // The first layer sets the size and resolution of the resulting grid. Subsequent grid layers of different dimensions must be interpolated to fit this size...
                var first = layers.ElementAt(0);
                target = new GridLayer(first.Width, first.Height)
                {
                    DeltaLatitude = first.DeltaLatitude, DeltaLongitude = first.DeltaLongitude, Flags = first.Flags, Latitude0 = first.Latitude0, Longitude0 = first.Longitude0, NoValueMarker = first.NoValueMarker
                };
                list.Add(first);
                for (int i = 1; i < layers.Count(); ++i)
                {
                    var layer = layers.ElementAt(i);
                    if (!first.MatchesResolution(layer))
                    {
                        ProgressMessage("Resizing layer {0}...", layer.Name);
                        var newlayer = new GridLayer(first.Width, first.Height)
                        {
                            DeltaLatitude = first.DeltaLatitude, DeltaLongitude = first.DeltaLongitude, Flags = first.Flags, Latitude0 = first.Latitude0, Longitude0 = first.Longitude0, NoValueMarker = first.NoValueMarker
                        };
                        for (int y = 0; y < first.Height; ++y)
                        {
                            double lat = first.Latitude0 + (y * first.DeltaLatitude);           // Work out Lat. of this cell.
                            for (int x = 0; x < first.Width; ++x)
                            {
                                double lon = first.Longitude0 + (x * first.DeltaLongitude); // Work out Long. of this cell.
                                newlayer.SetCellValue(x, y, layer.GetValueAt(lat, lon, first.NoValueMarker));
                            }
                        }
                        list.Add(newlayer);
                    }
                    else
                    {
                        list.Add(layer);
                    }
                }

                // now get the points ready...
                var points = new ModelPointSet();
                ProgressMessage("Preparing points...");
                foreach (MapPointSet set in pointSets)
                {
                    foreach (MapPoint p in set)
                    {
                        if (p.Latitude == 0 && p.Longitude == 0)
                        {
                            continue;
                        }

                        double fudge = (double)(first.DeltaLatitude / 2.0);

                        var x = Math.Abs((int)((p.Longitude - (first.Longitude0 - fudge)) / first.DeltaLongitude));
                        var y = Math.Abs((int)((p.Latitude - (first.Latitude0 - fudge)) / first.DeltaLatitude));

                        if (!points.ContainsCell(x, y))
                        {
                            points.AddPoint(new ModelPoint(layers.Count())
                            {
                                X = p.Longitude, Y = p.Latitude, CellX = x, CellY = y
                            });
                        }
                    }
                }

                if (!IsCancelled)
                {
                    if (ProgressObserver != null)
                    {
                        ProgressObserver.ProgressStart("Running model", false);
                    }
                    RunModelImpl(target, layers, points);
                    if (ProgressObserver != null)
                    {
                        if (IsCancelled)
                        {
                            ProgressObserver.ProgressEnd("Model cancelled!");
                        }
                        else
                        {
                            ProgressObserver.ProgressEnd("Model complete");
                        }
                    }
                }
            }

            return(IsCancelled ? null : target);
        }
Ejemplo n.º 17
0
        public bool MatchesResolution(GridLayer other)
        {
            if (this.Width != other.Width) return false;
            if (Height != other.Height) return false;
            if (Latitude0 != other.Latitude0) return false;
            if (Longitude0 != other.Longitude0) return false;
            if (DeltaLatitude != other.DeltaLatitude) return false;
            if (DeltaLongitude != other.DeltaLongitude) return false;

            return true;
        }
Ejemplo n.º 18
0
        protected override void RunModelImpl(GridLayer targetLayer, IEnumerable<GridLayer> layers, ModelPointSet points)
        {
            double fVal, fDestNoVal;
            bool bTestFailed;

            // Work out ranges for the varying percentile bands across all layers...
            if (layers.Count() <= 0) {
                return;
            }

            List<EnvironmentalLayerRange[]> Range = new List<EnvironmentalLayerRange[]>();
            foreach (GridLayer layer in layers) {
                var array = new EnvironmentalLayerRange[4];
                array[0] = layer.GetRangeForPoints(points.Points, 0.0);
                array[1] = layer.GetRangeForPoints(points.Points, 0.05);
                array[2] = layer.GetRangeForPoints(points.Points, 0.1);
                array[3] = layer.GetRangeForPoints(points.Points, 0.25);
                Range.Add(array);
            }

            // For each cell for each layer...
            var firstLayer = layers.ElementAt(0);
            fDestNoVal = firstLayer.NoValueMarker;

            targetLayer.SetAllCells(fDestNoVal);

            for (int y = 0; y < firstLayer.Height; ++y) {
                for (int x = 0; x < firstLayer.Width; ++x) {
                    // First test %0 - %100
                    bTestFailed = false;
                    for (int i = 0; i < layers.Count(); ++i) {
                        var layer = layers.ElementAt(i);
                        fVal = layer.GetCellValue(x, y);
                        if (fVal == layer.NoValueMarker) {
                            bTestFailed = true;
                            break;
                        } else {
                            if ((fVal < Range[i][0].Min) || (fVal > Range[i][0].Max)) { // %0-%100
                                bTestFailed = true;
                                targetLayer.SetCellValue(x, y, fDestNoVal);
                                break;
                            }
                        }
                    }

                    if (!bTestFailed) {

                        targetLayer.SetCellValue(x, y, 1);
                        // Second test %5 - %95
                        for (int i = 0; i < layers.Count(); ++i) {
                            var layer = layers.ElementAt(i);
                            fVal = layer.GetCellValue(x, y);
                            if ((fVal < Range[i][1].Min) || (fVal > Range[i][1].Max)) { // %5-%95
                                bTestFailed = true;
                                break;
                            }
                        }

                        if (!bTestFailed) {
                            targetLayer.SetCellValue(x, y, 2);
                            for (int i = 0; i < layers.Count(); ++i) {
                                var layer = layers.ElementAt(i);
                                fVal = layer.GetCellValue(x, y);
                                if ((fVal < Range[i][2].Min) || (fVal > Range[i][2].Max)) { // %10-%90
                                    bTestFailed = true;
                                    break;
                                }
                            }

                            if (!bTestFailed) {
                                targetLayer.SetCellValue(x, y, 3);
                                for (int i = 0; i < layers.Count(); ++i) {
                                    var layer = layers.ElementAt(i);
                                    fVal = layer.GetCellValue(x, y);
                                    if ((fVal < Range[i][3].Min) || (fVal > Range[i][3].Max)) { // %25-%75
                                        bTestFailed = true;
                                        break;
                                    }
                                }

                                if (!bTestFailed) {
                                    targetLayer.SetCellValue(x, y, 4);
                                }
                            }
                        }
                    }
                }

                if ((y % 20) == 0) {
                    int percent = (int)(((float)y / (float)targetLayer.Height) * (float)100);
                    if (ProgressObserver != null) {
                        ProgressObserver.ProgressMessage("Running BOXCAR Model...", percent);
                    }

                    if (IsCancelled) {
                        return;
                    }
                }
            }

            if (ProgressObserver != null) {
                ProgressObserver.ProgressMessage("Running BOXCAR Model...", 100);
            }
        }
Ejemplo n.º 19
0
 public static string GenerateTemporaryImageFile(GridLayer layer, string filenamePrefix, Color lowcolor, Color highcolor, Color novaluecolor, double cutoff = 0, int intervals = 256)
 {
     var filename = TempFileManager.NewTempFilename("bmp", filenamePrefix);
     CreateImageFileFromGrid(layer, filename, lowcolor, highcolor, novaluecolor, cutoff, intervals);
     var image = LayerImageGenerator.GetImageForLayer(layer, lowcolor, highcolor, novaluecolor, cutoff, intervals);
     TempFileManager.Attach(filename + "w");
     return filename;
 }
Ejemplo n.º 20
0
        public GridLayer RunModel(IEnumerable<GridLayer> layers, IEnumerable<MapPointSet> pointSets)
        {
            IsCancelled = false;

            GridLayer target = null;
            var list = new List<GridLayer>();
            if (layers.Count() > 0) {
                ProgressMessage("Initializing Model...");
                // The first layer sets the size and resolution of the resulting grid. Subsequent grid layers of different dimensions must be interpolated to fit this size...
                var first = layers.ElementAt(0);
                target = new GridLayer(first.Width, first.Height) { DeltaLatitude = first.DeltaLatitude, DeltaLongitude = first.DeltaLongitude, Flags = first.Flags, Latitude0 = first.Latitude0, Longitude0 = first.Longitude0, NoValueMarker = first.NoValueMarker };
                list.Add(first);
                for (int i = 1; i < layers.Count(); ++i) {
                    var layer = layers.ElementAt(i);
                    if (!first.MatchesResolution(layer)) {
                        ProgressMessage("Resizing layer {0}...", layer.Name);
                        var newlayer = new GridLayer(first.Width, first.Height) { DeltaLatitude = first.DeltaLatitude, DeltaLongitude = first.DeltaLongitude, Flags = first.Flags, Latitude0 = first.Latitude0, Longitude0 = first.Longitude0, NoValueMarker = first.NoValueMarker };
                        for (int y = 0; y < first.Height; ++y) {
                            double lat = first.Latitude0 + (y * first.DeltaLatitude);		// Work out Lat. of this cell.
                            for (int x = 0; x < first.Width; ++x) {
                                double lon = first.Longitude0 + (x * first.DeltaLongitude); // Work out Long. of this cell.
                                newlayer.SetCellValue(x,y, layer.GetValueAt(lat, lon, first.NoValueMarker));
                            }
                        }
                        list.Add(newlayer);

                    } else {
                        list.Add(layer);
                    }
                }

                // now get the points ready...
                var points = new ModelPointSet();
                ProgressMessage("Preparing points...");
                foreach (MapPointSet set in pointSets) {
                    foreach (MapPoint p in set) {

                        if (p.Latitude == 0 && p.Longitude == 0) {
                            continue;
                        }

                        double fudge = (double) ( first.DeltaLatitude / 2.0 );

                        var x = Math.Abs( (int) ((p.Longitude - ( first.Longitude0 - fudge) ) / first.DeltaLongitude));
                        var y = Math.Abs( (int) ((p.Latitude - ( first.Latitude0 - fudge) ) / first.DeltaLatitude));

                        if (!points.ContainsCell(x, y)) {
                            points.AddPoint(new ModelPoint(layers.Count()) { X = p.Longitude, Y = p.Latitude, CellX = x, CellY = y });
                        }
                    }
                }

                if (!IsCancelled) {
                    if (ProgressObserver != null) {
                        ProgressObserver.ProgressStart("Running model", false);
                    }
                    RunModelImpl(target, layers, points);
                    if (ProgressObserver != null) {
                        if (IsCancelled) {
                            ProgressObserver.ProgressEnd("Model cancelled!");
                        } else {
                            ProgressObserver.ProgressEnd("Model complete");
                        }
                    }
                }

            }

            return IsCancelled ? null : target;
        }
        protected override void RunModelImpl(GridLayer targetLayer, IEnumerable <GridLayer> layers, ModelPointSet points)
        {
            int layerCount = layers.Count();

            double noValue = targetLayer.NoValueMarker;

            var pointsArray = points.Points.ToArray();
            var layerArray  = layers.ToArray();

            // Preprocess points...
            layers.ForEachIndex((layer, i) => {
                foreach (ModelPoint p in pointsArray)
                {
                    var value        = layer.GetValueAt(p.Y, p.X, noValue);
                    p.LayerValues[i] = value;
                    p.UsePoint       = value != noValue;
                }
            });

            double[] range = new double[layers.Count()];
            // Find ranges;
            for (int layerIndex = 0; layerIndex < layerArray.Length; ++layerIndex)
            {
                bool   rangeSet = false;
                double min      = 0;
                double max      = 0;

                for (int i = 0; i < pointsArray.Length; ++i)
                {
                    var p = pointsArray[i];

                    if (p.UsePoint)
                    {
                        var value = p.LayerValues[layerIndex];
                        if (!rangeSet)
                        {
                            min      = value;
                            max      = value;
                            rangeSet = true;
                        }
                        else
                        {
                            if (value < min)
                            {
                                min = value;
                            }
                            else if (value > max)
                            {
                                max = value;
                            }
                        }
                    }
                }

                var tmp = Math.Abs(max - min);
                range[layerIndex] = tmp == 0 ? 1 : tmp;
            }

            int height = targetLayer.Height;
            int width  = targetLayer.Width;


            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    var fMinDist = noValue;
                    var minSet   = false;

                    for (int i = 0; i < pointsArray.Length; ++i)
                    {
                        double fdist = 0;
                        var    p     = pointsArray[i];

                        if (p.UsePoint)
                        {
                            for (int layerIndex = 0; layerIndex < layerCount; layerIndex++)
                            {
                                var fCellVal = layerArray[layerIndex].Data[x, y];
                                if (fCellVal == noValue)
                                {
                                    fMinDist = noValue;
                                    goto SetPoint;
                                }
                                fdist = fdist + (Math.Abs(fCellVal - p.LayerValues[layerIndex]) / range[layerIndex]);
                            }
                            ;

                            fdist = fdist / layers.Count();
                            if (!minSet)
                            {
                                fMinDist = fdist;
                                minSet   = true;
                            }
                            else
                            {
                                if (fdist < fMinDist)
                                {
                                    fMinDist = fdist;
                                }
                            }
                        }
                    }

                    fMinDist = (1.0 - fMinDist) * 100.0;
SetPoint:
                    targetLayer.Data[x, y] = fMinDist;
                }

                if (y % 20 == 0)
                {
                    var percent = ((double)y / (double)height) * 100;
                    if (ProgressObserver != null)
                    {
                        ProgressObserver.ProgressMessage("Running Model...", percent);
                    }

                    if (IsCancelled)
                    {
                        return;
                    }
                }
            }
        }
Ejemplo n.º 22
0
        public static BitmapSource GetImageForLayer(GridLayer layer, Color lowcolor, Color highcolor, Color novaluecolor, double cutoff = 0, int intervals = 256)
        {
            var palette = CreateGradientPalette(lowcolor, highcolor, novaluecolor, intervals);
            var bmp = new WriteableBitmap(layer.Width, layer.Height, 96, 96, PixelFormats.Indexed8, palette);

            var range = layer.GetRange();

            if (cutoff == 0 && range != null && range.Range > 0) {
                cutoff = range.Min;
            }

            double max = 0;
            if (range != null) {
                max = range.Max;
            }

            double dx = Math.Abs(max - cutoff) / (intervals - 1);
            byte[] array = new byte[layer.Width * layer.Height];
            byte index = 0;
            for (int y = 0; y < layer.Height; y++) {
                for (int x = 0; x < layer.Width; x++) {
                    var value = layer.GetCellValue(x, (layer.Height - 1) - y);
                    if (value == layer.NoValueMarker) {
                        index = 0;
                    } else {
                        if (value >= cutoff || cutoff == 0) {
                            index = (byte) (((value - cutoff) / dx) + 1);
                        } else {
                            index = 0;
                        }
                    }
                    array[(y * layer.Width) + x] = index;
                }
            }

            Int32Rect r= new Int32Rect(0, 0, layer.Width, layer.Height);

            bmp.WritePixels(r, array, layer.Width, 0, 0);

            return bmp;
        }
Ejemplo n.º 23
0
        public static void CreateWorldFile(GridLayer layer, string filename)
        {
            double lat0;

            lat0 = layer.Latitude0 + (layer.DeltaLatitude * layer.Height);

            using (FileStream fs = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.Write)) {
                using (var writer = new StreamWriter(fs)) {
                    writer.WriteLine("{0}", layer.DeltaLongitude);
                    writer.WriteLine("0");
                    writer.WriteLine("0");
                    writer.WriteLine("{0}", -layer.DeltaLatitude);
                    writer.WriteLine("{0}", layer.Longitude0);
                    writer.WriteLine("{0}", lat0);
                }
            }
        }
Ejemplo n.º 24
0
 protected abstract void RunModelImpl(GridLayer targetLayer, IEnumerable<GridLayer> layers, ModelPointSet points);
Ejemplo n.º 25
0
        protected override void RunModelImpl(GridLayer targetLayer, IEnumerable<GridLayer> layers, ModelPointSet points)
        {
            int layerCount = layers.Count();

            double noValue = targetLayer.NoValueMarker;

            var pointsArray = points.Points.ToArray();
            var layerArray = layers.ToArray();

            // Preprocess points...
            layers.ForEachIndex((layer, i) => {
                foreach (ModelPoint p in pointsArray) {
                    var value = layer.GetValueAt(p.Y, p.X, noValue);
                    p.LayerValues[i] = value;
                    p.UsePoint = value != noValue;
                }
            });

            double[] range = new double[layers.Count()];
            // Find ranges;
            for (int layerIndex = 0; layerIndex < layerArray.Length; ++layerIndex) {

                bool rangeSet = false;
                double min = 0;
                double max = 0;

                for (int i = 0; i < pointsArray.Length; ++i) {
                    var p = pointsArray[i];

                    if (p.UsePoint) {
                        var value = p.LayerValues[layerIndex];
                        if (!rangeSet) {
                            min = value;
                            max = value;
                            rangeSet = true;
                        } else {
                            if (value < min) {
                                min = value;
                            } else if (value > max) {
                                max = value;
                            }
                        }
                    }
                }

                var tmp = Math.Abs(max - min);
                range[layerIndex] = tmp == 0 ? 1 : tmp;
            }

            int height = targetLayer.Height;
            int width = targetLayer.Width;

            for (int y = 0; y < height; y++) {
                for (int x = 0; x < width; x++) {
                    var fMinDist = noValue;
                    var minSet = false;

                    for (int i = 0; i < pointsArray.Length; ++i) {
                        double fdist = 0;
                        var p = pointsArray[i];

                        if (p.UsePoint) {
                            for (int layerIndex = 0; layerIndex < layerCount; layerIndex++) {

                                var fCellVal = layerArray[layerIndex].Data[x, y];
                                if (fCellVal == noValue) {
                                    fMinDist = noValue;
                                    goto SetPoint;
                                }
                                fdist = fdist + (Math.Abs(fCellVal - p.LayerValues[layerIndex]) / range[layerIndex]);
                            };

                            fdist = fdist / layers.Count();
                            if (!minSet) {
                                fMinDist = fdist;
                                minSet = true;
                            } else {
                                if (fdist < fMinDist) {
                                    fMinDist = fdist;
                                }
                            }
                        }
                    }

                    fMinDist = (1.0 - fMinDist) * 100.0;
                SetPoint:
                    targetLayer.Data[x, y] = fMinDist;
                }

                if (y % 20 == 0) {
                    var percent = ((double)y / (double)height) * 100;
                    if (ProgressObserver != null) {
                        ProgressObserver.ProgressMessage("Running Model...", percent);
                    }

                    if (IsCancelled) {
                        return;
                    }
                }

            }
        }