@@ -13,10 +13,10 @@ |
import com.mongodb.client.MongoDatabase; |
import com.mongodb.client.model.Filters; |
import javax.ws.rs.Path; |
-import oms3.annotations.*; |
import csip.ModelDataService; |
import csip.ServiceException; |
import csip.SessionLogger; |
+import csip.annotations.*; |
import java.io.FileNotFoundException; |
import java.io.IOException; |
import java.text.ParseException; |
@@ -43,6 +43,8 @@ |
*/ |
@Name("Timeseries query") |
@Description("Query a timeseries collection in mongo.") |
+@Author(org = "CSU") |
+@License(License.MIT) |
@Path("m/query/1.0") |
public class V1_0 extends ModelDataService { |
|
@@ -51,6 +53,7 @@ |
|
JSONArray results = new JSONArray(); |
|
+ |
protected void open(String mongoclienturi) { |
MongoClientURI u = new MongoClientURI(mongoclienturi); |
mongo = new MongoClient(u); |
@@ -82,7 +85,8 @@ |
} |
run(mongo_uri, mongo_collection, search_feature, search_id, start_date, end_date, metadata_only, LOG); |
} |
- |
+ |
+ |
public void run(String mongo_uri, String mongo_collection, JSONObject search_feature, String search_id, |
String start_date_str, String end_date_str, boolean metadata_only, SessionLogger LOG) throws JSONException { |
open(mongo_uri); |
@@ -109,7 +113,7 @@ |
c = collection.find(Filters.eq("_id", search_id)); |
} |
} |
- for (Document doc: c) { |
+ for (Document doc : c) { |
JSONObject res = getResult(doc, start_date, end_date, metadata_only); |
results.put(res); |
} |
@@ -125,7 +129,7 @@ |
Logger.getLogger(V1_0.class.getName()).log(Level.SEVERE, null, ex); |
} |
AggregateIterable<Document> c = collection.aggregate(agg_match); |
- for (Document doc: c) { |
+ for (Document doc : c) { |
JSONObject res = getResult(doc, start_date, end_date, metadata_only); |
results.put(res); |
} |
@@ -133,20 +137,21 @@ |
} |
} |
|
+ |
private JSONObject getResult(Document doc, LocalDate start_date, LocalDate end_date, boolean metadata_only) throws JSONException { |
- String date_fmt = (String)doc.get("date_fmt"); |
+ String date_fmt = (String) doc.get("date_fmt"); |
SimpleDateFormat ft = new SimpleDateFormat(date_fmt); |
ft.setTimeZone(TimeZone.getTimeZone("UTC")); |
|
- List<String> header = (ArrayList)doc.get("header"); |
- List<List<Object>> data = (ArrayList)doc.get("data"); |
+ List<String> header = (ArrayList) doc.get("header"); |
+ List<List<Object>> data = (ArrayList) doc.get("data"); |
JSONArray output = new JSONArray(); |
List<List<Object>> filtered = new ArrayList(); |
|
if (!metadata_only) { |
// Filter by period and format date |
- for (int irow=0; irow<data.size(); irow++) { |
- Date dt = (Date)data.get(irow).get(0); |
+ for (int irow = 0; irow < data.size(); irow++) { |
+ Date dt = (Date) data.get(irow).get(0); |
LocalDate dt_local = dt.toInstant().atZone(ZoneId.of("UTC")).toLocalDate(); |
if (start_date != null && dt_local.isBefore(start_date)) { |
continue; |
@@ -157,19 +162,19 @@ |
filtered.add(data.get(irow)); |
} |
} |
- |
+ |
JSONObject res = new JSONObject(); |
res.put("_id", doc.get("_id")); |
- Document location = (Document)doc.get("location"); |
- List<Double> coords = (List<Double>)location.get("coordinates"); |
+ Document location = (Document) doc.get("location"); |
+ List<Double> coords = (List<Double>) location.get("coordinates"); |
res.put("location", coords); |
res.put("header", header); |
if (!metadata_only) { |
res.put("data", filtered); |
} |
- Document metadata = (Document)doc.get("metadata"); |
+ Document metadata = (Document) doc.get("metadata"); |
res.put("metadata", new JSONObject(metadata.toJson())); |
- |
+ |
return res; |
} |
|
@@ -178,13 +183,14 @@ |
protected void postProcess() throws Exception { |
putResult("result", results); |
} |
- |
- public List<Bson> getMatch(JSONObject feat, String search_id) throws JSONException, IOException { |
- String geom = feat.getJSONObject("geometry").toString(); |
+ |
+ |
+ public List<Bson> getMatch(JSONObject feat, String search_id) throws JSONException, IOException { |
+ String geom = feat.getJSONObject("geometry").toString(); |
Document match_arg = new Document("location", |
- new Document("$geoWithin", |
- new Document("$geometry", Document.parse(geom)) |
- ) |
+ new Document("$geoWithin", |
+ new Document("$geometry", Document.parse(geom)) |
+ ) |
); |
if (search_id != null) { |
// Check for object ID (will be a hexidecimal string) |
@@ -201,50 +207,51 @@ |
agg_match.add(new_match); |
return agg_match; |
} |
- |
- public static void main(String [] args) throws FileNotFoundException, IOException, JSONException, ParseException { |
+ |
+ |
+ public static void main(String[] args) throws FileNotFoundException, IOException, JSONException, ParseException { |
V1_0 timeseries = new V1_0(); |
- JSONObject searchFeature = new JSONObject("{\n" + |
-" \"type\": \"FeatureCollection\",\n" + |
-" \"features\": [\n" + |
-" {\n" + |
-" \"geometry\": {\n" + |
-" \"type\": \"MultiPolygon\",\n" + |
-" \"coordinates\": [\n" + |
-" [\n" + |
-" [\n" + |
-" [\n" + |
-" -105.09928580027,\n" + |
-" 40.701406829918\n" + |
-" ],\n" + |
-" [\n" + |
-" -105.09447928172,\n" + |
-" 40.365324016903\n" + |
-" ],\n" + |
-" [\n" + |
-" -104.72849722611,\n" + |
-" 40.358522384503\n" + |
-" ],\n" + |
-" [\n" + |
-" -104.72437735306,\n" + |
-" 40.701927386425\n" + |
-" ],\n" + |
-" [\n" + |
-" -105.09928580027,\n" + |
-" 40.701406829918\n" + |
-" ]\n" + |
-" ]\n" + |
-" ]\n" + |
-" ]\n" + |
-" },\n" + |
-" \"type\": \"Feature\",\n" + |
-" \"properties\": {\n" + |
-" \"gid\": 1,\n" + |
-" \"name\": \"gid 1\"\n" + |
-" }\n" + |
-" }\n" + |
-" ]\n" + |
-" }"); |
+ JSONObject searchFeature = new JSONObject("{\n" |
+ + " \"type\": \"FeatureCollection\",\n" |
+ + " \"features\": [\n" |
+ + " {\n" |
+ + " \"geometry\": {\n" |
+ + " \"type\": \"MultiPolygon\",\n" |
+ + " \"coordinates\": [\n" |
+ + " [\n" |
+ + " [\n" |
+ + " [\n" |
+ + " -105.09928580027,\n" |
+ + " 40.701406829918\n" |
+ + " ],\n" |
+ + " [\n" |
+ + " -105.09447928172,\n" |
+ + " 40.365324016903\n" |
+ + " ],\n" |
+ + " [\n" |
+ + " -104.72849722611,\n" |
+ + " 40.358522384503\n" |
+ + " ],\n" |
+ + " [\n" |
+ + " -104.72437735306,\n" |
+ + " 40.701927386425\n" |
+ + " ],\n" |
+ + " [\n" |
+ + " -105.09928580027,\n" |
+ + " 40.701406829918\n" |
+ + " ]\n" |
+ + " ]\n" |
+ + " ]\n" |
+ + " ]\n" |
+ + " },\n" |
+ + " \"type\": \"Feature\",\n" |
+ + " \"properties\": {\n" |
+ + " \"gid\": 1,\n" |
+ + " \"name\": \"gid 1\"\n" |
+ + " }\n" |
+ + " }\n" |
+ + " ]\n" |
+ + " }"); |
SessionLogger LOG = new SessionLogger(); |
String search_id = null; |
timeseries.run("mongodb://eds0.engr.colostate.edu:27017/csip_timeseries", "test_coll", searchFeature, search_id, "2014-04-04", "2014-05-08", false, LOG); |