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);
            }
        }
예제 #2
0
 protected abstract void RunModelImpl(GridLayer targetLayer, IEnumerable<GridLayer> layers, ModelPointSet points);
예제 #3
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;
        }
예제 #4
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);
            }
        }
예제 #5
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);
        }
예제 #6
0
 protected abstract void RunModelImpl(GridLayer targetLayer, IEnumerable <GridLayer> layers, ModelPointSet points);
        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;
                    }
                }
            }
        }
예제 #8
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;
                    }
                }

            }
        }