1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io;
|
---|
3 |
|
---|
4 | import java.util.Map.Entry;
|
---|
5 | import java.util.Set;
|
---|
6 | import java.util.stream.Collectors;
|
---|
7 |
|
---|
8 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
---|
9 | import org.openstreetmap.josm.tools.Logging;
|
---|
10 |
|
---|
11 | /**
|
---|
12 | * Retrieves a set of {@link org.openstreetmap.josm.data.osm.OsmPrimitive}s from an Overpass API server.
|
---|
13 | *
|
---|
14 | * @since 9241
|
---|
15 | */
|
---|
16 | public class MultiFetchOverpassObjectReader extends MultiFetchServerObjectReader {
|
---|
17 |
|
---|
18 | private static String getPackageString(final OsmPrimitiveType type, Set<Long> idPackage) {
|
---|
19 | return idPackage.stream().map(String::valueOf)
|
---|
20 | .collect(Collectors.joining(",", type.getAPIName() + (idPackage.size() == 1 ? "(" : "(id:"), ");"));
|
---|
21 | }
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Create a single query for all elements
|
---|
25 | * @return the request string
|
---|
26 | */
|
---|
27 | protected String buildComplexRequestString() {
|
---|
28 | StringBuilder sb = new StringBuilder();
|
---|
29 | int countTypes = 0;
|
---|
30 | for (Entry<OsmPrimitiveType, Set<Long>> e : primitivesMap.entrySet()) {
|
---|
31 | if (!e.getValue().isEmpty()) {
|
---|
32 | countTypes++;
|
---|
33 | String list = getPackageString(e.getKey(), e.getValue());
|
---|
34 | switch (e.getKey()) {
|
---|
35 | case MULTIPOLYGON:
|
---|
36 | case RELATION:
|
---|
37 | if (recurseDownRelations) {
|
---|
38 | sb.append('(').append(list);
|
---|
39 | sb.setLength(sb.length()-1); // remove semicolon
|
---|
40 | //recurse down only one level, see #18835
|
---|
41 | sb.append("->.r;.r;rel(r);.r;way(r);>;.r;node(r););");
|
---|
42 | } else {
|
---|
43 | sb.append(list);
|
---|
44 | }
|
---|
45 | break;
|
---|
46 | case CLOSEDWAY:
|
---|
47 | case WAY:
|
---|
48 | sb.append('(').append(list).append(">;);");
|
---|
49 | break;
|
---|
50 | case NODE:
|
---|
51 | sb.append(list);
|
---|
52 | }
|
---|
53 | }
|
---|
54 | }
|
---|
55 | String query = sb.toString();
|
---|
56 | if (countTypes > 1) {
|
---|
57 | query = "(" + query + ");";
|
---|
58 | }
|
---|
59 | query += "out meta;";
|
---|
60 | Logging.debug("{0} {1}", "Generated Overpass query:", query);
|
---|
61 | return query;
|
---|
62 | }
|
---|
63 |
|
---|
64 | @Override
|
---|
65 | protected String getBaseUrl() {
|
---|
66 | return OverpassDownloadReader.OVERPASS_SERVER.get();
|
---|
67 | }
|
---|
68 | }
|
---|