private static JObject GetGeoJSONMultiPolygonFromDbGeography(DbGeography geography)
        {
            JArray coordinates = new JArray();
            for (int i = 1; i <= geography.ElementCount; i++)
            {
                JArray polygonJs = new JArray();
                var polygon = geography.ElementAt(i);
                for (int j = 1; j <= polygon.ElementCount; j++)
                {
                    var ringJs = new JArray();
                    var ring = polygon.ElementAt(j);
                    for (int k = 1; k <= ring.PointCount; k++)
                    {
                        var point = ring.PointAt(k);
                        ringJs.Add(new JArray(point.Longitude, point.Latitude));
                    }

                    polygonJs.Add(ringJs);
                }

                coordinates.Add(polygonJs);
            }

            JObject result = new JObject();
            result.Add("type", geography.SpatialTypeName);
            result.Add("coordinates", coordinates);
            return result;
        }
        private static JObject GetGeoJSONGeometryCollectionFromDbGeography(DbGeography geography)
        {
            JArray geometries = new JArray();
            for (int i = 1; i <= geography.ElementCount; i++)
            {
                var geometry = geography.ElementAt(i);
                geometries.Add(geography.ToGeoJSON());
            }

            JObject result = new JObject();
            result.Add("type", geography.SpatialTypeName);
            result.Add("geometries", geometries);
            return result;
        }
        private static JObject GetGeoJSONMultiLineStringFromDbGeography(DbGeography geography)
        {
            JArray coordinates = new JArray();
            for (int i = 1; i <= geography.ElementCount; i++)
            {
                JArray jsonLine = new JArray();
                var line = geography.ElementAt(i);
                for (int j = 1; j <= line.PointCount; j++)
                {
                    var point = line.PointAt(j);
                    jsonLine.Add(new JArray(point.Longitude, point.Latitude));
                }

                coordinates.Add(jsonLine);
            }

            JObject result = new JObject();
            result.Add("type", geography.SpatialTypeName);
            result.Add("coordinates", coordinates);
            return result;
        }
Example #4
0
        public static ICollection <Position> ToPositions(this DbGeography dbGeography)
        {
            IList <Position> positions = new List <Position>();

            var data = dbGeography.ProviderValue.ToString();

            if (data.Contains("POLYGON"))
            {
                data = data.Substring(10);
                data = data.Replace("))", "");//.Replace(" ","");
                var resultPoints = data.Split(new[] { ", " }, StringSplitOptions.None);


                foreach (var resultPoint in resultPoints)
                {
                    var da        = resultPoint.Split(' ');
                    var latitude  = double.Parse(da[1]);
                    var longitude = double.Parse(da[0]);
                    positions.Add(new Position(latitude, longitude));
                }

                return(positions);
            }



            if (dbGeography.PointCount > 0)
            {
                for (int i = 1; i <= dbGeography.PointCount; i++)
                {
                    var point = dbGeography.ElementAt(i);
                    positions.Add(new Position(point.Latitude.Value, point.Longitude.Value));
                }
            }
            return(positions);
        }