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