GeometryEnvironment provides a way of creating geometries from different inputs and setting or getting global variables for controlling the behavior of geometry methods. It also provides Java and .NET friendly versions of methods originally defined on other geometry objects (see the IGeometryBridge and IGeometryBridge2 interfaces).
The GeometryEnvironment object is a singleton object, so calling new several times does not create a new object each time. Instead, it returns a reference to the existing GeometryEnvironment.
Using the geometry and spatial reference environments The following code example uses the IGeometryBridge2 interface on the GeometryEnvironment singleton object to define a polyline from an array of WKSPoint structures. It also uses the SpatialReferenceEnvironment singleton object to create a predefined projected coordinate system. Some of the concepts discussed in the previous introduction are used here.
[C#]
public IPolyline TestGeometryEnvironment()
{
ISpatialReferenceFactory spatialReferenceFactory = new SpatialReferenceEnvironmentClass();
//Create a projected coordinate system and define its domain, resolution, and x,y tolerance.
ISpatialReferenceResolution spatialReferenceResolution = spatialReferenceFactory.CreateProjectedCoordinateSystem((int)esriSRProjCSType.esriSRProjCS_NAD1983UTM_11N) as ISpatialReferenceResolution;
spatialReferenceResolution.ConstructFromHorizon();
ISpatialReferenceTolerance spatialReferenceTolerance = spatialReferenceResolution as ISpatialReferenceTolerance;
spatialReferenceTolerance.SetDefaultXYTolerance();
ISpatialReference spatialReference = spatialReferenceResolution as ISpatialReference;
//Create an array of WKSPoint structures starting in the middle of the x,y domain of the //projected coordinate system.double xMin;
double xMax;
double yMin;
double yMax;
spatialReference.GetDomain(out xMin, out xMax, out yMin, out yMax);
double xFactor = (xMin + xMax) * 0.5;
double yFactor = (yMin + yMax) * 0.5;
WKSPoint[] wksPoints = new WKSPoint[10];
for (int i = 0; i < wksPoints.Length; i++)
{
wksPoints[i].X = xFactor + i;
wksPoints[i].Y = yFactor + i;
}
IPointCollection4 pointCollection = new PolylineClass();
IGeometryBridge2 geometryBridge = new GeometryEnvironmentClass();
geometryBridge.AddWKSPoints(pointCollection, ref wksPoints);
IPolyline polyline = pointCollection as IPolyline;
polyline.SpatialReference = spatialReference;
return polyline;
}
[VB.NET]
PublicFunction TestGeometryEnvironment() As IPolyline
Dim spatialReferenceFactory As ISpatialReferenceFactory = New SpatialReferenceEnvironmentClass()
'Create a projected coordinate system and define its domain, resolution, and x,y tolerance.Dim spatialReferenceResolution As ISpatialReferenceResolution = spatialReferenceFactory.CreateProjectedCoordinateSystem(esriSRProjCSType.esriSRProjCS_NAD1983UTM_11N)
spatialReferenceResolution.ConstructFromHorizon()
Dim spatialReferenceTolerance As ISpatialReferenceTolerance = CType(spatialReferenceResolution, ISpatialReferenceTolerance)
spatialReferenceTolerance.SetDefaultXYTolerance()
Dim spatialReference As ISpatialReference = CType(spatialReferenceResolution, ISpatialReference)
'Create an array of WKSPoint structures starting in the middle of the x,y domain of the 'projected coordinate system.Dim xMin AsDoubleDim xMax AsDoubleDim yMin AsDoubleDim yMax AsDouble
spatialReference.GetDomain(xMin, xMax, yMin, yMax)
Dim xFactor AsDouble = (xMin + xMax) * 0.5
Dim yFactor AsDouble = (yMin + yMax) * 0.5
Dim wksPoints(9) As WKSPoint
For i AsInteger = 0 To wksPoints.Length - 1 Step 1
wksPoints(i).X = xFactor + i
wksPoints(i).Y = yFactor + i
Next i
Dim pointCollection As IPointCollection4 = New PolylineClass()
Dim geometryBridge As IGeometryBridge2 = New GeometryEnvironmentClass()
geometryBridge.AddWKSPoints(pointCollection, wksPoints)
Dim polyline As IPolyline = CType(pointCollection, IPolyline)
polyline.SpatialReference = spatialReference
Return polyline
EndFunction