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.InputStream;
|
---|
7 |
|
---|
8 | import javax.xml.stream.XMLStreamConstants;
|
---|
9 | import javax.xml.stream.XMLStreamException;
|
---|
10 |
|
---|
11 | import org.openstreetmap.josm.data.Bounds;
|
---|
12 | import org.openstreetmap.josm.data.DataSource;
|
---|
13 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
14 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
15 | import org.openstreetmap.josm.tools.Utils;
|
---|
16 |
|
---|
17 | /**
|
---|
18 | * Read content from an Overpass server.
|
---|
19 | *
|
---|
20 | * @since 8744
|
---|
21 | */
|
---|
22 | public class OverpassDownloadReader extends BoundingBoxDownloader {
|
---|
23 |
|
---|
24 | final String overpassServer;
|
---|
25 | final String overpassQuery;
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * Constructs a new {@code OverpassDownloadReader}.
|
---|
29 | *
|
---|
30 | * @param downloadArea The area to download
|
---|
31 | * @param overpassServer The Overpass server to use
|
---|
32 | * @param overpassQuery The Overpass query
|
---|
33 | */
|
---|
34 | public OverpassDownloadReader(Bounds downloadArea, String overpassServer, String overpassQuery) {
|
---|
35 | super(downloadArea);
|
---|
36 | this.overpassServer = overpassServer;
|
---|
37 | this.overpassQuery = overpassQuery.trim();
|
---|
38 | }
|
---|
39 |
|
---|
40 | @Override
|
---|
41 | protected String getBaseUrl() {
|
---|
42 | return overpassServer;
|
---|
43 | }
|
---|
44 |
|
---|
45 | @Override
|
---|
46 | protected String getRequestForBbox(double lon1, double lat1, double lon2, double lat2) {
|
---|
47 | if (overpassQuery.isEmpty())
|
---|
48 | return super.getRequestForBbox(lon1, lat1, lon2, lat2);
|
---|
49 | else {
|
---|
50 | String realQuery = completeOverpassQuery(overpassQuery);
|
---|
51 | return "interpreter?data=" + Utils.encodeUrl(realQuery)
|
---|
52 | + "&bbox=" + lon1 + "," + lat1 + "," + lon2 + "," + lat2;
|
---|
53 | }
|
---|
54 | }
|
---|
55 |
|
---|
56 | private String completeOverpassQuery(String query) {
|
---|
57 | int firstColon = query.indexOf(";");
|
---|
58 | if (firstColon == -1) {
|
---|
59 | return "[bbox];" + query;
|
---|
60 | }
|
---|
61 | int bboxPos = query.indexOf("[bbox");
|
---|
62 | if (bboxPos > -1 && bboxPos < firstColon) {
|
---|
63 | return query;
|
---|
64 | }
|
---|
65 |
|
---|
66 | int bracketCount = 0;
|
---|
67 | int pos = 0;
|
---|
68 | for (; pos < firstColon; ++pos) {
|
---|
69 | if (query.charAt(pos) == '[')
|
---|
70 | ++bracketCount;
|
---|
71 | else if (query.charAt(pos) == '[')
|
---|
72 | --bracketCount;
|
---|
73 | else if (bracketCount == 0) {
|
---|
74 | if (!Character.isWhitespace(query.charAt(pos)))
|
---|
75 | break;
|
---|
76 | }
|
---|
77 | }
|
---|
78 |
|
---|
79 | if (pos < firstColon) {
|
---|
80 | // We start with a statement, not with declarations
|
---|
81 | return "[bbox];" + query;
|
---|
82 | }
|
---|
83 |
|
---|
84 | // We start with declarations. Add just one more declaration in this case.
|
---|
85 | return "[bbox]" + query;
|
---|
86 | }
|
---|
87 |
|
---|
88 | @Override
|
---|
89 | protected InputStream getInputStreamRaw(String urlStr, ProgressMonitor progressMonitor, String reason,
|
---|
90 | boolean uncompressAccordingToContentDisposition) throws OsmTransferException {
|
---|
91 | try {
|
---|
92 | return super.getInputStreamRaw(urlStr, progressMonitor, reason, uncompressAccordingToContentDisposition);
|
---|
93 | } catch (OsmApiException ex) {
|
---|
94 | final String errorIndicator = "Error</strong>: ";
|
---|
95 | if (ex.getMessage() != null && ex.getMessage().contains(errorIndicator)) {
|
---|
96 | final String errorPlusRest = ex.getMessage().split(errorIndicator)[1];
|
---|
97 | if (errorPlusRest != null) {
|
---|
98 | final String error = errorPlusRest.split("</")[0];
|
---|
99 | ex.setErrorHeader(error);
|
---|
100 | }
|
---|
101 | }
|
---|
102 | throw ex;
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | @Override
|
---|
107 | protected String getTaskName() {
|
---|
108 | return tr("Contacting Server...");
|
---|
109 | }
|
---|
110 |
|
---|
111 | @Override
|
---|
112 | protected DataSet parseDataSet(InputStream source, ProgressMonitor progressMonitor) throws IllegalDataException {
|
---|
113 | return new OsmReader() {
|
---|
114 | @Override
|
---|
115 | protected void parseUnknown(boolean printWarning) throws XMLStreamException {
|
---|
116 | if ("remark".equals(parser.getLocalName())) {
|
---|
117 | if (parser.getEventType() == XMLStreamConstants.START_ELEMENT) {
|
---|
118 | final String text = parser.getElementText();
|
---|
119 | if (text.contains("runtime error")) {
|
---|
120 | throw new XMLStreamException(text);
|
---|
121 | }
|
---|
122 | }
|
---|
123 | }
|
---|
124 | super.parseUnknown(printWarning);
|
---|
125 | }
|
---|
126 | }.doParseDataSet(source, progressMonitor);
|
---|
127 | }
|
---|
128 |
|
---|
129 | @Override
|
---|
130 | public DataSet parseOsm(ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
131 |
|
---|
132 | DataSet ds = super.parseOsm(progressMonitor);
|
---|
133 |
|
---|
134 | // add bounds if necessary (note that Overpass API does not return bounds in the response XML)
|
---|
135 | if (ds != null && ds.dataSources.isEmpty()) {
|
---|
136 | if (crosses180th) {
|
---|
137 | Bounds bounds = new Bounds(lat1, lon1, lat2, 180.0);
|
---|
138 | DataSource src = new DataSource(bounds, getBaseUrl());
|
---|
139 | ds.dataSources.add(src);
|
---|
140 |
|
---|
141 | bounds = new Bounds(lat1, -180.0, lat2, lon2);
|
---|
142 | src = new DataSource(bounds, getBaseUrl());
|
---|
143 | ds.dataSources.add(src);
|
---|
144 | } else {
|
---|
145 | Bounds bounds = new Bounds(lat1, lon1, lat2, lon2);
|
---|
146 | DataSource src = new DataSource(bounds, getBaseUrl());
|
---|
147 | ds.dataSources.add(src);
|
---|
148 | }
|
---|
149 | }
|
---|
150 |
|
---|
151 | return ds;
|
---|
152 | }
|
---|
153 | }
|
---|