[6380] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[444] | 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 |
|
---|
[2327] | 9 | import org.openstreetmap.josm.data.Bounds;
|
---|
[1811] | 10 | import org.openstreetmap.josm.data.gpx.GpxData;
|
---|
[444] | 11 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
[1811] | 12 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
[6474] | 13 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
[5874] | 14 | import org.openstreetmap.josm.tools.Utils;
|
---|
[444] | 15 | import org.xml.sax.SAXException;
|
---|
| 16 |
|
---|
[6244] | 17 | /**
|
---|
| 18 | * Read content from OSM server for a given bounding box
|
---|
| 19 | * @since 627
|
---|
| 20 | */
|
---|
[444] | 21 | public class BoundingBoxDownloader extends OsmServerReader {
|
---|
| 22 |
|
---|
[1169] | 23 | /**
|
---|
| 24 | * The boundings of the desired map data.
|
---|
| 25 | */
|
---|
[5097] | 26 | protected final double lat1;
|
---|
| 27 | protected final double lon1;
|
---|
| 28 | protected final double lat2;
|
---|
| 29 | protected final double lon2;
|
---|
| 30 | protected final boolean crosses180th;
|
---|
[444] | 31 |
|
---|
[6244] | 32 | /**
|
---|
| 33 | * Constructs a new {@code BoundingBoxDownloader}.
|
---|
| 34 | * @param downloadArea The area to download
|
---|
| 35 | */
|
---|
[2327] | 36 | public BoundingBoxDownloader(Bounds downloadArea) {
|
---|
[6474] | 37 | CheckParameterUtil.ensureParameterNotNull(downloadArea, "downloadArea");
|
---|
[6203] | 38 | this.lat1 = downloadArea.getMinLat();
|
---|
| 39 | this.lon1 = downloadArea.getMinLon();
|
---|
| 40 | this.lat2 = downloadArea.getMaxLat();
|
---|
| 41 | this.lon2 = downloadArea.getMaxLon();
|
---|
[4580] | 42 | this.crosses180th = downloadArea.crosses180thMeridian();
|
---|
[1169] | 43 | }
|
---|
[444] | 44 |
|
---|
[4580] | 45 | private GpxData downloadRawGps(String url, ProgressMonitor progressMonitor) throws IOException, OsmTransferException, SAXException {
|
---|
| 46 | boolean done = false;
|
---|
| 47 | GpxData result = null;
|
---|
| 48 | for (int i = 0;!done;++i) {
|
---|
| 49 | progressMonitor.subTask(tr("Downloading points {0} to {1}...", i * 5000, ((i + 1) * 5000)));
|
---|
| 50 | InputStream in = getInputStream(url+i, progressMonitor.createSubTaskMonitor(1, true));
|
---|
| 51 | if (in == null) {
|
---|
| 52 | break;
|
---|
| 53 | }
|
---|
| 54 | progressMonitor.setTicks(0);
|
---|
| 55 | GpxReader reader = new GpxReader(in);
|
---|
[5494] | 56 | gpxParsedProperly = reader.parse(false);
|
---|
[5679] | 57 | GpxData currentGpx = reader.getGpxData();
|
---|
[4580] | 58 | if (result == null) {
|
---|
| 59 | result = currentGpx;
|
---|
| 60 | } else if (currentGpx.hasTrackPoints()) {
|
---|
| 61 | result.mergeFrom(currentGpx);
|
---|
| 62 | } else{
|
---|
| 63 | done = true;
|
---|
| 64 | }
|
---|
[5874] | 65 | Utils.close(in);
|
---|
[4580] | 66 | activeConnection = null;
|
---|
| 67 | }
|
---|
| 68 | result.fromServer = true;
|
---|
| 69 | return result;
|
---|
| 70 | }
|
---|
[6070] | 71 |
|
---|
[4521] | 72 | @Override
|
---|
| 73 | public GpxData parseRawGps(ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
[1811] | 74 | progressMonitor.beginTask("", 1);
|
---|
[1169] | 75 | try {
|
---|
[1811] | 76 | progressMonitor.indeterminateSubTask(tr("Contacting OSM Server..."));
|
---|
[4580] | 77 | if (crosses180th) {
|
---|
| 78 | // API 0.6 does not support requests crossing the 180th meridian, so make two requests
|
---|
| 79 | GpxData result = downloadRawGps("trackpoints?bbox="+lon1+","+lat1+",180.0,"+lat2+"&page=", progressMonitor);
|
---|
| 80 | result.mergeFrom(downloadRawGps("trackpoints?bbox=-180.0,"+lat1+","+lon2+","+lat2+"&page=", progressMonitor));
|
---|
| 81 | return result;
|
---|
| 82 | } else {
|
---|
| 83 | // Simple request
|
---|
| 84 | return downloadRawGps("trackpoints?bbox="+lon1+","+lat1+","+lon2+","+lat2+"&page=", progressMonitor);
|
---|
[1169] | 85 | }
|
---|
| 86 | } catch (IllegalArgumentException e) {
|
---|
| 87 | // caused by HttpUrlConnection in case of illegal stuff in the response
|
---|
| 88 | if (cancel)
|
---|
| 89 | return null;
|
---|
[4521] | 90 | throw new OsmTransferException("Illegal characters within the HTTP-header response.", e);
|
---|
[1169] | 91 | } catch (IOException e) {
|
---|
| 92 | if (cancel)
|
---|
| 93 | return null;
|
---|
[4521] | 94 | throw new OsmTransferException(e);
|
---|
[1169] | 95 | } catch (SAXException e) {
|
---|
[4521] | 96 | throw new OsmTransferException(e);
|
---|
[1835] | 97 | } catch (OsmTransferException e) {
|
---|
| 98 | throw e;
|
---|
[2676] | 99 | } catch (RuntimeException e) {
|
---|
[1169] | 100 | if (cancel)
|
---|
| 101 | return null;
|
---|
[2676] | 102 | throw e;
|
---|
[1811] | 103 | } finally {
|
---|
| 104 | progressMonitor.finishTask();
|
---|
[1169] | 105 | }
|
---|
| 106 | }
|
---|
[444] | 107 |
|
---|
[5097] | 108 | protected String getRequestForBbox(double lon1, double lat1, double lon2, double lat2) {
|
---|
| 109 | return "map?bbox=" + lon1 + "," + lat1 + "," + lon2 + "," + lat2;
|
---|
| 110 | }
|
---|
| 111 |
|
---|
[1670] | 112 | @Override
|
---|
[1811] | 113 | public DataSet parseOsm(ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
| 114 | progressMonitor.beginTask(tr("Contacting OSM Server..."), 10);
|
---|
[1879] | 115 | InputStream in = null;
|
---|
[1169] | 116 | try {
|
---|
[4580] | 117 | DataSet ds = null;
|
---|
[1811] | 118 | progressMonitor.indeterminateSubTask(null);
|
---|
[4580] | 119 | if (crosses180th) {
|
---|
| 120 | // API 0.6 does not support requests crossing the 180th meridian, so make two requests
|
---|
[5097] | 121 | in = getInputStream(getRequestForBbox(lon1, lat1, 180.0, lat2), progressMonitor.createSubTaskMonitor(9, false));
|
---|
[4580] | 122 | if (in == null)
|
---|
| 123 | return null;
|
---|
| 124 | ds = OsmReader.parseDataSet(in, progressMonitor.createSubTaskMonitor(1, false));
|
---|
| 125 |
|
---|
[5097] | 126 | in = getInputStream(getRequestForBbox(-180.0, lat1, lon2, lat2), progressMonitor.createSubTaskMonitor(9, false));
|
---|
[4580] | 127 | if (in == null)
|
---|
| 128 | return null;
|
---|
| 129 | DataSet ds2 = OsmReader.parseDataSet(in, progressMonitor.createSubTaskMonitor(1, false));
|
---|
| 130 | if (ds2 == null)
|
---|
| 131 | return null;
|
---|
| 132 | ds.mergeFrom(ds2);
|
---|
[6070] | 133 |
|
---|
[4580] | 134 | } else {
|
---|
| 135 | // Simple request
|
---|
[5097] | 136 | in = getInputStream(getRequestForBbox(lon1, lat1, lon2, lat2), progressMonitor.createSubTaskMonitor(9, false));
|
---|
[4580] | 137 | if (in == null)
|
---|
| 138 | return null;
|
---|
| 139 | ds = OsmReader.parseDataSet(in, progressMonitor.createSubTaskMonitor(1, false));
|
---|
| 140 | }
|
---|
| 141 | return ds;
|
---|
[1670] | 142 | } catch(OsmTransferException e) {
|
---|
[1169] | 143 | throw e;
|
---|
| 144 | } catch (Exception e) {
|
---|
[1670] | 145 | throw new OsmTransferException(e);
|
---|
[1811] | 146 | } finally {
|
---|
| 147 | progressMonitor.finishTask();
|
---|
[5874] | 148 | Utils.close(in);
|
---|
[1879] | 149 | activeConnection = null;
|
---|
[1169] | 150 | }
|
---|
| 151 | }
|
---|
[444] | 152 | }
|
---|