Find a country by coordinates:
Country country = session.CreateCriteria(typeof(Country))
.Add(SpatialExpression.Contains("Boundaries", new Point(-70.40, -33.24)))
.UniqueResult() as Country;
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:
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))))
.List();
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:
ProjectionQuery<County> query = new ProjectionQuery<County>(
Projections.ProjectionList()
.Add(Projections.GroupProperty("State"))
.Add(SpatialProjections.Union("Boundaries"))
);
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:
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;
session.Save(tracking);
}
More, please!
You also will find lots of sample usage code in the tests project (Tests.NHibernate.Spatial). Check out: