private Polygon CreateKmlPolygon(IPolygon polygon)
        {
            var kmlPolygon = new Polygon();
            var ring       = new LinearRing {
                Coordinates = new CoordinateCollection()
            };

            kmlPolygon.OuterBoundary = new OuterBoundary {
                LinearRing = ring
            };

            foreach (var coordinate in polygon.ExteriorRing.Coordinates)
            {
                ring.Coordinates.Add(new Vector(coordinate.Y, coordinate.X));
            }

            foreach (var interiorRing in polygon.InteriorRings)
            {
                var innerBoundary = new InnerBoundary();
                kmlPolygon.AddInnerBoundary(innerBoundary);

                ring = new LinearRing {
                    Coordinates = new CoordinateCollection()
                };

                innerBoundary.LinearRing = ring;

                foreach (var coordinate in interiorRing.Coordinates)
                {
                    ring.Coordinates.Add(new Vector(coordinate.Y, coordinate.X));
                }
            }

            return(kmlPolygon);
        }
Beispiel #2
0
        public PlacemarkExtensionsTests()
        {
            _polygon = new Polygon
            {
                OuterBoundary = new OuterBoundary
                {
                    LinearRing = new LinearRing
                    {
                        Coordinates = new CoordinateCollection
                        {
                            new Vector
                            {
                                Longitude = 30,
                                Latitude  = 10
                            },
                            new Vector
                            {
                                Longitude = 40.5,
                                Latitude  = 40
                            },
                            new Vector
                            {
                                Longitude = 20,
                                Latitude  = 40
                            },
                            new Vector
                            {
                                Longitude = 10,
                                Latitude  = 20
                            },
                            new Vector
                            {
                                Longitude = 30,
                                Latitude  = 10
                            }
                        }
                    }
                }
            };

            var polygonWithHole = new Polygon
            {
                OuterBoundary = new OuterBoundary
                {
                    LinearRing = new LinearRing
                    {
                        Coordinates = new CoordinateCollection
                        {
                            new Vector
                            {
                                Longitude = 35,
                                Latitude  = 10
                            },
                            new Vector
                            {
                                Longitude = 45,
                                Latitude  = 45
                            },
                            new Vector
                            {
                                Longitude = 15,
                                Latitude  = 40
                            },
                            new Vector
                            {
                                Longitude = 10,
                                Latitude  = 20
                            },
                            new Vector
                            {
                                Longitude = 35,
                                Latitude  = 10
                            }
                        }
                    }
                }
            };

            polygonWithHole.AddInnerBoundary(new InnerBoundary
            {
                LinearRing = new LinearRing
                {
                    Coordinates = new CoordinateCollection
                    {
                        new Vector
                        {
                            Longitude = 20,
                            Latitude  = 30
                        },
                        new Vector
                        {
                            Longitude = 35,
                            Latitude  = 35
                        },
                        new Vector
                        {
                            Longitude = 30,
                            Latitude  = 20
                        },
                        new Vector
                        {
                            Longitude = 20,
                            Latitude  = 30
                        }
                    }
                }
            });

            _multipleGeometry = new MultipleGeometry();
            _multipleGeometry.AddGeometry(new Polygon
            {
                OuterBoundary = new OuterBoundary
                {
                    LinearRing = new LinearRing
                    {
                        Coordinates = new CoordinateCollection
                        {
                            new Vector
                            {
                                Longitude = 30,
                                Latitude  = 20
                            },
                            new Vector
                            {
                                Longitude = 45,
                                Latitude  = 40
                            },
                            new Vector
                            {
                                Longitude = 10,
                                Latitude  = 40
                            },
                            new Vector
                            {
                                Longitude = 30,
                                Latitude  = 20
                            }
                        }
                    }
                }
            });
            _multipleGeometry.AddGeometry(new Polygon
            {
                OuterBoundary = new OuterBoundary
                {
                    LinearRing = new LinearRing
                    {
                        Coordinates = new CoordinateCollection
                        {
                            new Vector
                            {
                                Longitude = 15,
                                Latitude  = 5
                            },
                            new Vector
                            {
                                Longitude = 40,
                                Latitude  = 10
                            },
                            new Vector
                            {
                                Longitude = 10,
                                Latitude  = 20
                            },
                            new Vector
                            {
                                Longitude = 5,
                                Latitude  = 10
                            },
                            new Vector
                            {
                                Longitude = 15,
                                Latitude  = 5
                            }
                        }
                    }
                }
            });

            var linestring = new LineString
            {
                Coordinates = new CoordinateCollection(new List <Vector>
                {
                    new Vector
                    {
                        Longitude = 30,
                        Latitude  = 10
                    },
                    new Vector
                    {
                        Longitude = 10,
                        Latitude  = 30
                    },
                    new Vector
                    {
                        Longitude = 40,
                        Latitude  = 40
                    }
                })
            };

            _placemark = new Placemark
            {
                Geometry = _polygon
            };
            _placemarkWithHole = new Placemark
            {
                Geometry = polygonWithHole
            };
            _multiplePlacemark = new Placemark
            {
                Geometry = _multipleGeometry
            };
            _placemarkWithPoint = new Placemark
            {
                Geometry = new Point {
                    Coordinate = new Vector(15, 5)
                }
            };
            _placemarkWithLinestring = new Placemark
            {
                Geometry = linestring
            };
        }
        /// <summary>
        /// Converts JSON geometry to KML geometry.
        /// </summary>
        /// <param name="geometry">ArcGIS JSON geometry</param>
        /// <returns><see cref="Geometry"/></returns>
        public static Geometry JsonToKmlGeometry(Dictionary <string, object> geometry)
        {
            Geometry placemarkGeometry;

            if (geometry.Keys.Contains("x"))
            {
                // Create point geometry
                // x and y are decimals;
                var x = Convert.ToDouble(geometry["x"]);
                var y = Convert.ToDouble(geometry["y"]);
                placemarkGeometry = new Point {
                    Coordinate = new Vector(y, x)
                };
            }
            else if (geometry.Keys.Contains("rings"))
            {
                // Create polygon geometry
                var rings = (ArrayList)geometry["rings"];

                var linearRings = from ArrayList ring in rings
                                  select new LinearRing
                {
                    Coordinates = new CoordinateCollection(from ArrayList point in ring
                                                           select new Vector(
                                                               Convert.ToDouble(point[1]),
                                                               Convert.ToDouble(point[0])
                                                               ))
                };
                if (linearRings.Count() > 1)
                {
                    var polygon = new Polygon();
                    polygon.OuterBoundary = new OuterBoundary {
                        LinearRing = linearRings.ElementAt(0)
                    };
                    var innerBoundaries = from lr in linearRings.Skip(1)
                                          select new InnerBoundary {
                        LinearRing = lr
                    };
                    foreach (var ib in innerBoundaries)
                    {
                        polygon.AddInnerBoundary(ib);
                    }
                    placemarkGeometry = polygon;
                }
                else
                {
                    placemarkGeometry = linearRings.First();
                }
            }
            else if (geometry.Keys.Contains("paths"))
            {
                // Create line geometry
                var paths = (ArrayList)geometry["paths"];

                var lineStrings = from ArrayList path in paths
                                  select new LineString
                {
                    Coordinates = new CoordinateCollection(from ArrayList point in path
                                                           select new Vector(
                                                               Convert.ToDouble(point[1]),
                                                               Convert.ToDouble(point[0])
                                                               ))
                };
                if (lineStrings.Count() > 1)
                {
                    var multiGeo = new MultipleGeometry();
                    foreach (var ls in lineStrings)
                    {
                        multiGeo.AddGeometry(ls);
                    }
                    placemarkGeometry = multiGeo;
                }
                else
                {
                    placemarkGeometry = lineStrings.First();
                }
            }
            else
            {
                placemarkGeometry = null;
            }
            return(placemarkGeometry);
        }