コード例 #1
0
        public static Map2DModel GeoJsonFeaureToMap2DModel(GeoJsonFeature <GeoJsonGeometry> feature, List <double> minMaxLatLong)
        {
            double longLenth = minMaxLatLong[1] - minMaxLatLong[0];
            double latLength = minMaxLatLong[3] - minMaxLatLong[2];

            Map2DModel map2D = new Map2DModel(); map2D.Id = feature.Id;

            map2D.Name            = feature.Properties == null ? "" : feature.Properties.Name;
            map2D.CapitalLocation = feature.Properties == null ? default(Point) : feature.Properties.CapitalLocationPoint;

            map2D.MinLongitude = minMaxLatLong[0];
            map2D.MaxLongitude = minMaxLatLong[1];
            map2D.MinLatitude  = minMaxLatLong[2];
            map2D.MaxLatitude  = minMaxLatLong[3];

            List <List <Point> > areaList = new List <List <Point> >();

            foreach (List <Point> area in feature.Geometry.PointList)
            {
                List <Point> pointList = new List <Point>();
                areaList.Add(pointList);

                foreach (Point point in area)
                {
                    Point newPoint = LongLatPointToNormalPoint(minMaxLatLong, point);

                    pointList.Add(newPoint);
                }
            }

            map2D.GeometryPointList = areaList;
            return(map2D);
        }
コード例 #2
0
        private void TestRoundTrip <TCoordinates>(string expected, GeoJsonFeature <TCoordinates> feature) where TCoordinates : GeoJsonCoordinates
        {
            var json = feature.ToJson();

            Assert.AreEqual(expected, json);

            var rehydrated = BsonSerializer.Deserialize <GeoJsonFeature <TCoordinates> >(json);

            Assert.AreEqual(expected, rehydrated.ToJson());
        }
コード例 #3
0
        /// <summary>
        /// Parses the specified <paramref name="json"/> object into an instance deriving from <see cref="GeoJsonObject"/>.
        ///
        /// As the GeoJSON format specified the type of a
        /// </summary>
        /// <param name="json">The JSON object.</param>
        /// <returns>An instance that derives from <see cref="GeoJsonObject"/>.</returns>
        private static GeoJsonObject Parse(JObject json)
        {
            if (json == null)
            {
                return(null);
            }

            // Get the value of the "type" property
            string type = json.GetString("type");

            if (string.IsNullOrWhiteSpace(type))
            {
                throw new GeoJsonParseException("The JSON object doesn't specify a type", json);
            }

            // Parse the type into an enum
            if (EnumUtils.TryParseEnum(type, out GeoJsonType result) == false)
            {
                throw new GeoJsonParseException($"Unknown type {type}", json);
            }

            switch (result)
            {
            case GeoJsonType.Feature:
                return(GeoJsonFeature.Parse(json));

            case GeoJsonType.FeatureCollection:
                return(GeoJsonFeatureCollection.Parse(json));

            case GeoJsonType.Point:
                return(GeoJsonPoint.Parse(json));

            case GeoJsonType.LineString:
                return(GeoJsonLineString.Parse(json));

            case GeoJsonType.Polygon:
                return(GeoJsonPolygon.Parse(json));

            case GeoJsonType.MultiPoint:
                return(GeoJsonMultiPoint.Parse(json));

            //case GeoJsonType.MultiLineString:
            //    return GeoJsonMultiLineString.Parse(obj);

            case GeoJsonType.MultiPolygon:
                return(GeoJsonMultiPolygon.Parse(json));

            case GeoJsonType.GeometryCollection:
                return(GeoJsonGeometryCollection.Parse(json));

            default:
                throw new GeoJsonParseException($"Unknown type {type}", json);
            }
        }
コード例 #4
0
        public virtual GeoJsonFeature ConvertPlacemark(KmlPlacemark placemark, IStyleProvider provider)
        {
            // Initialize a new feature
            GeoJsonFeature feature = new GeoJsonFeature();

            // Convert the name and description
            if (placemark.Name.HasValue())
            {
                feature.Properties.Name = placemark.Name;
            }
            if (placemark.Description.HasValue())
            {
                feature.Properties.Description = placemark.Description;
            }

            switch (placemark.Geometry)
            {
            case KmlPoint point:
                feature.Geometry = Convert(point);
                break;

            case KmlLineString lineString:
                feature.Geometry = Convert(lineString);
                break;

            case KmlPolygon polygon:
                feature.Geometry = Convert(polygon);
                break;

            default:
                throw new KmlException("Geometry type " + placemark.Geometry.GetType() + " not supported");
            }

            if (string.IsNullOrWhiteSpace(placemark.StyleUrl) == false && provider != null)
            {
                if (provider.TryGetStyleMapById(placemark.StyleUrl.Substring(1), out KmlStyleMap styleMap))
                {
                    if (styleMap.Normal != null && provider.TryGetStyleById(styleMap.Normal.StyleUrl.Substring(1), out KmlStyle style))
                    {
                        ConvertProperties(style, feature.Properties);
                    }
                }
                else
                {
                    if (provider.TryGetStyleById(placemark.StyleUrl.Substring(1), out KmlStyle style))
                    {
                        ConvertProperties(style, feature.Properties);
                    }
                }
            }

            return(feature);
        }
コード例 #5
0
        public void ToJson1()
        {
            // Initialize a new feature
            GeoJsonFeature feature = new GeoJsonFeature(
                new GeoJsonPoint(9.536067, 55.708116)
                );

            // Convert the feature to a JSON string
            string json = feature.ToJson(Formatting.None);

            Assert.AreEqual("{\"type\":\"Feature\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[9.536067,55.708116]}}", json);
        }
コード例 #6
0
        public void ToJson2()
        {
            GeoJsonFeature feature = new GeoJsonFeature(
                new GeoJsonPoint(9.536067, 55.708116),
                new GeoJsonProperties {
                Name = "My feature"
            }
                );

            // Convert the point to a JSON string
            string json = feature.ToJson(Formatting.None);

            Assert.AreEqual("{\"type\":\"Feature\",\"properties\":{\"name\":\"My feature\"},\"geometry\":{\"type\":\"Point\",\"coordinates\":[9.536067,55.708116]}}", json);
        }
コード例 #7
0
        public static GeoJson <GeoJsonGeometry> DecodeGeoJson(string geoJson)
        {
            string dealedGeoJson = geoJson.Replace("coordinates\":[[", "coordinates\":[").Replace("\"]],", "\"],").Replace("encodeOffsets\":[[[", "encodeOffsets\":[[").Replace("]]]},\"properties", "]]},\"properties");
            GeoJson <GeoJsonGeometryCoded> geoJsonCodedModel = JsonConvert.DeserializeObject <GeoJson <GeoJsonGeometryCoded> >(dealedGeoJson);

            List <GeoJsonFeature <GeoJsonGeometryCoded> > features = geoJsonCodedModel.Features;

            foreach (GeoJsonFeature <GeoJsonGeometryCoded> feature in features)
            {
                if (feature != null && feature.Properties != null && feature.Properties.CapitalLocation != null && feature.Properties.CapitalLocation.Count == 2)
                {
                    feature.Properties.CapitalLocationPoint = new Point()
                    {
                        X = feature.Properties.CapitalLocation[0], Y = feature.Properties.CapitalLocation[1]
                    };
                }
            }

            GeoJson <GeoJsonGeometry> geoJsonModel = new GeoJson <GeoJsonGeometry>();

            geoJsonModel.Type     = geoJsonCodedModel.Type;
            geoJsonModel.Features = new List <GeoJsonFeature <GeoJsonGeometry> >();

            //解压
            foreach (GeoJsonFeature <GeoJsonGeometryCoded> feature in features)
            {
                GeoJsonGeometryCoded  geometry      = feature.Geometry;
                List <string>         coordinates   = geometry.Coordinates;
                List <List <double> > encodeOffsets = geometry.EncodeOffsets;

                List <List <Point> > gemeryPointList = new List <List <Point> >();
                for (int i = 0; i < coordinates.Count; i++)
                {
                    List <Point> pointList = DecodePolygon(coordinates[i], encodeOffsets[i]);
                    gemeryPointList.Add(pointList);
                }

                GeoJsonFeature <GeoJsonGeometry> geojsonFeature = new GeoJsonFeature <GeoJsonGeometry>();
                geojsonFeature.Id         = feature.Id;
                geojsonFeature.Type       = feature.Type;
                geojsonFeature.Properties = feature.Properties;
                geojsonFeature.Geometry   = new GeoJsonGeometry();

                geojsonFeature.Geometry.Type      = geometry.Type;
                geojsonFeature.Geometry.PointList = gemeryPointList;
                geoJsonModel.Features.Add(geojsonFeature);
            }

            return(geoJsonModel);
        }
コード例 #8
0
        private static Graphic CreateRoadGraphic(GeoJsonFeature <GeoJsonPolygon> roadFeature, Esri.ArcGISRuntime.Geometry.SpatialReference originalSpatialReference, Esri.ArcGISRuntime.Geometry.SpatialReference targetSpatialReference)
        {
            var roadPolyline = CreatePolyline(roadFeature.Geometry.Coordinates, originalSpatialReference);

            if (!originalSpatialReference.IsEqual(targetSpatialReference))
            {
                // In den Raumbezug der Karte projezieren
                roadPolyline = (Esri.ArcGISRuntime.Geometry.Polyline)GeometryEngine.Project(roadPolyline, targetSpatialReference);
            }

            var roadGraphic = new Graphic(roadPolyline, roadFeature.Properties);

            return(roadGraphic);
        }
コード例 #9
0
        /// <inheritdoc />
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            if (reader.TokenType == JsonToken.Null)
            {
                return(null);
            }
            if (reader.TokenType != JsonToken.StartObject)
            {
                return(null);
            }

            JObject obj = JObject.Load(reader);

            string type = obj.Value <string>("type");

            switch (type?.ToLower())
            {
            case "feature":
                return(GeoJsonFeature.Parse(obj));

            case "featurecollection":
                return(GeoJsonFeatureCollection.Parse(obj));

            case "point":
                return(GeoJsonPoint.Parse(obj));

            case "multipoint":
                return(GeoJsonMultiPoint.Parse(obj));

            case "linestring":
                return(GeoJsonLineString.Parse(obj));

            case "multilinestring":
                return(GeoJsonMultiLineString.Parse(obj));

            case "polygon":
                return(GeoJsonPolygon.Parse(obj));

            case "multipolygon":
                return(GeoJsonMultiPolygon.Parse(obj));

            default:
                if (objectType == typeof(GeoJsonProperties))
                {
                    return(ReadJsonProperties(reader));
                }
                throw new GeoJsonParseException($"Unknown shape: {type}", obj);
            }
        }
コード例 #10
0
        public void Constructor1()
        {
            GeoJsonFeature feature = new GeoJsonFeature(new GeoJsonPoint(9.536067, 55.708116));

            Assert.IsNotNull(feature.Geometry);
            Assert.IsNotNull(feature.Properties);

            GeoJsonPoint point = feature.Geometry as GeoJsonPoint;

            Assert.IsNotNull(point);

            Assert.AreEqual(9.536067, point.X, "X");
            Assert.AreEqual(55.708116, point.Y, "Y");
            Assert.AreEqual(0, point.Altitude, "Altitude");
        }
コード例 #11
0
        public void Parse1()
        {
            const string json = "{\"type\":\"Feature\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[9.536067,55.708116]}}";

            GeoJsonFeature feature = GeoJsonFeature.Parse(json);

            Assert.IsNotNull(feature);
            Assert.IsNotNull(feature.Geometry);
            Assert.IsNotNull(feature.Properties);

            GeoJsonPoint point = feature.Geometry as GeoJsonPoint;

            Assert.IsNotNull(point);
            Assert.AreEqual(9.536067, point.X, "X");
            Assert.AreEqual(55.708116, point.Y, "Y");
            Assert.AreEqual(0, point.Altitude, "Altitude");
        }
コード例 #12
0
        public void Parse2()
        {
            const string json = "{\"type\":\"Feature\",\"properties\":{\"name\":\"My feature\"},\"geometry\":{\"type\":\"Point\",\"coordinates\":[9.536067,55.708116,100]}}";

            GeoJsonFeature feature = GeoJsonFeature.Parse(json);

            Assert.IsNotNull(feature);
            Assert.IsNotNull(feature.Geometry);
            Assert.IsNotNull(feature.Properties);

            GeoJsonPoint point = feature.Geometry as GeoJsonPoint;

            Assert.IsNotNull(point);
            Assert.AreEqual(9.536067, point.X, "X");
            Assert.AreEqual(55.708116, point.Y, "Y");
            Assert.AreEqual(100, point.Altitude, "Altitude");

            Assert.AreEqual("My feature", feature.Properties.Name);
            Assert.AreEqual(null, feature.Properties.Description);
        }
コード例 #13
0
        public static GeoJson <GeoJsonGeometry> DecodeStandardGeoJson(string geoJson)
        {
            GeoJson <GeoJsonGeometryStandard> geoJsonCodedModel = JsonConvert.DeserializeObject <GeoJson <GeoJsonGeometryStandard> >(geoJson);

            GeoJson <GeoJsonGeometry> geoJsonModel = new GeoJson <GeoJsonGeometry>();

            geoJsonModel.Type     = geoJsonCodedModel.Type;
            geoJsonModel.Features = new List <GeoJsonFeature <GeoJsonGeometry> >();

            List <GeoJsonFeature <GeoJsonGeometryStandard> > features = geoJsonCodedModel.Features;

            foreach (GeoJsonFeature <GeoJsonGeometryStandard> feature in features)
            {
                List <List <List <double> > > coordinates = feature.Geometry.Coordinates;

                List <List <Point> > geometryPointsList = new List <List <Point> >();
                for (int k = 0; k < coordinates.Count; k++)
                {
                    List <Point> pointList = new List <Point>();
                    geometryPointsList.Add(pointList);

                    for (int i = 0; i < coordinates[0].Count; i++)
                    {
                        Point point = new Point();
                        point.X = coordinates[k][i][0];
                        point.Y = coordinates[k][i][1];

                        pointList.Add(point);
                    }
                }

                GeoJsonGeometry geoJsonGeometry = new GeoJsonGeometry();
                geoJsonGeometry.PointList = geometryPointsList;

                GeoJsonFeature <GeoJsonGeometry> geoJsonFeature = new GeoJsonFeature <GeoJsonGeometry>();
                geoJsonFeature.Geometry = geoJsonGeometry;

                geoJsonModel.Features.Add(geoJsonFeature);
            }
            return(geoJsonModel);
        }
コード例 #14
0
ファイル: MapFactory.cs プロジェクト: mzemljak/LiveCharts2
        /// <inheritdoc cref="IMapFactory{TDrawingContext}.ConvertToPathShape(GeoJsonFeature, MapContext{TDrawingContext})"/>
        public IEnumerable <IDrawable <SkiaSharpDrawingContext> > ConvertToPathShape(
            GeoJsonFeature feature, MapContext <SkiaSharpDrawingContext> context)
        {
            var projector = context.Projector;

            var paths = new List <HeatPathShape>();
            var d     = new double[0][][][];

            foreach (var geometry in feature.Geometry?.Coordinates ?? d)
            {
                foreach (var segment in geometry)
                {
                    var path = new HeatPathShape {
                        IsClosed = true
                    };

                    var isFirst = true;
                    foreach (var point in segment)
                    {
                        var p = projector.ToMap(point);

                        if (isFirst)
                        {
                            isFirst = false;
                            path.AddCommand(new MoveToPathCommand {
                                X = p[0], Y = p[1]
                            });
                            continue;
                        }

                        path.AddCommand(new LineSegment {
                            X = p[0], Y = p[1]
                        });
                    }

                    paths.Add(path);
                }
            }

            return(paths);
        }
コード例 #15
0
        public void GeoJson_SerializationOfSampleGeometryPoint_ShouldWork()
        {
            var point = new GeoJsonFeature
            {
                Type     = GeoJsonType.Feature.ToString(),
                Geometry = new GeoJsonGeometry
                {
                    Type        = GeoJsonType.Point.ToString(),
                    Coordinates = (new double[] { 125.6, 10.1 }).Select(c => c as object).ToList()
                },
                Properties = new
                {
                    name = "Dinagat Islands"
                }
            };

            var thisJToken  = JObject.Parse(JsonConvert.SerializeObject(point));
            var smpleJToken = JObject.Parse(JsonGeometryPointStringSample);

            Assert.IsTrue(JToken.DeepEquals(thisJToken, smpleJToken));
        }
コード例 #16
0
        /// <summary>
        /// Returns an instance of <see cref="IRectangle"/> representing the bounding box of the GeoJSON specified <paramref name="feature."/>
        /// </summary>
        /// <param name="feature">The feature.</param>
        /// <returns>An instance of <see cref="IRectangle"/>.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="feature"/> is <c>null</c>.</exception>
        public static IRectangle GetBoundingBox(GeoJsonFeature feature)
        {
            if (feature == null)
            {
                throw new ArgumentNullException(nameof(feature));
            }

            IGeometry geometry = Convert(feature.Geometry);

            switch (geometry)
            {
            case IPoint point:
                return(new Rectangle(point, point));

            case IShape shape:
                return(shape.GetBoundingBox());

            default:
                throw new GeoJsonException($"Unsupported geometry {geometry.GetType()}");
            }
        }
コード例 #17
0
        // ***************** Export As GeoJson *******
        // *******************************************
        public static ILegendCommand CreateExportDrawingItemLayerAsGeoJson(MapPresenter map, DrawingItemLayer layer)
        {
            var result = new LegendCommand()
            {
                PathMarkup = IRI.Jab.Common.Assets.ShapeStrings.Others.json,
                Layer      = layer,
                ToolTip    = _saveAsGeoJsonToolTip,
            };

            result.Command = new RelayCommand(async param =>
            {
                try
                {
                    var file = map.DialogService.ShowSaveFileDialog("*.json|*.json", null, layer.LayerName);

                    if (string.IsNullOrWhiteSpace(file))
                    {
                        return;
                    }

                    var feature = GeoJsonFeature.Create(layer.Geometry.Project(SrsBases.GeodeticWgs84).AsGeoJson());

                    GeoJsonFeatureSet featureSet = new GeoJsonFeatureSet()
                    {
                        Features = new List <GeoJsonFeature>()
                        {
                            feature
                        }, TotalFeatures = 1
                    };

                    featureSet.Save(file, false, false);
                }
                catch (Exception ex)
                {
                    await map.DialogService.ShowMessageAsync(null, ex.Message, null, null);
                }
            });

            return(result);
        }
コード例 #18
0
 /// <summary>
 /// Serializes a value.
 /// </summary>
 /// <param name="context">The serialization context.</param>
 /// <param name="args">The serialization args.</param>
 /// <param name="value">The value.</param>
 protected override void SerializeValue(BsonSerializationContext context, BsonSerializationArgs args, GeoJsonFeature <TCoordinates> value)
 {
     _helper.SerializeMembers(context, value, SerializeDerivedMembers);
 }
コード例 #19
0
 private void SerializeDerivedMembers(BsonSerializationContext context, GeoJsonFeature <TCoordinates> value)
 {
     SerializeGeometry(context, value.Geometry);
     SerializeId(context, value.Id);
     SerializeProperties(context, value.Properties);
 }
コード例 #20
0
        private void ConvertFeature(List <VectorTileFeature> features, GeoJsonFeature feature, double tolerance)
        {
            if (feature.Geometry == null)
            {
                return;
            }

            var geom = feature.Geometry;
            var type = geom.Type;

            if (type == GeoJsonObject.GeoJsonPointType)
            {
                var point = geom as Point;
                features.Add(Create(feature.Properties, 1, new[] { new VectorTileGeometry {
                                                                       ProjectPoint(point.Coordinates)
                                                                   } }));
            }
            else if (type == GeoJsonObject.GeoJsonMultiPointType)
            {
                var multiPoint = geom as MultiPoint;
                features.Add(Create(feature.Properties, 1, new[] { Project(multiPoint.Coordinates) }));
            }
            else if (type == GeoJsonObject.GeoJsonLineStringType)
            {
                var linestring = geom as LineString;
                features.Add(Create(feature.Properties, 2, new[] { Project(linestring.Coordinates, tolerance) }));
            }
            else if (type == GeoJsonObject.GeoJsonMultiLineStringType || type == GeoJsonObject.GeoJsonPolygonType)
            {
                var coords = (geom as MultiLineStringPolygonGeometry).Coordinates;
                var rings  = new List <VectorTileGeometry>();
                for (var i = 0; i < coords.Length; i++)
                {
                    rings.Add(Project(coords[i], tolerance));
                }
                features.Add(Create(feature.Properties, type == GeoJsonObject.GeoJsonPolygonType ? 3 : 2, rings.ToArray()));
            }
            else if (type == GeoJsonObject.GeoJsonMultiPolygonType)
            {
                var coords = (geom as MultiPolygon).Coordinates;
                var rings  = new List <VectorTileGeometry>();
                for (var i = 0; i < coords.Length; i++)
                {
                    for (var j = 0; j < coords[i].Length; j++)
                    {
                        rings.Add(Project(coords[i][j], tolerance));
                    }
                }
                features.Add(Create(feature.Properties, 3, rings.ToArray()));
            }
            else if (type == GeoJsonObject.GeoJsonGeometryCollectionType)
            {
                var collection = geom as GeometryCollection;
                for (var i = 0; i < collection.Geometries.Length; i++)
                {
                    ConvertFeature(features, new GeoJsonFeature
                    {
                        Geometry   = collection.Geometries[i],
                        Properties = feature.Properties,
                    }, tolerance);
                }
            }
            else
            {
                throw new Exception("Input data is not a valid GeoJSON object.");
            }
        }