Changeset 9704 in josm


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

fix #12469 - Update overpass wizard code

Location:
trunk
Files:
1 added
1 deleted
2 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) {
  • trunk/test/unit/org/openstreetmap/josm/tools/OverpassTurboQueryWizardTest.java

    r9669 r9704  
    1818    @BeforeClass
    1919    public static void setUp() {
    20         JOSMFixture.createUnitTestFixture().init(true);
     20        JOSMFixture.createUnitTestFixture().init(false);
    2121        OverpassTurboQueryWizard.getInstance();
    2222    }
     
    2929        final String query = OverpassTurboQueryWizard.getInstance().constructQuery("amenity=drinking_water");
    3030        assertEquals("" +
    31                 "[timeout:25];\n" +
    32                 "// gather results\n" +
     31                "[out:xml][timeout:25];\n" +
    3332                "(\n" +
    34                 "  // query part for: “amenity=drinking_water”\n" +
    3533                "  node[\"amenity\"=\"drinking_water\"];\n" +
    3634                "  way[\"amenity\"=\"drinking_water\"];\n" +
    3735                "  relation[\"amenity\"=\"drinking_water\"];\n" +
    3836                ");\n" +
    39                 "// print results\n" +
    40                 "out meta;\n" +
    41                 ">;\n" +
     37                "(._;>;);\n" +
    4238                "out meta;", query);
    4339    }
Note: See TracChangeset for help on using the changeset viewer.