コード例 #1
0
        public List <VectorTileFeature> Convert(GeoJsonObject data, double tolerance)
        {
            var features = new List <VectorTileFeature>();

            if (data.Type == GeoJsonObject.FeatureCollectionType)
            {
                var featureCollection = data as GeoJsonFeatureCollection;

                for (int i = 0; i < featureCollection.Features.Length; i++)
                {
                    ConvertFeature(features, featureCollection.Features[i], tolerance);
                }
            }
            else if (data.Type == GeoJsonObject.FeatureType)
            {
                ConvertFeature(features, data as GeoJsonFeature, tolerance);
            }
            else
            {
                // single geometry or a geometry collection
                ConvertFeature(features, new GeoJsonFeature {
                    Geometry = data as GeometryObject
                }, tolerance);
            }

            return(features);
        }
コード例 #2
0
        /// <summary>
        /// Serializes the fields.
        /// </summary>
        /// <param name="bsonWriter">The BsonWriter.</param>
        /// <param name="obj">The GeoJson object.</param>
        protected override void SerializeFields(BsonWriter bsonWriter, GeoJsonObject <TCoordinates> obj)
        {
            var feature = (GeoJsonFeature <TCoordinates>)obj;

            SerializeGeometry(bsonWriter, feature.Geometry);
            SerializeId(bsonWriter, feature.Id);
            SerializeProperties(bsonWriter, feature.Properties);
        }
コード例 #3
0
        public List <GeoJsonObject> GetObjects()
        {
            List <GeoJsonObject> result = new();

            using var dbConn = GetConnection();
            dbConn.Open();

            using var dbCmd = dbConn.CreateCommand();

            dbCmd.CommandText =
                @"select 
                    mrid, 
                    ST_AsGeoJSON(ST_Transform(coord,4326)) as coord, 
                    routenode_kind, 
                    routenode_function, 
                    naming_name, 
                    mapping_method,
                    lifecycle_deployment_state
                  from 
                    route_network.route_node 
                  where
                    coord is not null and
                    marked_to_be_deleted = false
                  order by mrid
                ";

            using var dbReader = dbCmd.ExecuteReader();

            while (dbReader.Read())
            {
                var mrid     = dbReader[0].ToString();
                var geojson  = dbReader[1].ToString();
                var kind     = dbReader.IsDBNull(2) ? null : dbReader[2].ToString();
                var function = dbReader.IsDBNull(3) ? null : dbReader[3].ToString();
                var name     = dbReader.IsDBNull(4) ? null : dbReader[4].ToString();
                var method   = dbReader.IsDBNull(5) ? null : dbReader[5].ToString();
                var status   = dbReader.IsDBNull(6) ? null : dbReader[6].ToString();

                var jsonObj = new GeoJsonObject(_counter.GetNext(), Name, geojson, MinZoomLevel(kind, function), MaxZoomLevel(kind, function));

                jsonObj.AddProperty("mrid", mrid);
                jsonObj.AddProperty("kind", kind);
                jsonObj.AddProperty("function", function);
                jsonObj.AddProperty("name", name);
                jsonObj.AddProperty("method", method);
                jsonObj.AddProperty("status", status);

                result.Add(jsonObj);
            }

            return(result);
        }
コード例 #4
0
        private JObject CreatePropertiesJsonObject(GeoJsonObject feature)
        {
            JObject jsonProperties = new();

            jsonProperties.Add(new JProperty("objecttype", feature.ObjectType));

            foreach (var attr in feature.Properties)
            {
                jsonProperties.Add(new JProperty(attr.Name, attr.Value));
            }

            return(jsonProperties);
        }
コード例 #5
0
 /// <summary>
 /// Serializes a GeoJson object.
 /// </summary>
 /// <param name="bsonWriter">The BsonWriter.</param>
 /// <param name="obj">The GeoJson object.</param>
 protected void SerializeGeoJsonObject(BsonWriter bsonWriter, GeoJsonObject <TCoordinates> obj)
 {
     if (obj == null)
     {
         bsonWriter.WriteNull();
     }
     else
     {
         bsonWriter.WriteStartDocument();
         SerializeType(bsonWriter, obj.Type);
         SerializeCoordinateReferenceSystem(bsonWriter, obj.CoordinateReferenceSystem);
         SerializeBoundingBox(bsonWriter, obj.BoundingBox);
         SerializeFields(bsonWriter, obj);
         SerializeExtraMembers(bsonWriter, obj.ExtraMembers);
         bsonWriter.WriteEndDocument();
     }
 }
コード例 #6
0
        public void ProcessData(GeoJsonObject data)
        {
            Logger.LogDebug($"Preprocessing data");
            var z2       = 1 << Options.MaxZoom;//2^z
            var features = Converter.Convert(data, Options.Tolerance / (z2 * Options.Extent));

            Logger.LogDebug($"Preprocessing data end");

            //   Tiles = new Dictionary<string, VectorTile>();
            //   TileCoords = new List<VectorTileCoord>();

            features = Wrapper.Wrap(features, Options.Buffer / Options.Extent, IntersectX);

            // start slicing from the top tile down
            if (features.Count > 0)
            {
                SplitTile(features, new VectorTileCoord());
            }
        }
コード例 #7
0
        private JObject CreateFeatureJsonObject(GeoJsonObject feature)
        {
            dynamic jsonFeature = new JObject();

            jsonFeature.type     = "Feature";
            jsonFeature.id       = feature.Id;
            jsonFeature.geometry = JObject.Parse(feature.GeoJson);

            if (feature.Properties.Count > 0)
            {
                jsonFeature.properties = CreatePropertiesJsonObject(feature);
            }

            dynamic tippecanoe = new JObject();

            tippecanoe.minzoom = feature.MinZoom;
            tippecanoe.maxzoom = feature.MaxZoom;

            jsonFeature.tippecanoe = tippecanoe;

            return(jsonFeature);
        }
コード例 #8
0
        public void Parse3()
        {
            const string json = "{\"type\":\"Feature\",\"properties\":{\"name\":\"My feature\"},\"geometry\":{\"type\":\"Point\",\"coordinates\":[9.536067,55.708116,100]}}";

            GeoJsonObject obj = GeoJsonUtils.Parse(json);

            Assert.IsNotNull(obj);

            GeoJsonFeature feature = obj as GeoJsonFeature;

            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);
        }
コード例 #9
0
        /// <summary>
        /// Serializes the fields.
        /// </summary>
        /// <param name="bsonWriter">The BsonWriter.</param>
        /// <param name="obj">The GeoJson object.</param>
        protected override void SerializeFields(BsonWriter bsonWriter, GeoJsonObject <TCoordinates> obj)
        {
            var lineString = (GeoJsonLineString <TCoordinates>)obj;

            SerializeCoordinates(bsonWriter, lineString.Coordinates);
        }
コード例 #10
0
        /// <summary>
        /// Serializes the fields.
        /// </summary>
        /// <param name="bsonWriter">The BsonWriter.</param>
        /// <param name="obj">The GeoJson object.</param>
        protected override void SerializeFields(BsonWriter bsonWriter, GeoJsonObject <TCoordinates> obj)
        {
            var multiPoint = (GeoJsonMultiPoint <TCoordinates>)obj;

            SerializeCoordinates(bsonWriter, multiPoint.Coordinates);
        }
コード例 #11
0
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("Type or paste the folder location containing the csv-file...");
                var folder = Console.ReadLine();

                using (StreamReader nephropsReader = new StreamReader("Nephrops_FU.json"))
                    using (StreamReader r = new StreamReader("ICES_areas_trimmed.json"))
                    {
                        string json         = r.ReadToEnd();
                        string nephropsJson = nephropsReader.ReadToEnd();

                        IcesArea icesAreas = JsonConvert.DeserializeObject <IcesArea>(json);
                        var      features  = icesAreas.features;

                        FunctionalUnit functionalUnits  = JsonConvert.DeserializeObject <FunctionalUnit>(nephropsJson);
                        var            nephropsFeatures = functionalUnits.features;

                        Dictionary <int, GeoJsonObject> speciesPerYear = new Dictionary <int, GeoJsonObject>();

                        var files = Directory.GetFiles("C:\\temp", "*.csv", SearchOption.TopDirectoryOnly);

                        foreach (var file in files)
                        {
                            bool isNephropsFile = file.Contains("NEP");
                            speciesPerYear = new Dictionary <int, GeoJsonObject>();
                            TextReader reader    = new StreamReader(Path.Combine(folder, file));
                            var        csvReader = new CsvReader(reader);
                            csvReader.Configuration.Delimiter = ";";
                            var records = csvReader.GetRecords <dynamic>().ToList();
                            int count   = records.Count();
                            var species = records.First().Species;


                            //foreach line in the csv
                            foreach (var record in records)
                            {
                                Feature  feature = new Feature();
                                int      year    = int.Parse(record.AssessmentYear);
                                string[] icesDivisionsOrFunctionalUnits;
                                if (isNephropsFile)
                                {
                                    icesDivisionsOrFunctionalUnits = record.Functional_Unit.ToString().Split("-");
                                }
                                else
                                {
                                    icesDivisionsOrFunctionalUnits = record.ICES_Areas.ToString().Split("~");
                                }

                                feature.type                 = "Feature";
                                feature.properties           = record;
                                feature.geometry             = new Geometry();
                                feature.geometry.type        = "MultiPolygon";
                                feature.geometry.coordinates = new List <List <List <object> > >();
                                //foreach icesdivision in the cell
                                for (int i = 0; i < icesDivisionsOrFunctionalUnits.Length; i++)
                                {
                                    Feature f;
                                    if (isNephropsFile)
                                    {
                                        f = nephropsFeatures.FirstOrDefault(x => x.properties.FU.ToString().Contains(icesDivisionsOrFunctionalUnits[i].Trim()));
                                    }
                                    else
                                    {
                                        f = features.FirstOrDefault(x => x.properties.Area_Full.ToString().Contains(icesDivisionsOrFunctionalUnits[i].Trim()));
                                    }

                                    if (f == null)
                                    {
                                        continue;
                                    }
                                    if (f.geometry.type == "MultiPolygon")
                                    {
                                        foreach (var multipolygon in f.geometry.coordinates)
                                        {
                                            var mp = new List <List <object> >();
                                            foreach (var polygon in multipolygon)
                                            {
                                                var p = new List <object>();
                                                foreach (var coords in polygon)
                                                {
                                                    p.Add(coords);
                                                }
                                                mp.Add(p);
                                            }
                                            feature.geometry.coordinates.Add(mp);
                                        }
                                    }
                                    else if (f.geometry.type == "Polygon")
                                    {
                                        var mp = new List <List <object> >();
                                        foreach (var polygon in f.geometry.coordinates)
                                        {
                                            var p = new List <object>();
                                            foreach (var coords in polygon)
                                            {
                                                p.Add(coords);
                                            }
                                            mp.Add(p);
                                        }
                                        feature.geometry.coordinates.Add(mp);
                                    }
                                }
                                //add the geoJson if it does not exist for the year
                                if (!speciesPerYear.ContainsKey(year))
                                {
                                    GeoJsonObject geoJson = new GeoJsonObject();
                                    geoJson.type         = "FeatureCollection";
                                    geoJson.features     = new List <Feature>();
                                    speciesPerYear[year] = geoJson;
                                }
                                if (((List <List <List <object> > >)feature.geometry.coordinates).Any())
                                {
                                    speciesPerYear[year].features.Add(feature);
                                }
                            }

                            foreach (KeyValuePair <int, GeoJsonObject> entry in speciesPerYear)
                            {
                                var jsonString = JsonConvert.SerializeObject(entry.Value);
                                var filename   = $@"{species}_{entry.Key}.geojson";
                                Console.WriteLine($@"Creating File {filename} ...");
                                File.WriteAllText(Path.Combine(folder, filename), jsonString, Encoding.UTF8);
                            }
                            reader.Close();
                        }
                    }
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("All done!");
            }
            catch (Exception e)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Error:");
                Console.WriteLine(e.Message);
            }
            finally
            {
                Console.ResetColor();
            }
        }
コード例 #12
0
        /// <summary>
        /// Serializes the fields.
        /// </summary>
        /// <param name="bsonWriter">The BsonWriter.</param>
        /// <param name="obj">The GeoJson object.</param>
        protected override void SerializeFields(BsonWriter bsonWriter, GeoJsonObject <TCoordinates> obj)
        {
            var polygon = (GeoJsonPolygon <TCoordinates>)obj;

            SerializeCoordinates(bsonWriter, polygon.Coordinates);
        }
コード例 #13
0
        /// <summary>
        /// Serializes the fields.
        /// </summary>
        /// <param name="bsonWriter">The BsonWriter.</param>
        /// <param name="obj">The GeoJson object.</param>
        protected override void SerializeFields(BsonWriter bsonWriter, GeoJsonObject <TCoordinates> obj)
        {
            var geometryCollection = (GeoJsonGeometryCollection <TCoordinates>)obj;

            SerializeGeometries(bsonWriter, geometryCollection.Geometries);
        }
コード例 #14
0
 /// <summary>
 /// Serializes the fields.
 /// </summary>
 /// <param name="bsonWriter">The BsonWriter.</param>
 /// <param name="obj">The GeoJson object.</param>
 protected virtual void SerializeFields(BsonWriter bsonWriter, GeoJsonObject <TCoordinates> obj)
 {
 }
コード例 #15
0
        /// <summary>
        /// Serializes the fields.
        /// </summary>
        /// <param name="bsonWriter">The BsonWriter.</param>
        /// <param name="obj">The GeoJson object.</param>
        protected override void SerializeFields(BsonWriter bsonWriter, GeoJsonObject <TCoordinates> obj)
        {
            var featureCollection = (GeoJsonFeatureCollection <TCoordinates>)obj;

            SerializeFeatures(bsonWriter, featureCollection.Features);
        }