コード例 #1
0
        private async Task GetPolygonData()
        {
            var country = await _dbContext.Countries.FirstOrDefaultAsync();

            //Geometry to geoJSON
            var lines               = new List <GeoJSON.Net.Geometry.LineString>();
            var polygon             = country.Border as NetTopologySuite.Geometries.Polygon;
            List <Coordinate[]> res = new List <Coordinate[]>();

            res.Add(polygon.Shell.Coordinates);
            foreach (ILineString interiorRing in polygon.InteriorRings)
            {
                res.Add(interiorRing.Coordinates);
            }
            foreach (var line in res)
            {
                var coordinates = new List <IPosition>();
                foreach (var item in line)
                {
                    coordinates.Add(new Position(item.X, item.Y, item.Z));
                }
                lines.Add(new GeoJSON.Net.Geometry.LineString(coordinates));
            }
            GeoJSON.Net.Geometry.Polygon jsonPolygon = new GeoJSON.Net.Geometry.Polygon(lines);
            var s = JsonConvert.SerializeObject(jsonPolygon);
            //Geometry to wkt
            var writer = new NetTopologySuite.IO.WKTWriter();
            var s2     = writer.Write(country.Border);

            Console.WriteLine($"GeoJSON结果:{s},WKT结果:{s2}");
        }
コード例 #2
0
        public IActionResult GetPolygon(int countryId)
        {
            var country = _dbContext.Countries.FirstOrDefault(x => x.CountryID == countryId);

            if (country == null)
            {
                return(Json($"查询不到id为{countryId}的国家"));
            }

            //Geometry to GeoJSON
            //https://nettopologysuite.github.io/html/class_net_topology_suite_1_1_geometries_1_1_polygon.html
            //https://github.com/synhershko/nettopologysuite/blob/master/NetTopologySuite.IO/NetTopologySuite.IO.GeoJSON/Converters/GeometryConverter.cs
            //->已copy到GeometryConverter和GeometryArrayConverter
            var lines               = new List <GeoJSON.Net.Geometry.LineString>();
            var polygon             = country.Border as NetTopologySuite.Geometries.Polygon;
            List <Coordinate[]> res = new List <Coordinate[]>();

            res.Add(polygon.Shell.Coordinates);
            foreach (ILineString interiorRing in polygon.InteriorRings)
            {
                res.Add(interiorRing.Coordinates);
            }
            foreach (var line in res)
            {
                var coordinates = new List <IPosition>();
                foreach (var item in line)
                {
                    coordinates.Add(new Position(item.X, item.Y, item.Z));
                }
                lines.Add(new GeoJSON.Net.Geometry.LineString(coordinates));
            }
            GeoJSON.Net.Geometry.Polygon jsonPolygon = new GeoJSON.Net.Geometry.Polygon(lines);
            var s = JsonConvert.SerializeObject(jsonPolygon);
            //Geometry to wkt
            //点和线的是静态方法,面的是方法_(:з」∠)_
            var writer = new NetTopologySuite.IO.WKTWriter();
            var s2     = writer.Write(country.Border);

            return(Json($"GeoJSON结果:{s},WKT结果:{s2}"));
        }