Changeset 6244 in josm for trunk/src/org/openstreetmap


Ignore:
Timestamp:
2013-09-21T13:19:50+02:00 (11 years ago)
Author:
Don-vip
Message:

fix #9091 - rework osm/gpx download tasks - take into account recent changes from OSM server:

  • new hashtag scheme
  • GPX traces now returned as Bzip2-compressed files
Location:
trunk/src/org/openstreetmap/josm
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/downloadtasks/DownloadGpsTask.java

    r6069 r6244  
    2727import org.openstreetmap.josm.io.OsmServerReader;
    2828import org.openstreetmap.josm.io.OsmTransferException;
     29import org.openstreetmap.josm.tools.CheckParameterUtil;
    2930import org.xml.sax.SAXException;
    3031
     
    3637    private DownloadTask downloadTask;
    3738
    38     private static final String PATTERN_TRACE_ID = "http://.*openstreetmap.org/trace/\\p{Digit}+/data";
     39    private static final String PATTERN_TRACE_ID = "http://.*(osm|openstreetmap).org/trace/\\p{Digit}+/data";
    3940
    4041    private static final String PATTERN_TRACKPOINTS_BBOX = "http://.*/api/0.6/trackpoints\\?bbox=.*,.*,.*,.*";
     
    5859    public Future<?> download(boolean newLayer, Bounds downloadArea, ProgressMonitor progressMonitor) {
    5960        downloadTask = new DownloadTask(newLayer,
    60                 new BoundingBoxDownloader(downloadArea), progressMonitor);
     61                new BoundingBoxDownloader(downloadArea), progressMonitor, false);
    6162        // We need submit instead of execute so we can wait for it to finish and get the error
    6263        // message if necessary. If no one calls getErrorMessage() it just behaves like execute.
     
    6667    @Override
    6768    public Future<?> loadUrl(boolean newLayer, String url, ProgressMonitor progressMonitor) {
    68         if (url != null && (url.matches(PATTERN_TRACE_ID) || url.matches(PATTERN_EXTERNAL_GPX_SCRIPT) || url.matches(PATTERN_EXTERNAL_GPX_FILE))) {
     69        CheckParameterUtil.ensureParameterNotNull(url, "url");
     70        if (url.matches(PATTERN_TRACE_ID) || url.matches(PATTERN_EXTERNAL_GPX_SCRIPT) || url.matches(PATTERN_EXTERNAL_GPX_FILE)) {
    6971            downloadTask = new DownloadTask(newLayer,
    70                     new OsmServerLocationReader(url), progressMonitor);
     72                    new OsmServerLocationReader(url), progressMonitor, url.matches(PATTERN_TRACE_ID));
    7173            // Extract .gpx filename from URL to set the new layer name
    7274            Matcher matcher = Pattern.compile(PATTERN_EXTERNAL_GPX_FILE).matcher(url);
     
    7678            return Main.worker.submit(downloadTask);
    7779
    78         } else if (url != null && url.matches(PATTERN_TRACKPOINTS_BBOX)) {
     80        } else if (url.matches(PATTERN_TRACKPOINTS_BBOX)) {
    7981            String[] table = url.split("\\?|=|&");
    8082            for (int i = 0; i<table.length; i++) {
     
    9799        private GpxData rawData;
    98100        private final boolean newLayer;
    99 
    100         public DownloadTask(boolean newLayer, OsmServerReader reader, ProgressMonitor progressMonitor) {
     101        private final boolean compressed;
     102
     103        public DownloadTask(boolean newLayer, OsmServerReader reader, ProgressMonitor progressMonitor, boolean compressed) {
    101104            super(tr("Downloading GPS data"));
    102105            this.reader = reader;
    103106            this.newLayer = newLayer;
     107            this.compressed = compressed;
    104108        }
    105109
     
    108112                if (isCanceled())
    109113                    return;
    110                 rawData = reader.parseRawGps(progressMonitor.createSubTaskMonitor(ProgressMonitor.ALL_TICKS, false));
     114                ProgressMonitor subMonitor = progressMonitor.createSubTaskMonitor(ProgressMonitor.ALL_TICKS, false);
     115                if (compressed) {
     116                    rawData = reader.parseRawGpsBzip2(subMonitor);
     117                } else {
     118                    rawData = reader.parseRawGps(subMonitor);
     119                }
    111120            } catch(Exception e) {
    112121                if (isCanceled())
  • trunk/src/org/openstreetmap/josm/actions/downloadtasks/DownloadOsmUrlTask.java

    r6032 r6244  
    2222    @Override
    2323    public String[] getPatterns() {
    24         return new String[]{"http://www\\.openstreetmap\\.org/\\?lat=.*&lon=.*"};
     24        return new String[]{
     25                "http://www\\.(osm|openstreetmap)\\.org/\\?lat=.*&lon=.*",
     26                "http://www\\.(osm|openstreetmap)\\.org/#map=\\p{Digit}+/.*/.*"};
    2527    }
    2628
  • trunk/src/org/openstreetmap/josm/io/BoundingBoxDownloader.java

    r6203 r6244  
    1414import org.xml.sax.SAXException;
    1515
     16/**
     17 * Read content from OSM server for a given bounding box
     18 * @since 627
     19 */
    1620public class BoundingBoxDownloader extends OsmServerReader {
    1721
     
    2529    protected final boolean crosses180th;
    2630
     31    /**
     32     * Constructs a new {@code BoundingBoxDownloader}.
     33     * @param downloadArea The area to download
     34     */
    2735    public BoundingBoxDownloader(Bounds downloadArea) {
    2836        this.lat1 = downloadArea.getMinLat();
     
    6068    }
    6169
    62     /**
    63      * Retrieve raw gps waypoints from the server API.
    64      * @return A list of all primitives retrieved. Currently, the list of lists
    65      *      contain only one list, since the server cannot distinguish between
    66      *      ways.
    67      */
    6870    @Override
    6971    public GpxData parseRawGps(ProgressMonitor progressMonitor) throws OsmTransferException {
     
    106108    }
    107109
    108     /**
    109      * Read the data from the osm server address.
    110      * @return A data set containing all data retrieved from that url
    111      */
    112110    @Override
    113111    public DataSet parseOsm(ProgressMonitor progressMonitor) throws OsmTransferException {
  • trunk/src/org/openstreetmap/josm/io/OsmServerLocationReader.java

    r6070 r6244  
    66import java.io.IOException;
    77import java.io.InputStream;
    8 import java.util.zip.GZIPInputStream;
    98
    10 import org.apache.tools.bzip2.CBZip2InputStream;
    119import org.openstreetmap.josm.actions.downloadtasks.DownloadGpsTask;
    1210import org.openstreetmap.josm.data.gpx.GpxData;
     
    1614import org.xml.sax.SAXException;
    1715
     16/**
     17 * Read content from OSM server for a given URL
     18 * @since 1146
     19 */
    1820public class OsmServerLocationReader extends OsmServerReader {
    1921
    2022    protected final String url;
    2123
     24    /**
     25     * Constructs a new {@code OsmServerLocationReader}.
     26     * @param url The URL to fetch
     27     */
    2228    public OsmServerLocationReader(String url) {
    2329        this.url = url;
     
    2531
    2632    protected abstract class Parser<T> {
    27         public InputStream in = null;
     33        protected final ProgressMonitor progressMonitor;
     34        protected final Compression compression;
     35        protected InputStream in = null;
     36
     37        public Parser(ProgressMonitor progressMonitor, Compression compression) {
     38            this.progressMonitor = progressMonitor;
     39            this.compression = compression;
     40        }
     41       
     42        protected final InputStream getUncompressedInputStream() throws IOException {
     43            switch (compression) {
     44                case BZIP2: return FileImporter.getBZip2InputStream(in);
     45                case GZIP: return FileImporter.getGZipInputStream(in);
     46                case NONE:
     47                default: return in;
     48            }
     49        }
     50       
    2851        public abstract T parse() throws OsmTransferException, IllegalDataException, IOException, SAXException;
    2952    }
     
    4770    }
    4871
    49     /**
    50      * Method to download OSM files from somewhere
    51      */
    5272    @Override
    5373    public DataSet parseOsm(final ProgressMonitor progressMonitor) throws OsmTransferException {
    54         return doParse(new Parser<DataSet>() {
    55             @Override
    56             public DataSet parse() throws OsmTransferException, IllegalDataException {
    57                 in = getInputStreamRaw(url, progressMonitor.createSubTaskMonitor(9, false));
    58                 if (in == null)
    59                     return null;
    60                 progressMonitor.subTask(tr("Downloading OSM data..."));
    61                 return OsmReader.parseDataSet(in, progressMonitor.createSubTaskMonitor(1, false));
    62             }
    63         }, progressMonitor);
     74        return doParse(new OsmParser(progressMonitor, Compression.NONE), progressMonitor);
    6475    }
    6576
    66     /**
    67      * Method to download BZip2-compressed OSM files from somewhere
    68      */
    6977    @Override
    7078    public DataSet parseOsmBzip2(final ProgressMonitor progressMonitor) throws OsmTransferException {
    71         return doParse(new Parser<DataSet>() {
    72             @Override
    73             public DataSet parse() throws OsmTransferException, IllegalDataException, IOException {
    74                 in = getInputStreamRaw(url, progressMonitor.createSubTaskMonitor(9, false));
    75                 if (in == null)
    76                     return null;
    77                 CBZip2InputStream bzin = FileImporter.getBZip2InputStream(in);
    78                 progressMonitor.subTask(tr("Downloading OSM data..."));
    79                 return OsmReader.parseDataSet(bzin, progressMonitor.createSubTaskMonitor(1, false));
    80             }
    81         }, progressMonitor);
     79        return doParse(new OsmParser(progressMonitor, Compression.BZIP2), progressMonitor);
    8280    }
    8381
    84     /**
    85      * Method to download GZip-compressed OSM files from somewhere
    86      */
    8782    @Override
    8883    public DataSet parseOsmGzip(final ProgressMonitor progressMonitor) throws OsmTransferException {
    89         return doParse(new Parser<DataSet>() {
    90             @Override
    91             public DataSet parse() throws OsmTransferException, IllegalDataException, IOException {
    92                 in = getInputStreamRaw(url, progressMonitor.createSubTaskMonitor(9, false));
    93                 if (in == null)
    94                     return null;
    95                 GZIPInputStream gzin = FileImporter.getGZipInputStream(in);
    96                 progressMonitor.subTask(tr("Downloading OSM data..."));
    97                 return OsmReader.parseDataSet(gzin, progressMonitor.createSubTaskMonitor(1, false));
    98             }
    99         }, progressMonitor);
     84        return doParse(new OsmParser(progressMonitor, Compression.GZIP), progressMonitor);
    10085    }
    10186
    102     /* (non-Javadoc)
    103      * @see org.openstreetmap.josm.io.OsmServerReader#parseOsmChange(org.openstreetmap.josm.gui.progress.ProgressMonitor)
    104      */
    10587    @Override
    106     public DataSet parseOsmChange(final ProgressMonitor progressMonitor)
    107             throws OsmTransferException {
    108         return doParse(new Parser<DataSet>() {
    109             @Override
    110             public DataSet parse() throws OsmTransferException, IllegalDataException {
    111                 in = getInputStreamRaw(url, progressMonitor.createSubTaskMonitor(9, false));
    112                 if (in == null)
    113                     return null;
    114                 progressMonitor.subTask(tr("Downloading OSM data..."));
    115                 return OsmChangeReader.parseDataSet(in, progressMonitor.createSubTaskMonitor(1, false));
    116             }
    117         }, progressMonitor);
     88    public DataSet parseOsmChange(final ProgressMonitor progressMonitor) throws OsmTransferException {
     89        return doParse(new OsmChangeParser(progressMonitor, Compression.NONE), progressMonitor);
    11890    }
    11991
    120     /**
    121      * Method to download BZip2-compressed OSM Change files from somewhere
    122      */
    12392    @Override
    12493    public DataSet parseOsmChangeBzip2(final ProgressMonitor progressMonitor) throws OsmTransferException {
    125         return doParse(new Parser<DataSet>() {
    126             @Override
    127             public DataSet parse() throws OsmTransferException, IllegalDataException, IOException {
    128                 in = getInputStreamRaw(url, progressMonitor.createSubTaskMonitor(9, false));
    129                 if (in == null)
    130                     return null;
    131                 CBZip2InputStream bzin = FileImporter.getBZip2InputStream(in);
    132                 progressMonitor.subTask(tr("Downloading OSM data..."));
    133                 return OsmChangeReader.parseDataSet(bzin, progressMonitor.createSubTaskMonitor(1, false));
    134             }
    135         }, progressMonitor);
     94        return doParse(new OsmChangeParser(progressMonitor, Compression.BZIP2), progressMonitor);
    13695    }
    13796
    138     /**
    139      * Method to download GZip-compressed OSM Change files from somewhere
    140      */
    14197    @Override
    14298    public DataSet parseOsmChangeGzip(final ProgressMonitor progressMonitor) throws OsmTransferException {
    143         return doParse(new Parser<DataSet>() {
    144             @Override
    145             public DataSet parse() throws OsmTransferException, IllegalDataException, IOException {
    146                 in = getInputStreamRaw(url, progressMonitor.createSubTaskMonitor(9, false));
    147                 if (in == null)
    148                     return null;
    149                 GZIPInputStream gzin = FileImporter.getGZipInputStream(in);
    150                 progressMonitor.subTask(tr("Downloading OSM data..."));
    151                 return OsmChangeReader.parseDataSet(gzin, progressMonitor.createSubTaskMonitor(1, false));
    152             }
    153         }, progressMonitor);
     99        return doParse(new OsmChangeParser(progressMonitor, Compression.GZIP), progressMonitor);
    154100    }
    155101
    156102    @Override
    157103    public GpxData parseRawGps(final ProgressMonitor progressMonitor) throws OsmTransferException {
    158         return doParse(new Parser<GpxData>() {
    159             @Override
    160             public GpxData parse() throws OsmTransferException, IllegalDataException, IOException, SAXException {
    161                 in = getInputStreamRaw(url, progressMonitor.createSubTaskMonitor(1, true));
    162                 if (in == null)
    163                     return null;
    164                 progressMonitor.subTask(tr("Downloading OSM data..."));
    165                 GpxReader reader = new GpxReader(in);
    166                 gpxParsedProperly = reader.parse(false);
    167                 GpxData result = reader.getGpxData();
    168                 result.fromServer = DownloadGpsTask.isFromServer(url);
    169                 return result;
    170             }
    171         }, progressMonitor);
     104        return doParse(new GpxParser(progressMonitor, Compression.NONE), progressMonitor);
     105    }
     106
     107    @Override
     108    public GpxData parseRawGpsBzip2(final ProgressMonitor progressMonitor) throws OsmTransferException {
     109        return doParse(new GpxParser(progressMonitor, Compression.BZIP2), progressMonitor);
     110    }
     111
     112    protected class OsmParser extends Parser<DataSet> {
     113        protected OsmParser(ProgressMonitor progressMonitor, Compression compression) {
     114            super(progressMonitor, compression);
     115        }
     116
     117        @Override
     118        public DataSet parse() throws OsmTransferException, IllegalDataException, IOException {
     119            in = getInputStreamRaw(url, progressMonitor.createSubTaskMonitor(9, false));
     120            if (in == null)
     121                return null;
     122            progressMonitor.subTask(tr("Downloading OSM data..."));
     123            return OsmReader.parseDataSet(getUncompressedInputStream(), progressMonitor.createSubTaskMonitor(1, false));
     124        }
     125    }
     126   
     127    protected class OsmChangeParser extends Parser<DataSet> {
     128        protected OsmChangeParser(ProgressMonitor progressMonitor, Compression compression) {
     129            super(progressMonitor, compression);
     130        }
     131
     132        @Override
     133        public DataSet parse() throws OsmTransferException, IllegalDataException, IOException {
     134            in = getInputStreamRaw(url, progressMonitor.createSubTaskMonitor(9, false));
     135            if (in == null)
     136                return null;
     137            progressMonitor.subTask(tr("Downloading OSM data..."));
     138            return OsmChangeReader.parseDataSet(getUncompressedInputStream(), progressMonitor.createSubTaskMonitor(1, false));
     139        }
     140    }
     141
     142    protected class GpxParser extends Parser<GpxData> {
     143        protected GpxParser(ProgressMonitor progressMonitor, Compression compression) {
     144            super(progressMonitor, compression);
     145        }
     146       
     147        @Override
     148        public GpxData parse() throws OsmTransferException, IllegalDataException, IOException, SAXException {
     149            in = getInputStreamRaw(url, progressMonitor.createSubTaskMonitor(1, true));
     150            if (in == null)
     151                return null;
     152            progressMonitor.subTask(tr("Downloading OSM data..."));
     153            GpxReader reader = new GpxReader(getUncompressedInputStream());
     154            gpxParsedProperly = reader.parse(false);
     155            GpxData result = reader.getGpxData();
     156            result.fromServer = DownloadGpsTask.isFromServer(url);
     157            return result;
     158        }
    172159    }
    173160}
  • trunk/src/org/openstreetmap/josm/io/OsmServerReader.java

    r6089 r6244  
    3434    private boolean doAuthenticate = false;
    3535    protected boolean gpxParsedProperly;
     36   
     37    protected enum Compression {
     38        NONE,
     39        BZIP2,
     40        GZIP
     41    }
    3642
    3743    /**
     
    162168    }
    163169
     170    /**
     171     * Download OSM files from somewhere
     172     * @param progressMonitor The progress monitor
     173     * @return The corresponding dataset
     174     * @throws OsmTransferException if any error occurs
     175     */
    164176    public abstract DataSet parseOsm(final ProgressMonitor progressMonitor) throws OsmTransferException;
    165177
     178    /**
     179     * Download OSM Change files from somewhere
     180     * @param progressMonitor The progress monitor
     181     * @return The corresponding dataset
     182     * @throws OsmTransferException if any error occurs
     183     */
    166184    public DataSet parseOsmChange(final ProgressMonitor progressMonitor) throws OsmTransferException {
    167185        return null;
    168186    }
    169187
     188    /**
     189     * Download BZip2-compressed OSM Change files from somewhere
     190     * @param progressMonitor The progress monitor
     191     * @return The corresponding dataset
     192     * @throws OsmTransferException if any error occurs
     193     */
    170194    public DataSet parseOsmChangeBzip2(final ProgressMonitor progressMonitor) throws OsmTransferException {
    171195        return null;
    172196    }
    173197
     198    /**
     199     * Download GZip-compressed OSM Change files from somewhere
     200     * @param progressMonitor The progress monitor
     201     * @return The corresponding dataset
     202     * @throws OsmTransferException if any error occurs
     203     */
    174204    public DataSet parseOsmChangeGzip(final ProgressMonitor progressMonitor) throws OsmTransferException {
    175205        return null;
    176206    }
    177207
     208    /**
     209     * Retrieve raw gps waypoints from the server API.
     210     * @param progressMonitor The progress monitor
     211     * @return The corresponding GPX tracks
     212     * @throws OsmTransferException if any error occurs
     213     */
    178214    public GpxData parseRawGps(final ProgressMonitor progressMonitor) throws OsmTransferException {
    179215        return null;
    180216    }
    181217
     218    /**
     219     * Retrieve BZip2-compressed GPX files from somewhere.
     220     * @param progressMonitor The progress monitor
     221     * @return The corresponding GPX tracks
     222     * @throws OsmTransferException if any error occurs
     223     * @since 6244
     224     */
     225    public GpxData parseRawGpsBzip2(final ProgressMonitor progressMonitor) throws OsmTransferException {
     226        return null;
     227    }
     228
     229    /**
     230     * Download BZip2-compressed OSM files from somewhere
     231     * @param progressMonitor The progress monitor
     232     * @return The corresponding dataset
     233     * @throws OsmTransferException if any error occurs
     234     */
    182235    public DataSet parseOsmBzip2(final ProgressMonitor progressMonitor) throws OsmTransferException {
    183236        return null;
    184237    }
    185238
     239    /**
     240     * Download GZip-compressed OSM files from somewhere
     241     * @param progressMonitor The progress monitor
     242     * @return The corresponding dataset
     243     * @throws OsmTransferException if any error occurs
     244     */
    186245    public DataSet parseOsmGzip(final ProgressMonitor progressMonitor) throws OsmTransferException {
    187246        return null;
Note: See TracChangeset for help on using the changeset viewer.