Ejemplo n.º 1
0
        /// <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;
        }
Ejemplo n.º 2
0
        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;
        }