source: josm/trunk/src/org/openstreetmap/josm/tools/OverpassTurboQueryWizard.java@ 8795

Last change on this file since 8795 was 8744, checked in by simon04, 9 years ago

see #11428 - Refactoring (class/package structure)

File size: 3.1 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.tools;
3
4import java.io.IOException;
5import java.io.InputStreamReader;
6import java.io.Reader;
7import java.nio.charset.StandardCharsets;
8import java.util.regex.Pattern;
9
10import javax.script.Invocable;
11import javax.script.ScriptEngine;
12import javax.script.ScriptEngineManager;
13import javax.script.ScriptException;
14
15/**
16 * Uses <a href="https://github.com/tyrasd/overpass-turbo/">Overpass Turbo</a> query wizard code
17 * to build an Overpass QL from a {@link org.openstreetmap.josm.actions.search.SearchAction} like query.
18 *
19 * Requires a JavaScript {@link ScriptEngine}.
20 * @since 8744
21 */
22public final class OverpassTurboQueryWizard {
23
24 private static OverpassTurboQueryWizard instance;
25 private final ScriptEngine engine = new ScriptEngineManager().getEngineByName("JavaScript");
26
27 /**
28 * An exception to indicate a failed parse.
29 */
30 public static class ParseException extends RuntimeException {
31 }
32
33 /**
34 * Replies the unique instance of this class.
35 *
36 * @return the unique instance of this class
37 */
38 public static synchronized OverpassTurboQueryWizard getInstance() {
39 if (instance == null) {
40 instance = new OverpassTurboQueryWizard();
41 }
42 return instance;
43 }
44
45 private OverpassTurboQueryWizard() {
46 // overpass-turbo is MIT Licensed
47
48 try (final Reader reader = new InputStreamReader(
49 getClass().getResourceAsStream("/data/overpass-turbo-ffs.js"), StandardCharsets.UTF_8)) {
50 //engine.eval("var turbo = {ffs: {noPresets: true}};");
51 engine.eval("var console = {log: function(){}};");
52 engine.eval(reader);
53 engine.eval("var construct_query = turbo.ffs().construct_query;");
54 } catch (ScriptException | IOException ex) {
55 throw new RuntimeException("Failed to initialize OverpassTurboQueryWizard", ex);
56 }
57 }
58
59 /**
60 * Builds an Overpass QL from a {@link org.openstreetmap.josm.actions.search.SearchAction} like query.
61 * @param search the {@link org.openstreetmap.josm.actions.search.SearchAction} like query
62 * @return an Overpass QL query
63 * @throws ParseException when the parsing fails
64 */
65 public String constructQuery(String search) throws ParseException {
66 try {
67 final Object result = ((Invocable) engine).invokeFunction("construct_query", search);
68 if (result == Boolean.FALSE) {
69 throw new ParseException();
70 }
71 String query = (String) result;
72 query = Pattern.compile("^.*\\[out:json\\]", Pattern.DOTALL).matcher(query).replaceFirst("");
73 query = Pattern.compile("^out.*", Pattern.MULTILINE).matcher(query).replaceAll("out meta;");
74 query = query.replace("({{bbox}})", "");
75 return query;
76 } catch (NoSuchMethodException e) {
77 throw new IllegalStateException();
78 } catch (ScriptException e) {
79 throw new RuntimeException("Failed to execute OverpassTurboQueryWizard", e);
80 }
81 }
82
83}
Note: See TracBrowser for help on using the repository browser.