Tuesday, November 4, 2008

Extending the IGeometryCollection type with methods provided on the HashSet(T) class

Author: Baxevanis, Nikos

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

“Extension methods are defined as static methods but are called by using instance method syntax. Their first parameter specifies which type the method operates on, and the parameter is preceded by the this modifier. Extension methods are only in scope when you explicitly import the namespace into your source code with a using directive.” http://msdn.microsoft.com/en-us/library/bb359438.aspx

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)
{
HashSet geomSet =
new HashSet();

foreach (Topology.Geometries.IGeometry geom in geomCol)
geomSet.Add(geom);

HashSet otherSet =
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);
}
Using the extension methods in your code

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