Changeset 9704 in josm for trunk/src


Ignore:
Timestamp:
2016-02-01T16:16:17+01:00 (9 years ago)
Author:
simon04
Message:

fix #12469 - Update overpass wizard code

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/tools/OverpassTurboQueryWizard.java

    r9385 r9704  
    11// License: GPL. For details, see LICENSE file.
    22package org.openstreetmap.josm.tools;
     3
     4import org.openstreetmap.josm.Main;
    35
    46import java.io.IOException;
     
    68import java.io.Reader;
    79import java.nio.charset.StandardCharsets;
    8 import java.util.regex.Pattern;
     10import java.util.HashMap;
    911
    1012import javax.script.Invocable;
     
    1416
    1517/**
    16  * Uses <a href="https://github.com/tyrasd/overpass-turbo/">Overpass Turbo</a> query wizard code
     18 * Uses <a href="https://github.com/tyrasd/overpass-wizard/">Overpass Turbo query wizard</a> code (MIT Licensed)
    1719 * to build an Overpass QL from a {@link org.openstreetmap.josm.actions.search.SearchAction} like query.
    1820 *
     
    3840
    3941    private OverpassTurboQueryWizard() {
    40         // overpass-turbo is MIT Licensed
    41 
    4242        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 = {};");
    4546            engine.eval(reader);
    46             engine.eval("var construct_query = turbo.ffs().construct_query;");
    4747        } catch (ScriptException | IOException ex) {
    4848            throw new RuntimeException("Failed to initialize OverpassTurboQueryWizard", ex);
     
    5858    public String constructQuery(String search) throws UncheckedParseException {
    5959        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                    }});
    6166            if (result == Boolean.FALSE) {
    6267                throw new UncheckedParseException();
    6368            }
    6469            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}}]", "");
    6871            return query;
    6972        } catch (NoSuchMethodException e) {
Note: See TracChangeset for help on using the changeset viewer.