[2512] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.io;
|
---|
| 3 |
|
---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
[2563] | 5 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
[2512] | 6 |
|
---|
[7033] | 7 | import java.io.IOException;
|
---|
[2512] | 8 | import java.io.InputStream;
|
---|
| 9 | import java.net.HttpURLConnection;
|
---|
[5387] | 10 | import java.util.ArrayList;
|
---|
[2512] | 11 | import java.util.Collection;
|
---|
[9241] | 12 | import java.util.Collections;
|
---|
[2512] | 13 | import java.util.HashSet;
|
---|
| 14 | import java.util.Iterator;
|
---|
[4081] | 15 | import java.util.LinkedHashSet;
|
---|
[5387] | 16 | import java.util.List;
|
---|
[2512] | 17 | import java.util.Set;
|
---|
[5387] | 18 | import java.util.concurrent.Callable;
|
---|
| 19 | import java.util.concurrent.CompletionService;
|
---|
| 20 | import java.util.concurrent.ExecutionException;
|
---|
| 21 | import java.util.concurrent.ExecutorCompletionService;
|
---|
[9324] | 22 | import java.util.concurrent.ExecutorService;
|
---|
[5387] | 23 | import java.util.concurrent.Executors;
|
---|
| 24 | import java.util.concurrent.Future;
|
---|
[2512] | 25 |
|
---|
| 26 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
| 27 | import org.openstreetmap.josm.data.osm.DataSetMerger;
|
---|
| 28 | import org.openstreetmap.josm.data.osm.Node;
|
---|
| 29 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
| 30 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
---|
[2599] | 31 | import org.openstreetmap.josm.data.osm.PrimitiveId;
|
---|
[2512] | 32 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
| 33 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
[4081] | 34 | import org.openstreetmap.josm.data.osm.SimplePrimitiveId;
|
---|
[2512] | 35 | import org.openstreetmap.josm.data.osm.Way;
|
---|
| 36 | import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
|
---|
| 37 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
[12846] | 38 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
[12620] | 39 | import org.openstreetmap.josm.tools.Logging;
|
---|
[8734] | 40 | import org.openstreetmap.josm.tools.Utils;
|
---|
[2512] | 41 |
|
---|
| 42 | /**
|
---|
[5266] | 43 | * Retrieves a set of {@link OsmPrimitive}s from an OSM server using the so called
|
---|
[2512] | 44 | * Multi Fetch API.
|
---|
| 45 | *
|
---|
| 46 | * Usage:
|
---|
| 47 | * <pre>
|
---|
| 48 | * MultiFetchServerObjectReader reader = MultiFetchServerObjectReader()
|
---|
| 49 | * .append(2345,2334,4444)
|
---|
| 50 | * .append(new Node(72343));
|
---|
| 51 | * reader.parseOsm();
|
---|
| 52 | * if (!reader.getMissingPrimitives().isEmpty()) {
|
---|
[12620] | 53 | * Logging.info("There are missing primitives: " + reader.getMissingPrimitives());
|
---|
[2512] | 54 | * }
|
---|
| 55 | * if (!reader.getSkippedWays().isEmpty()) {
|
---|
[12620] | 56 | * Logging.info("There are skipped ways: " + reader.getMissingPrimitives());
|
---|
[2512] | 57 | * }
|
---|
| 58 | * </pre>
|
---|
| 59 | */
|
---|
[9059] | 60 | public class MultiFetchServerObjectReader extends OsmServerReader {
|
---|
[2512] | 61 | /**
|
---|
| 62 | * the max. number of primitives retrieved in one step. Assuming IDs with 7 digits,
|
---|
[2771] | 63 | * this leads to a max. request URL of ~ 1600 Bytes ((7 digits + 1 Separator) * 200),
|
---|
[2512] | 64 | * which should be safe according to the
|
---|
| 65 | * <a href="http://www.boutell.com/newfaq/misc/urllength.html">WWW FAQ</a>.
|
---|
| 66 | */
|
---|
[6792] | 67 | private static final int MAX_IDS_PER_REQUEST = 200;
|
---|
[2512] | 68 |
|
---|
[9078] | 69 | private final Set<Long> nodes;
|
---|
| 70 | private final Set<Long> ways;
|
---|
| 71 | private final Set<Long> relations;
|
---|
[4081] | 72 | private Set<PrimitiveId> missingPrimitives;
|
---|
[9078] | 73 | private final DataSet outputDataSet;
|
---|
[2512] | 74 |
|
---|
| 75 | /**
|
---|
[5387] | 76 | * Constructs a {@code MultiFetchServerObjectReader}.
|
---|
[2512] | 77 | */
|
---|
[9241] | 78 | protected MultiFetchServerObjectReader() {
|
---|
[7005] | 79 | nodes = new LinkedHashSet<>();
|
---|
| 80 | ways = new LinkedHashSet<>();
|
---|
| 81 | relations = new LinkedHashSet<>();
|
---|
[2512] | 82 | this.outputDataSet = new DataSet();
|
---|
[7005] | 83 | this.missingPrimitives = new LinkedHashSet<>();
|
---|
[2512] | 84 | }
|
---|
| 85 |
|
---|
| 86 | /**
|
---|
[9241] | 87 | * Creates a new instance of {@link MultiFetchServerObjectReader} or {@link MultiFetchOverpassObjectReader}
|
---|
[12816] | 88 | * depending on the {@link OverpassDownloadReader#FOR_MULTI_FETCH preference}.
|
---|
[9241] | 89 | *
|
---|
| 90 | * @return a new instance
|
---|
| 91 | * @since 9241
|
---|
| 92 | */
|
---|
| 93 | public static MultiFetchServerObjectReader create() {
|
---|
[12816] | 94 | return create(OverpassDownloadReader.FOR_MULTI_FETCH.get());
|
---|
[9241] | 95 | }
|
---|
| 96 |
|
---|
| 97 | /**
|
---|
| 98 | * Creates a new instance of {@link MultiFetchServerObjectReader} or {@link MultiFetchOverpassObjectReader}
|
---|
| 99 | * depending on the {@code fromMirror} parameter.
|
---|
| 100 | *
|
---|
| 101 | * @param fromMirror {@code false} for {@link MultiFetchServerObjectReader}, {@code true} for {@link MultiFetchOverpassObjectReader}
|
---|
| 102 | * @return a new instance
|
---|
| 103 | * @since 9241
|
---|
| 104 | */
|
---|
| 105 | static MultiFetchServerObjectReader create(final boolean fromMirror) {
|
---|
| 106 | if (fromMirror) {
|
---|
| 107 | return new MultiFetchOverpassObjectReader();
|
---|
| 108 | } else {
|
---|
| 109 | return new MultiFetchServerObjectReader();
|
---|
| 110 | }
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | /**
|
---|
[5266] | 114 | * Remembers an {@link OsmPrimitive}'s id. The id will
|
---|
[2512] | 115 | * later be fetched as part of a Multi Get request.
|
---|
| 116 | *
|
---|
[2599] | 117 | * Ignore the id if it represents a new primitives.
|
---|
[2512] | 118 | *
|
---|
| 119 | * @param id the id
|
---|
| 120 | */
|
---|
[2599] | 121 | protected void remember(PrimitiveId id) {
|
---|
| 122 | if (id.isNew()) return;
|
---|
| 123 | switch(id.getType()) {
|
---|
| 124 | case NODE: nodes.add(id.getUniqueId()); break;
|
---|
| 125 | case WAY: ways.add(id.getUniqueId()); break;
|
---|
| 126 | case RELATION: relations.add(id.getUniqueId()); break;
|
---|
[10216] | 127 | default: throw new AssertionError();
|
---|
[2512] | 128 | }
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | /**
|
---|
[5387] | 132 | * appends a {@link OsmPrimitive} id to the list of ids which will be fetched from the server.
|
---|
[6070] | 133 | *
|
---|
[5387] | 134 | * @param ds the {@link DataSet} to which the primitive belongs
|
---|
| 135 | * @param id the primitive id
|
---|
[8509] | 136 | * @param type The primitive type. Must be one of {@link OsmPrimitiveType#NODE NODE}, {@link OsmPrimitiveType#WAY WAY},
|
---|
| 137 | * {@link OsmPrimitiveType#RELATION RELATION}
|
---|
[5387] | 138 | * @return this
|
---|
| 139 | */
|
---|
[2512] | 140 | public MultiFetchServerObjectReader append(DataSet ds, long id, OsmPrimitiveType type) {
|
---|
[8510] | 141 | OsmPrimitive p = ds.getPrimitiveById(id, type);
|
---|
[2512] | 142 | switch(type) {
|
---|
| 143 | case NODE:
|
---|
[8510] | 144 | return appendNode((Node) p);
|
---|
[2512] | 145 | case WAY:
|
---|
[8510] | 146 | return appendWay((Way) p);
|
---|
[2512] | 147 | case RELATION:
|
---|
[8510] | 148 | return appendRelation((Relation) p);
|
---|
[10671] | 149 | default:
|
---|
| 150 | return this;
|
---|
[2512] | 151 | }
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | /**
|
---|
[5387] | 155 | * appends a {@link Node} id to the list of ids which will be fetched from the server.
|
---|
[2512] | 156 | *
|
---|
| 157 | * @param node the node (ignored, if null)
|
---|
| 158 | * @return this
|
---|
| 159 | */
|
---|
[4081] | 160 | public MultiFetchServerObjectReader appendNode(Node node) {
|
---|
[2512] | 161 | if (node == null) return this;
|
---|
[2599] | 162 | remember(node.getPrimitiveId());
|
---|
[2512] | 163 | return this;
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 | /**
|
---|
[5387] | 167 | * appends a {@link Way} id and the list of ids of nodes the way refers to the list of ids which will be fetched from the server.
|
---|
[2512] | 168 | *
|
---|
| 169 | * @param way the way (ignored, if null)
|
---|
| 170 | * @return this
|
---|
| 171 | */
|
---|
[4081] | 172 | public MultiFetchServerObjectReader appendWay(Way way) {
|
---|
[2512] | 173 | if (way == null) return this;
|
---|
| 174 | if (way.isNew()) return this;
|
---|
[9325] | 175 | for (Node node: !recursesDown() ? way.getNodes() : Collections.<Node>emptyList()) {
|
---|
[2512] | 176 | if (!node.isNew()) {
|
---|
[2599] | 177 | remember(node.getPrimitiveId());
|
---|
[2512] | 178 | }
|
---|
| 179 | }
|
---|
[2599] | 180 | remember(way.getPrimitiveId());
|
---|
[2512] | 181 | return this;
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | /**
|
---|
[5387] | 185 | * appends a {@link Relation} id to the list of ids which will be fetched from the server.
|
---|
[2512] | 186 | *
|
---|
| 187 | * @param relation the relation (ignored, if null)
|
---|
| 188 | * @return this
|
---|
| 189 | */
|
---|
[4081] | 190 | protected MultiFetchServerObjectReader appendRelation(Relation relation) {
|
---|
[2512] | 191 | if (relation == null) return this;
|
---|
| 192 | if (relation.isNew()) return this;
|
---|
[2599] | 193 | remember(relation.getPrimitiveId());
|
---|
[9325] | 194 | for (RelationMember member : !recursesDown() ? relation.getMembers() : Collections.<RelationMember>emptyList()) {
|
---|
[11386] | 195 | // avoid infinite recursion in case of cyclic dependencies in relations
|
---|
| 196 | if (OsmPrimitiveType.from(member.getMember()).equals(OsmPrimitiveType.RELATION)
|
---|
| 197 | && relations.contains(member.getMember().getId())) {
|
---|
| 198 | continue;
|
---|
[2512] | 199 | }
|
---|
[2609] | 200 | if (!member.getMember().isIncomplete()) {
|
---|
[4081] | 201 | append(member.getMember());
|
---|
[2609] | 202 | }
|
---|
[2512] | 203 | }
|
---|
| 204 | return this;
|
---|
| 205 | }
|
---|
| 206 |
|
---|
[5387] | 207 | /**
|
---|
| 208 | * appends an {@link OsmPrimitive} to the list of ids which will be fetched from the server.
|
---|
| 209 | * @param primitive the primitive
|
---|
| 210 | * @return this
|
---|
| 211 | */
|
---|
[4081] | 212 | public MultiFetchServerObjectReader append(OsmPrimitive primitive) {
|
---|
[11809] | 213 | if (primitive instanceof Node) {
|
---|
| 214 | return appendNode((Node) primitive);
|
---|
| 215 | } else if (primitive instanceof Way) {
|
---|
| 216 | return appendWay((Way) primitive);
|
---|
| 217 | } else if (primitive instanceof Relation) {
|
---|
| 218 | return appendRelation((Relation) primitive);
|
---|
[5387] | 219 | }
|
---|
[2512] | 220 | return this;
|
---|
| 221 | }
|
---|
| 222 |
|
---|
| 223 | /**
|
---|
[5266] | 224 | * appends a list of {@link OsmPrimitive} to the list of ids which will be fetched from the server.
|
---|
[2512] | 225 | *
|
---|
| 226 | * @param primitives the list of primitives (ignored, if null)
|
---|
| 227 | * @return this
|
---|
| 228 | *
|
---|
[5387] | 229 | * @see #append(OsmPrimitive)
|
---|
[2512] | 230 | */
|
---|
| 231 | public MultiFetchServerObjectReader append(Collection<? extends OsmPrimitive> primitives) {
|
---|
| 232 | if (primitives == null) return this;
|
---|
| 233 | for (OsmPrimitive primitive : primitives) {
|
---|
[4081] | 234 | append(primitive);
|
---|
[2512] | 235 | }
|
---|
| 236 | return this;
|
---|
| 237 | }
|
---|
| 238 |
|
---|
| 239 | /**
|
---|
[5266] | 240 | * extracts a subset of max {@link #MAX_IDS_PER_REQUEST} ids from <code>ids</code> and
|
---|
[2512] | 241 | * replies the subset. The extracted subset is removed from <code>ids</code>.
|
---|
| 242 | *
|
---|
| 243 | * @param ids a set of ids
|
---|
| 244 | * @return the subset of ids
|
---|
| 245 | */
|
---|
| 246 | protected Set<Long> extractIdPackage(Set<Long> ids) {
|
---|
[8338] | 247 | Set<Long> pkg = new HashSet<>();
|
---|
[2512] | 248 | if (ids.isEmpty())
|
---|
| 249 | return pkg;
|
---|
| 250 | if (ids.size() > MAX_IDS_PER_REQUEST) {
|
---|
| 251 | Iterator<Long> it = ids.iterator();
|
---|
[8510] | 252 | for (int i = 0; i < MAX_IDS_PER_REQUEST; i++) {
|
---|
[2512] | 253 | pkg.add(it.next());
|
---|
| 254 | }
|
---|
| 255 | ids.removeAll(pkg);
|
---|
| 256 | } else {
|
---|
| 257 | pkg.addAll(ids);
|
---|
| 258 | ids.clear();
|
---|
| 259 | }
|
---|
| 260 | return pkg;
|
---|
| 261 | }
|
---|
| 262 |
|
---|
| 263 | /**
|
---|
[8390] | 264 | * builds the Multi Get request string for a set of ids and a given {@link OsmPrimitiveType}.
|
---|
[2512] | 265 | *
|
---|
[8509] | 266 | * @param type The primitive type. Must be one of {@link OsmPrimitiveType#NODE NODE}, {@link OsmPrimitiveType#WAY WAY},
|
---|
| 267 | * {@link OsmPrimitiveType#RELATION RELATION}
|
---|
[2512] | 268 | * @param idPackage the package of ids
|
---|
| 269 | * @return the request string
|
---|
| 270 | */
|
---|
[9241] | 271 | protected String buildRequestString(final OsmPrimitiveType type, Set<Long> idPackage) {
|
---|
| 272 | return type.getAPIName() + "s?" + type.getAPIName() + "s=" + Utils.join(",", idPackage);
|
---|
[2512] | 273 | }
|
---|
| 274 |
|
---|
[2563] | 275 | protected void rememberNodesOfIncompleteWaysToLoad(DataSet from) {
|
---|
| 276 | for (Way w: from.getWays()) {
|
---|
[2609] | 277 | if (w.hasIncompleteNodes()) {
|
---|
[2563] | 278 | for (Node n: w.getNodes()) {
|
---|
[2578] | 279 | if (n.isIncomplete()) {
|
---|
[2563] | 280 | nodes.add(n.getId());
|
---|
| 281 | }
|
---|
| 282 | }
|
---|
| 283 | }
|
---|
| 284 | }
|
---|
| 285 | }
|
---|
| 286 |
|
---|
[2512] | 287 | /**
|
---|
[5266] | 288 | * merges the dataset <code>from</code> to {@link #outputDataSet}.
|
---|
[2512] | 289 | *
|
---|
| 290 | * @param from the other dataset
|
---|
| 291 | */
|
---|
| 292 | protected void merge(DataSet from) {
|
---|
[8510] | 293 | final DataSetMerger visitor = new DataSetMerger(outputDataSet, from);
|
---|
[2512] | 294 | visitor.merge();
|
---|
| 295 | }
|
---|
| 296 |
|
---|
| 297 | /**
|
---|
[5266] | 298 | * fetches a set of ids of a given {@link OsmPrimitiveType} from the server
|
---|
[2512] | 299 | *
|
---|
| 300 | * @param ids the set of ids
|
---|
[8509] | 301 | * @param type The primitive type. Must be one of {@link OsmPrimitiveType#NODE NODE}, {@link OsmPrimitiveType#WAY WAY},
|
---|
| 302 | * {@link OsmPrimitiveType#RELATION RELATION}
|
---|
[9231] | 303 | * @param progressMonitor progress monitor
|
---|
[5387] | 304 | * @throws OsmTransferException if an error occurs while communicating with the API server
|
---|
[2512] | 305 | */
|
---|
[5387] | 306 | protected void fetchPrimitives(Set<Long> ids, OsmPrimitiveType type, ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
[10217] | 307 | String msg;
|
---|
[9241] | 308 | final String baseUrl = getBaseUrl();
|
---|
[5387] | 309 | switch (type) {
|
---|
[10378] | 310 | // CHECKSTYLE.OFF: SingleSpaceSeparator
|
---|
[5387] | 311 | case NODE: msg = tr("Fetching a package of nodes from ''{0}''", baseUrl); break;
|
---|
| 312 | case WAY: msg = tr("Fetching a package of ways from ''{0}''", baseUrl); break;
|
---|
| 313 | case RELATION: msg = tr("Fetching a package of relations from ''{0}''", baseUrl); break;
|
---|
[10378] | 314 | // CHECKSTYLE.ON: SingleSpaceSeparator
|
---|
[10217] | 315 | default: throw new AssertionError();
|
---|
[2512] | 316 | }
|
---|
[4217] | 317 | progressMonitor.setTicksCount(ids.size());
|
---|
| 318 | progressMonitor.setTicks(0);
|
---|
[8734] | 319 | // The complete set containing all primitives to fetch
|
---|
[7005] | 320 | Set<Long> toFetch = new HashSet<>(ids);
|
---|
[5387] | 321 | // Build a list of fetchers that will download smaller sets containing only MAX_IDS_PER_REQUEST (200) primitives each.
|
---|
| 322 | // we will run up to MAX_DOWNLOAD_THREADS concurrent fetchers.
|
---|
[12846] | 323 | int threadsNumber = Config.getPref().getInt("osm.download.threads", OsmApi.MAX_DOWNLOAD_THREADS);
|
---|
[11702] | 324 | threadsNumber = Utils.clamp(threadsNumber, 1, OsmApi.MAX_DOWNLOAD_THREADS);
|
---|
[9324] | 325 | final ExecutorService exec = Executors.newFixedThreadPool(
|
---|
| 326 | threadsNumber, Utils.newThreadFactory(getClass() + "-%d", Thread.NORM_PRIORITY));
|
---|
[7005] | 327 | CompletionService<FetchResult> ecs = new ExecutorCompletionService<>(exec);
|
---|
| 328 | List<Future<FetchResult>> jobs = new ArrayList<>();
|
---|
[5387] | 329 | while (!toFetch.isEmpty()) {
|
---|
| 330 | jobs.add(ecs.submit(new Fetcher(type, extractIdPackage(toFetch), progressMonitor)));
|
---|
| 331 | }
|
---|
| 332 | // Run the fetchers
|
---|
| 333 | for (int i = 0; i < jobs.size() && !isCanceled(); i++) {
|
---|
[8846] | 334 | progressMonitor.subTask(msg + "... " + progressMonitor.getTicks() + '/' + progressMonitor.getTicksCount());
|
---|
[2512] | 335 | try {
|
---|
[5387] | 336 | FetchResult result = ecs.take().get();
|
---|
| 337 | if (result.missingPrimitives != null) {
|
---|
| 338 | missingPrimitives.addAll(result.missingPrimitives);
|
---|
| 339 | }
|
---|
| 340 | if (result.dataSet != null && !isCanceled()) {
|
---|
| 341 | rememberNodesOfIncompleteWaysToLoad(result.dataSet);
|
---|
| 342 | merge(result.dataSet);
|
---|
| 343 | }
|
---|
[7004] | 344 | } catch (InterruptedException | ExecutionException e) {
|
---|
[12620] | 345 | Logging.error(e);
|
---|
[2512] | 346 | }
|
---|
| 347 | }
|
---|
[9324] | 348 | exec.shutdown();
|
---|
| 349 | // Cancel requests if the user chose to
|
---|
[5387] | 350 | if (isCanceled()) {
|
---|
| 351 | for (Future<FetchResult> job : jobs) {
|
---|
| 352 | job.cancel(true);
|
---|
| 353 | }
|
---|
| 354 | }
|
---|
[2512] | 355 | }
|
---|
| 356 |
|
---|
| 357 | /**
|
---|
[5266] | 358 | * invokes one or more Multi Gets to fetch the {@link OsmPrimitive}s and replies
|
---|
[2512] | 359 | * the dataset of retrieved primitives. Note that the dataset includes non visible primitives too!
|
---|
| 360 | * In contrast to a simple Get for a node, a way, or a relation, a Multi Get always replies
|
---|
| 361 | * the latest version of the primitive (if any), even if the primitive is not visible (i.e. if
|
---|
| 362 | * visible==false).
|
---|
| 363 | *
|
---|
[5266] | 364 | * Invoke {@link #getMissingPrimitives()} to get a list of primitives which have not been
|
---|
[2512] | 365 | * found on the server (the server response code was 404)
|
---|
| 366 | *
|
---|
| 367 | * @return the parsed data
|
---|
[5387] | 368 | * @throws OsmTransferException if an error occurs while communicating with the API server
|
---|
[2512] | 369 | * @see #getMissingPrimitives()
|
---|
| 370 | *
|
---|
| 371 | */
|
---|
| 372 | @Override
|
---|
| 373 | public DataSet parseOsm(ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
[2563] | 374 | int n = nodes.size() + ways.size() + relations.size();
|
---|
[7432] | 375 | progressMonitor.beginTask(trn("Downloading {0} object from ''{1}''",
|
---|
| 376 | "Downloading {0} objects from ''{1}''", n, n, OsmApi.getOsmApi().getBaseUrl()));
|
---|
[2512] | 377 | try {
|
---|
[7005] | 378 | missingPrimitives = new HashSet<>();
|
---|
[5387] | 379 | if (isCanceled()) return null;
|
---|
[8510] | 380 | fetchPrimitives(ways, OsmPrimitiveType.WAY, progressMonitor);
|
---|
[5387] | 381 | if (isCanceled()) return null;
|
---|
[8510] | 382 | fetchPrimitives(nodes, OsmPrimitiveType.NODE, progressMonitor);
|
---|
[5387] | 383 | if (isCanceled()) return null;
|
---|
[8510] | 384 | fetchPrimitives(relations, OsmPrimitiveType.RELATION, progressMonitor);
|
---|
[3362] | 385 | if (outputDataSet != null) {
|
---|
| 386 | outputDataSet.deleteInvisible();
|
---|
| 387 | }
|
---|
[2512] | 388 | return outputDataSet;
|
---|
| 389 | } finally {
|
---|
| 390 | progressMonitor.finishTask();
|
---|
| 391 | }
|
---|
| 392 | }
|
---|
| 393 |
|
---|
| 394 | /**
|
---|
| 395 | * replies the set of ids of all primitives for which a fetch request to the
|
---|
| 396 | * server was submitted but which are not available from the server (the server
|
---|
| 397 | * replied a return code of 404)
|
---|
| 398 | *
|
---|
| 399 | * @return the set of ids of missing primitives
|
---|
| 400 | */
|
---|
[4081] | 401 | public Set<PrimitiveId> getMissingPrimitives() {
|
---|
[2512] | 402 | return missingPrimitives;
|
---|
| 403 | }
|
---|
[6070] | 404 |
|
---|
[5387] | 405 | /**
|
---|
[9325] | 406 | * Whether this reader fetches nodes when loading ways, or members when loading relations.
|
---|
| 407 | *
|
---|
| 408 | * @return {@code true} if the reader recurses down
|
---|
| 409 | */
|
---|
| 410 | protected boolean recursesDown() {
|
---|
| 411 | return false;
|
---|
| 412 | }
|
---|
| 413 |
|
---|
| 414 | /**
|
---|
[6070] | 415 | * The class holding the results given by {@link Fetcher}.
|
---|
[5387] | 416 | * It is only a wrapper of the resulting {@link DataSet} and the collection of {@link PrimitiveId} that could not have been loaded.
|
---|
| 417 | */
|
---|
| 418 | protected static class FetchResult {
|
---|
[6070] | 419 |
|
---|
[5387] | 420 | /**
|
---|
| 421 | * The resulting data set
|
---|
| 422 | */
|
---|
| 423 | public final DataSet dataSet;
|
---|
[6070] | 424 |
|
---|
[5387] | 425 | /**
|
---|
| 426 | * The collection of primitive ids that could not have been loaded
|
---|
| 427 | */
|
---|
| 428 | public final Set<PrimitiveId> missingPrimitives;
|
---|
[6070] | 429 |
|
---|
[5387] | 430 | /**
|
---|
| 431 | * Constructs a {@code FetchResult}
|
---|
| 432 | * @param dataSet The resulting data set
|
---|
| 433 | * @param missingPrimitives The collection of primitive ids that could not have been loaded
|
---|
| 434 | */
|
---|
| 435 | public FetchResult(DataSet dataSet, Set<PrimitiveId> missingPrimitives) {
|
---|
| 436 | this.dataSet = dataSet;
|
---|
| 437 | this.missingPrimitives = missingPrimitives;
|
---|
| 438 | }
|
---|
| 439 | }
|
---|
[6070] | 440 |
|
---|
[5387] | 441 | /**
|
---|
[8509] | 442 | * The class that actually download data from OSM API.
|
---|
| 443 | * Several instances of this class are used by {@link MultiFetchServerObjectReader} (one per set of primitives to fetch).
|
---|
[5387] | 444 | * The inheritance of {@link OsmServerReader} is only explained by the need to have a distinct OSM connection by {@code Fetcher} instance.
|
---|
| 445 | * @see FetchResult
|
---|
| 446 | */
|
---|
[9241] | 447 | protected class Fetcher extends OsmServerReader implements Callable<FetchResult> {
|
---|
[5387] | 448 |
|
---|
| 449 | private final Set<Long> pkg;
|
---|
| 450 | private final OsmPrimitiveType type;
|
---|
| 451 | private final ProgressMonitor progressMonitor;
|
---|
| 452 |
|
---|
| 453 | /**
|
---|
| 454 | * Constructs a {@code Fetcher}
|
---|
[8509] | 455 | * @param type The primitive type. Must be one of {@link OsmPrimitiveType#NODE NODE}, {@link OsmPrimitiveType#WAY WAY},
|
---|
| 456 | * {@link OsmPrimitiveType#RELATION RELATION}
|
---|
[5387] | 457 | * @param idsPackage The set of primitives ids to fetch
|
---|
| 458 | * @param progressMonitor The progress monitor
|
---|
| 459 | */
|
---|
| 460 | public Fetcher(OsmPrimitiveType type, Set<Long> idsPackage, ProgressMonitor progressMonitor) {
|
---|
| 461 | this.pkg = idsPackage;
|
---|
| 462 | this.type = type;
|
---|
| 463 | this.progressMonitor = progressMonitor;
|
---|
| 464 | }
|
---|
| 465 |
|
---|
| 466 | @Override
|
---|
| 467 | public DataSet parseOsm(ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
[8509] | 468 | // This method is implemented because of the OsmServerReader inheritance, but not used,
|
---|
| 469 | // as the main target of this class is the call() method.
|
---|
[5387] | 470 | return fetch(progressMonitor).dataSet;
|
---|
| 471 | }
|
---|
[6070] | 472 |
|
---|
[5387] | 473 | @Override
|
---|
| 474 | public FetchResult call() throws Exception {
|
---|
| 475 | return fetch(progressMonitor);
|
---|
| 476 | }
|
---|
[6070] | 477 |
|
---|
[5387] | 478 | /**
|
---|
| 479 | * fetches the requested primitives and updates the specified progress monitor.
|
---|
| 480 | * @param progressMonitor the progress monitor
|
---|
| 481 | * @return the {@link FetchResult} of this operation
|
---|
| 482 | * @throws OsmTransferException if an error occurs while communicating with the API server
|
---|
| 483 | */
|
---|
| 484 | protected FetchResult fetch(ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
| 485 | try {
|
---|
| 486 | return multiGetIdPackage(type, pkg, progressMonitor);
|
---|
| 487 | } catch (OsmApiException e) {
|
---|
| 488 | if (e.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
|
---|
[12620] | 489 | Logging.info(tr("Server replied with response code 404, retrying with an individual request for each object."));
|
---|
[5387] | 490 | return singleGetIdPackage(type, pkg, progressMonitor);
|
---|
| 491 | } else {
|
---|
| 492 | throw e;
|
---|
| 493 | }
|
---|
| 494 | }
|
---|
| 495 | }
|
---|
[6070] | 496 |
|
---|
[9241] | 497 | @Override
|
---|
| 498 | protected String getBaseUrl() {
|
---|
| 499 | return MultiFetchServerObjectReader.this.getBaseUrl();
|
---|
| 500 | }
|
---|
| 501 |
|
---|
[5387] | 502 | /**
|
---|
| 503 | * invokes a Multi Get for a set of ids and a given {@link OsmPrimitiveType}.
|
---|
| 504 | * The retrieved primitives are merged to {@link #outputDataSet}.
|
---|
| 505 | *
|
---|
[8509] | 506 | * @param type The primitive type. Must be one of {@link OsmPrimitiveType#NODE NODE}, {@link OsmPrimitiveType#WAY WAY},
|
---|
| 507 | * {@link OsmPrimitiveType#RELATION RELATION}
|
---|
[5387] | 508 | * @param pkg the package of ids
|
---|
[9231] | 509 | * @param progressMonitor progress monitor
|
---|
[5387] | 510 | * @return the {@link FetchResult} of this operation
|
---|
| 511 | * @throws OsmTransferException if an error occurs while communicating with the API server
|
---|
| 512 | */
|
---|
[8540] | 513 | protected FetchResult multiGetIdPackage(OsmPrimitiveType type, Set<Long> pkg, ProgressMonitor progressMonitor)
|
---|
| 514 | throws OsmTransferException {
|
---|
[5387] | 515 | String request = buildRequestString(type, pkg);
|
---|
[7033] | 516 | FetchResult result = null;
|
---|
| 517 | try (InputStream in = getInputStream(request, NullProgressMonitor.INSTANCE)) {
|
---|
| 518 | if (in == null) return null;
|
---|
| 519 | progressMonitor.subTask(tr("Downloading OSM data..."));
|
---|
| 520 | try {
|
---|
| 521 | result = new FetchResult(OsmReader.parseDataSet(in, progressMonitor.createSubTaskMonitor(pkg.size(), false)), null);
|
---|
[10212] | 522 | } catch (IllegalDataException e) {
|
---|
[7033] | 523 | throw new OsmTransferException(e);
|
---|
| 524 | }
|
---|
| 525 | } catch (IOException ex) {
|
---|
[12620] | 526 | Logging.warn(ex);
|
---|
[5387] | 527 | }
|
---|
[7033] | 528 | return result;
|
---|
[5387] | 529 | }
|
---|
| 530 |
|
---|
| 531 | /**
|
---|
| 532 | * invokes a Multi Get for a single id and a given {@link OsmPrimitiveType}.
|
---|
| 533 | * The retrieved primitive is merged to {@link #outputDataSet}.
|
---|
| 534 | *
|
---|
[8509] | 535 | * @param type The primitive type. Must be one of {@link OsmPrimitiveType#NODE NODE}, {@link OsmPrimitiveType#WAY WAY},
|
---|
| 536 | * {@link OsmPrimitiveType#RELATION RELATION}
|
---|
[5387] | 537 | * @param id the id
|
---|
[9231] | 538 | * @param progressMonitor progress monitor
|
---|
[5387] | 539 | * @return the {@link DataSet} resulting of this operation
|
---|
| 540 | * @throws OsmTransferException if an error occurs while communicating with the API server
|
---|
| 541 | */
|
---|
| 542 | protected DataSet singleGetId(OsmPrimitiveType type, long id, ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
[9241] | 543 | String request = buildRequestString(type, Collections.singleton(id));
|
---|
[7033] | 544 | DataSet result = null;
|
---|
| 545 | try (InputStream in = getInputStream(request, NullProgressMonitor.INSTANCE)) {
|
---|
| 546 | if (in == null) return null;
|
---|
| 547 | progressMonitor.subTask(tr("Downloading OSM data..."));
|
---|
| 548 | try {
|
---|
| 549 | result = OsmReader.parseDataSet(in, progressMonitor.createSubTaskMonitor(1, false));
|
---|
[10212] | 550 | } catch (IllegalDataException e) {
|
---|
[7033] | 551 | throw new OsmTransferException(e);
|
---|
| 552 | }
|
---|
| 553 | } catch (IOException ex) {
|
---|
[12620] | 554 | Logging.warn(ex);
|
---|
[5387] | 555 | }
|
---|
[7033] | 556 | return result;
|
---|
[5387] | 557 | }
|
---|
| 558 |
|
---|
| 559 | /**
|
---|
| 560 | * invokes a sequence of Multi Gets for individual ids in a set of ids and a given {@link OsmPrimitiveType}.
|
---|
| 561 | * The retrieved primitives are merged to {@link #outputDataSet}.
|
---|
| 562 | *
|
---|
| 563 | * This method is used if one of the ids in pkg doesn't exist (the server replies with return code 404).
|
---|
| 564 | * If the set is fetched with this method it is possible to find out which of the ids doesn't exist.
|
---|
| 565 | * Unfortunately, the server does not provide an error header or an error body for a 404 reply.
|
---|
| 566 | *
|
---|
[8509] | 567 | * @param type The primitive type. Must be one of {@link OsmPrimitiveType#NODE NODE}, {@link OsmPrimitiveType#WAY WAY},
|
---|
| 568 | * {@link OsmPrimitiveType#RELATION RELATION}
|
---|
[5387] | 569 | * @param pkg the set of ids
|
---|
[9231] | 570 | * @param progressMonitor progress monitor
|
---|
[5387] | 571 | * @return the {@link FetchResult} of this operation
|
---|
| 572 | * @throws OsmTransferException if an error occurs while communicating with the API server
|
---|
| 573 | */
|
---|
[8540] | 574 | protected FetchResult singleGetIdPackage(OsmPrimitiveType type, Set<Long> pkg, ProgressMonitor progressMonitor)
|
---|
| 575 | throws OsmTransferException {
|
---|
[5387] | 576 | FetchResult result = new FetchResult(new DataSet(), new HashSet<PrimitiveId>());
|
---|
| 577 | String baseUrl = OsmApi.getOsmApi().getBaseUrl();
|
---|
| 578 | for (long id : pkg) {
|
---|
| 579 | try {
|
---|
[10216] | 580 | String msg;
|
---|
[5387] | 581 | switch (type) {
|
---|
[10378] | 582 | // CHECKSTYLE.OFF: SingleSpaceSeparator
|
---|
[5387] | 583 | case NODE: msg = tr("Fetching node with id {0} from ''{1}''", id, baseUrl); break;
|
---|
| 584 | case WAY: msg = tr("Fetching way with id {0} from ''{1}''", id, baseUrl); break;
|
---|
| 585 | case RELATION: msg = tr("Fetching relation with id {0} from ''{1}''", id, baseUrl); break;
|
---|
[10378] | 586 | // CHECKSTYLE.ON: SingleSpaceSeparator
|
---|
[10216] | 587 | default: throw new AssertionError();
|
---|
[5387] | 588 | }
|
---|
| 589 | progressMonitor.setCustomText(msg);
|
---|
| 590 | result.dataSet.mergeFrom(singleGetId(type, id, progressMonitor));
|
---|
| 591 | } catch (OsmApiException e) {
|
---|
| 592 | if (e.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
|
---|
[12620] | 593 | Logging.info(tr("Server replied with response code 404 for id {0}. Skipping.", Long.toString(id)));
|
---|
[5387] | 594 | result.missingPrimitives.add(new SimplePrimitiveId(id, type));
|
---|
| 595 | } else {
|
---|
| 596 | throw e;
|
---|
| 597 | }
|
---|
| 598 | }
|
---|
| 599 | }
|
---|
| 600 | return result;
|
---|
| 601 | }
|
---|
| 602 | }
|
---|
[2512] | 603 | }
|
---|