将数据从PostGIS序列化为JSON

时间:2018-10-05 18:54:30

标签: serialization json.net leaflet postgis geojson

我正在使用.NET Core REST服务,该服务将具有一个控制器,该控制器会将几何数据发送回GeoJson,以便可以在Leaflet地图上显示它。

我无法将类转换为JSON。

我已经尝试过:

StringBuilder sb = new StringBuilder();
JsonTextWriter writer = new JsonTextWriter(new StringWriter(sb));
var jsonSerializer = GeoJsonSerializer.Create(new JsonSerializerSettings()
    {NullValueHandling = NullValueHandling.Ignore});
jsonSerializer.Serialize(writer, _context.Locations);
writer.Flush();
return Ok(sb.ToString());

但是然后我得到这个错误

Self referencing loop detected for property 'CoordinateValue' with type 'GeoAPI.Geometries.Coordinate'

我整天都在寻找示例和阅读文档,但无法正常工作。

我需要序列化的类:

public class SobekLocation
{
    public int Id { get; set; }
    public string LocationId { get; set; }
    public bool Use { get; set; }
    public Point Location { get; set; }
}

任何建议都值得赞赏。

修改

我正在制作自己的功能集,并尝试对其进行序列化。它正在做某事,但是它不是有效的GeoJSON。

    public FeatureCollection GetLocationsAsFeatureCollection()
    {
        var featureCollection = new FeatureCollection();
        foreach (var location in GetLocations().Take(5))
        {
            var attr = new AttributesTable
            {
                {"Id", location.Id},
                {"Use", location.Use},
                {"LocationId", location.LocationId}
            };

            var feature = new Feature(location.Location, attr);
            featureCollection.Add(feature);
        }

        return featureCollection;
    }

和反序列化部分:

var fc = _service.GetLocationsAsFeatureCollection();
var sb = new StringBuilder();
var jsonSerializer = JsonSerializer.Create(new JsonSerializerSettings
{
    // To prevent the Self referencing loop error:
    ReferenceLoopHandling = ReferenceLoopHandling.Ignore 
});

using (var sw = new StringWriter(sb))
{
    jsonSerializer.Serialize(sw, fc);
}

return Ok(sb.ToString());

序列化数据:

{  
   "Features":[  
      {  
         "Geometry":{  
            "CoordinateSequence":{  
               "Dimension":3,
               "Ordinates":7,
               "Count":1
            },
            "Coordinates":[  
               {  
                  "X":191390.015,
                  "Y":523064.716597462,
                  "Z":"NaN"
               }
            ],
            "NumPoints":1,
            "IsEmpty":false,
            "Dimension":0,
            "BoundaryDimension":-1,
            "X":191390.015,
            "Y":523064.716597462,
            "Coordinate":{  
               "X":191390.015,
               "Y":523064.716597462,
               "Z":"NaN"
            },
            "GeometryType":"Point",
            "OgcGeometryType":1,
            "Boundary":[  

            ],
            "Z":"NaN",
            "M":"NaN",
            "Factory":{  
               "PrecisionModel":{  
                  "IsFloating":true,
                  "MaximumSignificantDigits":16,
                  "Scale":0,
                  "PrecisionModelType":0,
                  "OffsetX":0,
                  "OffsetY":0
               },
               "CoordinateSequenceFactory":{  
                  "Ordinates":7
               },
               "SRID":28992
            },
            "UserData":null,
            "SRID":28992,
            "PrecisionModel":{  
               "IsFloating":true,
               "MaximumSignificantDigits":16,
               "Scale":0,
               "PrecisionModelType":0,
               "OffsetX":0,
               "OffsetY":0
            },
            "NumGeometries":1,
            "IsSimple":true,
            "IsValid":true,
            "Area":0,
            "Length":0,
            "EnvelopeInternal":{  
               "IsNull":false,
               "Width":0,
               "Height":0,
               "MinX":191390.015,
               "MaxX":191390.015,
               "MinY":523064.716597462,
               "MaxY":523064.716597462,
               "Area":0,
               "MinExtent":0,
               "MaxExtent":0,
               "Centre":{  
                  "X":191390.015,
                  "Y":523064.716597462,
                  "Z":"NaN"
               }
            },
            "IsRectangle":false
         },
         "Attributes":[  
            {  
               "Key":"Id",
               "Value":1
            },
            {  
               "Key":"Use",
               "Value":false
            },
            {  
               "Key":"LocationId",
               "Value":"KNP_1"
            }
         ],
         "BoundingBox":null
      }
   ],
   "Type":"FeatureCollection",
   "CRS":null,
   "Count":5,
   "BoundingBox":null
}

如您所见,所有属性均以大写字母开头,但它们应为小写字母。在手动更改属性后,geojsonlint.com报告:GeoJSON features must have a type=feature member

Edit2

我已经取得了一些进展。我现在正在使用GeoJSON.Net程序包,现在可以为http://geojsonlint.com/创建有效的GeoJSON标准。但是我的Leaflet地图仍在抱怨无效的GeoJSON,因此地图仍未显示任何内容;)

这是我的新转换器:

    public GeoJSON.Net.Feature.FeatureCollection GetLocationsAsGeoJsonFeatureCollection()
    {
        var featureCollection = new GeoJSON.Net.Feature.FeatureCollection();
        foreach (var location in GetLocations())
        {
            var attr = new Dictionary<string, object>()
            {
                {"Id", location.Id},
                {"Use", location.Use},
                {"LocationId", location.LocationId}
            };

            var position = new Position(location.Location.Coordinate.X, location.Location.Coordinate.Y);
            var geom = new GeoJSON.Net.Geometry.Point(position);
            var feature = new GeoJSON.Net.Feature.Feature(geom, attr, location.Id.ToString());
            featureCollection.Features.Add(feature);
        }

        return featureCollection;
    }

它产生的GeoJSON:

{  
   "type":"FeatureCollection",
   "features":[  
      {  
         "type":"Feature",
         "id":"1",
         "geometry":{  
            "type":"Point",
            "coordinates":[  
               523064.716597462,
               191390.015
            ]
         },
         "properties":{  
            "id":1,
            "use":false,
            "locationId":"KNP_1"
         }
      },
      {  
         "type":"Feature",
         "id":"2",
         "geometry":{  
            "type":"Point",
            "coordinates":[  
               519349.860113963,
               170162.249352578
            ]
         },
         "properties":{  
            "id":2,
            "use":false,
            "locationId":"CP_6"
         }
      },
      {  
         "type":"Feature",
         "id":"3",
         "geometry":{  
            "type":"Point",
            "coordinates":[  
               519952.507022603,
               170402.383673312
            ]
         },
         "properties":{  
            "id":3,
            "use":false,
            "locationId":"CP_UR_5_1"
         }
      },
      {  
         "type":"Feature",
         "id":"4",
         "geometry":{  
            "type":"Point",
            "coordinates":[  
               519948.062527202,
               170582.612655391
            ]
         },
         "properties":{  
            "id":4,
            "use":false,
            "locationId":"CP_UR_6_1"
         }
      },
      {  
         "type":"Feature",
         "id":"5",
         "geometry":{  
            "type":"Point",
            "coordinates":[  
               519902.432252114,
               170602.894875503
            ]
         },
         "properties":{  
            "id":5,
            "use":false,
            "locationId":"CP_UR_6_2"
         }
      }
   ]
}

这是我的Leaflet代码:

L.geoJson('http://localhost:5000/api/data/',
  {
    attribution: 'Mine'
  }
).addTo(map);

我仍在寻找有关如何在传单地图上轻松绘制我的PostGIS数据的建议。

0 个答案:

没有答案