TransWikia.com

Get Geometry object from Envelope in NetTopologySuite

Geographic Information Systems Asked on June 17, 2021

I’ve built some code to take multiple geometries and merge them into one envelope, but now I need to convert that envelope into a polygon/IGeometry shape that represents that area. Is there a way to do this?

2 Answers

For others, I ended up just doing the following and it seems to work well. Feel free to comment if anyone has any opinions:

public static IGeometry ToGeometry(this Envelope env)
        {
            if (env != null && env.MinX <= env.MaxX && env.MinY <= env.MaxY)
            {
                List<Coordinate> bounds = new List<Coordinate>()
                {
                    new Coordinate(env.MinX,env.MinY),
                new Coordinate(env.MaxX, env.MinY),
                new Coordinate(env.MaxX, env.MaxY),
                new Coordinate(env.MinX, env.MaxY),
                new Coordinate(env.MinX,env.MinY),
            };
                NetTopologySuite.Geometries.LinearRing ring= new NetTopologySuite.Geometries.LinearRing(bounds.ToArray());
                if (ring != null)
                {
                    NetTopologySuite.Geometries.Polygon poly = new NetTopologySuite.Geometries.Polygon(ring);
                    return poly;
                }
            }
            return null;
        }

Answered by Phil on June 17, 2021

There is a simpler option:

        Envelope envelope = new(95473, 101283, 435552, 445442);
        GeometryFactory factory = new();
        Geometry geometry = factory.ToGeometry(envelope);

Docs

Answered by Lev on June 17, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP