在 .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 字段?