Skip to content
This repository has been archived by the owner on Jan 21, 2022. It is now read-only.

Commit

Permalink
Differentiated GeoCoords to use two different Manifested Numerics for
Browse files Browse the repository at this point in the history
differing datatypes
  • Loading branch information
Brendan W. McAdams committed Dec 5, 2010
1 parent ddd4458 commit 4e71b49
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 21 deletions.
22 changes: 10 additions & 12 deletions casbah-query/src/main/scala/CoreOperators.scala
Original file line number Diff line number Diff line change
Expand Up @@ -745,32 +745,30 @@ trait GeospatialOps extends GeoNearOp
with GeoWithinOps


case class GeoCoords[T : Numeric : Manifest](val lat: T, val lon: T) {
case class GeoCoords[A : Numeric : Manifest, B : Numeric : Manifest]
(val lat: A, val lon: B) {
def toList = MongoDBList(lat, lon)

override def toString = "GeoCoords(%s, %s)".format(lat, lon)
}


/**
*
* Trait to provide the $near geospatial search method on appropriate callers
*
*
* Note that the args aren't TECHNICALLY latitude and longitude as they depend on:
* Note that the args aren't TECHNICALLY latitude and longitude as they depend on:
* a) the order you specified your actual index in
* b) if you're using actual world maps or something else
*
* Due to a quirk in the way I implemented type detection this fails if you mix numeric types. E.g. floats work, but not mixing floats and ints.
*
* This can be easily circumvented if you want 'ints' with floats by making your ints floats with .0:
*
* @author Brendan W. McAdams <brendan@10gen.com>
* @since 2.0
* @see http://www.mongodb.org/display/DOCS/Geospatial+Indexing
*/
trait GeoNearOp extends QueryOperator {
private val oper = "$near"

def $near(coords: GeoCoords[_]) = op(oper, coords.toList)
def $near(coords: GeoCoords[_, _]) = op(oper, coords.toList)
}

/**
Expand All @@ -793,7 +791,7 @@ trait GeoNearOp extends QueryOperator {
trait GeoNearSphereOp extends QueryOperator {
private val oper = "$nearSphere"

def $nearSphere(coords: GeoCoords[_]) = op(oper, coords.toList)
def $nearSphere(coords: GeoCoords[_,_]) = op(oper, coords.toList)
}

/**
Expand All @@ -820,13 +818,13 @@ trait GeoWithinOps extends QueryOperator {
val nestedOper = "$within"
val field = "$within"

def $box(lowerLeft: GeoCoords[_], upperRight: GeoCoords[_]) =
def $box(lowerLeft: GeoCoords[_,_], upperRight: GeoCoords[_,_]) =
op(oper, MongoDBList(lowerLeft.toList, upperRight.toList))

def $center[T : Numeric](center: GeoCoords[_], radius: T) =
def $center[T : Numeric](center: GeoCoords[_,_], radius: T) =
op(oper, MongoDBList(center.toList, radius))

def $centerSphere[T : Numeric](center: GeoCoords[_], radius: T) =
def $centerSphere[T : Numeric](center: GeoCoords[_,_], radius: T) =
op(oper, MongoDBList(center.toList, radius))
}

Expand Down
4 changes: 2 additions & 2 deletions casbah-query/src/main/scala/Implicits.scala
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ trait Implicits extends FluidQueryBarewordOps {
dbObj = Some(nested._2)
}

implicit def tupleToGeoCoords[T : Numeric : Manifest](coords: (T, T)) = GeoCoords(coords._1, coords._2)
implicit def tupleToGeoCoords[A : Numeric : Manifest, B : Numeric : Manifest](coords: (A, B)) = GeoCoords(coords._1, coords._2)



Expand All @@ -86,7 +86,7 @@ trait Imports extends BaseImports with TypeImports with Implicits with ValidDate
trait BaseImports

trait TypeImports {
type GeoCoords = com.mongodb.casbah.query.GeoCoords[_]
type GeoCoords = com.mongodb.casbah.query.GeoCoords[_,_]
}

trait ValidNumericType[T]
Expand Down
18 changes: 11 additions & 7 deletions casbah-query/src/test/scala/DSLCoreOperatorsSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1237,14 +1237,18 @@ class DSLCoreOperatorsSpec extends Specification with PendingUntilFixed with Log
typeOper must haveSuperClass[DBObject]
typeOper must beEqualTo(nonDSL("foo", "$type", org.bson.BSON.BINARY))
}
/*"Non-Binary Array" in {
val typeOper = "foo".$type[Array[_]]
typeOper must notBeNull
typeOper.toString must notBeNull
typeOper must haveSuperClass[DBObject]
typeOper must beEqualTo(nonDSL("foo", "$type", org.bson.BSON.ARRAY))

}*/
}

}


"Casbah's GeoSpatial Operators" should {
"Allow construction of GeoCoords" in {
"With two different numeric types" in {
val geo = GeoCoords(5.23, -123)
geo must notBeNull
}
}

}
Expand Down

0 comments on commit 4e71b49

Please sign in to comment.