/// <summary>
 /// Cast Polygon to WebPolygon
 /// </summary>
 /// <param name="polygon"></param>
 /// <returns></returns>
 public static WebPolygon ToWebPolygon(this IPolygon polygon)
 {
     return(new WebPolygon
     {
         LinearRings = polygon.ToWebLinearRings()
     });
 }
Пример #2
0
        public static bool PolygonToLineStripAdjacency(GeoAPI.Geometries.IPolygon _polygon, Ellipsoid _shape, List <Vector2> positions, List <ushort> indices)
        {
            if (_polygon == null)
            {
                throw new Exception("_polygon is Null");
            }
            //先进行二维的处理,在转换成贴于地表的三维坐标

            //填充顶点跟索引
            //详细流程,如果是投影坐标,将其转换成wgs84的经纬度坐标,再使用参考系计算出其真实的地理坐标
            ///0xFFFF/0xFFFFFFFF分别表示16位和32位的indice中断符
            ushort breakupIndice = 0xFFFF;
            var    extRing       = _polygon.ExteriorRing;
            var    interRing     = _polygon.InteriorRings;

            //添加外环
            LineStringToLineStripAdjacency(extRing, _shape, positions, indices);
            //完事添加间隔符
            indices.Add(breakupIndice);
            //添加内环
            foreach (var item in interRing)
            {
                LineStringToLineStripAdjacency(item, _shape, positions, indices);
                //完事添加间隔符
                indices.Add(breakupIndice);
            }
            return(true);
        }
Пример #3
0
        private static SMMultiPolygon SqlGeometryToSharpMapMultiPolygon(SqlGeometry geometry, Factory factory)
        {
            var fact     = factory ?? Services.CreateGeometryFactory((int)geometry.STSrid);
            var polygons = new SMPolygon[(int)geometry.STNumGeometries()];

            for (var i = 1; i <= geometry.STNumGeometries(); i++)
            {
                polygons[i - 1] = (SMPolygon)SqlGeometryToSharpMapPolygon(geometry.STGeometryN(i), fact);
            }
            return(fact.CreateMultiPolygon(polygons));
        }
Пример #4
0
        internal static Geometries.Polygon ToSharpMapPolygon(NTSPolygon geom)
        {
            Geometries.LinearRing exteriorRing = ToSharpMapLinearRing((NTSLinearRing)geom.ExteriorRing);
            Collection <Geometries.LinearRing> interiorRings = new Collection <Geometries.LinearRing>();

            foreach (NTSLineString interiorRing in geom.InteriorRings)
            {
                interiorRings.Add(ToSharpMapLinearRing((NTSLinearRing)interiorRing));
            }
            return(new Geometries.Polygon(exteriorRing, interiorRings));
        }
Пример #5
0
        internal static NTSMultiPolygon ToNTSMultiPolygon(Geometries.MultiPolygon geom,
                                                          IGeometryFactory factory)
        {
            NTSPolygon[] polygons = new NTSPolygon[geom.Polygons.Count];
            int          index    = 0;

            foreach (Geometries.Polygon polygon in geom.Polygons)
            {
                polygons[index++] = ToNTSPolygon(polygon, factory);
            }
            return(factory.CreateMultiPolygon(polygons) as NTSMultiPolygon);
        }
        private static IMultiPolygon ReProject(this IMultiPolygon multiPolygon, ICoordinateTransformation transformator)
        {
            var polygonCount = multiPolygon.NumGeometries;
            var polygons     = new IPolygon[polygonCount];

            for (var i = 0; i < polygonCount; i++)
            {
                polygons[i] = ((IPolygon)multiPolygon.GetGeometryN(i)).ReProject(transformator);
            }

            return(new MultiPolygon(polygons));
        }
        private static IPolygon ReProject(this IPolygon polygon, ICoordinateTransformation transformator)
        {
            var shell = ((ILinearRing)polygon.ExteriorRing).ReProject(transformator);

            var ringCount = polygon.NumInteriorRings;
            var holes     = new ILinearRing[ringCount];

            for (var i = 0; i < ringCount; i++)
            {
                holes[i] = ((ILinearRing)polygon.GetInteriorRingN(i)).ReProject(transformator);
            }

            return(new Polygon(shell, holes));
        }
Пример #8
0
        internal static DotSpatial.Topology.Polygon ToDotSpatialPolygon(GeoAPI.Geometries.IPolygon geom)
        {
            DotSpatial.Topology.LinearRing exteriorRing = ToDotSpatialLinearRing((GeoAPI.Geometries.ILinearRing)geom.ExteriorRing);

            DotSpatial.Topology.LinearRing[] interiorRings = new DotSpatial.Topology.LinearRing[geom.InteriorRings.Count()];

            //foreach (GeoAPI.Geometries.ILineString interiorRing in geom.InteriorRings)
            for (int i = 0; i < geom.InteriorRings.Count(); i++)
            {
                DotSpatial.Topology.LinearRing hole = ToDotSpatialLinearRing((GeoAPI.Geometries.ILinearRing)geom.InteriorRings[i]);
                interiorRings[i] = hole;
            }

            return(new DotSpatial.Topology.Polygon(exteriorRing, interiorRings));
        }
Пример #9
0
        /// <summary>
        /// 如果两个点的间距低于当前精度,则进一步细分
        /// </summary>
        /// <param name="_polygon"></param>
        /// <param name="_shape"></param>
        /// <returns></returns>
        public static bool PolygonToPoints(GeoAPI.Geometries.IPolygon _polygon, Ellipsoid _shape, List <Vector3> positions, List <ushort> indices)
        {
            if (_polygon == null)
            {
                throw new Exception("_polygon is Null");
            }
            List <Vector2> positions2D = new List <Vector2>();
            //填充顶点跟索引
            //详细流程,如果是投影坐标,将其转换成wgs84的经纬度坐标,再使用参考系计算出其真实的地理坐标
            ///0xFFFF/0xFFFFFFFF分别表示16位和32位的indice中断符
            ushort breakupIndice = 0xFFFF;
            var    extRing       = _polygon.ExteriorRing;
            var    interRing     = _polygon.InteriorRings;
            var    ccc           = extRing as GeoAPI.Geometries.ILinearRing;
            ///判断是否为逆时针,可能需要调整其方向
            var isCCW = ccc.IsCCW;
            ///添加外环
            ushort indicesMax = (ushort)positions.Count();

            foreach (var coord in extRing.Coordinates)
            {
                //将其转换成弧度制,自动贴地
                var geoDetic = new Vector2(MathExtension.ToRadius(coord.X), MathExtension.ToRadius(coord.Y));
                positions2D.Add(geoDetic);
                indices.Add(indicesMax++);
            }
            indices.Add(breakupIndice);
            //添加内环
            foreach (var item in interRing)
            {
                foreach (var coord in item.Coordinates)
                {
                    var geoDetic = new Vector2(MathExtension.ToRadius(coord.X), MathExtension.ToRadius(coord.Y));
                    positions2D.Add(geoDetic);
                    indices.Add(indicesMax++);
                }
                indices.Add(breakupIndice);
            }
            //这里计算的并不准确
            //if (SimplePolygonAlgorithms.ComputeWindingOrder(positions2D) != PolygonWindingOrder.Clockwise)
            //{
            //    positions2D.Reverse();
            //}
            positions.AddRange(positions2D.ConvertAll(i => _shape.ToVector3(new Geodetic2D(i.X, i.Y))));
            return(true);
        }
        /// <summary>
        /// Cast polygon to list of Web Linear rings
        /// </summary>
        /// <param name="polygon"></param>
        /// <returns></returns>
        public static List <WebLinearRing> ToWebLinearRings(this IPolygon polygon)
        {
            var linearRings = new List <WebLinearRing>();

            if (polygon.ExteriorRing == null)
            {
                return(null);
            }

            linearRings.Add(polygon.ExteriorRing.Coordinates.ToWebLinearRing());

            if (polygon.Holes != null)
            {
                linearRings.AddRange(from h in polygon.Holes select h.Coordinates.ToWebLinearRing());
            }

            return(linearRings);
        }
Пример #11
0
        public static Mesh <Vector3> PolygonToMesh(GeoAPI.Geometries.IPolygon _polygon, Ellipsoid _shape)
        {
            if (_polygon == null)
            {
                throw new Exception("_polygon is Null");
            }
            List <Vector2> positions = new List <Vector2>();

            //填充顶点跟索引
            //详细流程,如果是投影坐标,将其转换成wgs84的经纬度坐标,再使用参考系计算出其真实的地理坐标
            foreach (var coord in _polygon.Coordinates)
            {
                //将其转换成弧度制,自动贴地
                positions.Add(new Vector2(MathExtension.ToRadius(coord.X), MathExtension.ToRadius(coord.Y)));
            }
            //去除重复的数据
            var posClearUp = SimplePolygonAlgorithms.Cleanup <Vector2>(positions);

            //如果不是顺时针,强制转换成顺时针
            if (SimplePolygonAlgorithms.ComputeWindingOrder(posClearUp) != PolygonWindingOrder.Clockwise)
            {
                posClearUp = posClearUp.Reverse().ToArray();
            }
            var indices = EarClippingOnEllipsoid.Triangulate2D(posClearUp);
            //将vector2转换成vector3
            List <Vector3> worldPosition = new List <Vector3>();

            foreach (var item in posClearUp)
            {
                var vec = _shape.ToVector3(new Geodetic2D(item.X, item.Y));
                worldPosition.Add(vec);
            }
            var _mesh = new Mesh <Vector3>();

            _mesh.Indices   = indices.ToArray();
            _mesh.Positions = worldPosition.ToArray();
            return(_mesh);
        }
Пример #12
0
 private static SMMultiPolygon SqlGeometryToSharpMapMultiPolygon(SqlGeometry geometry, Factory factory)
 {
     var fact = factory ?? Services.CreateGeometryFactory((int)geometry.STSrid);
     var polygons = new SMPolygon[(int)geometry.STNumGeometries()];
     for (var i = 1; i <= geometry.STNumGeometries(); i++)
         polygons[i-1] = (SMPolygon)SqlGeometryToSharpMapPolygon(geometry.STGeometryN(i), fact);
     return fact.CreateMultiPolygon(polygons);
 }
Пример #13
0
 private static void SharpMapPolygonToSqlGeometry(SqlGeometryBuilder geomBuilder, SMPolygon polygon)
 {
     geomBuilder.BeginGeometry(OpenGisGeometryType.Polygon);
     AddRing(geomBuilder, (SMLinearRing )polygon.ExteriorRing);
     for (int i = 0; i < polygon.NumInteriorRings; i++)
         AddRing(geomBuilder, (SMLinearRing )polygon.GetInteriorRingN(i));
     geomBuilder.EndGeometry();
 }
Пример #14
0
        private void 处理KMLToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();

            if (ofd.ShowDialog() == DialogResult.OK)
            {
                NetTopologySuite.Geometries.GeometryFactory gFactory = new GeometryFactory();
                Dictionary <int, FeatureDataTable>          fdtDic   = new Dictionary <int, FeatureDataTable>();
                XmlDocument doc = new XmlDocument();
                doc.Load(ofd.FileName);
                XmlNode             root    = doc.DocumentElement;
                string              nameUri = root.NamespaceURI;
                XmlNamespaceManager xnm     = new XmlNamespaceManager(doc.NameTable);
                xnm.AddNamespace("pre", nameUri);
                XmlNodeList folderName = root.SelectNodes("pre:Document/pre:Folder/pre:Folder/pre:name", xnm);
                XmlNode     oddNode    = null;
                XmlNode     evenNode   = null;
                for (int i = 0; i < folderName.Count; i++)
                {
                    if (folderName[i].InnerText == "odd num")
                    {
                        oddNode = folderName[i].ParentNode;
                    }

                    if (folderName[i].InnerText == "even num")
                    {
                        evenNode = folderName[i].ParentNode;
                    }
                }
                XmlNodeList folders_Odd  = null;
                XmlNodeList folders_Even = null;
                if (oddNode != null)
                {
                    folders_Odd = oddNode.SelectNodes("pre:Folder", xnm);
                }
                if (evenNode != null)
                {
                    folders_Even = evenNode.SelectNodes("pre:Folder", xnm);
                }


                if (folders_Odd != null)
                {
                    for (int i = 0; i < folders_Odd.Count; i++)
                    {
                        FeatureDataTable srcfdt        = null;
                        string           flihgtNameStr = folders_Odd[i].SelectSingleNode("pre:name", xnm).InnerText.Trim();
                        //寻找偶数
                        XmlNode sameFlightEven = null;
                        if (folders_Even != null)
                        {
                            for (int j = 0; j < folders_Even.Count; j++)
                            {
                                XmlNode flightN        = folders_Odd[i].SelectSingleNode("pre:name", xnm);
                                string  tempFlightName = flightN.InnerText.Trim();
                                if (tempFlightName == flihgtNameStr)
                                {
                                    sameFlightEven = flightN.ParentNode;
                                }
                            }
                        }

                        XmlNodeList placemarks_Odd  = folders_Odd[i].SelectNodes("pre:Placemark", xnm);
                        XmlNodeList placemarks_even = null;
                        if (sameFlightEven != null)
                        {
                            placemarks_even = folders_Even[i].SelectNodes("pre:Placemark", xnm);
                        }

                        string[] flightName = flihgtNameStr.Split(' ');
                        int      flihgtId   = Convert.ToInt32(flightName[1]);
                        //if (fdtDic.Keys.Contains(flihgtId))
                        //{
                        //    srcfdt = fdtDic[flihgtId];
                        //}
                        //else
                        //{
                        //    srcfdt = new FeatureDataTable();
                        //    srcfdt.Columns.Add("FlightId", typeof(int));
                        //    srcfdt.Columns.Add("Name", typeof(string));
                        //    srcfdt.Columns.Add("OverlapCount", typeof(int));
                        //    fdtDic.Add(flihgtId, srcfdt);
                        //}
                        srcfdt = new FeatureDataTable();
                        srcfdt.Columns.Add("FlightId", typeof(int));
                        srcfdt.Columns.Add("Name", typeof(string));
                        srcfdt.Columns.Add("OverlapCount", typeof(int));
                        fdtDic.Add(flihgtId, srcfdt);
                        for (int k = 0; k < placemarks_Odd.Count; k++)
                        {
                            XmlNode      coordinatesN_odd  = placemarks_Odd[k].SelectSingleNode("pre:Polygon/pre:outerBoundaryIs/pre:LinearRing/pre:coordinates", xnm);
                            string       placemarkName_odd = placemarks_Odd[k].SelectSingleNode("pre:name", xnm).InnerText;
                            string       coordinateStr     = coordinatesN_odd.InnerText.Trim();
                            string[]     coordinateArry    = coordinateStr.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
                            Coordinate[] coordinates       = new Coordinate[coordinateArry.Length];
                            for (int j = 0; j < coordinateArry.Length; j++)
                            {
                                string[] xyh = coordinateArry[j].Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                                coordinates[j] = new Coordinate(Convert.ToDouble(xyh[0]), Convert.ToDouble(xyh[1]), Convert.ToDouble(xyh[2]));
                            }
                            if (coordinateArry.Length > 3)
                            {
                                GeoAPI.Geometries.IPolygon polygon = gFactory.CreatePolygon(coordinates);
                                FeatureDataRow             fdr     = srcfdt.NewRow();
                                fdr["FlightId"] = flihgtId;
                                Debug.WriteLine(flihgtId);
                                fdr["Name"]         = placemarkName_odd;
                                fdr["OverlapCount"] = 0;
                                fdr.Geometry        = polygon;
                                srcfdt.AddRow(fdr);
                            }

                            if (placemarks_even != null)
                            {
                                if (k >= placemarks_even.Count)
                                {
                                    continue;
                                }
                                XmlNode      coordinatesN_even  = placemarks_even[k].SelectSingleNode("pre:Polygon/pre:outerBoundaryIs/pre:LinearRing/pre:coordinates", xnm);
                                string       placemarkName_even = placemarks_even[k].SelectSingleNode("pre:name", xnm).InnerText;
                                string       coordStr           = coordinatesN_even.InnerText.Trim();
                                string[]     coordinateA_even   = coordStr.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
                                Coordinate[] coor = new Coordinate[coordinateA_even.Length];
                                for (int j = 0; j < coordinateA_even.Length; j++)
                                {
                                    string[] xyh = coordinateA_even[j].Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                                    coor[j] = new Coordinate(Convert.ToDouble(xyh[0]), Convert.ToDouble(xyh[1]), Convert.ToDouble(xyh[2]));
                                }
                                if (coor.Length > 3)
                                {
                                    GeoAPI.Geometries.IPolygon polygon = gFactory.CreatePolygon(coor);
                                    FeatureDataRow             fdr     = srcfdt.NewRow();
                                    fdr["FlightId"] = flihgtId;
                                    Debug.WriteLine(flihgtId);
                                    fdr["Name"]         = placemarkName_even;
                                    fdr["OverlapCount"] = 0;
                                    fdr.Geometry        = polygon;
                                    srcfdt.AddRow(fdr);
                                }
                            }
                        }
                    }
                }

                //对fdtDic中的所有航线进行旁向重叠计算
                int index = 0;
                foreach (var item in fdtDic.Values)
                {
                    overlapLR(item);
                    if (++index >= 1)
                    {
                        break;
                    }
                }

                FeatureDataTable allfdt = null;//所有的航线都放进来了
                if (fdtDic.Count > 0)
                {
                    foreach (FeatureDataTable item in fdtDic.Values)
                    {
                        if (allfdt == null)
                        {
                            allfdt = new FeatureDataTable();
                            allfdt.Columns.Add("FlightId", typeof(int));
                            allfdt.Columns.Add("Name", typeof(string));
                            allfdt.Columns.Add("OverlapCount", typeof(int));
                        }
                        foreach (var r in item.Rows)
                        {
                            FeatureDataRow srcFdr  = r as FeatureDataRow;
                            FeatureDataRow tempfdr = allfdt.NewRow();
                            tempfdr["FlightId"]     = srcFdr["FlightId"];
                            tempfdr["Name"]         = srcFdr["Name"];
                            tempfdr["OverlapCount"] = srcFdr["OverlapCount"];
                            if (Convert.ToInt32(srcFdr["OverlapCount"]) > 0)
                            {
                                Debug.WriteLine(Convert.ToInt32(srcFdr["OverlapCount"]));
                            }
                            tempfdr.Geometry = srcFdr.Geometry;
                            allfdt.Rows.Add(tempfdr);
                            Debug.WriteLine(srcFdr["FlightId"]);
                            Debug.WriteLine(srcFdr["Name"]);
                            Debug.WriteLine(srcFdr["OverlapCount"]);
                            Debug.WriteLine(tempfdr.Geometry.Area);
                            Debug.WriteLine(tempfdr.Geometry);
                            //if (++geolength>20)
                            //{
                            //    break;
                            //}
                        }
                    }

                    VectorStyle overlap0 = new VectorStyle();
                    overlap0.Fill          = new SolidBrush(Color.FromArgb(25, Color.Green));
                    overlap0.Outline       = new Pen(Color.Red, 1.0f);
                    overlap0.EnableOutline = true;

                    VectorStyle overlap2 = new VectorStyle();
                    overlap2.Fill          = new SolidBrush(Color.FromArgb(20, Color.Yellow));
                    overlap2.Outline       = new Pen(Color.Yellow, 1.0f);
                    overlap2.EnableOutline = true;

                    VectorStyle overlap3 = new VectorStyle();
                    overlap3.Fill          = new SolidBrush(Color.FromArgb(20, Color.Pink));
                    overlap3.Outline       = new Pen(Color.Yellow, 2.0f);
                    overlap3.EnableOutline = true;

                    VectorStyle overlap4 = new VectorStyle();
                    overlap4.Fill          = new SolidBrush(Color.FromArgb(20, Color.Blue));
                    overlap4.Outline       = new Pen(Color.Pink, 2.0f);
                    overlap4.EnableOutline = true;

                    VectorStyle overlap5 = new VectorStyle();
                    overlap5.Fill          = new SolidBrush(Color.FromArgb(20, Color.Black));
                    overlap5.Outline       = new Pen(Color.PowderBlue, 2.0f);
                    overlap5.EnableOutline = true;

                    VectorStyle defualtStyle = new VectorStyle();
                    defualtStyle.Fill = new SolidBrush(Color.FromArgb(25, Color.Green));
                    //overlap0.Outline = new Pen(Color.Yellow, 1.0f);
                    overlap0.EnableOutline = true;

                    Dictionary <int, IStyle> styles = new Dictionary <int, IStyle>();
                    styles.Add(0, overlap0);
                    styles.Add(1, overlap2);
                    styles.Add(2, overlap3);
                    styles.Add(3, overlap4);
                    styles.Add(4, overlap5);


                    VectorLayer vlayer = new VectorLayer("l");
                    vlayer.DataSource = new SharpMap.Data.Providers.GeometryFeatureProvider(allfdt);
                    vlayer.Theme      = new UniqueValuesTheme <int>("OverlapCount", styles, defualtStyle);
                    vlayer.Style.Fill = new SolidBrush(Color.FromArgb(30, Color.Green));
                    vlayer.Enabled    = true;
                    mbox.Map.Layers.Add(vlayer);
                    mbox.Map.ZoomToExtents();
                    mbox.ActiveTool = MapBox.Tools.Pan;
                    mbox.Refresh();
                }
            }
        }
Пример #15
0
 private static void SharpMapPolygonToSqlGeography(SqlGeographyBuilder geogBuilder, SMPolygon polygon)
 {
     geogBuilder.BeginGeography(OpenGisGeographyType.Polygon);
     //Note: Reverse Exterior ring orientation
     AddRing(geogBuilder, (SMLinearRing)polygon.ExteriorRing.Reverse());
     for (int i = 0; i < polygon.NumInteriorRings; i++)
     {
         AddRing(geogBuilder, (SMLinearRing)polygon.GetInteriorRingN(i));
     }
     geogBuilder.EndGeography();
 }
Пример #16
0
 private static void SharpMapPolygonToSqlGeometry(SqlGeometryBuilder geomBuilder, SMPolygon polygon)
 {
     geomBuilder.BeginGeometry(OpenGisGeometryType.Polygon);
     AddRing(geomBuilder, (SMLinearRing )polygon.ExteriorRing);
     for (int i = 0; i < polygon.NumInteriorRings; i++)
     {
         AddRing(geomBuilder, (SMLinearRing )polygon.GetInteriorRingN(i));
     }
     geomBuilder.EndGeometry();
 }
Пример #17
0
 internal static NTSMultiPolygon ToNTSMultiPolygon(Geometries.MultiPolygon geom,
     IGeometryFactory factory)
 {
     NTSPolygon[] polygons = new NTSPolygon[geom.Polygons.Count];
     int index = 0;
     foreach (Geometries.Polygon polygon in geom.Polygons)
         polygons[index++] = ToNTSPolygon(polygon, factory);
     return factory.CreateMultiPolygon(polygons) as NTSMultiPolygon;
 }
Пример #18
0
 internal static Geometries.Polygon ToSharpMapPolygon(NTSPolygon geom)
 {
     Geometries.LinearRing exteriorRing = ToSharpMapLinearRing((NTSLinearRing) geom.ExteriorRing);
     Collection<Geometries.LinearRing> interiorRings = new Collection<Geometries.LinearRing>();
     foreach (NTSLineString interiorRing in geom.InteriorRings)
         interiorRings.Add(ToSharpMapLinearRing((NTSLinearRing) interiorRing));
     return new Geometries.Polygon(exteriorRing, interiorRings);
 }
 /// <summary>
 /// Remove all holes in a polygon
 /// </summary>
 /// <param name="polygon"></param>
 /// <returns></returns>
 public static IGeometry RemoveHoles(this IPolygon polygon)
 {
     return(new Polygon((ILinearRing)polygon.ExteriorRing));
 }