-
Notifications
You must be signed in to change notification settings - Fork 121
ProSnippets Geometry
Language: C#
Subject: Geometry
Contributor: ArcGIS Pro SDK Team <[email protected]>
Organization: Esri, http://www.esri.com
Date: 6/24/2016
ArcGIS Pro: 1.3
Visual Studio: 2013, 2015
.NET Target Framework: 4.6.1
##Construct a SpatialReference - from a Well Known ID
// methods need to run on the MCT
ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
// use the builder constructor
SpatialReferenceBuilder srBuilder = new SpatialReferenceBuilder(3857);
SpatialReference sr3857 = srBuilder.ToSpatialReference();
// or use the convenience method
SpatialReference sr_3857 = SpatialReferenceBuilder.CreateSpatialReference(3857);
});
##Construct a SpatialReference - from a string
// methods need to run on the MCT
ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
string wkt = "GEOGCS[\"MyGCS84\",DATUM[\"D_WGS_1984\",SPHEROID[\"WGS_1984\",6378137.0,298.257223563]],PRIMEM[\"Greenwich\",0.0],UNIT[\"Radian\",1.0]]";
// use the builder constructor
SpatialReferenceBuilder builder = new SpatialReferenceBuilder(wkt);
SpatialReference sr = builder.ToSpatialReference();
// or use the convenience method
SpatialReference anotherSR = SpatialReferenceBuilder.CreateSpatialReference(wkt);
});
##Use WGS84 SpatialReference
SpatialReference wgs84 = SpatialReferences.WGS84;
bool isProjected = wgs84.IsProjected; // false
bool isGeographic = wgs84.IsGeographic; // true
##Construct a SpatialReference with a vertical coordinate system - from Well Known IDs
// see a list of vertical coordinate systems at http://resources.arcgis.com/en/help/arcgis-rest-api/index.html#/Vertical_coordinate_systems/02r3000000rn000000/
// methods need to run on the MCT
ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
// use the builder constructor
// 4326 = GCS_WGS_1984
// 115700 = vertical WGS_1984
SpatialReferenceBuilder sb = new SpatialReferenceBuilder(4326, 115700);
SpatialReference sr = sb.ToSpatialReference();
// or use the convenience method
SpatialReference sr_4326 = SpatialReferenceBuilder.CreateSpatialReference(4326, 115700);
});
// test creation with wkid, vwkid
##Construct a SpatialReference with a verical coordinate system - from a string
// methods need to run on the MCT
ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
// custom VCS - use vertical shift of -1.23 instead of 0
string custom_vWkt = @"VERTCS[""SHD_height"",VDATUM[""Singapore_Height_Datum""],PARAMETER[""Vertical_Shift"",-1.23],PARAMETER[""Direction"",-1.0],UNIT[""Meter"",1.0]]";
SpatialReferenceBuilder sb = new SpatialReferenceBuilder(4326, custom_vWkt);
SpatialReference sr = sb.ToSpatialReference();
int wkid = sr.Wkid; // wkid = 4326
int vert_wkid = sr.VcsWkid; // vert_wkid = 0
string vert_wkt = sr.VcsWkt; // vert_wkt = custom_vWkt
// or use the convenience method
SpatialReference sr_4326 = SpatialReferenceBuilder.CreateSpatialReference(4326, custom_vWkt);
});
##SpatialReference Properties
// methods need to run on the MCT
ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
// use the builder constructor
SpatialReferenceBuilder srBuilder = new SpatialReferenceBuilder(3857);
// spatialReferenceBuilder properties
int wkid = srBuilder.Wkid;
string wkt = srBuilder.Wkt;
string name = srBuilder.Name;
double xyScale = srBuilder.XYScale;
double xyTolerance = srBuilder.XYTolerance;
double xyResolution = srBuilder.XYResolution;
Unit unit = srBuilder.Unit;
double zScale = srBuilder.ZScale;
double zTolerance = srBuilder.ZTolerance;
Unit zUnit = srBuilder.ZUnit;
double mScale = srBuilder.MScale;
double mTolerance = srBuilder.MTolerance;
double falseX = srBuilder.FalseX;
double falseY = srBuilder.FalseY;
double falseZ = srBuilder.FalseZ;
double falseM = srBuilder.FalseM;
// get the spatial reference
SpatialReference sr3857 = srBuilder.ToSpatialReference();
// spatial reference properties
wkid = sr3857.Wkid;
wkt = sr3857.Wkt;
name = sr3857.Name;
xyScale = sr3857.XYScale;
xyTolerance = sr3857.XYTolerance;
xyResolution = sr3857.XYResolution;
unit = sr3857.Unit;
zScale = sr3857.ZScale;
zTolerance = sr3857.ZTolerance;
zUnit = sr3857.ZUnit;
mScale = sr3857.MScale;
mTolerance = sr3857.MTolerance;
falseX = sr3857.FalseX;
falseY = sr3857.FalseY;
falseZ = sr3857.FalseZ;
falseM = sr3857.FalseM;
});
##Builder Properties
// methods need to run on the MCT
ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
// list of points
List<MapPoint> points = new List<MapPoint>
{
MapPointBuilder.CreateMapPoint(0, 0, 2, 3, 1),
MapPointBuilder.CreateMapPoint(1, 1, 5, 6),
MapPointBuilder.CreateMapPoint(2, 1, 6),
MapPointBuilder.CreateMapPoint(0, 0)
};
// will have attributes because it is created with convenience method
Polyline polylineWithAttrs = PolylineBuilder.CreatePolyline(points);
bool hasZ = polylineWithAttrs.HasZ; // hasZ = true
bool hasM = polylineWithAttrs.HasM; // hasM = true
bool hasID = polylineWithAttrs.HasID; // hasID = true
// will not have attributes because it is passed something other than an attributed polyline
PolylineBuilder polylineB = new PolylineBuilder(points);
hasZ = polylineB.HasZ; // hasZ = false
hasM = polylineB.HasM; // hasM = false
hasID = polylineB.HasID; // hasID = false
// will have attributes because it is passed an attributed polyline
polylineB = new PolylineBuilder(polylineWithAttrs);
hasZ = polylineB.HasZ; // hasZ = true
hasM = polylineB.HasM; // hasM = true
hasID = polylineB.HasID; // hasID = true
// will have attributes because it is created with convenience method
Polygon polygonWithAttrs = PolygonBuilder.CreatePolygon(points);
hasZ = polygonWithAttrs.HasZ; // hasZ = true
hasM = polygonWithAttrs.HasM; // hasM = true
hasID = polygonWithAttrs.HasID; // hasID = true
// will not have attributes because it is passed something other than an attributed polygon
PolygonBuilder polygonB = new PolygonBuilder(points);
hasZ = polygonB.HasZ; // hasZ = false
hasM = polygonB.HasM; // hasM = false
hasID = polygonB.HasID; // hasID = false
// will have attributes because it is passed an attributed polygon
polygonB = new PolygonBuilder(polygonWithAttrs);
hasZ = polygonB.HasZ; // hasZ = true
hasM = polygonB.HasM; // hasM = true
hasID = polygonB.HasID; // hasID = true
});
##Construct a MapPoint
// methods need to run on the MCT
ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
// create a 3d point with M
// use the builder constructor
MapPointBuilder mb = new MapPointBuilder(1.0, 2.0, 3.0, 4.0);
MapPoint ptWithM = mb.ToGeometry();
MapPoint clone = ptWithM.Clone() as MapPoint;
// or use the convenience methods
MapPoint anotherPt = MapPointBuilder.CreateMapPoint(1.0, 2.0, 3.0, 4.0);
MapPoint anotherM = MapPointBuilder.CreateMapPoint(ptWithM);
});
##MapPoint Builder Properties
// methods need to run on the MCT
ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
MapPointBuilder mb = new MapPointBuilder(1.0, 2.0, 3.0);
bool hasZ = mb.HasZ; // hasZ = true
bool hasM = mb.HasM; // hasM = false
bool hasID = mb.HasID; // hasID = false
MapPoint pt = mb.ToGeometry();
double x = pt.X; // x = 1.0
double y = pt.Y; // y = 2.0
double z = pt.Z; // z = 3.0
double m = pt.M; // m = Nan
int ID = pt.ID; // ID = 0
hasZ = pt.HasZ; // hasZ = true
hasM = pt.HasM; // hasM = false
hasID = pt.HasID; // hasID = false
bool isEmpty = pt.IsEmpty; // isEmpty = false
MapPoint pt2 = MapPointBuilder.CreateMapPoint(pt);
x = pt.X; // x = 1.0
y = pt.Y; // y = 2.0
z = pt.Z; // z = 3.0
m = pt.M; // m = Nan
hasZ = pt2.HasZ; // hasZ = true
hasM = pt2.HasM; // hasM = false
hasID = pt2.HasID; // hasID = false
});
##MapPoint IsEqual
// methods need to run on the MCT
ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
MapPoint pt = MapPointBuilder.CreateMapPoint(1, 2, 3, 4, 5);
int ID = pt.ID; // ID = 5
bool hasID = pt.HasID; // hasID = true
MapPoint pt2 = MapPointBuilder.CreateMapPoint(1, 2, 3, 4, 0);
ID = pt2.ID; // ID = 0
hasID = pt2.HasID; // hasID = true
MapPoint pt3 = MapPointBuilder.CreateMapPoint(1, 2, 3, 4);
ID = pt3.ID; // ID = 0
hasID = pt3.HasID; // hasID = false
MapPoint pt4 = MapPointBuilder.CreateMapPoint(1, 2, 3, 44);
ID = pt4.ID; // ID = 0
hasID = pt4.HasID; // hasID = false
bool hasM = pt4.HasM; // hasM = true
MapPoint pt5 = MapPointBuilder.CreateMapPoint(1, 2, 3);
ID = pt5.ID; // ID = 0
hasID = pt5.HasID; // hasID = false
hasM = pt5.HasM; // hasM = false
bool isEqual = pt.IsEqual(pt2); // isEqual = false // different IDs
isEqual = pt2.IsEqual(pt3); // isEqual = false // HasId is different
isEqual = pt4.IsEqual(pt3); // isEqual = false // different Ms
isEqual = pt.IsEqual(pt5); // isEqual = false // pt has M, id but pt5 does not.
});
##Construct a Polyline - from an enumeration of MapPoints
// methods need to run on MCT
ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
MapPoint startPt = MapPointBuilder.CreateMapPoint(1.0, 1.0);
MapPoint endPt = MapPointBuilder.CreateMapPoint(2.0, 1.0);
List<MapPoint> list = new List<MapPoint>();
list.Add(startPt);
list.Add(endPt);
// use the builder constructor
PolylineBuilder pb = new PolylineBuilder(list);
pb.SpatialReference = SpatialReferences.WGS84;
Polyline polyline1 = pb.ToGeometry();
// or use the convenience method
Polyline polyline2 = PolylineBuilder.CreatePolyline(list, SpatialReferences.WGS84);
});
##Get the points of a Polyline
// get the points as a readonly Collection
ReadOnlyPointCollection pts = polyline.Points;
int numPts = polyline.PointCount;
// get an enumeration of the points
IEnumerator<MapPoint> enumPts = polyline.Points.GetEnumerator();
// get the point coordinates as a readonly list
IReadOnlyList<Coordinate> coordinates = polyline.CopyCoordinatesToList();
##Get the parts of a Polyline
int numParts = polyline.PartCount;
// get the parts as a readonly collection
ReadOnlyPartCollection parts = polyline.Parts;
##Enumerate the parts of a Polyline
ReadOnlyPartCollection polylineParts = polyline.Parts;
// enumerate the segments to get the length
double len = 0;
IEnumerator<ReadOnlySegmentCollection> segments = polylineParts.GetEnumerator();
while (segments.MoveNext())
{
ReadOnlySegmentCollection seg = segments.Current;
foreach (Segment s in seg)
{
len += s.Length;
// perhaps do something specific per segment type
switch (s.SegmentType)
{
case SegmentType.Line:
break;
case SegmentType.Bezier:
break;
case SegmentType.EllipticArc:
break;
}
}
}
##Reverse the order of points in a Polyline
// methods need to run on the MCT
ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
PolylineBuilder polylineBuilder = new PolylineBuilder(polyline);
polylineBuilder.ReverseOrientation();
Polyline reversedPolyline = polylineBuilder.ToGeometry();
});
##Build a multi-part Polyline
// methods need to run on the MCT
ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
List<MapPoint> firstPoints = new List<MapPoint>();
firstPoints.Add(MapPointBuilder.CreateMapPoint(1.0, 1.0));
firstPoints.Add(MapPointBuilder.CreateMapPoint(1.0, 2.0));
firstPoints.Add(MapPointBuilder.CreateMapPoint(2.0, 2.0));
firstPoints.Add(MapPointBuilder.CreateMapPoint(2.0, 1.0));
List<MapPoint> nextPoints = new List<MapPoint>();
nextPoints.Add(MapPointBuilder.CreateMapPoint(11.0, 1.0));
nextPoints.Add(MapPointBuilder.CreateMapPoint(11.0, 2.0));
nextPoints.Add(MapPointBuilder.CreateMapPoint(12.0, 2.0));
nextPoints.Add(MapPointBuilder.CreateMapPoint(12.0, 1.0));
PolylineBuilder pBuilder = new PolylineBuilder(firstPoints);
pBuilder.AddPart(nextPoints);
Polyline p = pBuilder.ToGeometry();
// polyline p has 2 parts
pBuilder.RemovePart(0);
p = pBuilder.ToGeometry();
// polyline p has 1 part
});
##Split Polyline at distance
// methods need to run on the MCT
ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
// create list of points
MapPoint startPt = MapPointBuilder.CreateMapPoint(1.0, 1.0);
MapPoint endPt = MapPointBuilder.CreateMapPoint(2.0, 1.0);
List<MapPoint> list = new List<MapPoint>();
list.Add(startPt);
list.Add(endPt);
// use the PolylineBuilder as we wish to manipulate the geometry
PolylineBuilder polylineBuilder = new PolylineBuilder(list);
// split at a distance 0.75
polylineBuilder.SplitAtDistance(0.75, false);
// get the polyline
Polyline p = polylineBuilder.ToGeometry();
// polyline p should have 3 points (1,1), (1.75, 1), (2,1)
// add another path
MapPoint p1 = MapPointBuilder.CreateMapPoint(4.0, 1.0);
MapPoint p2 = MapPointBuilder.CreateMapPoint(6.0, 1.0);
MapPoint p3 = MapPointBuilder.CreateMapPoint(7.0, 1.0);
List<MapPoint> pts = new List<MapPoint>();
pts.Add(p1);
pts.Add(p2);
pts.Add(p3);
polylineBuilder.AddPart(pts);
p = polylineBuilder.ToGeometry();
// polyline p has 2 parts. Each part has 3 points
// split the 2nd path half way - dont create a new path
polylineBuilder.SplitPartAtDistance(1, 0.5, true, false);
p = polylineBuilder.ToGeometry();
// polyline p still has 2 parts; but now has 7 points
});
##Construct a Polygon - from an enumeration of MapPoints
// methods need to run on the MCT
ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
MapPoint pt1 = MapPointBuilder.CreateMapPoint(1.0, 1.0);
MapPoint pt2 = MapPointBuilder.CreateMapPoint(1.0, 2.0);
MapPoint pt3 = MapPointBuilder.CreateMapPoint(2.0, 2.0);
MapPoint pt4 = MapPointBuilder.CreateMapPoint(2.0, 1.0);
List<MapPoint> list = new List<MapPoint>();
list.Add(pt1);
list.Add(pt2);
list.Add(pt3);
list.Add(pt4);
// use the builder constructor
PolygonBuilder pb = new PolygonBuilder(list);
pb.SpatialReference = SpatialReferences.WGS84;
Polygon polygon = pb.ToGeometry();
// or use the convenience method
Polygon anotherPolygon = PolygonBuilder.CreatePolygon(list, SpatialReferences.WGS84);
});
##Construct a Polygon - from an Envelope
// methods need to run on the MCT
ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
MapPoint minPt = MapPointBuilder.CreateMapPoint(1.0, 1.0);
MapPoint maxPt = MapPointBuilder.CreateMapPoint(2.0, 2.0);
// Create an envelope
Envelope env = EnvelopeBuilder.CreateEnvelope(minPt, maxPt);
// Create a polygon from the envelope
Polygon polygon = PolygonBuilder.CreatePolygon(env);
});
##Get the points of a Polygon
// get the points as a readonly Collection
ReadOnlyPointCollection pts = poly.Points;
// get an enumeration of the points
IEnumerator<MapPoint> enumPts = poly.Points.GetEnumerator();
// get the point coordinates as a readonly list
IReadOnlyList<Coordinate> coordinates = poly.CopyCoordinatesToList();
##Get the parts of a Polygon
// get the parts as a readonly collection
ReadOnlyPartCollection parts = poly.Parts;
##Enumerate the parts of a Polygon
int numSegments = 0;
IEnumerator<ReadOnlySegmentCollection> segments = poly.Parts.GetEnumerator();
while (segments.MoveNext())
{
ReadOnlySegmentCollection seg = segments.Current;
numSegments += seg.Count;
foreach (Segment s in seg)
{
// do something with the segment
}
}
##Build a donut polygon
// methods need to run on the MCT
ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
List<Coordinate2D> outerCoordinates = new List<Coordinate2D>();
outerCoordinates.Add(new Coordinate2D(10.0, 10.0));
outerCoordinates.Add(new Coordinate2D(10.0, 20.0));
outerCoordinates.Add(new Coordinate2D(20.0, 20.0));
outerCoordinates.Add(new Coordinate2D(20.0, 10.0));
// use the PolygonBuilder as we wish to manipulate the parts
PolygonBuilder pb = new PolygonBuilder(outerCoordinates);
Polygon donut = pb.ToGeometry();
double area = donut.Area; // area = 100
// define the inner polygon as anti-clockwise
List<Coordinate2D> innerCoordinates = new List<Coordinate2D>();
innerCoordinates.Add(new Coordinate2D(13.0, 13.0));
innerCoordinates.Add(new Coordinate2D(17.0, 13.0));
innerCoordinates.Add(new Coordinate2D(17.0, 17.0));
innerCoordinates.Add(new Coordinate2D(13.0, 17.0));
pb.AddPart(innerCoordinates);
donut = pb.ToGeometry();
area = donut.Area; // area = 84.0
});
##Construct an Envelope
// methods need to run on the MCT
ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
MapPoint minPt = MapPointBuilder.CreateMapPoint(1.0, 1.0);
MapPoint maxPt = MapPointBuilder.CreateMapPoint(2.0, 2.0);
EnvelopeBuilder ev = new EnvelopeBuilder(minPt, maxPt);
Envelope env = ev.ToGeometry();
});
##Construct an Envelope - from a JSON string
// methods need to run on the MCT
ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
string jsonString = "{ \"xmin\" : 1, \"ymin\" : 2,\"xmax\":3,\"ymax\":4,\"spatialReference\":{\"wkid\":4326}}";
Envelope envFromJson = EnvelopeBuilder.FromJson(jsonString);
});
##Union two Envelopes
// methods need to run on the MCT
ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
Envelope env1 = EnvelopeBuilder.CreateEnvelope(0, 0, 1, 1, SpatialReferences.WGS84);
Envelope env2 = EnvelopeBuilder.CreateEnvelope(0.5, 0.5, 1.5, 1.5, SpatialReferences.WGS84);
Envelope env3 = env1.Union(env2);
});
##Expand an Envelope
// methods need to run on the MCT
ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
Envelope envelope = EnvelopeBuilder.CreateEnvelope(100.0, 100.0, 500.0, 500.0);
// shrink the envelope by 50%
Envelope result = envelope.Expand(0.5, 0.5, true);
});
##Construct a Multipoint - from an enumeration of MapPoints
// methods need to run on the MCT
ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
List<MapPoint> list = new List<MapPoint>();
list.Add(MapPointBuilder.CreateMapPoint(1.0, 1.0));
list.Add(MapPointBuilder.CreateMapPoint(1.0, 2.0));
list.Add(MapPointBuilder.CreateMapPoint(2.0, 2.0));
list.Add(MapPointBuilder.CreateMapPoint(2.0, 1.0));
// use the builder constructor
MultipointBuilder mpb = new MultipointBuilder(list);
Multipoint mPt = mpb.ToGeometry();
// or use the convenience method
Multipoint multiPoint = MultipointBuilder.CreateMultipoint(list);
});
##Modify the points of a Multipoint
// methods need to run on the MCT
ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
// assume a multiPoint has been built from 4 points
// the modified multiPoint will have the first point removed and the last point moved
MultipointBuilder mpb = new MultipointBuilder(multiPt);
// remove the first point
mpb.RemovePoint(0);
// modify the coordinates of the last point
MapPoint pt = mpb.GetMapPoint(mpb.PointCount-1);
mpb.RemovePoint(mpb.PointCount - 1);
MapPoint newPt = MapPointBuilder.CreateMapPoint(pt.X + 1.0, pt.Y + 2.0);
mpb.Add(newPt);
Multipoint modifiedMultiPoint = mpb.ToGeometry();
});
##Construct a LineSegment using two MapPoints
// methods need to run on the MCT
ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
MapPoint startPt = MapPointBuilder.CreateMapPoint(1.0, 1.0);
MapPoint endPt = MapPointBuilder.CreateMapPoint(2.0, 1.0);
// use the builder constructor
LineBuilder lb = new LineBuilder(startPt, endPt);
LineSegment line = lb.ToSegment();
// or use the convenience method
LineSegment anotherLine = LineBuilder.CreateLineSegment(startPt, endPt);
});
##Construct a Cubic Bezier - from Coordinates
// builders need to run on the MCT
ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
Coordinate startPt = new Coordinate(1.0, 1.0, 3.0);
Coordinate endPt = new Coordinate(2.0, 2.0, 3.0);
Coordinate2D ctrl1Pt = new Coordinate2D(1.0, 2.0);
Coordinate2D ctrl2Pt = new Coordinate2D(2.0, 1.0);
// use the builder constructor
CubicBezierBuilder cbb = new CubicBezierBuilder(startPt, ctrl1Pt, ctrl2Pt, endPt, SpatialReferences.WGS84);
CubicBezierSegment bezier = cbb.ToSegment();
// or use the convenience method
CubicBezierSegment anotherBezier = CubicBezierBuilder.CreateCubicBezierSegment(startPt, ctrl1Pt, ctrl2Pt, endPt, SpatialReferences.WGS84);
});
##Construct a Cubic Bezier - from MapPoints
// builders need to run on the MCT
ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
MapPoint startPt = MapPointBuilder.CreateMapPoint(1.0, 1.0, SpatialReferences.WGS84);
MapPoint endPt = MapPointBuilder.CreateMapPoint(2.0, 2.0, SpatialReferences.WGS84);
MapPoint ctrl1Pt = MapPointBuilder.CreateMapPoint(1.0, 2.0, SpatialReferences.WGS84);
MapPoint ctrl2Pt = MapPointBuilder.CreateMapPoint(2.0, 1.0, SpatialReferences.WGS84);
// use the builder constructor
CubicBezierBuilder cbb = new CubicBezierBuilder(startPt, ctrl1Pt, ctrl2Pt, endPt);
CubicBezierSegment bezier = cbb.ToSegment();
// or use the convenience method
CubicBezierSegment anotherBezier = CubicBezierBuilder.CreateCubicBezierSegment(startPt, ctrl1Pt, ctrl2Pt, endPt);
});
##Construct a Cubic Bezier - from an enumeration of MapPoints
// builders need to run on the MCT
ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
MapPoint startPt = MapPointBuilder.CreateMapPoint(1.0, 1.0, SpatialReferences.WGS84);
MapPoint endPt = MapPointBuilder.CreateMapPoint(2.0, 2.0, SpatialReferences.WGS84);
MapPoint ctrl1Pt = MapPointBuilder.CreateMapPoint(1.0, 2.0, SpatialReferences.WGS84);
MapPoint ctrl2Pt = MapPointBuilder.CreateMapPoint(2.0, 1.0, SpatialReferences.WGS84);
List<MapPoint> listMapPoints = new List<MapPoint>();
listMapPoints.Add(startPt);
listMapPoints.Add(ctrl1Pt);
listMapPoints.Add(ctrl2Pt);
listMapPoints.Add(endPt);
// use the builder constructor
CubicBezierBuilder cbb = new CubicBezierBuilder(listMapPoints);
CubicBezierSegment bezier = cbb.ToSegment();
// or use the convenience method
CubicBezierSegment anotherBezier = CubicBezierBuilder.CreateCubicBezierSegment(listMapPoints);
});
##Cubic Bezier Builder Properties
// methods need to run on the MCT
ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
// retrieve the bezier curve's control points
CubicBezierBuilder cbb = new CubicBezierBuilder(bezierSegment);
MapPoint startPt = cbb.StartPoint;
Coordinate2D ctrlPt1 = cbb.ControlPoint1;
Coordinate2D ctrlPt2 = cbb.ControlPoint2;
MapPoint endPt = cbb.EndPoint;
// or use the QueryCoords method
cbb.QueryCoords(out startPt, out ctrlPt1, out ctrlPt2, out endPt);
});
##Cubic Bezier Properties
// methods need to run on the MCT
ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
// retrieve the bezier curve's control points
CubicBezierSegment cb = CubicBezierBuilder.CreateCubicBezierSegment(bezierSegment);
MapPoint startPt = cb.StartPoint;
Coordinate2D ctrlPt1 = cb.ControlPoint1;
Coordinate2D ctrlPt2 = cb.ControlPoint2;
MapPoint endPt = cb.EndPoint;
bool isCurve = cb.IsCurve;
double len = cb.Length;
});
##Construct a Polyline (from a Cubic Bezier)
// methods need to run on the MCT
ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
Polyline polyline = PolylineBuilder.CreatePolyline(bezierSegment);
});
##Construct a Circular Arc - using an interior point
// methods need to run on the MCT
ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
// Construct a circular arc from (2, 1) to (1, 2) with interior pt (1 + sqrt(2)/2, 1 + sqrt(2)/2).
Coordinate fromPt = new Coordinate(2, 1);
Coordinate toPt = new Coordinate(1, 2);
Coordinate2D interiorPt = new Coordinate2D(1 + Math.Sqrt(2) / 2, 1 + Math.Sqrt(2) / 2);
// use the builder constructor
EllipticArcBuilder cab = new EllipticArcBuilder(fromPt.ToMapPoint(), toPt.ToMapPoint(), interiorPt);
EllipticArcSegment circularArc = cab.ToSegment();
// or use the convenience method
EllipticArcSegment anotherCircularArc = EllipticArcBuilder.CreateEllipticArcSegment(fromPt.ToMapPoint(), toPt.ToMapPoint(), interiorPt);
});
##Construct a Circular Arc - using a chord length and bearing
// methods need to run on the MCT
ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
// Construct a circular arc counterclockwise from (2, 1) to (1, 2) such that the embedded
// circle has center point at (1, 1) and radius = 1
MapPoint fromPt = MapPointBuilder.CreateMapPoint(2, 1, SpatialReferences.WGS84);
double chordLength = Math.Sqrt(2);
double chordBearing = 3 * Math.PI / 4;
double radius = 1;
esriArcOrientation orientation = esriArcOrientation.esriArcCounterClockwise;
MinorOrMajor minorOrMajor = MinorOrMajor.Minor;
// use the builder constructor
EllipticArcBuilder cab = new EllipticArcBuilder(fromPt, chordLength, chordBearing, radius, orientation, minorOrMajor);
EllipticArcSegment circularArc = cab.ToSegment();
// or use the convenience method
EllipticArcSegment anotherCircularArc = EllipticArcBuilder.CreateEllipticArcSegment(fromPt, chordLength, chordBearing, radius, orientation, minorOrMajor);
});
##Construct a Circular Arc - using a center point and angle
// methods need to run on the MCT
ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
// Construct a circular arc with center point at (0, 0), from angle = 0,
// central angle = pi/2, radius = 1
SpatialReference sr4326 = SpatialReferences.WGS84;
Coordinate2D centerPt = new Coordinate2D(0, 0);
double fromAngle = 0;
double centralAngle = Math.PI / 2;
double radius = 1;
// use the builder constructor
EllipticArcBuilder cab = new EllipticArcBuilder(fromAngle, centralAngle, centerPt, radius, sr4326);
EllipticArcSegment circularArc = cab.ToSegment();
// or use the convenience method
EllipticArcSegment anotherCircularArc = EllipticArcBuilder.CreateEllipticArcSegment(fromAngle, centralAngle, centerPt, radius, sr4326);
});
##Construct a Circular Arc - using a center point and orientation
// methods need to run on the MCT
ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
// Construct a circular arc from (2, 1) to (1, 2)
// with center point at (1, 1) and orientation counterclockwise.
Coordinate fromPt = new Coordinate(2, 1);
Coordinate toPt = new Coordinate(1, 2);
Coordinate2D centerPtCoord = new Coordinate2D(1, 1);
// use the builder constructor
EllipticArcBuilder cab = new EllipticArcBuilder(fromPt.ToMapPoint(), toPt.ToMapPoint(), centerPtCoord, esriArcOrientation.esriArcCounterClockwise);
EllipticArcSegment circularArc = cab.ToSegment();
// or use the convenience method
EllipticArcSegment anotherCircularArc = EllipticArcBuilder.CreateEllipticArcSegment(fromPt.ToMapPoint(), toPt.ToMapPoint(), centerPtCoord, esriArcOrientation.esriArcCounterClockwise);
});
##Construct a Circle
// methods need to run on the MCT
ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
// Construct a circle with center at (-1,-1), radius = 2, and oriented clockwise.
Coordinate2D centerPtCoord = new Coordinate2D(-1, -1);
// use the builder constructor
EllipticArcBuilder builder = new EllipticArcBuilder(centerPtCoord, 2, esriArcOrientation.esriArcClockwise);
EllipticArcSegment circle = builder.ToSegment();
// or use the convenience method
EllipticArcSegment anotherCircle = EllipticArcBuilder.CreateEllipticArcSegment(centerPtCoord, 2, esriArcOrientation.esriArcClockwise);
});
##Construct an Ellipse
// methods need to run on the MCT
ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
// Construct an ellipse centered at (1, 2) with rotationAngle = -pi/6,
// semiMajorAxis = 5, minorMajorRatio = 0.2, oriented clockwise
Coordinate2D centerPt = new Coordinate2D(1, 2);
// use the builder constructor
EllipticArcBuilder builder = new EllipticArcBuilder(centerPt, -1 * Math.PI / 6, 5, 0.2, esriArcOrientation.esriArcClockwise);
EllipticArcSegment ellipse = builder.ToSegment();
// or use the convenience method
EllipticArcSegment anotherEllipse = EllipticArcBuilder.CreateEllipticArcSegment(centerPt, -1 * Math.PI / 6, 5, 0.2, esriArcOrientation.esriArcClockwise);
});
##Elliptic Arc Builder Properties
// methods need to run on the MCT
ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
// retrieve the curve's control points
EllipticArcBuilder builder = new EllipticArcBuilder(arcSegment);
MapPoint startPt = builder.StartPoint;
MapPoint endPt = builder.EndPoint;
Coordinate2D centerPt = builder.CenterPoint;
bool isCircular = builder.IsCircular;
bool isMinor = builder.IsMinor;
});
##Elliptic Arc Properties
// methods need to run on the MCT
ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
// retrieve the curve's control points
EllipticArcSegment arc = EllipticArcBuilder.CreateEllipticArcSegment(arcSegment);
MapPoint startPt = arc.StartPoint;
MapPoint endPt = arc.EndPoint;
Coordinate2D centerPt = arc.CenterPoint;
bool isCircular = arc.IsCircular;
bool isMinor = arc.IsMinor;
double startAngle, centralAngle, rotationAngle, semiMajorAxis, semiMinorAxis;
// or use QueryCoords
arc.QueryCoords(out centerPt, out startAngle, out centralAngle, out rotationAngle, out semiMajorAxis, out semiMinorAxis);
});
##Get the individual parts of a multipart feature
/// <summary>
/// This method takes an input multi part geometry and breaks the parts into individual standalone geometries.
/// This method must be called on the MCT. Use QueuedTask.Run.
/// </summary>
/// <param name="inputGeometry">The geometry to be exploded into the individual parts.</param>
/// <returns>An enumeration of individual parts as standalone geometries. The type of geometry is maintained, i.e.
/// if the input geometry is of type Polyline then each geometry in the return is of type Polyline as well.
/// If the input geometry is of type Unknown then an empty list is returned.</returns>
/// <remarks>This method must be called on the MCT. Use QueuedTask.Run.</remarks>
public IEnumerable<Geometry> MultipartToSinglePart(Geometry inputGeometry)
{
// list holding the part(s) of the input geometry
List<Geometry> singleParts = new List<Geometry>();
// check if the input is a null pointer or if the geometry is empty
if (inputGeometry == null || inputGeometry.IsEmpty)
return singleParts;
// based on the type of geometry, take the parts/points and add them individually into a list
switch (inputGeometry.GeometryType)
{
case GeometryType.Envelope:
singleParts.Add(inputGeometry.Clone() as Envelope);
break;
case GeometryType.Multipatch:
singleParts.Add(inputGeometry.Clone() as Multipatch);
break;
case GeometryType.Multipoint:
var multiPoint = inputGeometry as Multipoint;
foreach (var point in multiPoint.Points)
{
// add each point of collection as a standalone point into the list
singleParts.Add(point);
}
break;
case GeometryType.Point:
singleParts.Add(inputGeometry.Clone() as MapPoint);
break;
case GeometryType.Polygon:
var polygon = inputGeometry as Polygon;
foreach (var polygonPart in polygon.Parts)
{
// use the PolygonBuilder turning the segments into a standalone
// polygon instance
singleParts.Add(PolygonBuilder.CreatePolygon(polygonPart));
}
break;
case GeometryType.Polyline:
var polyline = inputGeometry as Polyline;
foreach (var polylinePart in polyline.Parts)
{
// use the PolylineBuilder turning the segments into a standalone
// polyline instance
singleParts.Add(PolylineBuilder.CreatePolyline(polylinePart));
}
break;
case GeometryType.Unknown:
break;
default:
break;
}
return singleParts;
}
##Get the outermost rings of a polygon
/// <summary>
/// The methods retrieves the outer ring(s) of the input polygon.
/// This method must be called on the MCT. Use QueuedTask.Run.
/// </summary>
/// <param name="inputPolygon">Input Polygon.</param>
/// <returns>The outer most (exterior, clockwise) ring(s) of the polygon. If the input is null or empty, a null pointer is returned.</returns>
/// <remarks>This method must be called on the MCT. Use QueuedTask.Run.</remarks>
public Polygon GetOutermostRings(Polygon inputPolygon)
{
if (inputPolygon == null || inputPolygon.IsEmpty)
return null;
PolygonBuilder outerRings = new PolygonBuilder();
List<Polygon> internalRings = new List<Polygon>();
// explode the parts of the polygon into a list of individual geometries
// see the "Get the individual parts of a multipart feature" snippet for MultipartToSinglePart method defintion
var parts = MultipartToSinglePart(inputPolygon);
// get an enumeration of clockwise geometries (area > 0) ordered by the area
var clockwiseParts = parts.Where(geom => ((Polygon)geom).Area > 0).OrderByDescending(geom => ((Polygon)geom).Area);
// for each of the exterior rings
foreach (var part in clockwiseParts)
{
// add the first (the largest) ring into the internal collection
if (internalRings.Count == 0)
internalRings.Add(part as Polygon);
// use flag to indicate if current part is within the already selection polygons
bool isWithin = false;
foreach (var item in internalRings)
{
if (GeometryEngine.Within(part, item))
isWithin = true;
}
// if the current polygon is not within any polygon of the internal collection
// then it is disjoint and needs to be added to
if (isWithin == false)
internalRings.Add(part as Polygon);
}
// now assemble a new polygon geometry based on the internal polygon collection
foreach (var ring in internalRings)
{
outerRings.AddParts(ring.Parts);
}
// return the final geometry of the outer rings
return outerRings.ToGeometry();
}
##Determine if a polygon ring is an outer ring or an inner ring
/// <summary>
/// Based on the shoelace formula (or aka Gauss's area formula) https://en.wikipedia.org/wiki/Shoelace_formula
/// It computes the area of a polygon by determining the area of ordered coordinate pairs.
/// This method is more efficient than using the Polygon.Area methodology.
/// </summary>
/// <param name="PolyToCheck">The geometry to investigate.</param>
private void FindOrientationSimple(Polygon PolyToCheck)
{
foreach (var part in PolyToCheck.Parts)
{
// construct a list of ordered coordinate pairs
List<Coordinate2D> ringCoordinates = new List<Coordinate2D>(PolyToCheck.PointCount);
foreach (var segment in part)
{
ringCoordinates.Add(segment.StartCoordinate);
ringCoordinates.Add(segment.EndCoordinate);
}
// this is not the true area of the part
// a negative number indicates an outer ring and a positive number represents an inner ring
// (this is the opposite from the ArcGIS.Core.Geometry understanding)
double signedArea = 0;
// for all coordinates pairs compute the area
// the last coordinate needs to reach back to the starting coordinate to complete
for (int cIndex = 0; cIndex < ringCoordinates.Count - 1; cIndex++)
{
double x1 = ringCoordinates[cIndex].X;
double y1 = ringCoordinates[cIndex].Y;
double x2, y2;
if (cIndex == ringCoordinates.Count - 2)
{
x2 = ringCoordinates[0].X;
y2 = ringCoordinates[0].Y;
}
else
{
x2 = ringCoordinates[cIndex + 1].X;
y2 = ringCoordinates[cIndex + 1].Y;
}
signedArea += ((x1 * y2) - (x2 * y1));
}
// if signedArea is a negative number => indicates an outer ring
// if signedArea is a positive number => indicates an inner ring
// (this is the opposite from the ArcGIS.Core.Geometry understanding)
}
}
##Retrieve Geometry from Geodatabase
// methods need to run on the MCT
ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
try
{
// open a gdb
ArcGIS.Core.Data.Geodatabase gdb = new ArcGIS.Core.Data.Geodatabase(@"c:\Temp\MyDatabase.gdb");
//Open a featureClass
ArcGIS.Core.Data.FeatureClass featureClass = gdb.OpenDataset<ArcGIS.Core.Data.FeatureClass>("Polygon") as ArcGIS.Core.Data.FeatureClass;
// find a field
ArcGIS.Core.Data.FeatureClassDefinition featureClassDefinition = featureClass.GetDefinition() as ArcGIS.Core.Data.FeatureClassDefinition;
int fldIndex = featureClassDefinition.FindField("SomeField");
if (fldIndex == -1)
{
return;
}
ArcGIS.Core.Data.QueryFilter filter = new ArcGIS.Core.Data.QueryFilter
{
WhereClause = "OBJECTID = 6"
};
// get the row
ArcGIS.Core.Data.RowCursor rowCursor = featureClass.Search(filter, false);
while (rowCursor.MoveNext())
{
long oid = rowCursor.Current.GetObjectID();
// get the shape from the row
ArcGIS.Core.Data.Feature feature = rowCursor.Current as ArcGIS.Core.Data.Feature;
Polygon polygon = feature.GetShape() as Polygon;
// get the attribute from the row (assume it's a double field)
double value = (double)rowCursor.Current.GetOriginalValue(fldIndex);
// do something here
}
}
catch (Exception ex)
{
// error - handle appropriately
}
});
##Import and Export Geometries to Well Known Text
// methods need to run on the MCT
ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
// create a point with z, m
MapPoint point = MapPointBuilder.CreateMapPoint(100, 200, 300, 400, SpatialReferences.WebMercator);
// set the flags
WKTExportFlags exportFlags = WKTExportFlags.wktExportDefaults;
WKTImportFlags importFlags = WKTImportFlags.wktImportDefaults;
// export and import
string wktString = GeometryEngine.ExportToWKT(exportFlags, point);
MapPoint importPoint = GeometryEngine.ImportFromWKT(importFlags, wktString, SpatialReferences.WebMercator) as MapPoint;
double x = importPoint.X; // x = 100
double y = importPoint.Y; // y = 200
bool hasZ = importPoint.HasZ; // hasZ = true
double z = importPoint.Z; // z = 300
bool hasM = importPoint.HasM; // hasM = true
double m = importPoint.M; // m = 400
// export without z
WKTExportFlags exportFlagsNoZ = WKTExportFlags.wktExportStripZs;
wktString = GeometryEngine.ExportToWKT(exportFlags, point);
importPoint = GeometryEngine.ImportFromWKT(importFlags, wktString, SpatialReferences.WebMercator) as MapPoint;
x = importPoint.X; // x = 100
y = importPoint.Y; // y = 200
hasZ = importPoint.HasZ; // hasZ = false
z = importPoint.Z; // z = Nan
hasM = importPoint.HasM; // hasM = true
m = importPoint.M; // m = 400
// export without m
WKTExportFlags exportFlagsNoM = WKTExportFlags.wktExportStripMs;
wktString = GeometryEngine.ExportToWKT(exportFlags, point);
importPoint = GeometryEngine.ImportFromWKT(importFlags, wktString, SpatialReferences.WebMercator) as MapPoint;
x = importPoint.X; // x = 100
y = importPoint.Y; // y = 200
hasZ = importPoint.HasZ; // hasZ = true
z = importPoint.Z; // z = 300
hasM = importPoint.HasM; // hasM = false
m = importPoint.M; // m = Nan
// export without z, m
wktString = GeometryEngine.ExportToWKT(exportFlagsNoZ | exportFlagsNoM, point);
importPoint = GeometryEngine.ImportFromWKT(importFlags, wktString, SpatialReferences.WebMercator) as MapPoint;
x = importPoint.X; // x = 100
y = importPoint.Y; // y = 200
hasZ = importPoint.HasZ; // hasZ = false
z = importPoint.Z; // z = Nan
hasM = importPoint.HasM; // hasM = false
m = importPoint.M; // m = Nan
});
##Import and Export Geometries to Well Known Binary
// methods need to run on the MCT
ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
// create a polyline
List<Coordinate2D> coords = new List<Coordinate2D>
{
new Coordinate2D(0, 0),
new Coordinate2D(0, 1),
new Coordinate2D(1, 1),
new Coordinate2D(1, 0)
};
Polyline polyline = PolylineBuilder.CreatePolyline(coords, SpatialReferences.WGS84);
WKBExportFlags exportFlags = WKBExportFlags.wkbExportDefaults;
WKBImportFlags importFlags = WKBImportFlags.wkbImportDefaults;
// export and import
byte[] buffer = GeometryEngine.ExportToWKB(exportFlags, polyline);
Geometry geometry = GeometryEngine.ImportFromWKB(importFlags, buffer, SpatialReferences.WGS84);
Polyline importPolyline = geometry as Polyline;
// alternatively, determine the size for the buffer
int bufferSize = GeometryEngine.GetWKBSize(exportFlags, polyline);
buffer = new byte[bufferSize];
// export
bufferSize = GeometryEngine.ExportToWKB(exportFlags, polyline, ref buffer);
// import
importPolyline = GeometryEngine.ImportFromWKB(importFlags, buffer, SpatialReferences.WGS84) as Polyline;
});
##Import and Export Geometries to EsriShape
// methods need to run on the MCT
ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
// create an envelope
List<Coordinate> coordsZM = new List<Coordinate>
{
new Coordinate(1001, 1002, 1003, 1004),
new Coordinate(2001, 2002, Double.NaN, 2004),
new Coordinate(3001, -3002, 3003, 3004),
new Coordinate(1001, -4002, 4003, 4004)
};
Envelope envelope = EnvelopeBuilder.CreateEnvelope(coordsZM[0], coordsZM[2], SpatialReferences.WGS84);
// export and import
EsriShapeExportFlags exportFlags = EsriShapeExportFlags.esriShapeExportDefaults;
EsriShapeImportFlags importFlags = EsriShapeImportFlags.esriShapeImportDefaults;
byte[] buffer = GeometryEngine.ExportToEsriShape(exportFlags, envelope);
Polygon importedPolygon = GeometryEngine.ImportFromEsriShape(importFlags, buffer, envelope.SpatialReference) as Polygon;
Envelope importedEnvelope = importedPolygon.Extent;
// export without z,m
buffer = GeometryEngine.ExportToEsriShape(EsriShapeExportFlags.esriShapeExportStripZs | EsriShapeExportFlags.esriShapeExportStripMs, envelope);
importedPolygon = GeometryEngine.ImportFromEsriShape(importFlags, buffer, SpatialReferences.WGS84) as Polygon;
importedEnvelope = importedPolygon.Extent;
bool hasZ = importedEnvelope.HasZ; // hasZ = false
bool hasM = importedEnvelope.HasM; // hasM = false
// export with shapeSize
int bufferSize = GeometryEngine.GetEsriShapeSize(exportFlags, envelope);
buffer = new byte[bufferSize];
bufferSize = GeometryEngine.ExportToEsriShape(exportFlags, envelope, ref buffer);
importedPolygon = GeometryEngine.ImportFromEsriShape(importFlags, buffer, envelope.SpatialReference) as Polygon;
importedEnvelope = importedPolygon.Extent;
});
##Determine the boundary of a multi-part Polygon
// methods need to run on the MCT
ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
// create a donut polygon. Must use the PolygonBuilder object
List<Coordinate2D> outerPts = new List<Coordinate2D>();
outerPts.Add(new Coordinate2D(10.0, 10.0));
outerPts.Add(new Coordinate2D(10.0, 20.0));
outerPts.Add(new Coordinate2D(20.0, 20.0));
outerPts.Add(new Coordinate2D(20.0, 10.0));
List<Coordinate2D> innerPts = new List<Coordinate2D>();
innerPts.Add(new Coordinate2D(13.0, 13.0));
innerPts.Add(new Coordinate2D(17.0, 13.0));
innerPts.Add(new Coordinate2D(17.0, 17.0));
innerPts.Add(new Coordinate2D(13.0, 17.0));
// add the outer points
PolygonBuilder pb = new PolygonBuilder(outerPts);
// add the inner points (note they are defined anticlockwise)
pb.AddPart(innerPts);
// get the polygon
Polygon donut = pb.ToGeometry();
// get the boundary
Geometry g = GeometryEngine.Boundary(donut);
Polyline boundary = g as Polyline;
});
##Buffer a MapPoint
// methods need to run on the MCT
ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
// buffer a point
MapPoint pt = MapPointBuilder.CreateMapPoint(1.0, 1.0, SpatialReferences.WGS84);
Geometry ptBuffer = GeometryEngine.Buffer(pt, 5.0);
Polygon buffer = ptBuffer as Polygon;
});
##Buffer a Circular Arc
// methods need to run on the MCT
ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
// create the circular arc
Coordinate fromPt = new Coordinate(2, 1);
Coordinate toPt = new Coordinate(1, 2);
Coordinate2D interiorPt = new Coordinate2D(1 + Math.Sqrt(2) / 2, 1 + Math.Sqrt(2) / 2);
EllipticArcSegment circularArc = EllipticArcBuilder.CreateEllipticArcSegment(fromPt.ToMapPoint(), toPt.ToMapPoint(), interiorPt);
// buffer the arc
Polyline polyline = PolylineBuilder.CreatePolyline(circularArc);
Geometry lineBuffer = GeometryEngine.Buffer(polyline, 10);
});
##Buffer multiple MapPoints
// methods need to run on the MCT
ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
// creates a buffer around each MapPoint
List<MapPoint> pts = new List<MapPoint>();
pts.Add(MapPointBuilder.CreateMapPoint(1.0, 1.0));
pts.Add(MapPointBuilder.CreateMapPoint(1.0, 2.0));
pts.Add(MapPointBuilder.CreateMapPoint(2.0, 2.0));
pts.Add(MapPointBuilder.CreateMapPoint(2.0, 1.0));
Geometry ptsBuffer = GeometryEngine.Buffer(pts, 0.25);
Polygon bufferResult = ptsBuffer as Polygon; // bufferResult will have 4 parts
});
##Buffer many different Geometry Types
// methods need to run on the MCT
ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
List<Coordinate2D> coords = new List<Coordinate2D>()
{
new Coordinate2D(1, 2), new Coordinate2D(3, 4), new Coordinate2D(4, 2),
new Coordinate2D(5, 6), new Coordinate2D(7, 8), new Coordinate2D(8, 4),
new Coordinate2D(9, 10), new Coordinate2D(11, 12), new Coordinate2D(12, 8),
new Coordinate2D(10, 8), new Coordinate2D(12, 12), new Coordinate2D(14, 10)
};
List<Geometry> manyGeometries = new List<Geometry>
{
MapPointBuilder.CreateMapPoint(coords[9]),
PolylineBuilder.CreatePolyline(new List<Coordinate2D>(){coords[0], coords[1], coords[2]}, SpatialReferences.WGS84),
PolylineBuilder.CreatePolyline(new List<Coordinate2D>(){coords[3], coords[4], coords[5]}),
PolygonBuilder.CreatePolygon(new List<Coordinate2D>(){coords[6], coords[7], coords[8]})
};
Geometry manyGeomBuffer = GeometryEngine.Buffer(manyGeometries, 0.25);
});
##Find a Polygon centroid
// methods need to run on the MCT
ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
// simple polygon
List<Coordinate2D> list2D = new List<Coordinate2D>();
list2D.Add(new Coordinate2D(0, 0));
list2D.Add(new Coordinate2D(0, 2));
list2D.Add(new Coordinate2D(2, 2));
list2D.Add(new Coordinate2D(2, 0));
Polygon polygon = PolygonBuilder.CreatePolygon(list2D, SpatialReferences.WGS84);
// verify it is simple
bool isSimple = GeometryEngine.IsSimpleAsFeature(polygon);
// find the centroid
MapPoint centroid = GeometryEngine.Centroid(polygon);
});
##Clip a Polyline
// methods need to run on the MCT
ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
// clip a polyline by an envelope
Envelope env = EnvelopeBuilder.CreateEnvelope(2.0, 2.0, 4.0, 4.0);
LineSegment line = LineBuilder.CreateLineSegment(new Coordinate(0, 3), new Coordinate(5.0, 3.0));
Polyline polyline = PolylineBuilder.CreatePolyline(line);
Geometry clipGeom = GeometryEngine.Clip(polyline, env);
});
##Clip a Polyline by a Polygon
// methods need to run on the MCT
ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
// clip a polyline by a polygon
List<Coordinate2D> list = new List<Coordinate2D>();
list.Add(new Coordinate2D(1.0, 1.0));
list.Add(new Coordinate2D(1.0, 4.0));
list.Add(new Coordinate2D(4.0, 4.0));
list.Add(new Coordinate2D(4.0, 1.0));
Polygon polygon = PolygonBuilder.CreatePolygon(list, SpatialReferences.WGS84);
LineSegment crossingLine = LineBuilder.CreateLineSegment(MapPointBuilder.CreateMapPoint(0, 3), MapPointBuilder.CreateMapPoint(5.0, 3.0));
Polyline p = PolylineBuilder.CreatePolyline(crossingLine);
Geometry geometry = GeometryEngine.Clip(p, polygon.Extent);
});
##Polygon contains MapPoints, Polylines, Polygons
// methods need to run on the MCT
ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
// build a polygon
List<MapPoint> pts = new List<MapPoint>();
pts.Add(MapPointBuilder.CreateMapPoint(1.0, 1.0));
pts.Add(MapPointBuilder.CreateMapPoint(1.0, 2.0));
pts.Add(MapPointBuilder.CreateMapPoint(2.0, 2.0));
pts.Add(MapPointBuilder.CreateMapPoint(2.0, 1.0));
Polygon poly = PolygonBuilder.CreatePolygon(pts);
// test if an inner point is contained
MapPoint innerPt = MapPointBuilder.CreateMapPoint(1.5, 1.5);
bool contains = GeometryEngine.Contains(poly, innerPt); // contains = true
// test a point on a boundary
contains = GeometryEngine.Contains(poly, poly.Points[0]); // contains = false
// test an interior line
MapPoint innerPt2 = MapPointBuilder.CreateMapPoint(1.25, 1.75);
List<MapPoint> innerLinePts = new List<MapPoint>();
innerLinePts.Add(innerPt);
innerLinePts.Add(innerPt2);
// test an inner polyline
Polyline polyline = PolylineBuilder.CreatePolyline(innerLinePts);
contains = GeometryEngine.Contains(poly, polyline); // contains = true
// test a line that crosses the boundary
MapPoint outerPt = MapPointBuilder.CreateMapPoint(3, 1.5);
List<MapPoint> crossingLinePts = new List<MapPoint>();
crossingLinePts.Add(innerPt);
crossingLinePts.Add(outerPt);
polyline = PolylineBuilder.CreatePolyline(crossingLinePts);
contains = GeometryEngine.Contains(poly, polyline); // contains = false
// test a polygon in polygon
Envelope env = EnvelopeBuilder.CreateEnvelope(innerPt, innerPt2);
contains = GeometryEngine.Contains(poly, env); // contains = true
});
##Intersection between two Polylines
// methods need to run on the MCT
ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
// determine intersection between two polylines
List<MapPoint> pts = new List<MapPoint>();
pts.Add(MapPointBuilder.CreateMapPoint(1.0, 1.0));
pts.Add(MapPointBuilder.CreateMapPoint(3.0, 3.0));
pts.Add(MapPointBuilder.CreateMapPoint(5.0, 1.0));
Polyline line1 = PolylineBuilder.CreatePolyline(pts);
List<MapPoint> pts2 = new List<MapPoint>();
pts2.Add(MapPointBuilder.CreateMapPoint(1.0, 3.0));
pts2.Add(MapPointBuilder.CreateMapPoint(3.0, 1.0));
pts2.Add(MapPointBuilder.CreateMapPoint(5.0, 3.0));
Polyline line2 = PolylineBuilder.CreatePolyline(pts2);
bool intersects = GeometryEngine.Intersects(line1, line2); // intersects = true
Geometry g = GeometryEngine.Intersection(line1, line2, GeometryDimension.esriGeometry0Dimension);
Multipoint resultMultipoint = g as Multipoint;
// result is a multiPoint that intersects at (2,2) and (4,2)
});
##Intersection between two Polygons
// methods need to run on the MCT
ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
// determine intersection between two polygons
Envelope env1 = EnvelopeBuilder.CreateEnvelope(new Coordinate2D(3.0, 2.0), new Coordinate2D(6.0, 6.0));
Polygon poly1 = PolygonBuilder.CreatePolygon(env1);
Envelope env2 = EnvelopeBuilder.CreateEnvelope(new Coordinate2D(1.0, 1.0), new Coordinate2D(4.0, 4.0));
Polygon poly2 = PolygonBuilder.CreatePolygon(env2);
Polygon polyResult = GeometryEngine.Intersection(poly1, poly2) as Polygon;
});
##Move a MapPoint
// methods need to run on the MCT
ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
MapPoint pt = MapPointBuilder.CreateMapPoint(1.0, 3.0);
MapPoint ptResult = GeometryEngine.Move(pt, -3.5, 2.5) as MapPoint;
// ptResult is (-2.5, 5.5)
});
##Move a z-aware MapPoint
// methods need to run on the MCT
ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
MapPoint zPt = MapPointBuilder.CreateMapPoint(1.0, 3.0, 2.0);
MapPoint zPtResult = GeometryEngine.Move(zPt, 4, 0.25, 0.5) as MapPoint;
// zPtResult is (5.0, 3.25, 2.5);
});
##Project from WGS84 to WebMercator
// methods need to run on the MCT
ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
MapPoint pt = MapPointBuilder.CreateMapPoint(1.0, 3.0, SpatialReferences.WGS84);
Geometry result = GeometryEngine.Project(pt, SpatialReferences.WebMercator);
MapPoint projectedPt = result as MapPoint;
});
##Project from WGS84
// methods need to run on the MCT
ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
// create the polygon
List<MapPoint> pts = new List<MapPoint>();
pts.Add(MapPointBuilder.CreateMapPoint(1.0, 1.0, SpatialReferences.WGS84));
pts.Add(MapPointBuilder.CreateMapPoint(1.0, 2.0, SpatialReferences.WGS84));
pts.Add(MapPointBuilder.CreateMapPoint(2.0, 2.0, SpatialReferences.WGS84));
pts.Add(MapPointBuilder.CreateMapPoint(2.0, 1.0, SpatialReferences.WGS84));
Polygon polygon = PolygonBuilder.CreatePolygon(pts);
// ensure it is simple
bool isSimple = GeometryEngine.IsSimpleAsFeature(polygon);
// create the spatial reference to project to
SpatialReference northPole = SpatialReferenceBuilder.CreateSpatialReference(102018); // north pole stereographic
// project
Geometry g = GeometryEngine.Project(polygon, northPole);
});
##Determine relationship between two geometries
// methods need to run on the MCT
ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
// set up some geometries
// points
MapPoint point0 = MapPointBuilder.CreateMapPoint(0, 0, SpatialReferences.WGS84);
MapPoint point1 = MapPointBuilder.CreateMapPoint(1, 1, SpatialReferences.WGS84);
MapPoint point2 = MapPointBuilder.CreateMapPoint(-5, 5, SpatialReferences.WGS84);
// multipoint
List<MapPoint> points = new List<MapPoint>() { point0, point1, point2 };
Multipoint multipoint = MultipointBuilder.CreateMultipoint(points, SpatialReferences.WGS84);
// polygon
List<Coordinate2D> polygonCoords = new List<Coordinate2D>()
{
new Coordinate2D(-10, 0),
new Coordinate2D(0, 10),
new Coordinate2D(10, 0),
new Coordinate2D(-10, 0)
};
Polygon polygon = PolygonBuilder.CreatePolygon(polygonCoords, SpatialReferences.WGS84);
// polylines
Polyline polyline1 = PolylineBuilder.CreatePolyline(LineBuilder.CreateLineSegment(new Coordinate(-9.1, 0.1), new Coordinate(0, 9)), SpatialReferences.WGS84);
Polyline polyline2 = PolylineBuilder.CreatePolyline(LineBuilder.CreateLineSegment(new Coordinate(-5, 5), new Coordinate(0, 5)), SpatialReferences.WGS84);
Polyline polyline3 = PolylineBuilder.CreatePolyline(LineBuilder.CreateLineSegment(new Coordinate(2.09, -2.04), new Coordinate(5, 10)), SpatialReferences.WGS84);
Polyline polyline4 = PolylineBuilder.CreatePolyline(LineBuilder.CreateLineSegment(new Coordinate(10, -5), new Coordinate(10, 5)), SpatialReferences.WGS84);
List<Segment> segments = new List<Segment>()
{
LineBuilder.CreateLineSegment(new Coordinate(5.05, -2.87), new Coordinate(6.35, 1.57)),
LineBuilder.CreateLineSegment(new Coordinate(6.35, 1.57), new Coordinate(4.13, 2.59)),
LineBuilder.CreateLineSegment(new Coordinate(4.13, 2.59), new Coordinate(5, 5))
};
Polyline polyline5 = PolylineBuilder.CreatePolyline(segments, SpatialReferences.WGS84);
segments.Add(LineBuilder.CreateLineSegment(new Coordinate(5, 5), new Coordinate(10, 10)));
Polyline polyline6 = PolylineBuilder.CreatePolyline(segments, SpatialReferences.WGS84);
Polyline polyline7 = PolylineBuilder.CreatePolyline(polyline5);
Polyline polyline8 = PolylineBuilder.CreatePolyline(LineBuilder.CreateLineSegment(new Coordinate(5, 5), new Coordinate(10, 10)), SpatialReferences.WGS84);
segments.Clear();
segments.Add(LineBuilder.CreateLineSegment(new Coordinate(0.6, 3.5), new Coordinate(0.7, 7)));
segments.Add(LineBuilder.CreateLineSegment(new Coordinate(0.7, 7), new Coordinate(3, 9)));
Polyline polyline9 = PolylineBuilder.CreatePolyline(segments, SpatialReferences.WGS84);
// now do the Related tests
// Interior/Interior Intersects
string scl = "T********";
bool related = GeometryEngine.Relate(polygon, polyline1, scl); // related = true
related = GeometryEngine.Relate(point0, point1, scl); // related = false
related = GeometryEngine.Relate(point0, multipoint, scl); // related = true
related = GeometryEngine.Relate(multipoint, polygon, scl); // related = true
related = GeometryEngine.Relate(multipoint, polyline1, scl); // related = false
related = GeometryEngine.Relate(polyline2, point2, scl); // related = false
related = GeometryEngine.Relate(point1, polygon, scl); // related = true
// Interior/Boundary Intersects
scl = "*T*******";
related = GeometryEngine.Relate(polygon, polyline2, scl); // related = true
related = GeometryEngine.Relate(polygon, polyline3, scl); // related = false
related = GeometryEngine.Relate(point1, polygon, scl); // related = false
// Boundary/Boundary Interior intersects
scl = "***T*****";
related = GeometryEngine.Relate(polygon, polyline4, scl); // related = true
// Overlaps Dim1
scl = "1*T***T**";
related = GeometryEngine.Relate(polygon, polyline5, scl); // related = true
// Crosses Area/Line (LineB crosses PolygonA)
scl = "1020F1102";
related = GeometryEngine.Relate(polygon, polyline6, scl); // related = false
related = GeometryEngine.Relate(polygon, polyline9, scl); // related = true
// Boundary/Boundary Touches
scl = "F***T****";
related = GeometryEngine.Relate(polygon, polyline7, scl); // related = false
related = GeometryEngine.Relate(polygon, polyline8, scl); // related = true
});
##Rotate a MapPoint
// methods need to run on the MCT
ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
MapPoint pt = MapPointBuilder.CreateMapPoint(1.0, 3.0);
MapPoint rotatePt = MapPointBuilder.CreateMapPoint(3.0, 3.0);
Geometry result = GeometryEngine.Rotate(pt, rotatePt, Math.PI / 2);
// result point is (3, 1)
});
##Rotate a Polyline
// methods need to run on the MCT
ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
// rotate a polyline
MapPoint fixedPt = MapPointBuilder.CreateMapPoint(3.0, 3.0);
List<MapPoint> pts = new List<MapPoint>();
pts.Add(MapPointBuilder.CreateMapPoint(1.0, 1.0));
pts.Add(MapPointBuilder.CreateMapPoint(1.0, 5.0));
pts.Add(MapPointBuilder.CreateMapPoint(5, 5));
pts.Add(MapPointBuilder.CreateMapPoint(5.0, 1.0));
Polyline polyline = PolylineBuilder.CreatePolyline(pts);
Polyline rotated = GeometryEngine.Rotate(polyline, fixedPt, Math.PI / 4) as Polyline; // rotate 45 deg
});
##Polygon touches another Polygon
// methods need to run on the MCT
ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
// two disjoint polygons
Envelope env = EnvelopeBuilder.CreateEnvelope(MapPointBuilder.CreateMapPoint(4.0, 4.0), MapPointBuilder.CreateMapPoint(8, 8));
Polygon poly1 = PolygonBuilder.CreatePolygon(env);
Envelope env2 = EnvelopeBuilder.CreateEnvelope(MapPointBuilder.CreateMapPoint(1.0, 1.0), MapPointBuilder.CreateMapPoint(5, 5));
Polygon poly2 = PolygonBuilder.CreatePolygon(env2);
bool touches = GeometryEngine.Touches(poly1, poly2); // touches = false
// another polygon that touches the first
Envelope env3 = EnvelopeBuilder.CreateEnvelope(MapPointBuilder.CreateMapPoint(1.0, 1.0), MapPointBuilder.CreateMapPoint(4, 4));
Polygon poly3 = PolygonBuilder.CreatePolygon(env3);
touches = GeometryEngine.Touches(poly1, poly3); // touches = true
});
##Union two MapPoints - creates a Multipoint
// methods need to run on the MCT
ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
MapPoint pt1 = MapPointBuilder.CreateMapPoint(1.0, 1.0);
MapPoint pt2 = MapPointBuilder.CreateMapPoint(2.0, 2.5);
Geometry geometry = GeometryEngine.Union(pt1, pt2);
Multipoint multipoint = geometry as Multipoint; // multipoint has point count of 2
});
##Union two Polygons
// methods need to run on the MCT
ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
// union two polygons
List<MapPoint> polyPts = new List<MapPoint>();
polyPts.Add(MapPointBuilder.CreateMapPoint(3.0, 2.0));
polyPts.Add(MapPointBuilder.CreateMapPoint(3.0, 6.0));
polyPts.Add(MapPointBuilder.CreateMapPoint(6.0, 6.0));
polyPts.Add(MapPointBuilder.CreateMapPoint(6.0, 2.0));
Polygon poly1 = PolygonBuilder.CreatePolygon(polyPts);
bool isSimple = GeometryEngine.IsSimpleAsFeature(poly1);
Envelope env = EnvelopeBuilder.CreateEnvelope(MapPointBuilder.CreateMapPoint(4.0, 4.0), MapPointBuilder.CreateMapPoint(8, 8));
Polygon poly2 = PolygonBuilder.CreatePolygon(env);
isSimple = GeometryEngine.IsSimpleAsFeature(poly2);
Geometry g = GeometryEngine.Union(poly1, poly2);
Polygon polyResult = g as Polygon;
});
##Union many Polylines
// methods need to run on the MCT
ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
// union many polylines
List<Coordinate2D> coords = new List<Coordinate2D>()
{
new Coordinate2D(1, 2), new Coordinate2D(3, 4), new Coordinate2D(4, 2),
new Coordinate2D(5, 6), new Coordinate2D(7, 8), new Coordinate2D(8, 4),
new Coordinate2D(9, 10), new Coordinate2D(11, 12), new Coordinate2D(12, 8),
new Coordinate2D(10, 8), new Coordinate2D(12, 12), new Coordinate2D(14, 10)
};
// create Disjoint lines
List<Polyline> manyLines = new List<Polyline>
{
PolylineBuilder.CreatePolyline(new List<Coordinate2D>(){coords[0], coords[1], coords[2]}, SpatialReferences.WGS84),
PolylineBuilder.CreatePolyline(new List<Coordinate2D>(){coords[3], coords[4], coords[5]}),
PolylineBuilder.CreatePolyline(new List<Coordinate2D>(){coords[6], coords[7], coords[8]})
};
Polyline polyline = GeometryEngine.Union(manyLines) as Polyline;
});
##Union many Polygons
// methods need to run on the MCT
ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
// union many polygons
List<Coordinate> coordsZ = new List<Coordinate>()
{
new Coordinate(1, 2, 0), new Coordinate(3, 4, 1), new Coordinate(4, 2, 2),
new Coordinate(5, 6, 3), new Coordinate(7, 8, 4), new Coordinate(8, 4, 5),
new Coordinate(9, 10, 6), new Coordinate(11, 12, 7), new Coordinate(12, 8, 8),
new Coordinate(10, 8, 9), new Coordinate(12, 12, 10), new Coordinate(14, 10, 11)
};
// create polygons
List<Polygon> manyPolygonsZ = new List<Polygon>
{
PolygonBuilder.CreatePolygon(new List<Coordinate>(){coordsZ[0], coordsZ[1], coordsZ[2]}, SpatialReferences.WGS84),
PolygonBuilder.CreatePolygon(new List<Coordinate>(){coordsZ[3], coordsZ[4], coordsZ[5]}),
PolygonBuilder.CreatePolygon(new List<Coordinate>(){coordsZ[6], coordsZ[7], coordsZ[8]})
};
Polygon polygon = GeometryEngine.Union(manyPolygonsZ) as Polygon;
});
##MapPoints, Polylines, Polygons within Polygon
// methods need to run on the MCT
ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
// build a polygon
List<MapPoint> pts = new List<MapPoint>();
pts.Add(MapPointBuilder.CreateMapPoint(1.0, 1.0));
pts.Add(MapPointBuilder.CreateMapPoint(1.0, 2.0));
pts.Add(MapPointBuilder.CreateMapPoint(2.0, 2.0));
pts.Add(MapPointBuilder.CreateMapPoint(2.0, 1.0));
Polygon poly = PolygonBuilder.CreatePolygon(pts);
// an inner point
MapPoint innerPt = MapPointBuilder.CreateMapPoint(1.5, 1.5);
bool within = GeometryEngine.Within(innerPt, poly); // within = true
// point on a boundary
within = GeometryEngine.Within(pts[0], poly); // within = false
// an interior line
MapPoint innerPt2 = MapPointBuilder.CreateMapPoint(1.25, 1.75);
List<MapPoint> innerLinePts = new List<MapPoint>();
innerLinePts.Add(innerPt);
innerLinePts.Add(innerPt2);
Polyline polyline = PolylineBuilder.CreatePolyline(innerLinePts);
within = GeometryEngine.Within(polyline, poly); // within = true
// a line that crosses the boundary
MapPoint outerPt = MapPointBuilder.CreateMapPoint(3, 1.5);
List<MapPoint> crossingLinePts = new List<MapPoint>();
crossingLinePts.Add(innerPt);
crossingLinePts.Add(outerPt);
polyline = PolylineBuilder.CreatePolyline(crossingLinePts);
within = GeometryEngine.Within(polyline, poly); // within = false
// polygon in polygon
Envelope env = EnvelopeBuilder.CreateEnvelope(innerPt, innerPt2);
within = GeometryEngine.Within(env, poly); // within = true
});
##Create Geographic Transformation
GeographicTransformation gt1478 = ArcGIS.Core.Geometry.GeographicTransformation.Create(1478);
string name = gt1478.Name;
string wkt = gt1478.Wkt;
GeographicTransformation another_gt1478 = ArcGIS.Core.Geometry.GeographicTransformation.Create(wkt);
##Create Composite Geographic Transformation
// Create singleton from wkid
CompositeGeographicTransformation cgt = ArcGIS.Core.Geometry.CompositeGeographicTransformation.Create(108272);
int count = cgt.Count; // count = 1
IList<GeographicTransformation> gts = cgt.Transformations as IList<GeographicTransformation>;
gts.Add(ArcGIS.Core.Geometry.GeographicTransformation.Create(1437, false));
count = cgt.Count; // count = 2
cgt = ArcGIS.Core.Geometry.CompositeGeographicTransformation.Create(gts);
GeographicTransformation gt0 = cgt[0];
GeographicTransformation gt1 = cgt[1];
##Create Projection Transformation
// methods need to be on the MCT
ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
SpatialReference sr4267 = SpatialReferenceBuilder.CreateSpatialReference(4267);
SpatialReference sr4326 = SpatialReferences.WGS84;
SpatialReference sr3857 = SpatialReferences.WebMercator;
// Create transformation from 4267 -> 3857
ProjectionTransformation projTransFromSRs = ArcGIS.Core.Geometry.ProjectionTransformation.Create(sr4267, sr3857);
// create an envelope
Envelope env = EnvelopeBuilder.CreateEnvelope(new Coordinate2D(2, 2), new Coordinate2D(3, 3), sr4267);
// Project with one geo transform 4267 -> 3857
Envelope projectedEnvEx = GeometryEngine.ProjectEx(env, projTransFromSRs) as Envelope;
// Create inverse transformation, 3857 -> 4267
ProjectionTransformation projTransFromSRsInverse = ArcGIS.Core.Geometry.ProjectionTransformation.Create(sr3857, sr4267);
// Project the projected envelope back using the inverse transformation
Envelope projectedEnvBack = GeometryEngine.ProjectEx(projectedEnvEx, projTransFromSRsInverse) as Envelope;
bool isEqual = env.IsEqual(projectedEnvBack);
});
##MapPoint - GeoCoordinateString Conversion
// methods need to be on the MCT
ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>
{
SpatialReference sr = SpatialReferences.WGS84;
SpatialReference sr2 = SpatialReferences.WebMercator;
// create some points
MapPoint point0 = MapPointBuilder.CreateMapPoint(0, 0, sr);
MapPoint point1 = MapPointBuilder.CreateMapPoint(10, 20, sr);
MapPoint point2 = GeometryEngine.Project(point1, sr2) as MapPoint;
MapPoint pointEmpty = MapPointBuilder.CreateMapPoint(sr);
MapPoint pointwithNoSR = MapPointBuilder.CreateMapPoint(1, 1);
MapPoint pointZM = MapPointBuilder.CreateMapPoint(1, 2, 3, 4, sr);
// convert to MGRS
ToGeoCoordinateParameter mgrsParam = new ToGeoCoordinateParameter(GeoCoordinateType.MGRS);
string geoCoordString = point0.ToGeoCoordinateString(mgrsParam); // 31NAA6602100000
// use the builder to create a new point from the string. Coordinates are the same
MapPoint outPoint = MapPointBuilder.FromGeoCoordinateString(geoCoordString, sr, GeoCoordinateType.MGRS); // outPoint.x = 0; outPoint.Y = 0
geoCoordString = point1.ToGeoCoordinateString(mgrsParam); // 32QPH0460911794
outPoint = MapPointBuilder.FromGeoCoordinateString(geoCoordString, sr, GeoCoordinateType.MGRS); // outPoint.X = 10; outPoint.Y = 20
// z, m are not transformed
geoCoordString = pointZM.ToGeoCoordinateString(mgrsParam);
outPoint = MapPointBuilder.FromGeoCoordinateString(geoCoordString, sr, GeoCoordinateType.MGRS); // outPoint.X = 1; outPoint.Y = 2; outPoint.Z = Nan; outPoint.M = Nan;
// set the number of digits to 2 and convert
mgrsParam.NumDigits = 2;
geoCoordString = point1.ToGeoCoordinateString(mgrsParam); // 32QPH0512
outPoint = MapPointBuilder.FromGeoCoordinateString(geoCoordString, sr, GeoCoordinateType.MGRS); // outPoint.X = 10; outPoint.Y = 20
// convert to UTM
ToGeoCoordinateParameter utmParam = new ToGeoCoordinateParameter(GeoCoordinateType.UTM);
geoCoordString = point0.ToGeoCoordinateString(utmParam); // 31N 166021 0000000
geoCoordString = point1.ToGeoCoordinateString(utmParam); // 32Q 604609 2211793
// convert to DMS
ToGeoCoordinateParameter dmsParam = new ToGeoCoordinateParameter(GeoCoordinateType.DMS);
geoCoordString = point0.ToGeoCoordinateString(dmsParam); // 00 00 00.00N 000 00 00.00E
geoCoordString = point1.ToGeoCoordinateString(dmsParam); // 20 00 00.00N 010 00 00.00E
// convert to DDM
ToGeoCoordinateParameter ddmParam = new ToGeoCoordinateParameter(GeoCoordinateType.DDM);
geoCoordString = point0.ToGeoCoordinateString(ddmParam); // 00 00.0000N 000 00.0000E
geoCoordString = point1.ToGeoCoordinateString(ddmParam); // 20 00.0000N 010 00.0000E
// convert to DD
ToGeoCoordinateParameter ddParam = new ToGeoCoordinateParameter(GeoCoordinateType.DD);
geoCoordString = point0.ToGeoCoordinateString(ddParam); // 00.000000N 000.000000E
geoCoordString = point1.ToGeoCoordinateString(ddParam); // 20.000000N 010.000000E
});
##AngularUnit - Convert between degrees and radians
// convert 45 degrees to radians
double radians = AngularUnit.Degrees.ConvertToRadians(45);
// convert PI to degrees
double degrees = AngularUnit.Degrees.ConvertFromRadians(Math.PI);
##AngularUnit - Create an AngularUnit with a factory code
try
{
// create a Grad unit
var grad = AngularUnit.CreateAngularUnit(9105);
string unitName = grad.Name; // Grad
double conversionFactor = grad.ConversionFactor; // 0.015708
double radiansPerUnit = grad.RadiansPerUnit;
int factoryCode = grad.FactoryCode; // 9105
// convert 10 grads to degrees
double val = grad.ConvertTo(10, AngularUnit.Degrees);
// convert 10 radians to grads
val = grad.ConvertFromRadians(10);
}
catch (ArgumentException ex)
{
// ArgumentException will be thrown by CreateAngularUnit in the following scenarios
// - if the factory code used is a non-angular factory code (i.e. it corresponds to square meters which is an area unit code)
// - if the factory code used is invalid (i.e. it is negative or doesn't correspond to any factory code)
}
##AngularUnit - Create a Custom AngularUnit
// custom unit - 3 radians per unit
var myAngularUnit = AngularUnit.CreateAngularUnit("myCustomAngularUnit", 3);
string Name = myAngularUnit.Name; // myCustomAngularUnit
double Factor = myAngularUnit.ConversionFactor; // 3
int Code = myAngularUnit.FactoryCode; // 0 because it is a custom angular unit
double radiansUnit = myAngularUnit.RadiansPerUnit; // 3
// convert 10 degrees to my unit
double converted = AngularUnit.Degrees.ConvertTo(10, myAngularUnit);
// convert it back to degrees
converted = myAngularUnit.ConvertTo(converted, AngularUnit.Degrees);
// convert 1 radian into my angular units
converted = myAngularUnit.ConvertFromRadians(1);
##LinearUnit - Convert between feet and meters
// convert 10 feet to meters
double metres = LinearUnit.Feet.ConvertToMeters(10);
// convert 20 meters to feet
double feet = LinearUnit.Feet.ConvertFromMeters(20.0);
##LinearUnit - Convert between centimeters and millimeters
// convert 11 centimeters to millimeters
double mm = LinearUnit.Centimeters.ConvertTo(11, LinearUnit.Millimeters);
// convert the result back to centimeters
double cm = LinearUnit.Millimeters.ConvertTo(mm, LinearUnit.Centimeters);
// convert the millimeter result back to meters
double meters = LinearUnit.Millimeters.ConvertToMeters(mm);
##LinearUnit - Create a LinearUnit with a factory code
try
{
// create a british 1936 foot
var britFoot = LinearUnit.CreateLinearUnit(9095);
string unitName = britFoot.Name; // "Foot_British_1936"
double conversionFactor = britFoot.ConversionFactor; // 0.3048007491
double metersPerUnit = britFoot.MetersPerUnit;
int factoryCode = britFoot.FactoryCode; // 9095
// convert 10 british 1936 feet to centimeters
double val = britFoot.ConvertTo(10, LinearUnit.Centimeters);
// convert 10 m to british 1936 feet
val = britFoot.ConvertFromMeters(10);
}
catch (ArgumentException ex)
{
// ArgumentException will be thrown by CreateLinearUnit in the following scenarios
// - if the factory code used is a non-linear factory code (i.e. it corresponds to square meters which is an area unit code)
// - if the factory code used is invalid (i.e. it is negative or doesn't correspond to any factory code)
}
##LinearUnit - Create a Custom LinearUnit
// create a custom linear unit - there are 0.33 meters per myLinearUnit
var myLinearUnit = LinearUnit.CreateLinearUnit("myCustomLinearUnit", 0.33);
string name = myLinearUnit.Name; // myCustomLinearUnit
double convFactor = myLinearUnit.ConversionFactor; // 0.33
int code = myLinearUnit.FactoryCode; // 0 for custom units
double metersUnit = myLinearUnit.MetersPerUnit; // 0.33
string toString = myLinearUnit.ToString(); // same as Name - myCustomLinearUnit
// convert 10 centimeters to myLinearUnit
double convertedVal = LinearUnit.Centimeters.ConvertTo(10, myLinearUnit);
##LinearUnit - Convert to AreaUnit
try
{
// convert metres to area unit
AreaUnit areaUnit = LinearUnit.Meters.ConvertToAreaUnit();
double factor = areaUnit.ConversionFactor; // 1.0
string areaName = areaUnit.Name; // Square_Meter
UnitType unitType = areaUnit.UnitType; // UnitType.Area
// convert back again
LinearUnit linearUnit = LinearUnit.CreateFromAreaUnit(areaUnit);
}
catch (ArgumentException ex)
{
}
##AreaUnit - Convert between square feet and square meters
// convert 700 square meters to square feet
double sqFeet = AreaUnit.SquareFeet.ConvertFromSquareMeters(700);
// convert 1100 square feet to square meters
double sqMeters = AreaUnit.SquareFeet.ConvertToSquareMeters(1000);
##AreaUnit - Convert between hectares and acres
// convert 2 hectares to acres
double acres = AreaUnit.Hectares.ConvertTo(2, AreaUnit.Acres);
##AreaUnit - Convert between hectares and square miles
// convert 300 hectares to square miles
double sqMiles = AreaUnit.Hectares.ConvertTo(300, AreaUnit.SquareMiles);
##AreaUnit - Convert to LinearUnit
try
{
// convert square meters to linear unit
LinearUnit linearUnit = AreaUnit.SquareMeters.ConvertToLinearUnit();
double factor = linearUnit.ConversionFactor;
string linearName = linearUnit.Name;
UnitType unitType = linearUnit.UnitType;
// convert back again
AreaUnit areaUnit = AreaUnit.CreateFromLinearUnit(linearUnit);
double SqMetersPerUnit = areaUnit.SquareMetersPerUnit;
}
catch (ArgumentException ex)
{
}
Home | API Reference | Requirements | Download | Samples
-
Construct a SpatialReference - from a well-known ID
-
Construct a SpatialReference - from a string
-
Use WGS84 SpatialReference
-
Construct a SpatialReference with a vertical coordinate system - from well-known IDs
-
Construct a SpatialReference with a vertical coordinate system - from a string
-
Construct a SpatialReference with a custom PCS - from a string
-
SpatialReference Properties
-
SpatialReference WKT2
-
Import and Export Spatial Reference
-
Determine grid convergence for a SpatialReference at a given point
-
Datum
-
SpatialReference Datum and datum properties
-
Vector Polar Coordinates
-
Getting vector inclination
-
Getting vector azimuth
-
Vector Operations
-
2D Vector Operations
-
Construct a Polyline - from an enumeration of MapPoints
-
Get the points of a Polyline
-
Get the parts of a Polyline
-
Enumerate the parts of a Polyline
-
Reverse the order of points in a Polyline
-
Get the segments of a Polyline
-
Build a multi-part Polyline
-
StartPoint of a Polyline
-
Construct a Clothoid by Angle
-
Construct a Clothoid by Length
-
Split Polyline at distance
-
Create 3D Polyline and set Z-values while preserving curve segments
-
Construct a Polygon - from an enumeration of MapPoints
-
Construct a Polygon - from an Envelope
-
Get the points of a Polygon
-
Get the parts of a Polygon
-
Enumerate the parts of a Polygon
-
Get the segments of a Polygon
-
Build a donut polygon
-
Create an N-sided regular polygon
-
Get the exterior rings of a polygon - polygon.GetExteriorRing
-
Construct an Envelope
-
Construct an Envelope - from a JSON string
-
Union two Envelopes
-
Intersect two Envelopes
-
Expand an Envelope
-
Update Coordinates of an Envelope
-
Construct a Multipoint - from an enumeration of MapPoints
-
Construct a Multipoint - using MultipointBuilderEx
-
Modify the points of a Multipoint
-
Retrieve Points, 2D Coordinates, 3D Coordinates from a multipoint
-
Construct a Cubic Bezier - from Coordinates
-
Construct a Cubic Bezier - from MapPoints
-
Construct a Cubic Bezier - from an enumeration of MapPoints
-
Cubic Bezier Builder Properties
-
Cubic Bezier Properties
-
Construct a Polyline - from a Cubic Bezier
-
Construct a Circular Arc - using an interior point
-
Construct a Circular Arc - using a chord length and bearing
-
Construct a Circular Arc - using a center point, angle and radius
-
Construct an Elliptic Arc - using a center point and rotation angle
-
Construct a Circular Arc - using a center point and orientation
-
Construct a Circular Arc - using two segments and radius
-
Construct a Circle
-
Construct an Ellipse
-
Elliptic Arc Builder Properties
-
Elliptic Arc Properties
-
Construct GeometryBag
-
Construct GeometryBag - from an enumeration of geometries
-
Construct GeometryBag - from JSON, Xml
-
Construct GeometryBag - adding or inserting an enumeration of geometries
-
Construct Multipatch via Extrusion of Polygon or Polyline
-
Multipatch Properties
-
Construct Multipatch
-
Construct Multipatch via MultipatchBuilderEx
-
Construct Multipatch from another Multipatch
-
Construct Multipatch from a 3D model file
-
Construct 3D special Multipatch shapes
-
Create BasicMaterial
-
Create BasicMaterial with JPEG texture
-
Create BasicMaterial with Uncompressed texture
-
Get the texture image of a multipatch
-
Get the normal coordinate of a multipatch
-
Get the normals of a multipatch
-
Get the material properties of a multipatch
-
Import and Export Geometries to well-known Text
-
Import and Export Geometries to well-known Binary
-
Import and Export Geometries to EsriShape
-
Import and Export Geometries to JSON
-
Import and Export Geometries to XML
-
Create Geographic Transformation
-
Create Composite Geographic Transformation
-
Create Projection Transformation
-
Create HV Datum Transformation
-
Create Composite HV Datum Transformation
-
Determine Transformations
-
AngularUnit - Convert between degrees and radians
-
AngularUnit - Create an AngularUnit with a factory code
-
AngularUnit - Create a Custom AngularUnit