首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用GeoJSON删除NetTopologySuite字段

使用GeoJSON删除NetTopologySuite字段
EN

Stack Overflow用户
提问于 2020-05-13 09:15:01
回答 1查看 432关注 0票数 1

在一个.NET核心3 WebAPI项目中,我正在使用NetTopologySuite创建一个FeatureCollection。

然后,我序列化为一个GeoJSON响应。完整代码如下:

代码语言:javascript
复制
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);
            }
        }
    }
}

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

代码语言:javascript
复制
{
    "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字段?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-05-13 09:46:58

查看NetTopologySuite.IO.GeoJSON https://github.com/NetTopologySuite/NetTopologySuite.IO.GeoJSON/blob/develop/src/NetTopologySuite.IO.GeoJSON/Converters/FeatureConverter.cs的代码

我找到了管理bbox属性的代码:

代码语言:javascript
复制
    // 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,解决了这个问题:

代码语言:javascript
复制
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);

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61770682

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档