使用NetTopologySuite删除GeoJSON的bbox字段。

问题描述 投票:0回答:1

在一个.NET Core 3 WebAPI项目中,我使用NetTopologySuite创建了一个FeatureCollection。

然后我将其序列化为一个GeoJSON响应。完整的代码如下。

using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using NetTopologySuite.Features;
using NetTopologySuite.Geometries;

namespace ProjectX.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class Xyz: ControllerBase
    {

        [HttpGet]
        [Route("markets")]
        [Produces("application/geo+json")]
        [ProducesResponseType(typeof(FeatureCollection), 200)]
        public ActionResult GetMarkets(int adm0code)
        {
            using (var db = new Models.Entities.Reporting_devContext())
            {
                var markets = (from m in db.Markets
                             where m.Adm0Code==adm0code 
                             && m.MarketDeleteDate==null 
                             && m.MarketLatitude.HasValue 
                             && m.MarketLongitude.HasValue
                             select new
                             {
                                 m.MarketId,
                                 m.Adm1Code,
                                 m.Adm2Code,
                                 m.MarketName,
                                 m.MarketLatitude,
                                 m.MarketLongitude
                             }).ToList();

                FeatureCollection fc = new FeatureCollection();
                foreach(var m in markets)
                {
                    AttributesTable attribs = new AttributesTable();
                    attribs.Add("id", m.MarketId);
                    attribs.Add("name", m.MarketName);
                    Point p = new Point(m.MarketLongitude.Value, m.MarketLatitude.Value);
                    IFeature feature = new Feature(p, attribs);
                    fc.Add(feature);
                }

                return Ok(fc);
            }
        }
    }
}

问题是它还添加了字段框,这对于一个点的集合来说是完全没用的。

{
    "type": "FeatureCollection",
    "features": [{
                "type": "Feature",
                "id": 266,
                "bbox": [
                    70.580022,
                    37.116638,
                    70.580022,
                    37.116638
                ],
                "geometry": {
                    "type": "Point",
                    "coordinates": [
                        70.580022,
                        37.116638
                    ]
                },
                "properties": {
                    "name": "Fayzabad"
                }
            },
        ...
    }]
}

我如何告诉NetTopologySuite不要添加bbox字段?

c# asp.net-core asp.net-web-api geojson nettopologysuite
1个回答
0
投票

从NetTopologySuite.IO.GeoJSON的代码来看https:/github.comNetTopologySuiteNetTopologySuite.IO.GeoJSONblobdevelopsrcNetTopologySuite.IO.GeoJSONConvertersFeatureConverter.cs。

我发现这段代码是管理bbox属性的。

    // bbox (optional)
    if (serializer.NullValueHandling == NullValueHandling.Include || !(feature.BoundingBox is null))
    {
        var bbox = feature.BoundingBox ?? feature.Geometry?.EnvelopeInternal;

        writer.WritePropertyName("bbox");
        serializer.Serialize(writer, bbox, typeof(Envelope));
    }

它说bbox是可选的,所以有办法避免写它,而且代码还检查了它是否有忽略空值... ...

在我的代码中,我没有给bbox设置任何值,顺便把序列器设置为忽略空值。options.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore。,解决这个问题。

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllers(options => {
        options.ModelMetadataDetailsProviders.Add(new SuppressChildValidationMetadataProvider(typeof(Point)));
        options.ModelMetadataDetailsProviders.Add(new SuppressChildValidationMetadataProvider(typeof(Coordinate)));
        options.ModelMetadataDetailsProviders.Add(new SuppressChildValidationMetadataProvider(typeof(LineString)));
        options.ModelMetadataDetailsProviders.Add(new SuppressChildValidationMetadataProvider(typeof(MultiLineString)));
    }).AddNewtonsoftJson(options => {
        foreach (var converter in NetTopologySuite.IO.GeoJsonSerializer.Create(new GeometryFactory(new PrecisionModel(), 4326)).Converters)
        {
            options.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
            options.SerializerSettings.Converters.Add(converter);
        }
    }).SetCompatibilityVersion(CompatibilityVersion.Version_3_0);

}
© www.soinside.com 2019 - 2024. All rights reserved.