Spacio Temporal Query

147 views
Skip to first unread message

Ronnie

unread,
Apr 23, 2012, 6:46:35 AM4/23/12
to mongodb-user
Hi,

Can any one provide a spatio - temporal query with in java from Mongo
db.

For example :

Suppose I define a polygon with certain co ordinates(Lat/Long)

Now if I want to know how many objects are inside that polygon , for a
certain date range?

Suppose this is my dataset :

{ "_id" : ObjectId("4f952458be050de430fc445a"), "timestamp" : "Time
instant: 2011-10-02T22:14:16.000+05:30,null", "procedure_id" :
"Test_25201", "feature_of_interest_id" : "TestRoom", "feature_type" :
"sa:SamplingPoint", "feature_of_interest_description"
: "", "feature_of_interest_name" : "Room1", "foi_geom" : [ 8.3,
53.15 ], "srid" : 4326, "phenomenon_id" : "urn:ogc:def:phenomenon:OGC:
1/0/30:temp", "offering_id" : "ROOM_TEMP", "value_type" :
"numeric_value", "mime_type" : null, "observation_id" :
16 }

Thanks in advance.

Marc

unread,
Apr 23, 2012, 3:20:36 PM4/23/12
to mongodb-user
It is possible to combine a geospatial query with a date range
query.
Here is an example, performed in the JS shell:

> db.points.save({_id:1, pos:[1,1], date:new Date("4/16/2012")});
> db.points.save({_id:2, pos:[2,1], date:new Date("4/21/2012")});
> db.points.save({_id:3, pos:[3,1], date:new Date("4/22/2012")});
> db.points.save({_id:4, pos:[5,1], date:new Date("4/22/2012")});
> db.points.ensureIndex({pos:"2d"})
> myPolygon = [[0,0], [0,4], [2,6], [4,4], [4,0]]
> db.points.find({ "pos" : { "$within" : { "$polygon" : myPolygon } }, date:{$gt:new Date("4/20/2012"), $lt:new Date("4/23/2012")} })
{ "_id" : 2, "pos" : [ 2, 1 ], "date" :
ISODate("2012-04-21T04:00:00Z") }
{ "_id" : 3, "pos" : [ 3, 1 ], "date" :
ISODate("2012-04-22T04:00:00Z") }

Only points with _id:2, and _id:3 are returned, because they are
inside of the polygon and match the given date range.

The documentation on bounds queries may be found on the "Geospatial
Indexing" page.
http://www.mongodb.org/display/DOCS/Geospatial+Indexing#GeospatialIndexing-BoundsQueries

Ronnie

unread,
Apr 24, 2012, 12:44:52 AM4/24/12
to mongodb-user
Thanks .

Can this be done in Java?
Can you provide some examples?

Thanks again!
> Indexing" page.http://www.mongodb.org/display/DOCS/Geospatial+Indexing#GeospatialInd...

Marc

unread,
Apr 24, 2012, 4:22:24 PM4/24/12
to mongodb-user
The following will hopefully get you started writing your query using
the Java Driver:

public static void main(String[] args) {
// TODO Auto-generated method stub
Mongo m = null;
try {
m = new Mongo("localhost", 27017);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MongoException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DB db = m.getDB("test");
DBCollection coll = db.getCollection("points");
//myPolygon = [[0,0], [0,4], [2,6], [4,4], [4,0]];

ArrayList<ArrayList<Integer>> myPolygon = new
ArrayList<ArrayList<Integer>>();
ArrayList<Integer> point1 = new ArrayList<Integer>();
point1.add(0);
point1.add(0);
ArrayList<Integer> point2 = new ArrayList<Integer>();
point2.add(0);
point2.add(4);
ArrayList<Integer> point3 = new ArrayList<Integer>();
point3.add(2);
point3.add(6);
ArrayList<Integer> point4 = new ArrayList<Integer>();
point4.add(4);
point4.add(4);
ArrayList<Integer> point5 = new ArrayList<Integer>();
point5.add(4);
point5.add(0);
myPolygon.add(point1);
myPolygon.add(point2);
myPolygon.add(point3);
myPolygon.add(point4);
myPolygon.add(point5);
System.out.println(myPolygon.toString());
BasicDBObject myQuery = new BasicDBObject();
BasicDBObject withinQuery = new BasicDBObject();
BasicDBObject dateQuery = new BasicDBObject();
withinQuery.append("$within", new BasicDBObject("$polygon",
myPolygon));
dateQuery.append("$gt", new Double("1334894400000"));//From JS Shell:
new Date("4/20/2012").valueOf() = 1334894400000
dateQuery.append("$lt", new Double("1335153600000"));//From JS Shell:
new Date("4/23/2012").valueOf() = 1335153600000
myQuery.append("pos", withinQuery);
myQuery.append("date", dateQuery);
System.out.println(myQuery.toString());
DBCursor c = coll.find(myQuery);
while(c.hasNext()){
System.out.println(c.next().toString());
}
}

The output from the above is as follows:

[[0, 0], [0, 4], [2, 6], [4, 4], [4, 0]]
{ "pos" : { "$within" : { "$polygon" : [ [ 0 , 0] , [ 0 , 4] , [ 2 ,
6] , [ 4 , 4] , [ 4 , 0]]}} , "date" : { "$gt" : 1.3348944E12 ,
"$lt" : 1.3351536E12}}
{ "_id" : 2.0 , "pos" : [ 2.0 , 1.0] , "date" : 1.3349808E12}
{ "_id" : 3.0 , "pos" : [ 3.0 , 1.0] , "date" : 1.3350672E12}

For convenience, I converted the dates into seconds since the Epoch.
If collections are created with one language and queried with another
language (in this instance I created the collection with the JS shell)
converting data types can be tricky. The ISODate objects created by
the JavaScript shell are not the same as Date objects created in Java,
and cannot be directly compared. If you are creating your documents
in Java and querying them in Java, this should not be an issue for
you.
Reply all
Reply to author
Forward
0 new messages