[NoRM MongoDB] Geospatial Features

16 views
Skip to first unread message

luke

unread,
Apr 21, 2010, 4:02:00 AM4/21/10
to NoRM mongodb
Hey guys,

I working on a project that is going to do some location based storage
and querying and I really want to give mongodb and norm a go.

Does Norm support any of these mongo geospacial features -
http://www.mongodb.org/display/DOCS/Geospatial+Indexing?

If not could I implement something like a distance based query using
the existing features of Norm?

--
You received this message because you are subscribed to the Google Groups "NoRM mongodb" group.
To post to this group, send email to norm-m...@googlegroups.com.
To unsubscribe from this group, send email to norm-mongodb...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/norm-mongodb?hl=en.

Sebastien Aube

unread,
Apr 21, 2010, 6:53:57 AM4/21/10
to NoRM mongodb
Here's come code that is working for me. I haven't had time to write
the unit tests and check it in. Sorry fot the code dump. Let me know
if you have any questions.




//NearQualifier

namespace Norm
{
/// <summary>
/// The within qualifier
/// </summary>
public class NearQualifier : QualifierCommand
{
/// <summary>
/// Initializes a new instance of the <see
cref="NearQualifier"/> class.
/// </summary>
/// <param name="center"></param>
public NearQualifier(LatLng center)
: base("$near", center.ToArray())
{

}
}
}

//STOP

using Norm.BSON;

namespace Norm
{

/// <summary>
/// Interface to limit classes for the Within Qualifier
/// </summary>
public interface IWhitinShapeQualifier
{

}

/// <summary>
/// The within qualifier
/// </summary>
public class WithinQualifier : QualifierCommand
{
/// <summary>
/// Initializes a new instance of the <see
cref="WithinQualifier"/> class.
/// </summary>
/// <param name="qualifier"></param>
public WithinQualifier(IWhitinShapeQualifier qualifier)
: base("$within", qualifier)
{

}
}

/// <summary>
/// Represents a coordinate
/// </summary>
public class LatLng {

/// <summary>
/// The latitude
/// </summary>
public double Latitude { get; set;}
/// <summary>
/// The longitude
/// </summary>
public double Longitude{get; set;}

/// <summary>
/// Creates an array of doubles
/// </summary>
/// <returns></returns>
public double[] ToArray(){
return new double[]{
this.Latitude ,
this.Longitude
};
}
}

/// <summary>
/// The Box Qualifier
/// </summary>
public class BoxQualifer : QualifierCommand,
IWhitinShapeQualifier
{
/// <summary>
/// Initializes a new instance of the <see cref="BoxQualifer"/
> class.
/// </summary>
/// <param name="northEast">The top right corner of the box</
param>
/// <param name="southWest">The bottom left corner of the box</
param>
public BoxQualifer(LatLng southWest, LatLng northEast)
: base("$box", new object[] { southWest.ToArray(),
northEast.ToArray() })
{

}
}

/// <summary>
/// The Circle Qualifier
/// </summary>
public class CircleQualifer : QualifierCommand,
IWhitinShapeQualifier
{
/// <summary>
/// Initializes a new instance of the <see
cref="CircleQualifer"/> class.
/// </summary>
/// <param name="center">The center point</param>
/// <param name="radius">The radius of the search</param>
public CircleQualifer(LatLng center, double radius )
: base("$center", new object [] { center.ToArray(),
radius })
{

}
}
}


//STOP

Add these to the Q.cs

/// <summary>
/// Builds a $near : [lat, lng] qualifier for the search.
/// </summary>
/// <param name="center">Center location</param>
/// <returns></returns>
public static NearQualifier Exists(LatLng center)
{
return new NearQualifier(center);
}

/// <summary>
/// Builds a $within {$box : [southwestnortheast, southwest]}
qualifier for the search.
/// </summary>
/// <param name="northEast">Top right</param>
/// <param name="southWest">Left bottom</param>
/// <returns></returns>
public static WithinQualifier WithinBox(LatLng southWest,
LatLng northEast)
{
IWhitinShapeQualifier shapeQualifier = new
BoxQualifer(southWest, northEast);
return new WithinQualifier(shapeQualifier);
}

/// <summary>
/// Builds a $within {$center : [center, radius]}qualifier
for the search.
/// </summary>
/// <param name="center">Center of the circle</param>
/// <param name="radius">Radius of the circle</param>
/// <returns></returns>
public static WithinQualifier WithinCircle(LatLng center,
double radius)
{
IWhitinShapeQualifier shapeQualifier = new
CircleQualifer(center, radius);
return new WithinQualifier(shapeQualifier);
}


//USAGE

var collection = _provider.DB.GetCollection<City>();


return collection.Find(new
{
loc = Q.WithinBox(
new LatLng() { Latitude = box.SouthWest.Latitude,
Longitude = box.SouthWest.Longitude },
new LatLng() { Latitude = box.NorthEast.Latitude,
Longitude = box.NorthEast.Longitude }
)
}).ToList();




On Apr 21, 5:02 am, luke <lukenlow...@gmail.com> wrote:
> Hey guys,
>
> I working on a project that is going to do some location based storage
> and querying and I really want to give mongodb and norm a go.
>
> Does Norm support any of these mongo geospacial features -http://www.mongodb.org/display/DOCS/Geospatial+Indexing?

Andrew Theken

unread,
Apr 21, 2010, 6:54:35 AM4/21/10
to luke, NoRM mongodb
The first part it so create a coordinate class:

public class Coordinate{
public double x{get;set;}
public double y{get;set;}
}

public class Bounds{
   public Coordinate UpperLeft{get;set;}
   public Coordinate LowerRight{get;set;}
}

public class RadialBounds{

   public Coordinate Center{get;set;}
   public double Radius{get;set;}
}

Next is to create the Qualifiers...
(Checkout the "Q" class, it should provide ample examples of what needs to happen.)

public static NearQualifier Q.Near(Coordinate epicenter);
public static WithinQualifier Q.Within(Bounds box);
public static WithinQualifier Q.Within(RadialBounds box);

And of course, tests (making sure they they conditionally run on v.1.3.3+ only)!!

It would be neat to provide hooks for SharpMap into MongoDB.

//Andrew Theken
Reply all
Reply to author
Forward
0 new messages