2

I've got a list of polygons in wgs84 WKB, I read them into NetTopologySuite Igeometries put them into an STRtree and then use the NearestNeighbour function to get the closest polygon to a point (also wgs84).

This doesn't work properly as long lat isn't flat. I know I need to re-project. But should I re-project my polygons when reading them from the WKB (and how do I do that?) or should I reproject every point in the IItemDistance?

A somewhat simplifed version of my code.

private STRtree<IGeometry> LoadSTRtree(List<byte[]> wkbs)
{
    var sourceLayer = new STRtree<ShapeDetail>();

    foreach (var wkb in wkbs)
    {
        var geometry = WKBReader(NtsGeometryServices.Instance).Read(wkb);
        sourceLayer.Insert(geometry.EnvelopeInternal, geometry);
    }
    sourceLayer.Build();
    return sourceLayer;
}

private IGeometry GetClosest(IGeometry location, STRtree<IGeometry> sources)
{
    return source.NearestNeighbour(location.EnvelopeInternal, location, new MyItemDistance());
}

private class ShapeDetailItemDistance : IItemDistance<Envelope, IGeometry>
{ 
    public double Distance(IBoundable<Envelope, IGeometry> item1, IBoundable<Envelope, IGeometry> item2)
    {
        return item1.Item.Distance(item2.Item);
    }
}

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.