コード例 #1
0
        /// <summary>
        /// Find the centroid of the geometry
        /// </summary>
        /// <param name="geometryId"></param>
        /// <returns>A point object representing the centroid</returns>
        public static Mapsui.Geometries.Point GetCentreOfGeometry(int geometryId)
        {
            var items = DataDAO.getDataForMap(App.CurrentProjectId);

            foreach (var item in items)
            {
                if (item.geomId == geometryId)
                {
                    var coords = item.shapeGeom.Centroid;
                    var centre = SphericalMercator.FromLonLat(coords.X, coords.Y);
                    return(centre);
                }
            }
            return(null);
        }
コード例 #2
0
        public static Dictionary <string, ILayer> CreateShapes()
        {
            var layerDic = new Dictionary <string, ILayer>();
            var items    = DataDAO.getDataForMap(App.CurrentProjectId);

            if (items != null && items.Count > 0)
            {
                var points    = new List <Feature>();
                var polygons  = new List <Feature>();
                var lines     = new List <Feature>();
                var allShapes = new List <Feature>();

                foreach (Shape item in items)
                {
                    var coords     = item.shapeGeom.Coordinates;
                    var coordCount = item.shapeGeom.Coordinates.Length;
                    if (coordCount > 0)
                    {
                        if (coordCount == 1)
                        {
                            //Point
                            var coord = coords[0];

                            var point = SphericalMercator.FromLonLat(coord.X, coord.Y);

                            var feature = new Feature
                            {
                                Geometry  = point,
                                ["Name"]  = item.geomId.ToString(),
                                ["Label"] = item.title
                            };
                            feature.Styles.Add(new LabelStyle
                            {
                                Text                = item.title,
                                BackColor           = new Mapsui.Styles.Brush(Mapsui.Styles.Color.White),
                                HorizontalAlignment = LabelStyle.HorizontalAlignmentEnum.Left,
                                Offset              = new Offset(20, 0, false)
                            });
                            points.Add(feature);
                            allShapes.Add(feature);
                        }
                        else
                        {
                            var coord0 = coords[0];
                            var coordx = coords[coordCount - 1];
                            if (coord0.X == coordx.X && coord0.Y == coordx.Y)
                            {
                                //Polygon
                                var polygon = new Polygon();

                                var localCoords = coords;
                                foreach (NetTopologySuite.Geometries.Coordinate coord in localCoords)
                                {
                                    var pt = SphericalMercator.FromLonLat(coord.X, coord.Y);
                                    polygon.ExteriorRing.Vertices.Add(new Mapsui.Geometries.Point(pt.X, pt.Y));
                                }
                                var feature = new Feature {
                                    Geometry  = polygon,
                                    ["Name"]  = item.geomId.ToString(),
                                    ["Label"] = item.title
                                };
                                feature.Styles.Add(new LabelStyle
                                {
                                    Text                = item.title,
                                    BackColor           = new Mapsui.Styles.Brush(Mapsui.Styles.Color.White),
                                    HorizontalAlignment = LabelStyle.HorizontalAlignmentEnum.Center
                                });

                                polygons.Add(feature);
                                allShapes.Add(feature);
                            }
                            else
                            {
                                //Line
                                var line        = new LineString();
                                var localCoords = coords;
                                foreach (NetTopologySuite.Geometries.Coordinate coord in localCoords)
                                {
                                    var pt = SphericalMercator.FromLonLat(coord.X, coord.Y);
                                    line.Vertices.Add(new Mapsui.Geometries.Point(pt.X, pt.Y));
                                }
                                var feature = new Feature
                                {
                                    Geometry  = line,
                                    ["Name"]  = item.geomId.ToString(),
                                    ["Label"] = item.title
                                };
                                feature.Styles.Add(new LabelStyle
                                {
                                    Text                = item.title,
                                    BackColor           = new Mapsui.Styles.Brush(Mapsui.Styles.Color.White),
                                    HorizontalAlignment = LabelStyle.HorizontalAlignmentEnum.Center
                                });
                                lines.Add(feature);
                                allShapes.Add(feature);
                            }
                        }
                    }
                }

                ILayer polygonLayer   = CreatePolygonLayer(polygons, new Mapsui.Styles.Color(0, 0, 200, 255), new Mapsui.Styles.Color(0, 0, 200, 32));
                ILayer pointLayer     = CreatePointLayer(points);
                ILayer lineLayer      = CreateLineLayer(lines, new Mapsui.Styles.Color(0, 0, 200, 255));
                ILayer allShapesLayer = CreatePolygonLayer(allShapes, new Mapsui.Styles.Color(0, 0, 200, 255), new Mapsui.Styles.Color(0, 0, 200, 32)); //AllShapes layer is created merely to get bounding box of all shapes. It does not have the correct styles for showing all the shapes


                layerDic.Add("polygons", polygonLayer);
                layerDic.Add("lines", lineLayer);
                layerDic.Add("points", pointLayer);
                layerDic.Add("all", allShapesLayer);
            }
            return(layerDic);
        }