コード例 #1
0
ファイル: Program.cs プロジェクト: jarped/naturtypekart
        private static void TileNorway()
        {
            var layer = new TiledVectorLayer("test2", WebMercator.BoundingBox1, 39135.75848201024 * 4);
            var store = new DiskTileStore(layer);

            store.Wipe();
            var tiler    = new VectorTiler(store, layer);
            var filename = FileLocator.FindFileInTree(@"data\norway.geojson");
            FeatureCollection        norway       = JsonConvert.DeserializeObject <FeatureCollection>(File.ReadAllText(filename));
            List <OmrådeMedGeometry> partOfNorway = new List <OmrådeMedGeometry>();

            foreach (var feature in norway.Features)
            {
                partOfNorway.Add(new OmrådeMedGeometry(new Område(3581, AreaType.Land), DotSpatialGeometry.From(feature)));
            }
            foreach (var omg in partOfNorway)
            {
                omg.Område.Number = -5;
            }
            for (int zoom = 0; zoom < 6; zoom++)
            {
                foreach (OmrådeMedGeometry polygon in partOfNorway)
                {
                    tiler.Update(polygon.Område, polygon.Geometry, zoom);
                }
            }
        }
コード例 #2
0
        public static GeoJSON.Net.Feature.FeatureCollection ToGeoJsonFeatureCollection(List <County> counties)
        {
            var featureCollection = new GeoJSON.Net.Feature.FeatureCollection();

            featureCollection.Features.AddRange(counties.Select(MakeFeatureWithRelevantProperties).ToList());
            return(featureCollection);
        }
コード例 #3
0
        public static GeoJSON.Net.Feature.FeatureCollection ToGeoJsonFeatureCollection(List <StormwaterJurisdiction> stormwaterJurisdictions)
        {
            var featureCollection = new GeoJSON.Net.Feature.FeatureCollection();

            featureCollection.Features.AddRange(stormwaterJurisdictions.Select(MakeFeatureWithRelevantProperties).ToList());
            return(featureCollection);
        }
コード例 #4
0
        public static GeoJSON.Net.Feature.FeatureCollection ToGeoJsonFeatureCollection(this IEnumerable <DelineationGeometryStaging> delineationGeometryStagings)
        {
            var featureCollection = new GeoJSON.Net.Feature.FeatureCollection();

            featureCollection.Features.AddRange(delineationGeometryStagings.Select(x =>
            {
                var feature = DbGeometryToGeoJsonHelper.FromDbGeometryWithReprojectionCheck(DbGeometry.FromText(""));

                feature.Properties.Add("FeatureWeight", 1);
                feature.Properties.Add("FillPolygon", true);
                feature.Properties.Add("FeatureColor", "#405d74");
                feature.Properties.Add("FillOpacity", "0.2");
                return(feature);
            }));
            return(featureCollection);
        }
コード例 #5
0
        public void Save(string key, VectorQuadTile tile)
        {
            var ro  = new FeatureCollection();
            var crs = new NamedCRS("EPSG:" + Config.Settings.Map.SpatialReferenceSystemIdentifier);

            ro.CRS = crs;

            foreach (var omg in tile.Områder)
            {
                var             område   = omg.Område;
                IGeometryObject geometry = GeoJsonGeometry.FromDotSpatial(omg.Geometry);
                var             props    = new Dictionary <string, object>
                {
                    { "category", område.Category },
                    { "name", område.Name },
                    { "number", område.Number },
                    { "value", område.Value },
                    { "type", område.Type.ToString() },
                    { "kind", område.kind }
                };
                var feature = new Feature(geometry, props)
                {
                    Id = område.AreaId.ToString()
                };
                var fp = new FeatureProperties {
                    nin = område.kind
                };
                if (område.Number == 0)
                {
                    throw new Exception("Område mangler nummer.");
                }
                ro.Features.Add(feature);
            }

            var fullPath          = GetFullPath(key);
            var fullPathSingleDir = GetFullPath(key.Replace("/", "_"));

            var settings = GetJsonSerializerSettings();

            File.WriteAllText(fullPath, JsonConvert.SerializeObject(ro, settings));
            //File.WriteAllText(fullPathSingleDir, JsonConvert.SerializeObject(ro, settings));

            //var serializer = new DataContractJsonSerializer(typeof(VectorQuadTile), CreateDataContractJsonSerializerSettings());
            //using (Stream stream = File.Create(fullPath + ".adf"))
            //    serializer.WriteObject(stream, tile);
        }
コード例 #6
0
        public void TileNorway()
        {
            var filename = FileLocator.FindFileInTree(@"data\norway.geojson");
            FeatureCollection        norway       = JsonConvert.DeserializeObject <FeatureCollection>(File.ReadAllText(filename));
            List <OmrådeMedGeometry> partOfNorway = new List <OmrådeMedGeometry>();

            foreach (var feature in norway.Features)
            {
                partOfNorway.Add(new OmrådeMedGeometry(new Område(3581, AreaType.Land), DotSpatialGeometry.From(feature)));
            }
            foreach (var omg in partOfNorway)
            {
                omg.Område.Number = -5;
            }
            for (int zoom = 0; zoom < 6; zoom++)
            {
                foreach (OmrådeMedGeometry polygon in partOfNorway)
                {
                    tiler.Update(polygon.Område, polygon.Geometry, zoom);
                }
            }
        }
コード例 #7
0
        public JsonResult GeoFences()
        {
            try{
                if (redis == null)
                {
                    string tile38Connection = _configuration.GetConnectionString("Tile38Connection");
                    redis = ConnectionMultiplexer.Connect(tile38Connection);
                    _logger.LogInformation($"Connected to Tile38 {tile38Connection}");
                }

                db = redis.GetDatabase();

                var result = db.Execute("CHANS", "*");
                _logger.LogInformation(result.ToString());

                GeoJSON.Net.Feature.FeatureCollection geofences = new GeoJSON.Net.Feature.FeatureCollection();

                // Top level - collection of features
                System.Diagnostics.Debug.Assert(result.Type == ResultType.MultiBulk);
                RedisResult[] topLevelRedisArrayResult = ((RedisResult[])result);
                foreach (RedisResult redisResult in topLevelRedisArrayResult)
                {
                    // Child level - the geofence
                    System.Diagnostics.Debug.Assert(redisResult.Type == ResultType.MultiBulk);
                    RedisResult[] childRedisArrayResult = ((RedisResult[])redisResult);


                    // First property should be geofence name
                    System.Diagnostics.Debug.Assert(childRedisArrayResult[0].Type == ResultType.BulkString);

                    // Last property should be contain 'WITHIN', 'enter,exit', etc, and geofence GeoJSON
                    RedisResult lastChildResult = childRedisArrayResult[childRedisArrayResult.Count() - 2];
                    System.Diagnostics.Debug.Assert(lastChildResult.Type == ResultType.MultiBulk);
                    RedisResult[] lastChildResultArray = (RedisResult[])lastChildResult;

                    RedisResult finalChildResult = lastChildResultArray[lastChildResultArray.Count() - 1];
                    System.Diagnostics.Debug.Assert(finalChildResult.Type == ResultType.BulkString);

                    if (finalChildResult.ToString().StartsWith("{\"type\":\"Polygon\""))
                    {
                        // _logger.LogInformation("Found GeoJSON!");
                        GeoJSON.Net.Feature.Feature feature = new GeoJSON.Net.Feature.Feature(Newtonsoft.Json.JsonConvert.DeserializeObject <GeoJSON.Net.Geometry.Polygon>(finalChildResult.ToString()));
                        feature.Properties["Name"] = childRedisArrayResult[0].ToString();

                        geofences.Features.Add(feature);
                    }
                }

                return(new JsonResult(geofences));
            } catch (StackExchange.Redis.RedisConnectionException ex) {
                string message = "Unable to connect to Tile38";
                _logger.LogError(0, ex, message);
                HttpContext.Response.StatusCode = 500;
                return(new JsonResult(new { message = message, exception = ex }));
            }
            catch (Exception ex) {
                string message = "Unable to retrieve GeoFences from Tile38";
                _logger.LogError(0, ex, message);
                HttpContext.Response.StatusCode = 500;
                return(new JsonResult(new { message = message, exception = ex }));
            }
        }
コード例 #8
0
        // public JsonResult WITHIN(string key, double xMin, double yMin, double xMax, double yMax){
        public JsonResult WITHIN(string key, string xMin, string yMin, string xMax, string yMax, string resubscribeToEvents)
        {
            try{
                if (redis == null)
                {
                    string tile38Connection = _configuration.GetConnectionString("Tile38Connection");
                    redis = ConnectionMultiplexer.Connect(tile38Connection);
                    _logger.LogInformation($"Connected to Tile38 {tile38Connection}");
                }

                db = redis.GetDatabase();

                _logger.LogInformation($"WITHIN {key} BOUNDS {xMin} {yMin} {xMax} {yMax}");

                var result = db.Execute("WITHIN", key, "BOUNDS", xMin, yMin, xMax, yMax);
                _logger.LogInformation(result.ToString());

                GeoJSON.Net.Feature.FeatureCollection featureCollection = new GeoJSON.Net.Feature.FeatureCollection();

                // Top level - collection of features
                System.Diagnostics.Debug.Assert(result.Type == ResultType.MultiBulk);
                RedisResult[] withinResult = ((RedisResult[])result);

                System.Diagnostics.Debug.Assert(withinResult[0].Type == ResultType.Integer);
                _logger.LogInformation($"WITHIN returned {withinResult[0]} features");

                System.Diagnostics.Debug.Assert(withinResult[1].Type == ResultType.MultiBulk);

                RedisResult[] withinFeatures = ((RedisResult[])withinResult[1]);

                foreach (RedisResult featureResult in withinFeatures)
                {
                    System.Diagnostics.Debug.Assert(featureResult.Type == ResultType.MultiBulk);
                    RedisResult[] featureDetails = ((RedisResult[])featureResult);
                    System.Diagnostics.Debug.Assert(featureDetails.Length == 2);
                    System.Diagnostics.Debug.Assert(featureDetails[0].Type == ResultType.BulkString); // ID
                    System.Diagnostics.Debug.Assert(featureDetails[1].Type == ResultType.BulkString); // GeoJSON

                    if (featureDetails[1].ToString().StartsWith("{\"type\":\"Feature\",\"geometry\":{\"type\":\"Point\""))
                    {
                        // _logger.LogInformation("Found GeoJSON!");
                        GeoJSON.Net.Feature.Feature feature = Newtonsoft.Json.JsonConvert.DeserializeObject <GeoJSON.Net.Feature.Feature>(featureDetails[1].ToString());

                        featureCollection.Features.Add(feature);
                        if (!feature.Properties.ContainsKey("id"))
                        {
                            feature.Properties["id"] = featureDetails[0].ToString();
                        }
                    }
                    else
                    {
                        if (featureDetails[1].ToString().StartsWith("{\"type\":\"Point\""))
                        {
                            // _logger.LogInformation("Found GeoJSON!");
                            GeoJSON.Net.Geometry.Point  point   = Newtonsoft.Json.JsonConvert.DeserializeObject <GeoJSON.Net.Geometry.Point>(featureDetails[1].ToString());
                            GeoJSON.Net.Feature.Feature feature = new GeoJSON.Net.Feature.Feature(point);

                            featureCollection.Features.Add(feature);
                            if (!feature.Properties.ContainsKey("id"))
                            {
                                feature.Properties["id"] = featureDetails[0].ToString();
                            }
                        }
                    }
                }

                return(new JsonResult(featureCollection));
            } catch (StackExchange.Redis.RedisConnectionException ex) {
                string message = "Unable to connect to Tile38";
                _logger.LogError(0, ex, message);
                HttpContext.Response.StatusCode = 500;
                return(new JsonResult(new { message = message, exception = ex }));
            }
            catch (Exception ex) {
                string message = "Unable to execute WITHIN against Tile38";
                _logger.LogError(0, ex, message);
                HttpContext.Response.StatusCode = 500;
                return(new JsonResult(new { message = message, exception = ex }));
            }
        }
コード例 #9
0
        /// <summary>
        /// Extracts a bbox of a GeoJSON feature collection object
        /// </summary>
        /// <param name="fc"></param>
        /// <returns></returns>
        protected static (double?minX, double?minY, double?maxX, double?maxY) ExtractGeoJsonBbox(FeatureCollection fc)
        {
            if (fc.BoundingBoxes != null)
            {
                return(fc.BoundingBoxes[0], fc.BoundingBoxes[1], fc.BoundingBoxes[2], fc.BoundingBoxes[3]);
            }

            double?minX = null;
            double?minY = null;
            double?maxX = null;
            double?maxY = null;

            fc.Features.ForEach(f =>
            {
                var bbox = f.BoundingBoxes;

                if (bbox == null)
                {
                    switch (f.Geometry.Type)
                    {
                    case GeoJSONObjectType.Point:
                        bbox = ((GeoJSON.Net.Geometry.Point)f.Geometry).BoundingBoxes;
                        break;

                    case GeoJSONObjectType.MultiPoint:
                        bbox = ((GeoJSON.Net.Geometry.MultiPoint)f.Geometry).BoundingBoxes;
                        break;

                    case GeoJSONObjectType.LineString:
                        bbox = ((GeoJSON.Net.Geometry.LineString)f.Geometry).BoundingBoxes;
                        break;

                    case GeoJSONObjectType.MultiLineString:
                        bbox = ((GeoJSON.Net.Geometry.MultiLineString)f.Geometry).BoundingBoxes;
                        break;

                    case GeoJSONObjectType.Polygon:
                        bbox = ((GeoJSON.Net.Geometry.Polygon)f.Geometry).BoundingBoxes;
                        break;

                    case GeoJSONObjectType.MultiPolygon:
                        bbox = ((GeoJSON.Net.Geometry.MultiPolygon)f.Geometry).BoundingBoxes;
                        break;
                    }
                }

                if (bbox == null)
                {
                    return;
                }

                minX = minX.HasValue ? Math.Min(minX.Value, bbox[0]) : bbox[0];
                minY = minY.HasValue ? Math.Min(minY.Value, bbox[1]) : bbox[1];
                maxX = maxX.HasValue ? Math.Max(maxX.Value, bbox[2]) : bbox[2];
                maxY = maxY.HasValue ? Math.Max(maxY.Value, bbox[3]) : bbox[3];
            });

            return(minX, minY, maxX, maxY);
        }
コード例 #10
0
 public static string ToGeoJsonString(this GeoJSON.Net.Feature.FeatureCollection featureCollection)
 {
     return(JsonTools.SerializeObject(featureCollection));
 }
コード例 #11
0
ファイル: GeoSource.cs プロジェクト: Levrum/Levrum
        public static bool GetProjectionFromGeoJson(string fileName, out string projectionName, out string projection)
        {
            string       geoJson = File.ReadAllText(fileName);
            ProtoGeoJSON obj     = JsonConvert.DeserializeObject <ProtoGeoJSON>(geoJson);

            projectionName = string.Empty;
            projection     = string.Empty;
            ICRSObject crs = null;

            switch (obj.Type)
            {
            case "FeatureCollection":
                GFeatureCollection features = JsonConvert.DeserializeObject <GFeatureCollection>(geoJson);
                crs = features.CRS;
                break;

            case "Feature":
                GFeature feature = JsonConvert.DeserializeObject <GFeature>(geoJson);
                crs = feature.CRS;
                break;

            case "MultiPolygon":
                GMultiPolygon multiPoly = JsonConvert.DeserializeObject <GMultiPolygon>(geoJson);
                crs = multiPoly.CRS;
                break;

            case "Polygon":
                GPolygon poly = JsonConvert.DeserializeObject <GPolygon>(geoJson);
                crs = poly.CRS;
                break;

            default:
                break;
            }

            if (crs is NamedCRS)
            {
                NamedCRS nCrs = crs as NamedCRS;
                object   value;
                if (nCrs.Properties.TryGetValue("name", out value))
                {
                    string name = value as string;
                    if (name != null)
                    {
                        name = name.ToLower();
                        if (name.EndsWith("3857"))
                        {
                            projectionName = "EPSG:3857 Pseudo-Mercator";
                            projection     = CoordinateConverter.WebMercator.WKT;
                            return(true);
                        }
                        else
                        {
                            int    index      = name.IndexOf("epsg::");
                            string epsgStr    = name.Substring(index);
                            string authNumber = epsgStr.Substring(epsgStr.IndexOf("::") + 2);
                            projection = AutoProjection.GetProjection(authNumber);
                            if (projection != string.Empty)
                            {
                                projectionName = string.Format("EPSG:{0}", authNumber);
                                return(true);
                            }
                        }
                    }
                }
            }

            projectionName = "EPSG:4326 Lat-Long";
            projection     = CoordinateConverter.WGS84.WKT;
            return(true);
        }