Extract geo keys field from MongoDB "NYC restaurant" collection by JAVA -
i use dataset nyc restaurants in mongodb. each restaurant has address example:
"address" : { "building" : "461", "coord" : [-74.138492, 40.631136], "street" : "port richmond ave", "zipcode" : "10302" }
i tried "coord"
pair of double
java code - not work list. data structure [-74.138492, 40.631136]
? want compare restaurant coordinate location find restaurants in specific range.
meantime i've found way "coord" fields double. code far perfect. if knows more elegant way - i'll glad know. "closestpoints" method calculate box of defined range around location. meantime prints out filtered coordinates.
//.... code.....
db db = mongoclient.getdb("test"); dbcollection coll = db.getcollection("restaurants"); basicdbobject query = new basicdbobject(); query.put("cuisine", "chinese"); dbcursor cursor = coll.find(query); //coordinates box of defined range around location. double lat1 = mypoint.closestpoints(1)[0]; double lat2 = mypoint.closestpoints(1)[1]; double lon1 = mypoint.closestpoints(1)[2]; double lon2 = mypoint.closestpoints(1)[3]; try { while(cursor.hasnext()) { basicbsonobject addressobj = (basicbsonobject) cursor.next().get("address"); basicdblist addresslist = (basicdblist) addressobj.get("coord"); double restlongitude = (double) addresslist.get(0); double restlatitude = (double) addresslist.get(1); if ((restlatitude < lat2 && restlatitude > lat1) && (restlongitude < lon2 && restlongitude > lon1)) { system.out.println(restlatitude + "," + restlongitude + " box lat " + lat1 + ", " + lat2 + " box lon " + lon1 + "," + lon2 ); } } } catch (nullpointerexception e) { system.err.println("nullpointerexception: " + e.getmessage()); } { cursor.close(); } mongoclient.close();
//... code ...
Comments
Post a Comment