1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.io.IOException;
|
---|
7 | import java.io.InputStream;
|
---|
8 | import java.util.EnumMap;
|
---|
9 | import java.util.List;
|
---|
10 | import java.util.NoSuchElementException;
|
---|
11 | import java.util.concurrent.TimeUnit;
|
---|
12 | import java.util.regex.Matcher;
|
---|
13 | import java.util.regex.Pattern;
|
---|
14 |
|
---|
15 | import javax.xml.stream.XMLStreamConstants;
|
---|
16 | import javax.xml.stream.XMLStreamException;
|
---|
17 |
|
---|
18 | import org.openstreetmap.josm.Main;
|
---|
19 | import org.openstreetmap.josm.data.Bounds;
|
---|
20 | import org.openstreetmap.josm.data.DataSource;
|
---|
21 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
22 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
---|
23 | import org.openstreetmap.josm.data.osm.PrimitiveId;
|
---|
24 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
25 | import org.openstreetmap.josm.tools.HttpClient;
|
---|
26 | import org.openstreetmap.josm.tools.UncheckedParseException;
|
---|
27 | import org.openstreetmap.josm.tools.Utils;
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * Read content from an Overpass server.
|
---|
31 | *
|
---|
32 | * @since 8744
|
---|
33 | */
|
---|
34 | public class OverpassDownloadReader extends BoundingBoxDownloader {
|
---|
35 |
|
---|
36 | static final class OverpassOsmReader extends OsmReader {
|
---|
37 | @Override
|
---|
38 | protected void parseUnknown(boolean printWarning) throws XMLStreamException {
|
---|
39 | if ("remark".equals(parser.getLocalName()) && parser.getEventType() == XMLStreamConstants.START_ELEMENT) {
|
---|
40 | final String text = parser.getElementText();
|
---|
41 | if (text.contains("runtime error")) {
|
---|
42 | throw new XMLStreamException(text);
|
---|
43 | }
|
---|
44 | }
|
---|
45 | super.parseUnknown(printWarning);
|
---|
46 | }
|
---|
47 | }
|
---|
48 |
|
---|
49 | final String overpassServer;
|
---|
50 | final String overpassQuery;
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * Constructs a new {@code OverpassDownloadReader}.
|
---|
54 | *
|
---|
55 | * @param downloadArea The area to download
|
---|
56 | * @param overpassServer The Overpass server to use
|
---|
57 | * @param overpassQuery The Overpass query
|
---|
58 | */
|
---|
59 | public OverpassDownloadReader(Bounds downloadArea, String overpassServer, String overpassQuery) {
|
---|
60 | super(downloadArea);
|
---|
61 | this.overpassServer = overpassServer;
|
---|
62 | this.overpassQuery = overpassQuery.trim();
|
---|
63 | }
|
---|
64 |
|
---|
65 | @Override
|
---|
66 | protected String getBaseUrl() {
|
---|
67 | return overpassServer;
|
---|
68 | }
|
---|
69 |
|
---|
70 | @Override
|
---|
71 | protected String getRequestForBbox(double lon1, double lat1, double lon2, double lat2) {
|
---|
72 | if (overpassQuery.isEmpty())
|
---|
73 | return super.getRequestForBbox(lon1, lat1, lon2, lat2);
|
---|
74 | else {
|
---|
75 | final String query = this.overpassQuery.replace("{{bbox}}", lat1 + "," + lon1 + "," + lat2 + "," + lon2);
|
---|
76 | final String expandedOverpassQuery = expandExtendedQueries(query);
|
---|
77 | return "interpreter?data=" + Utils.encodeUrl(expandedOverpassQuery);
|
---|
78 | }
|
---|
79 | }
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * Evaluates some features of overpass turbo extended query syntax.
|
---|
83 | * See https://wiki.openstreetmap.org/wiki/Overpass_turbo/Extended_Overpass_Turbo_Queries
|
---|
84 | * @param query unexpanded query
|
---|
85 | * @return expanded query
|
---|
86 | */
|
---|
87 | static String expandExtendedQueries(String query) {
|
---|
88 | final StringBuffer sb = new StringBuffer();
|
---|
89 | final Matcher matcher = Pattern.compile("\\{\\{(geocodeArea):([^}]+)\\}\\}").matcher(query);
|
---|
90 | while (matcher.find()) {
|
---|
91 | try {
|
---|
92 | switch (matcher.group(1)) {
|
---|
93 | case "geocodeArea":
|
---|
94 | matcher.appendReplacement(sb, geocodeArea(matcher.group(2)));
|
---|
95 | break;
|
---|
96 | default:
|
---|
97 | Main.warn("Unsupported syntax: " + matcher.group(1));
|
---|
98 | }
|
---|
99 | } catch (UncheckedParseException ex) {
|
---|
100 | final String msg = tr("Failed to evaluate {0}", matcher.group());
|
---|
101 | Main.warn(ex, msg);
|
---|
102 | matcher.appendReplacement(sb, "// " + msg + "\n");
|
---|
103 | }
|
---|
104 | }
|
---|
105 | matcher.appendTail(sb);
|
---|
106 | return sb.toString();
|
---|
107 | }
|
---|
108 |
|
---|
109 | private static String geocodeArea(String area) {
|
---|
110 | // Offsets defined in https://wiki.openstreetmap.org/wiki/Overpass_API/Overpass_QL#By_element_id
|
---|
111 | final EnumMap<OsmPrimitiveType, Long> idOffset = new EnumMap<>(OsmPrimitiveType.class);
|
---|
112 | idOffset.put(OsmPrimitiveType.NODE, 0L);
|
---|
113 | idOffset.put(OsmPrimitiveType.WAY, 2_400_000_000L);
|
---|
114 | idOffset.put(OsmPrimitiveType.RELATION, 3_600_000_000L);
|
---|
115 | try {
|
---|
116 | final List<NameFinder.SearchResult> results = NameFinder.queryNominatim(area);
|
---|
117 | final PrimitiveId osmId = results.iterator().next().getOsmId();
|
---|
118 | return String.format("area(%d)", osmId.getUniqueId() + idOffset.get(osmId.getType()));
|
---|
119 | } catch (IOException | NoSuchElementException ex) {
|
---|
120 | throw new UncheckedParseException(ex);
|
---|
121 | }
|
---|
122 | }
|
---|
123 |
|
---|
124 | @Override
|
---|
125 | protected InputStream getInputStreamRaw(String urlStr, ProgressMonitor progressMonitor, String reason,
|
---|
126 | boolean uncompressAccordingToContentDisposition) throws OsmTransferException {
|
---|
127 | try {
|
---|
128 | return super.getInputStreamRaw(urlStr, progressMonitor, reason, uncompressAccordingToContentDisposition);
|
---|
129 | } catch (OsmApiException ex) {
|
---|
130 | final String errorIndicator = "Error</strong>: ";
|
---|
131 | if (ex.getMessage() != null && ex.getMessage().contains(errorIndicator)) {
|
---|
132 | final String errorPlusRest = ex.getMessage().split(errorIndicator)[1];
|
---|
133 | if (errorPlusRest != null) {
|
---|
134 | final String error = errorPlusRest.split("</")[0];
|
---|
135 | ex.setErrorHeader(error);
|
---|
136 | }
|
---|
137 | }
|
---|
138 | throw ex;
|
---|
139 | }
|
---|
140 | }
|
---|
141 |
|
---|
142 | @Override
|
---|
143 | protected void adaptRequest(HttpClient request) {
|
---|
144 | // see https://wiki.openstreetmap.org/wiki/Overpass_API/Overpass_QL#timeout
|
---|
145 | final Matcher timeoutMatcher = Pattern.compile("\\[timeout:(\\d+)\\]").matcher(overpassQuery);
|
---|
146 | final int timeout;
|
---|
147 | if (timeoutMatcher.find()) {
|
---|
148 | timeout = (int) TimeUnit.SECONDS.toMillis(Integer.parseInt(timeoutMatcher.group(1)));
|
---|
149 | } else {
|
---|
150 | timeout = (int) TimeUnit.MINUTES.toMillis(3);
|
---|
151 | }
|
---|
152 | request.setConnectTimeout(timeout);
|
---|
153 | request.setReadTimeout(timeout);
|
---|
154 | }
|
---|
155 |
|
---|
156 | @Override
|
---|
157 | protected String getTaskName() {
|
---|
158 | return tr("Contacting Server...");
|
---|
159 | }
|
---|
160 |
|
---|
161 | @Override
|
---|
162 | protected DataSet parseDataSet(InputStream source, ProgressMonitor progressMonitor) throws IllegalDataException {
|
---|
163 | return new OverpassOsmReader().doParseDataSet(source, progressMonitor);
|
---|
164 | }
|
---|
165 |
|
---|
166 | @Override
|
---|
167 | public DataSet parseOsm(ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
168 |
|
---|
169 | DataSet ds = super.parseOsm(progressMonitor);
|
---|
170 |
|
---|
171 | // add bounds if necessary (note that Overpass API does not return bounds in the response XML)
|
---|
172 | if (ds != null && ds.dataSources.isEmpty() && overpassQuery.contains("{{bbox}}")) {
|
---|
173 | if (crosses180th) {
|
---|
174 | Bounds bounds = new Bounds(lat1, lon1, lat2, 180.0);
|
---|
175 | DataSource src = new DataSource(bounds, getBaseUrl());
|
---|
176 | ds.dataSources.add(src);
|
---|
177 |
|
---|
178 | bounds = new Bounds(lat1, -180.0, lat2, lon2);
|
---|
179 | src = new DataSource(bounds, getBaseUrl());
|
---|
180 | ds.dataSources.add(src);
|
---|
181 | } else {
|
---|
182 | Bounds bounds = new Bounds(lat1, lon1, lat2, lon2);
|
---|
183 | DataSource src = new DataSource(bounds, getBaseUrl());
|
---|
184 | ds.dataSources.add(src);
|
---|
185 | }
|
---|
186 | }
|
---|
187 |
|
---|
188 | return ds;
|
---|
189 | }
|
---|
190 | }
|
---|