Changeset 9704 in josm
- Timestamp:
- 2016-02-01T16:16:17+01:00 (9 years ago)
- Location:
- trunk
- Files:
-
- 1 added
- 1 deleted
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/tools/OverpassTurboQueryWizard.java
r9385 r9704 1 1 // License: GPL. For details, see LICENSE file. 2 2 package org.openstreetmap.josm.tools; 3 4 import org.openstreetmap.josm.Main; 3 5 4 6 import java.io.IOException; … … 6 8 import java.io.Reader; 7 9 import java.nio.charset.StandardCharsets; 8 import java.util. regex.Pattern;10 import java.util.HashMap; 9 11 10 12 import javax.script.Invocable; … … 14 16 15 17 /** 16 * Uses <a href="https://github.com/tyrasd/overpass- turbo/">Overpass Turbo</a> query wizard code18 * Uses <a href="https://github.com/tyrasd/overpass-wizard/">Overpass Turbo query wizard</a> code (MIT Licensed) 17 19 * to build an Overpass QL from a {@link org.openstreetmap.josm.actions.search.SearchAction} like query. 18 20 * … … 38 40 39 41 private OverpassTurboQueryWizard() { 40 // overpass-turbo is MIT Licensed41 42 42 try (final Reader reader = new InputStreamReader( 43 getClass().getResourceAsStream("/data/overpass-turbo-ffs.js"), StandardCharsets.UTF_8)) { 44 engine.eval("var console = {log: function(){}};"); 43 getClass().getResourceAsStream("/data/overpass-wizard.js"), StandardCharsets.UTF_8)) { 44 engine.eval("var console = {error: " + Main.class.getCanonicalName() + ".warn};"); 45 engine.eval("var global = {};"); 45 46 engine.eval(reader); 46 engine.eval("var construct_query = turbo.ffs().construct_query;");47 47 } catch (ScriptException | IOException ex) { 48 48 throw new RuntimeException("Failed to initialize OverpassTurboQueryWizard", ex); … … 58 58 public String constructQuery(String search) throws UncheckedParseException { 59 59 try { 60 final Object result = ((Invocable) engine).invokeFunction("construct_query", search); 60 final Object result = ((Invocable) engine).invokeMethod(engine.get("global"), 61 "overpassWizard", search, new HashMap<String, Object>() {{ 62 put("comment", false); 63 put("outputFormat", "xml"); 64 put("outputMode", "recursive_meta"); 65 }}); 61 66 if (result == Boolean.FALSE) { 62 67 throw new UncheckedParseException(); 63 68 } 64 69 String query = (String) result; 65 query = Pattern.compile("^.*\\[out:json\\]", Pattern.DOTALL).matcher(query).replaceFirst(""); 66 query = Pattern.compile("^out.*", Pattern.MULTILINE).matcher(query).replaceAll("out meta;"); 67 query = query.replace("({{bbox}})", ""); 70 query = query.replace("[bbox:{{bbox}}]", ""); 68 71 return query; 69 72 } catch (NoSuchMethodException e) { -
trunk/test/unit/org/openstreetmap/josm/tools/OverpassTurboQueryWizardTest.java
r9669 r9704 18 18 @BeforeClass 19 19 public static void setUp() { 20 JOSMFixture.createUnitTestFixture().init( true);20 JOSMFixture.createUnitTestFixture().init(false); 21 21 OverpassTurboQueryWizard.getInstance(); 22 22 } … … 29 29 final String query = OverpassTurboQueryWizard.getInstance().constructQuery("amenity=drinking_water"); 30 30 assertEquals("" + 31 "[timeout:25];\n" + 32 "// gather results\n" + 31 "[out:xml][timeout:25];\n" + 33 32 "(\n" + 34 " // query part for: “amenity=drinking_water”\n" +35 33 " node[\"amenity\"=\"drinking_water\"];\n" + 36 34 " way[\"amenity\"=\"drinking_water\"];\n" + 37 35 " relation[\"amenity\"=\"drinking_water\"];\n" + 38 36 ");\n" + 39 "// print results\n" + 40 "out meta;\n" + 41 ">;\n" + 37 "(._;>;);\n" + 42 38 "out meta;", query); 43 39 }
Note:
See TracChangeset
for help on using the changeset viewer.