Skip to content

v0.2.48..v0.2.49 changeset PullOverpassCommand.java

Garret Voltz edited this page Oct 2, 2019 · 1 revision
diff --git a/hoot-services/src/main/java/hoot/services/controllers/grail/PullOverpassCommand.java b/hoot-services/src/main/java/hoot/services/controllers/grail/PullOverpassCommand.java
index 66235a8..982644c 100644
--- a/hoot-services/src/main/java/hoot/services/controllers/grail/PullOverpassCommand.java
+++ b/hoot-services/src/main/java/hoot/services/controllers/grail/PullOverpassCommand.java
@@ -26,10 +26,15 @@
  */
 package hoot.services.controllers.grail;
 
+import static hoot.services.HootProperties.GRAIL_OVERPASS_QUERY;
+import static hoot.services.HootProperties.HOME_FOLDER;
+import static hoot.services.HootProperties.PUBLIC_OVERPASS_URL;
 import static hoot.services.HootProperties.replaceSensitiveData;
 
 import java.io.File;
+import java.io.UnsupportedEncodingException;
 import java.net.URL;
+import java.net.URLEncoder;
 import java.time.LocalDateTime;
 
 import javax.ws.rs.WebApplicationException;
@@ -82,38 +87,55 @@ class PullOverpassCommand implements InternalCommand {
     private void getOverpass() {
         String url = "";
         try {
-                BoundingBox boundingBox = new BoundingBox(params.getBounds());
-
-    // Compact QL
-    // https://overpass-api.de/api/interpreter?data=(node(-34.0044,150.9982,-33.9728,151.0656);<;>;);out meta qt;
-
-    // XML
-    // <osm-script>
-    //   <union into="_">
-    //     <bbox-query s="-34.0044" w="150.9982" n="-33.9728" e="151.0656"/>
-    //     <recurse from="_" into="_" type="up"/>
-    //     <recurse from="_" into="_" type="down"/>
-    //   </union>
-    //   <print e="" from="_" geometry="skeleton" ids="yes" limit="" mode="meta" n="" order="quadtile" s="" w=""/>
-    // </osm-script>
-
-                // This is Ugly! It is the encoded version of the compact QL script above
-                url = replaceSensitiveData(params.getPullUrl()) +
-                    "/api/interpreter?data=(node(" +
-                    boundingBox.getMinLat() + "%2C" +
-                    boundingBox.getMinLon() + "%2C" +
-                    boundingBox.getMaxLat() + "%2C" +
-                    boundingBox.getMaxLon() + ")%3B%3C%3B%3E%3B)%3Bout%20meta%20qt%3B";
-
-                URL requestUrl = new URL(url);
-                File outputFile = new File(params.getOutput());
-
-                FileUtils.copyURLToFile(requestUrl,outputFile, Integer.parseInt(HootProperties.HTTP_TIMEOUT), Integer.parseInt(HootProperties.HTTP_TIMEOUT));
-            }
-            catch (Exception ex) {
-                String msg = "Failure to pull data from Overpass [" + url + "]" + ex.getMessage();
-                // throw new RuntimeException(msg, ex);
-                throw new WebApplicationException(ex, Response.serverError().entity(msg).build());
-            }
+            url = replaceSensitiveData(getOverpassUrl(params.getBounds(), "xml"));
+
+            URL requestUrl = new URL(url);
+            File outputFile = new File(params.getOutput());
+
+            FileUtils.copyURLToFile(requestUrl,outputFile, Integer.parseInt(HootProperties.HTTP_TIMEOUT), Integer.parseInt(HootProperties.HTTP_TIMEOUT));
+        }
+        catch (Exception ex) {
+            String msg = "Failure to pull data from Overpass [" + url + "]" + ex.getMessage();
+            throw new WebApplicationException(ex, Response.serverError().entity(msg).build());
+        }
+    }
+
+    /**
+     * Returns the overpass query, with the expected output format set to json
+     * @param bbox
+     * @return
+     */
+    static String getOverpassUrl(String bbox) {
+        return getOverpassUrl(bbox, "json");
+    }
+
+    /**
+     * Returns the overpass query, with the expected output format set to json
+     * @param bbox
+     * @param outputFormat if set to 'xml' then the output of the returned query, when run, will be xml. json is the default if non xml is specified
+     * @return
+     */
+    static String getOverpassUrl(String bbox, String outputFormat) {
+        // Get grail overpass query from the file and store it in a string
+        String overpassQuery;
+        File overpassQueryFile = new File(HOME_FOLDER, GRAIL_OVERPASS_QUERY);
+        try {
+            overpassQuery = FileUtils.readFileToString(overpassQueryFile, "UTF-8");
+        } catch(Exception exc) {
+            throw new IllegalArgumentException("Grail pull overpass error. Couldn't read overpass query file: " + overpassQueryFile.getName());
+        }
+
+        //replace the {{bbox}} from the overpass query with the actual coordinates and encode the query
+        overpassQuery = overpassQuery.replace("{{bbox}}", new BoundingBox(bbox).toOverpassString());
+
+        if (outputFormat.equals("xml")) {
+            overpassQuery = overpassQuery.replace("out:json", "out:xml"); // Need this because the rails pull data is also xml
+        }
+
+        try {
+            overpassQuery = URLEncoder.encode(overpassQuery, "UTF-8").replace("+", "%20");
+        } catch (UnsupportedEncodingException ignored) {} // Can be safely ignored because UTF-8 is always supported
+
+        return PUBLIC_OVERPASS_URL + "/api/interpreter?data=" + overpassQuery;
     }
 }
Clone this wiki locally