Ignore:
Timestamp:
2016-01-01T18:52:02+01:00 (4 years ago)
Author:
simon04
Message:

see #7670 - Possibility to download from mirror with all download action

Set preference key download.overpass.for-multi-fetch to true in order to use Overpass API

Location:
trunk/src/org/openstreetmap/josm/io
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/io/MultiFetchServerObjectReader.java

    r9231 r9241  
    1010import java.util.ArrayList;
    1111import java.util.Collection;
     12import java.util.Collections;
    1213import java.util.HashSet;
    1314import java.util.Iterator;
     
    7475     * Constructs a {@code MultiFetchServerObjectReader}.
    7576     */
    76     public MultiFetchServerObjectReader() {
     77    protected MultiFetchServerObjectReader() {
    7778        nodes = new LinkedHashSet<>();
    7879        ways = new LinkedHashSet<>();
     
    8081        this.outputDataSet = new DataSet();
    8182        this.missingPrimitives = new LinkedHashSet<>();
     83    }
     84
     85    /**
     86     * Creates a new instance of {@link MultiFetchServerObjectReader} or {@link MultiFetchOverpassObjectReader}
     87     * depending on the {@code download.overpass.for-multi-fetch} preference.
     88     *
     89     * @return a new instance
     90     * @since 9241
     91     */
     92    public static MultiFetchServerObjectReader create() {
     93        return create(Main.pref.getBoolean("download.overpass.for-multi-fetch", false));
     94    }
     95
     96    /**
     97     * Creates a new instance of {@link MultiFetchServerObjectReader} or {@link MultiFetchOverpassObjectReader}
     98     * depending on the {@code fromMirror} parameter.
     99     *
     100     * @param fromMirror {@code false} for {@link MultiFetchServerObjectReader}, {@code true} for {@link MultiFetchOverpassObjectReader}
     101     * @return a new instance
     102     * @since 9241
     103     */
     104    static MultiFetchServerObjectReader create(final boolean fromMirror) {
     105        if (fromMirror) {
     106            return new MultiFetchOverpassObjectReader();
     107        } else {
     108            return new MultiFetchServerObjectReader();
     109        }
    82110    }
    83111
     
    240268     * @return the request string
    241269     */
    242     protected static String buildRequestString(OsmPrimitiveType type, Set<Long> idPackage) {
    243         StringBuilder sb = new StringBuilder();
    244         sb.append(type.getAPIName()).append("s?")
    245         .append(type.getAPIName()).append("s=");
    246 
    247         Iterator<Long> it = idPackage.iterator();
    248         for (int i = 0; i < idPackage.size(); i++) {
    249             sb.append(it.next());
    250             if (i < idPackage.size()-1) {
    251                 sb.append(',');
    252             }
    253         }
    254         return sb.toString();
    255     }
    256 
    257     /**
    258      * builds the Multi Get request string for a single id and a given {@link OsmPrimitiveType}.
    259      *
    260      * @param type The primitive type. Must be one of {@link OsmPrimitiveType#NODE NODE}, {@link OsmPrimitiveType#WAY WAY},
    261      * {@link OsmPrimitiveType#RELATION RELATION}
    262      * @param id the id
    263      * @return the request string
    264      */
    265     protected static String buildRequestString(OsmPrimitiveType type, long id) {
    266         StringBuilder sb = new StringBuilder();
    267         sb.append(type.getAPIName()).append("s?")
    268         .append(type.getAPIName()).append("s=")
    269         .append(id);
    270         return sb.toString();
     270    protected String buildRequestString(final OsmPrimitiveType type, Set<Long> idPackage) {
     271        return type.getAPIName() + "s?" + type.getAPIName() + "s=" + Utils.join(",", idPackage);
     272    }
     273
     274    @Override
     275    protected String getBaseUrl() {
     276        return super.getBaseUrl();
    271277    }
    272278
     
    304310    protected void fetchPrimitives(Set<Long> ids, OsmPrimitiveType type, ProgressMonitor progressMonitor) throws OsmTransferException {
    305311        String msg = "";
    306         String baseUrl = OsmApi.getOsmApi().getBaseUrl();
     312        final String baseUrl = getBaseUrl();
    307313        switch (type) {
    308314            case NODE:     msg = tr("Fetching a package of nodes from ''{0}''",     baseUrl); break;
     
    429435     * @see FetchResult
    430436     */
    431     protected static class Fetcher extends OsmServerReader implements Callable<FetchResult> {
     437    protected class Fetcher extends OsmServerReader implements Callable<FetchResult> {
    432438
    433439        private final Set<Long> pkg;
     
    477483                }
    478484            }
     485        }
     486
     487        @Override
     488        protected String getBaseUrl() {
     489            return MultiFetchServerObjectReader.this.getBaseUrl();
    479490        }
    480491
     
    520531         */
    521532        protected DataSet singleGetId(OsmPrimitiveType type, long id, ProgressMonitor progressMonitor) throws OsmTransferException {
    522             String request = buildRequestString(type, id);
     533            String request = buildRequestString(type, Collections.singleton(id));
    523534            DataSet result = null;
    524535            try (InputStream in = getInputStream(request, NullProgressMonitor.INSTANCE)) {
Note: See TracChangeset for help on using the changeset viewer.