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