# generate polygon from envelop object (XY min/max coordinates) or extent

632
3
Jump to solution
05-03-2021 07:50 AM
New Contributor III

Hello,

I'm trying to generate an Envelope object, passing it x and y coordinates min and max.
Using this object I am trying to generate a polygon but I can't get it.

Envelope extent = new Envelope(enteredPointMin.X, enteredPointMin.Y, enteredPointMax.X, enteredPointMax.Y);

Is it possible to generate a polygon object from an envelope element or extent? or from XY Min/Max coordinates??

In the JS API it is allowed to generate a polygon form Extent, but in .NET SDK maybe not.

Thanks !

1 Solution

Accepted Solutions
Esri Contributor

In fact, there seams not to be a method to directly create a polygon from an Envelope.

But this is a way you can do it:

``````        private Geometry CreateFromEnvelope(Envelope envelope)
{
var builder = new PolygonBuilder(envelope.SpatialReference);
builder.AddPoint(new MapPoint(envelope.XMin, envelope.YMin));
builder.AddPoint(new MapPoint(envelope.XMin, envelope.YMax));
builder.AddPoint(new MapPoint(envelope.XMax, envelope.YMax));
builder.AddPoint(new MapPoint(envelope.XMax, envelope.YMin));
return builder.ToGeometry();
}``````

Cheers

3 Replies
Esri Contributor

In fact, there seams not to be a method to directly create a polygon from an Envelope.

But this is a way you can do it:

``````        private Geometry CreateFromEnvelope(Envelope envelope)
{
var builder = new PolygonBuilder(envelope.SpatialReference);
builder.AddPoint(new MapPoint(envelope.XMin, envelope.YMin));
builder.AddPoint(new MapPoint(envelope.XMin, envelope.YMax));
builder.AddPoint(new MapPoint(envelope.XMax, envelope.YMax));
builder.AddPoint(new MapPoint(envelope.XMax, envelope.YMin));
return builder.ToGeometry();
}``````

Cheers

Esri Regular Contributor

@MaximilianGlas's answer is good, but a couple of other thoughts…

Since you already have the coordinates, can you just construct a Polygon instead of an Envelope in the first place? Obviously that depends on your use case.

Polygon has constructors that take an IEnumerable of MapPoints. You could use that with the envelope coordinates instead of the PolygonBuilder. I wouldn't say one is preferred over another from a performance perspective (unless you're doing this in a tight loop), but it might be simpler code.

Another way to get a polygon from an Envelope is to use GeometryEngine.ConvexHull().

New Contributor III

Thanks !