Opened 8 years ago

Last modified 6 years ago

#15085 new enhancement

Fetch overpass turbo queries from OSM preferences API — at Initial Version

Reported by: Don-vip Owned by: team
Priority: normal Milestone:
Component: Core Version:
Keywords: mirrored_download overpass turbo Cc: bastiK, michael2402, Stereo

Description

From #15057:

Overpass turbo now has an option to save a query directly in the OSM account data. We could fetch that data (and in principle add new entries as well): ​https://www.openstreetmap.org/user/tyr_asd/diary/41793

This code allows to fetch overpass turbo queries (with r12502 + overpass-turbo-lzw.js):

            try {
                Map<String, String> map = new OsmServerUserPreferencesReader().fetchUserPreferences(NullProgressMonitor.INSTANCE);
                String scount = map.get("overpass-ide_query-count");
                if (scount != null) {
                    try {
                        ScriptEngine engine = new ScriptEngineManager().getEngineByName("JavaScript");
                        try (CachedFile cf = new CachedFile("resource://data/overpass-turbo-lzw.js");
                                Reader reader = cf.getContentReader()) {
                            engine.eval(reader);
                        }
                        Object base64 = engine.get("Base64");

                        for (int i = 0 ; i < Integer.parseInt(scount); i++) {
                            String v = map.get("overpass-ide_query_" + i + "_0");
                            int idx = v.indexOf("p=") + 2;
                            int p = Integer.parseInt(v.substring(idx, v.indexOf('&', idx)));
                            idx = v.indexOf("n=") + 2;
                            String name = v.substring(idx, v.indexOf('&', idx));
                            StringBuffer sb = new StringBuffer(v.substring(v.indexOf("q=") + 2));
                            for (int j = 1; j < p ; j++) {
                                sb.append(map.get("overpass-ide_query_" + i + "_" + j));
                            }
                            String query = (String) ((Invocable) engine).invokeFunction("lzw_decode",
                                    ((Invocable) engine).invokeMethod(base64, "decode", sb.toString()));
                        }
                    } catch (NumberFormatException ex) {
                        Main.trace(ex);
                    }
                }
            } catch (OsmTransferException | ScriptException | NoSuchMethodException | IOException ex) {
                Main.error(ex);
            }

We can add an extra button to fetch/sync them, but this would be a separate feature. The problem I see is that we should be consistent on where we store preferences: We currently only store all preferences locally. Best would be to allow the user to store all JOSM preferences to the server and be consistent there.
PS: Wouldn't this be much cleaner using Regexp (p=(\\d+), untested)?

Change History (1)

by Don-vip, 8 years ago

Attachment: overpass-turbo-lzw.js added
Note: See TracTickets for help on using tickets.