Select to view content in your preferred language

Create polygon without sketching

1084
2
Jump to solution
11-14-2022 03:45 PM
AndrewAlexander1
Emerging Contributor

Hello,

Another simple question, I'm sure.  I'm wanting to draw a polygon on the map programmatically.  I don't want to sketch with the mouse.

I see the code for making the polygon, but all the examples I've found have the geometry input from sketching.  But I don't have that geometry.  What does the geometry look like that I can send to the polygon?  I've tried all kinds of ways but so far haven't gotten it.

Ultimately, I want to have text and other objects inside the polygon.

Thanks
Andy

 

0 Kudos
2 Solutions

Accepted Solutions
FridjofSchmidt
Frequent Contributor

You can use PolygonBuilderEx.CreatePolygon() to create polygons from 2D or 3D coordinates, MapPoints, Segments, Multipoints, Envelopes or other Polygons, with or without Spatial Reference.

View solution in original post

0 Kudos
NarelleChedzey
Esri Contributor

Hi Andy, 

Here's a short example of how to create a polygon.  

// current exent of mapView
var extent = MapView.Active.Extent;

var height = extent.Height;
var width = extent.Width;
var centerPt = extent.Center;

// new xmin, xmax, ymin, ymax values - a subset of the extent
var xmin = centerPt.X - width / 4;
var xmax = centerPt.X + width / 4;
var ymin = centerPt.Y - height / 4;
var ymax = centerPt.Y + height / 4;

// create 4 corners of the new polgyon
var pt1 = MapPointBuilderEx.CreateMapPoint(xmin, ymin, MapView.Active.Map.SpatialReference);
var pt2 = MapPointBuilderEx.CreateMapPoint(xmin, ymax, MapView.Active.Map.SpatialReference);
var pt3 = MapPointBuilderEx.CreateMapPoint(xmax, ymax, MapView.Active.Map.SpatialReference);
var pt4 = MapPointBuilderEx.CreateMapPoint(xmax, ymin, MapView.Active.Map.SpatialReference);

// then create a polygon from the set of points
var listPoints = new List<MapPoint>() { pt1, pt2, pt3, pt4 }; 
var polygon2 = PolygonBuilderEx.CreatePolygon(listPoints, MapView.Active.Map.SpatialReference);

 

There's also the following documentation that can help you discover more about building geometries.   - https://github.com/esri/arcgis-pro-sdk/wiki/ProConcepts-Geometry

There are a lot of different code snippets here too. - https://github.com/esri/arcgis-pro-sdk/wiki/ProSnippets-Geometry

 

Thanks

Narelle

 

View solution in original post

2 Replies
FridjofSchmidt
Frequent Contributor

You can use PolygonBuilderEx.CreatePolygon() to create polygons from 2D or 3D coordinates, MapPoints, Segments, Multipoints, Envelopes or other Polygons, with or without Spatial Reference.

0 Kudos
NarelleChedzey
Esri Contributor

Hi Andy, 

Here's a short example of how to create a polygon.  

// current exent of mapView
var extent = MapView.Active.Extent;

var height = extent.Height;
var width = extent.Width;
var centerPt = extent.Center;

// new xmin, xmax, ymin, ymax values - a subset of the extent
var xmin = centerPt.X - width / 4;
var xmax = centerPt.X + width / 4;
var ymin = centerPt.Y - height / 4;
var ymax = centerPt.Y + height / 4;

// create 4 corners of the new polgyon
var pt1 = MapPointBuilderEx.CreateMapPoint(xmin, ymin, MapView.Active.Map.SpatialReference);
var pt2 = MapPointBuilderEx.CreateMapPoint(xmin, ymax, MapView.Active.Map.SpatialReference);
var pt3 = MapPointBuilderEx.CreateMapPoint(xmax, ymax, MapView.Active.Map.SpatialReference);
var pt4 = MapPointBuilderEx.CreateMapPoint(xmax, ymin, MapView.Active.Map.SpatialReference);

// then create a polygon from the set of points
var listPoints = new List<MapPoint>() { pt1, pt2, pt3, pt4 }; 
var polygon2 = PolygonBuilderEx.CreatePolygon(listPoints, MapView.Active.Map.SpatialReference);

 

There's also the following documentation that can help you discover more about building geometries.   - https://github.com/esri/arcgis-pro-sdk/wiki/ProConcepts-Geometry

There are a lot of different code snippets here too. - https://github.com/esri/arcgis-pro-sdk/wiki/ProSnippets-Geometry

 

Thanks

Narelle