Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
LINQPad IPolygon2.QueryExteriorRingsEx crash test (ArcGIS 10.1 SP1)
<Query Kind="Program">
<GACReference>ESRI.ArcGIS.Geometry, Version=10.1.0.0, Culture=neutral, PublicKeyToken=8fc3cc631e44ad86</GACReference>
<GACReference>ESRI.ArcGIS.System, Version=10.1.0.0, Culture=neutral, PublicKeyToken=8fc3cc631e44ad86</GACReference>
<GACReference>ESRI.ArcGIS.Version, Version=10.1.0.0, Culture=neutral, PublicKeyToken=8fc3cc631e44ad86</GACReference>
<Namespace>ESRI.ArcGIS.esriSystem</Namespace>
<Namespace>ESRI.ArcGIS.Geometry</Namespace>
</Query>
void Main()
{
if (ESRI.ArcGIS.RuntimeManager.ActiveRuntime == null)
ESRI.ArcGIS.RuntimeManager.BindLicense(ESRI.ArcGIS.ProductCode.EngineOrDesktop);
var polygon1 = CreateSinglePartPolygon().Dump();
var polygon2 = CreateDonutPolygon().Dump();
var polygon3 = CreateMultiPartPolygon().Dump();
PolygonRingsToPolylines(polygon1).Dump();
PolygonRingsToPolylines(polygon2).Dump();
PolygonRingsToPolylines(polygon3).Dump();
QueryExteriorRingsExTest(polygon1).Dump();
QueryExteriorRingsExTest(polygon2).Dump();
QueryExteriorRingsExTest(polygon3).Dump();
}
private static IEnumerable<IRing> QueryExteriorRingsExTest(IPolygon polygon)
{
var polygon2 = (IPolygon2)polygon;
IRing exteriorRings;
polygon2.QueryExteriorRingsEx(polygon2.ExteriorRingCount, out exteriorRings);
for (int i = 0; i < polygon2.ExteriorRingCount; i++)
{
yield return exteriorRings;
}
}
private static IEnumerable<IPolyline> PolygonRingsToPolylines(IPolygon polygon)
{
var polygon4 = (IPolygon4)polygon;
var exteriorRingGeometryBag = (IGeometryBag)polygon4.ExteriorRingBag;
var exteriorRingGeometryCollection = (IGeometryCollection)exteriorRingGeometryBag;
for (int i = 0; i < exteriorRingGeometryCollection.GeometryCount; i++)
{
var exteriorRingGeometry = exteriorRingGeometryCollection.get_Geometry(i);
yield return SegmentCollectionToPolyline((ISegmentCollection)exteriorRingGeometry);
var interiorRingGeometryBag = polygon4.get_InteriorRingBag((IRing)exteriorRingGeometry);
var interiorRingGeometryCollection = (IGeometryCollection)interiorRingGeometryBag;
for (int j = 0; j < interiorRingGeometryCollection.GeometryCount; j++)
{
var interiorRingGeometry = interiorRingGeometryCollection.get_Geometry(i);
yield return SegmentCollectionToPolyline((ISegmentCollection)interiorRingGeometry);
}
}
}
private static IPolyline SegmentCollectionToPolyline(ISegmentCollection segmentCollection)
{
var polyline = (IPolyline)new PolylineClass();
var geomcoll = (IGeometryCollection)polyline;
var pathcoll = (ISegmentCollection)new PathClass();
for (int i = 0; i < segmentCollection.SegmentCount; i++)
{
var segment = segmentCollection.get_Segment(i);
pathcoll.AddSegment(segment);
}
geomcoll.AddGeometry((IGeometry)pathcoll);
geomcoll.GeometriesChanged();
return polyline;
}
private static IPolygon CreateSinglePartPolygon()
{
var polygon = (IPolygon)new PolygonClass();
var pointcoll = (IPointCollection4)polygon;
var geometryBridge2 = (IGeometryBridge2)new GeometryEnvironmentClass();
var aWKSPointBuffer = new WKSPoint[5];
// Exterior ring
aWKSPointBuffer[0].X = 0;
aWKSPointBuffer[0].Y = 0;
aWKSPointBuffer[1].X = 0;
aWKSPointBuffer[1].Y = 1;
aWKSPointBuffer[2].X = 1;
aWKSPointBuffer[2].Y = 1;
aWKSPointBuffer[3].X = 1;
aWKSPointBuffer[3].Y = 0;
aWKSPointBuffer[4].X = 0;
aWKSPointBuffer[4].Y = 0;
geometryBridge2.AddWKSPoints(pointcoll, ref aWKSPointBuffer);
var topoOp = (ITopologicalOperator2)polygon;
topoOp.IsKnownSimple_2 = false;
topoOp.Simplify();
return polygon;
}
private static IPolygon CreateDonutPolygon()
{
var polygon = (IPolygon)new PolygonClass();
var pointcoll = (IPointCollection4)polygon;
var geometryBridge2 = (IGeometryBridge2)new GeometryEnvironmentClass();
var aWKSPointBuffer = new WKSPoint[5];
// Exterior ring
aWKSPointBuffer[0].X = 0;
aWKSPointBuffer[0].Y = 0;
aWKSPointBuffer[1].X = 0;
aWKSPointBuffer[1].Y = 4;
aWKSPointBuffer[2].X = 4;
aWKSPointBuffer[2].Y = 4;
aWKSPointBuffer[3].X = 4;
aWKSPointBuffer[3].Y = 0;
aWKSPointBuffer[4].X = 0;
aWKSPointBuffer[4].Y = 0;
geometryBridge2.AddWKSPoints(pointcoll, ref aWKSPointBuffer);
// Interior ring
aWKSPointBuffer[0].X = 1;
aWKSPointBuffer[0].Y = 1;
aWKSPointBuffer[1].X = 3;
aWKSPointBuffer[1].Y = 1;
aWKSPointBuffer[2].X = 3;
aWKSPointBuffer[2].Y = 3;
aWKSPointBuffer[3].X = 1;
aWKSPointBuffer[3].Y = 3;
aWKSPointBuffer[4].X = 1;
aWKSPointBuffer[4].Y = 1;
geometryBridge2.AddWKSPoints(pointcoll, ref aWKSPointBuffer);
var topoOp = (ITopologicalOperator2)polygon;
topoOp.IsKnownSimple_2 = false;
topoOp.Simplify();
return polygon;
}
private static IPolygon CreateMultiPartPolygon()
{
var polygon = (IPolygon)new PolygonClass();
var pointcoll = (IPointCollection4)polygon;
var geometryBridge2 = (IGeometryBridge2)new GeometryEnvironmentClass();
var aWKSPointBuffer = new WKSPoint[5];
// Exterior ring 1
aWKSPointBuffer[0].X = 0;
aWKSPointBuffer[0].Y = 0;
aWKSPointBuffer[1].X = 0;
aWKSPointBuffer[1].Y = 1;
aWKSPointBuffer[2].X = 1;
aWKSPointBuffer[2].Y = 1;
aWKSPointBuffer[3].X = 1;
aWKSPointBuffer[3].Y = 0;
aWKSPointBuffer[4].X = 0;
aWKSPointBuffer[4].Y = 0;
geometryBridge2.AddWKSPoints(pointcoll, ref aWKSPointBuffer);
// Exterior ring 2
aWKSPointBuffer[0].X = 2;
aWKSPointBuffer[0].Y = 2;
aWKSPointBuffer[1].X = 2;
aWKSPointBuffer[1].Y = 3;
aWKSPointBuffer[2].X = 3;
aWKSPointBuffer[2].Y = 3;
aWKSPointBuffer[3].X = 3;
aWKSPointBuffer[3].Y = 2;
aWKSPointBuffer[4].X = 2;
aWKSPointBuffer[4].Y = 2;
geometryBridge2.AddWKSPoints(pointcoll, ref aWKSPointBuffer);
var topoOp = (ITopologicalOperator2)polygon;
topoOp.IsKnownSimple_2 = false;
topoOp.Simplify();
return polygon;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.