Overview
As of Kim Hamilton’s post, http://blogs.msdn.com/bclteam/archive/2006/11/09/introducing-hashset-t-kim-hamilton.aspx “HashSet determines equality according to the EqualityComparer you specify, or the default EqualityComparer for the type (if you didn’t specify)”.
The IGeometryCollection type implements the IComparable(T). Using extension methods we “add” methods from the HashSet(T) class so any type that implements the IGeometryCollection appears to have instance methods such as IntersectWith, UnionWith etc.
The IGeometryCollection type implements the IComparable(T).
A HashSet(T) operation modifies the set it’s called on and doesn’t create a new set. The extension methods provided with this article return an IGeometryCollection object rather than modifying the caller set.
Implementation
Using the extension methods in your code
public static Topology.Geometries.IGeometryCollection UnionWith(this Topology.Geometries.IGeometryCollection geomCol, Topology.Geometries.IGeometryCollection otherCol)
{
return Operation(geomCol, otherCol, OperationType.Union);
}
public static Topology.Geometries.IGeometryCollection IntersectWith(this Topology.Geometries.IGeometryCollection geomCol, Topology.Geometries.IGeometryCollection otherCol)
{
return Operation(geomCol, otherCol, OperationType.Intersection);
}
private static Topology.Geometries.IGeometryCollection Operation(Topology.Geometries.IGeometryCollection geomCol, Topology.Geometries.IGeometryCollection otherCol, OperationType typeOp)
{
HashSetgeomSet =
new HashSet();
foreach (Topology.Geometries.IGeometry geom in geomCol)
geomSet.Add(geom);
HashSetotherSet =
new HashSet();
foreach (Topology.Geometries.IGeometry geom in otherCol)
otherSet.Add(geom);
switch (typeOp)
{
case OperationType.Intersection:
geomSet.IntersectWith(otherSet);
break;
case OperationType.Union:
geomSet.UnionWith(otherSet);
break;
case OperationType.Except:
geomSet.ExceptWith(otherSet);
break;
case OperationType.SymmetricExcept:
geomSet.SymmetricExceptWith(otherSet);
break;
default:
return Topology.Geometries.GeometryCollection.Empty;
}
Topology.Geometries.IGeometry[] array =
new Topology.Geometries.IGeometry[geomSet.Count];
geomSet.CopyTo(array);
return new Topology.Geometries.GeometryFactory().CreateGeometryCollection(array);
}
You have to add a reference to Topology.Geometries.GeometryCollectionExtensions.dll
To use the HashSet(T) operations first bring them into scope with a using Topology.Geometries.GeometryCollectionExtensions directive.
Using the HashSet(T) operations
Available for download here: http://tf-net.googlecode.com/files/GeometryCollectionExtensions-Source.zip
No comments:
Post a Comment