示例#1
0
        /// <summary>
        /// Gets the FeatureLayer that is used for the selection tests.
        /// </summary>
        /// <param name="cat">Reference to the second category.</param>
        /// <returns>The FeatureLayer.</returns>
        private static IFeatureLayer GetFeatureLayer(out PolygonCategory cat)
        {
            // load layer with us states
            ShapefileLayerProvider provider = new ShapefileLayerProvider();
            var fl = (IFeatureLayer)provider.OpenLayer(Path.Combine(@"TestFiles", "50mil_us_states.shp"), false, null, null);

            Assert.IsNotNull(fl);

            // add two categories for testing category.SelectionEnabled
            PolygonScheme scheme = new PolygonScheme();

            scheme.ClearCategories();
            scheme.AddCategory(new PolygonCategory(Color.LightBlue, Color.DarkBlue, 1)
            {
                FilterExpression = "[FIPS] >= 10",
                LegendText       = ">= 10"
            });
            cat = new PolygonCategory(Color.Pink, Color.DarkRed, 1)
            {
                FilterExpression = "[FIPS] < 10",
                LegendText       = "< 10"
            };
            scheme.AddCategory(cat);
            fl.Symbology = scheme;
            Assert.IsTrue(cat.SelectionEnabled, "Categories must be initialized with SelectionEnabled = true.");
            return(fl);
        }
示例#2
0
        private void menu_Click(object sender, EventArgs e)
        {
            FormContour Frm = new FormContour();

            Frm.layers = App.Map.GetRasterLayers();

            if (Frm.layers.GetLength(0) <= 0)
            {
                MessageBox.Show("No raster layer found!");
                return;
            }

            if (Frm.ShowDialog() == DialogResult.OK)
            {
                IMapFeatureLayer fl = App.Map.Layers.Add(Frm.Contours);
                fl.LegendText = Frm.LayerName + " - Contours";

                int numlevs = Frm.lev.GetLength(0);

                switch (Frm.contourtype)
                {
                case (Contour.ContourType.Line):
                {
                    LineScheme ls = new LineScheme();
                    ls.Categories.Clear();

                    for (int i = 0; i < Frm.color.GetLength(0); i++)
                    {
                        LineCategory lc = new LineCategory(Frm.color[i], 2.0);

                        lc.FilterExpression = "[Value] = " + Frm.lev[i].ToString();
                        lc.LegendText       = Frm.lev[i].ToString();

                        ls.AddCategory(lc);
                    }

                    fl.Symbology = ls;
                }
                break;

                case (Contour.ContourType.Polygon):
                {
                    PolygonScheme ps = new PolygonScheme();
                    ps.Categories.Clear();

                    for (int i = 0; i < Frm.color.GetLength(0); i++)
                    {
                        PolygonCategory pc = new PolygonCategory(Frm.color[i], Color.Transparent, 0);
                        pc.FilterExpression = "[Lev] = " + i.ToString();
                        pc.LegendText       = Frm.lev[i].ToString() + " - " + Frm.lev[i + 1].ToString();

                        ps.AddCategory(pc);
                    }

                    fl.Symbology = ps;
                }
                break;
                }
            }
        }
示例#3
0
        private void btnFilterByPopState_Click(object sender, EventArgs e)
        {
            //check the number of layers from map control

            if (map1.Layers.Count > 0)
            {
                //Delacre a MapPolygonLayer
                MapPolygonLayer stateLayer = default(MapPolygonLayer);

                //Type cast the FirstLayer of MapControl to MapPolygonLayer
                stateLayer = (MapPolygonLayer)map1.Layers[0];

                //Check the MapPolygonLayer ( Make sure that it has a polygon layer)
                if (stateLayer == null)
                {
                    MessageBox.Show("The layer is not a polygon layer.");
                }
                else
                {
                    //!!!-------------------- this line is necessary otherwise the code doesn't work------------------------!!!!!!!!!!!!!!!!!!!!
                }
                stateLayer.DataSet.FillAttributes();

                //Create a new PolygonScheme
                PolygonScheme scheme = new PolygonScheme();

                //Create a new PolygonCategory
                PolygonCategory category = new PolygonCategory(Color.Yellow, Color.Red, 1);

                //Declare a filter string
                //[POP1990],[STATE_NAME] are attributes from the attribute table of the given shape file.

                string filter = "[POP1990] > 10000000 OR [STATE_NAME] = 'Idaho'";

                //Set/Assign the filter expression to PolygonCategory
                category.FilterExpression = filter;

                //Set the Legend Text
                category.LegendText = "population > 10 Million";

                //Add the PolygonCategory in to the PolygonScheme
                scheme.AddCategory(category);

                //Set the scheme in to the MapPolygonLayer's symbology
                stateLayer.Symbology = scheme;
            }
            else
            {
                MessageBox.Show("Please add a layer to the map.");
            }
        }
示例#4
0
        /// <summary>
        /// Translates the given style as polygon style and adds it to the given polygon scheme.
        /// </summary>
        /// <param name="scheme">The scheme the style gets added to.</param>
        /// <param name="style">The style that gets translated.</param>
        private static void TranslatePolygonStyle(PolygonScheme scheme, string style)
        {
            if (string.IsNullOrWhiteSpace(style))
            {
                return;
            }

            var tools = GetToolNames(style);

            Color  col      = Color.Black;
            Color  fillCol  = Color.White;
            double numWidth = 1;

            foreach (var tool in tools)
            {
                switch (tool.Key)
                {
                case "PEN":
                {
                    var myStyle = tool.Value.Substring(4);
                    col      = GetColor(ref myStyle, Parameters.Color);
                    numWidth = GetWidth(ref myStyle);
                    break;
                }

                case "BRUSH":
                {
                    var myStyle = tool.Value.Substring(6);
                    fillCol = GetColor(ref myStyle, Parameters.BrushForeColor);
                    break;
                }
                }
            }

            var cat = new PolygonCategory(fillCol, col, numWidth)
            {
                FilterExpression = $"[style] = '{style}'",
                LegendText       = style,
                Symbolizer       =
                {
                    ScaleMode = ScaleMode.Simple,
                    Smoothing = false
                }
            };

            scheme.AddCategory(cat);
        }
示例#5
0
        /// <summary>
        /// Translates the style strings from the list to DotSpatial style categories and adds them to the given layer.
        /// </summary>
        /// <param name="layer">The layer the styles get added to.</param>
        /// <param name="styles">The style strings that should get translated.</param>
        public static void TranslateStyles(IMapFeatureLayer layer, IList <string> styles)
        {
            var featureType = layer.DataSet.FeatureType;

            switch (featureType)
            {
            case FeatureType.MultiPoint:
            case FeatureType.Point:
            {
                // create the scheme
                var scheme = new PointScheme();
                scheme.Categories.Clear();
                scheme.EditorSettings.ClassificationType = ClassificationType.UniqueValues;
                scheme.EditorSettings.FieldName          = "style";
                scheme.LegendText = "Point";

                // add the default category
                var cat = new PointCategory(Color.Black, Symbology.PointShape.Rectangle, 5)
                {
                    LegendText = "default",
                    Symbolizer =
                    {
                        ScaleMode = ScaleMode.Simple,
                        Smoothing = false
                    }
                };
                scheme.AddCategory(cat);

                var labelLayer = new MapLabelLayer();
                labelLayer.Symbology.Categories.Clear();

                bool needsLabels = styles.Any(_ => !string.IsNullOrWhiteSpace(_) && _.StartsWith("LABEL"));

                foreach (var style in styles)
                {
                    TranslatePointStyle(scheme, labelLayer.Symbology, style);
                }

                // assign the scheme
                layer.Symbology = scheme;

                // assign the label layer if needed
                if (needsLabels)
                {
                    layer.LabelLayer = labelLayer;
                    layer.ShowLabels = true;
                    layer.LabelLayer.CreateLabels();
                }

                layer.DataSet.UpdateExtent();
                layer.DataSet.InitializeVertices();
                layer.AssignFastDrawnStates();

                break;
            }

            case FeatureType.Line:
            {
                // create the scheme
                var scheme = new LineScheme();
                scheme.Categories.Clear();
                scheme.EditorSettings.ClassificationType = ClassificationType.UniqueValues;
                scheme.EditorSettings.FieldName          = "style";
                scheme.LegendText = "Line";

                // add the default category
                var cat = new LineCategory(Color.Black, 1)
                {
                    LegendText = "default",
                    Symbolizer =
                    {
                        ScaleMode = ScaleMode.Simple,
                        Smoothing = false
                    }
                };
                scheme.AddCategory(cat);

                // add the categories from the file
                foreach (var style in styles)
                {
                    TranslateLineStyle(scheme, style);
                }

                // assign the scheme
                layer.Symbology = scheme;

                layer.DataSet.UpdateExtent();
                layer.DataSet.InitializeVertices();
                layer.AssignFastDrawnStates();

                break;
            }

            case FeatureType.Polygon:
            {
                // create the scheme
                var scheme = new PolygonScheme();
                scheme.Categories.Clear();
                scheme.EditorSettings.ClassificationType = ClassificationType.UniqueValues;
                scheme.EditorSettings.FieldName          = "style";
                scheme.LegendText = "Polygon";

                // add the default category
                var cat = new PolygonCategory(Color.GhostWhite, Color.Black, 1)
                {
                    LegendText = "default",
                    Symbolizer =
                    {
                        ScaleMode = ScaleMode.Simple,
                        Smoothing = false
                    }
                };
                scheme.AddCategory(cat);

                // add the categories from the file
                foreach (var style in styles)
                {
                    TranslatePolygonStyle(scheme, style);
                }

                // assign the scheme
                layer.Symbology = scheme;

                layer.DataSet.UpdateExtent();
                layer.DataSet.InitializeVertices();
                layer.AssignFastDrawnStates();

                break;
            }

            default: throw new ArgumentOutOfRangeException(nameof(featureType), featureType, null);
            }
        }
示例#6
0
        public void SelectionTest()
        {
            IMap map = new Map();

            // load layer with us states
            IMapLayer     ml = map.AddLayer(Path.Combine(@"Testfiles", "50mil_us_states.shp"));
            IFeatureLayer fl = ml as IFeatureLayer;

            Assert.IsNotNull(fl);

            // add two categories for testing category.SelectionEnabled
            PolygonScheme scheme = new PolygonScheme();

            scheme.ClearCategories();
            scheme.AddCategory(new PolygonCategory(Color.LightBlue, Color.DarkBlue, 1)
            {
                FilterExpression = "[FIPS] >= 10",
                LegendText       = ">= 10"
            });
            var cat = new PolygonCategory(Color.Pink, Color.DarkRed, 1)
            {
                FilterExpression = "[FIPS] < 10",
                LegendText       = "< 10"
            };

            scheme.AddCategory(cat);
            fl.Symbology = scheme;
            Assert.IsTrue(cat.SelectionEnabled, "Categories must be initialized with SelectionEnabled = true.");

            // load the second layer for testing the layers SelectionEnabled property
            IMapLayer ml2 = map.AddLayer(Path.Combine(@"Testfiles", "50m_admin_0_countries.shp"));

            ml2.SelectionEnabled = false;
            IFeatureLayer fl2 = ml2 as IFeatureLayer;

            Assert.IsNotNull(fl2);

            // select the first area
            Envelope e = new Envelope(-72, -66, 40, 48);

            Assert.IsTrue(map.Select(e, e, ClearStates.False));
            Assert.AreEqual(7, fl.Selection.Count, "Error selecting 50mil_us_states");
            Assert.AreEqual(0, fl2.Selection.Count, "Error selecting 50m_admin_0_countries");

            // invert a slighly larger area, ignoring the features of the second category
            cat.SelectionEnabled = false;
            Envelope e2 = new Envelope(-78, -66, 40, 48);

            Assert.IsTrue(map.InvertSelection(e2, e2));
            Assert.AreEqual(4, fl.Selection.Count, "Error inverting selection 50mil_us_states");
            Assert.AreEqual(0, fl2.Selection.Count, "Error inverting selection 50m_admin_0_countries");

            // add another area allowing the second layer to be selected too
            ml2.SelectionEnabled = true;
            Envelope e3 = new Envelope(-89, -77, 24, 33);

            Assert.IsTrue(map.Select(e3, e3, ClearStates.False));
            Assert.AreEqual(9, fl.Selection.Count, "Error adding to selection 50mil_us_states");
            Assert.AreEqual(2, fl2.Selection.Count, "Error adding to selection 50m_admin_0_countries");
            ml2.SelectionEnabled = false;

            // unselect the whole area ignoring the second layer and the deactivated category
            Envelope e4 = new Envelope(-89, -66, 24, 48);

            Assert.IsTrue(map.UnSelect(e4, e4));
            Assert.AreEqual(1, fl.Selection.Count, "Error unselecting 50mil_us_states");
            Assert.AreEqual(2, fl2.Selection.Count, "Error unselecting 50m_admin_0_countries");
        }
示例#7
0
        private void MenuClick(object sender, EventArgs e)
        {
            using (FormContour frm = new FormContour())
            {
                frm.Layers = App.Map.GetRasterLayers();
                if (frm.Layers.GetLength(0) <= 0)
                {
                    MessageBox.Show(Resources.NoRasterLayerFound);
                    return;
                }

                if (frm.ShowDialog() != DialogResult.OK)
                {
                    return;
                }
                IMapFeatureLayer fl = App.Map.Layers.Add(frm.Contours);
                fl.LegendText = frm.LayerName + " - Contours";

                int numlevs = frm.Lev.GetLength(0);

                switch (frm.Contourtype)
                {
                case Contour.ContourType.Line:
                {
                    LineScheme ls = new LineScheme();
                    ls.Categories.Clear();

                    for (int i = 0; i < frm.Color.GetLength(0); i++)
                    {
                        LineCategory lc = new LineCategory(frm.Color[i], 2.0)
                        {
                            FilterExpression = "[Value] = " + frm.Lev[i],
                            LegendText       = frm.Lev[i].ToString(CultureInfo.InvariantCulture)
                        };

                        ls.AddCategory(lc);
                    }

                    fl.Symbology = ls;
                }

                break;

                case Contour.ContourType.Polygon:
                {
                    PolygonScheme ps = new PolygonScheme();
                    ps.Categories.Clear();

                    for (int i = 0; i < frm.Color.GetLength(0); i++)
                    {
                        PolygonCategory pc = new PolygonCategory(frm.Color[i], Color.Transparent, 0)
                        {
                            FilterExpression = "[Lev] = " + i,
                            LegendText       = frm.Lev[i] + " - " + frm.Lev[i + 1]
                        };
                        ps.AddCategory(pc);
                    }

                    fl.Symbology = ps;
                }

                break;
                }
            }
        }
示例#8
0
        //-------------------------------------------------------
        public MapPolygonLayer AddFieldLayer(string Fieldname)
        {
            MapPolygonLayer MyLayer = null;

            if (MapDataTable != null)
            {
                if (MapDataTable.Columns.Contains(Fieldname))
                {
                    try
                    {
                        DataColumn DC = MapDataTable.Columns[Fieldname];

                        MyLayer = AddProviderShapeFile(Fieldname); //(MapPolygonLayer)ProviderMap.AddLayer(DataPath + ShapeFilename);
                        if (MyLayer == null)
                        {
                            MessageBox.Show("The Base ShapeFile is not a polygon layer.");
                        }
                        else
                        {
                            // Get the years
                            string yrfield = WaterSimManager_DB.rdbfSimYear;

                            // MyLayer.DataSet.FillAttributes();

                            DataTable  LayerDT   = MyLayer.DataSet.DataTable;
                            DataColumn AddColumn = new DataColumn(DC.ColumnName, DC.DataType);
                            LayerDT.Columns.Add(AddColumn);

                            foreach (DataRow DR in LayerDT.Rows)
                            {
                                Boolean found  = false;
                                string  pfcode = DR["Provider"].ToString().Trim(); // Get the Provider Field code for this SHape File Record
                                // find this code in Scenario DataTable
                                foreach (DataRow ScnDR in MapDataTable.Rows)
                                {
                                    if (ScnDR[yrfield].ToString().Trim() == MapTargetYear)
                                    {
                                        string Scnpfcode = ScnDR[WaterSimManager_DB.rdbfProviderCode].ToString().Trim(); // "PRVDCODE"
                                        if (pfcode == Scnpfcode)
                                        {
                                            DR[Fieldname] = ScnDR[Fieldname].ToString();
                                            found         = true;
                                            break;
                                        }
                                        if (!found)
                                        {
                                            if (DC.DataType == System.Type.GetType("System.String"))
                                            {
                                                DR[Fieldname] = "";
                                            }
                                            else
                                            {
                                                DR[Fieldname] = 0;
                                            }
                                        }
                                    } //= provider
                                }     // = target year
                            }
                            //MyLayer.DataSet.FillAttributes();

                            if (DC.DataType == System.Type.GetType("System.String"))
                            {
                                PolygonScheme FldScheme = new PolygonScheme();
                                FldScheme.EditorSettings.ClassificationType = ClassificationType.UniqueValues;
                                ////Set the UniqueValue field name
                                FldScheme.EditorSettings.FieldName = Fieldname;
                                // Create Catagories based on data
                                FldScheme.CreateCategories(LayerDT);
                                MyLayer.Symbology = FldScheme;
                            }
                            else
                            {
                                PolygonScheme FldScheme = new PolygonScheme();
                                FldScheme.EditorSettings.ClassificationType = ClassificationType.Quantities;
                                FldScheme.EditorSettings.IntervalMethod     = IntervalMethod.StandardDeviation;
                                FldScheme.EditorSettings.NumBreaks          = 8;
                                FldScheme.EditorSettings.FieldName          = Fieldname;
                                FldScheme.AppearsInLegend = true;
                                FldScheme.CreateCategories(LayerDT);
                                PolygonCategory NullCat = new PolygonCategory(Color.WhiteSmoke, Color.DarkBlue, 1);
                                NullCat.FilterExpression = "[" + Fieldname + "] = NULL";
                                NullCat.LegendText       = "[" + Fieldname + "] = NULL";
                                FldScheme.AddCategory(NullCat);
                                MyLayer.Symbology = FldScheme;
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show("Error Loading Shape File:[ " + DataPath + ShapeFilename + "] :" + e.Message);
                    }
                }
            }
            return(MyLayer);
        }