The object-relational mapper for .NET


This page is converted from the old nhforge.org Wiki. First published by: Ricardo Stuven on 09-26-2008, Last revision by: Ricardo Stuven on 10-01-2008

Sample usage

Find a country by coordinates:

  • Using NHibernate:

Country country = session.CreateCriteria(typeof(Country))
    .Add(SpatialExpression.Contains("Boundaries", new Point(-70.40, -33.24)))
    .UniqueResult() as Country;

  • Using ActiveRecord:

Country country = Country.FindOne(
    SpatialExpression.Contains("Boundaries", new Point(-70.40, -33.24))


Find all the towns in a bounding box, except the one located at some coordinates:

  • Using NHibernate:
IList towns = session.CreateCriteria(typeof(Town))
    .Add(SpatialExpression.Filter("Boundaries", new Envelope(-70, -68, -32, -34)))
    .Add(Expression.Not(SpatialExpression.Contains("Boundaries", new Point(-70.40, -33.24))))
  • Using ActiveRecord:

Town[] towns = Town.FindAll(
    SpatialExpression.Filter("Boundaries", new Envelope(-70, -68, -32, -34)),
    Expression.Not(SpatialExpression.Contains("Boundaries", new Point(-70.40, -33.24)))

The previous example just shows how to combine a spatial criterion (Contains) with a normal criterion (Not), but in this case we could use simply Disjoint.


Get states boundaries grouping counties by state:

  • Using ActiveRecord:
ProjectionQuery<County> query = new ProjectionQuery<County>(
IList<object[]> stateBoundaries= query.Execute();

Note: For people with a GIS backgroud, the "projection" term could be confusing. In this context, it has nothing to do with cartographic projections. It's rather a term borrowed from relational algebra.

Create a new vehicle GPS position:

  • Using NHibernate:
public void TrackVehicule(Vehicle vehicule, double longitude, double latitude, DateTime when)
    Tracking tracking = new Tracking();
    tracking.Vehicle = vehicle;
    tracking.Position = new Point(longitude, latitude);
    tracking.When = when;


More, please!

You also will find lots of sample usage code in the tests project (Tests.NHibernate.Spatial). Check out:


© NHibernate Community 2022