Changeset 1811 in josm for trunk/src/org/openstreetmap/josm/io/MultiFetchServerObjectReader.java
- Timestamp:
- 2009-07-19T17:38:55+02:00 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/io/MultiFetchServerObjectReader.java
r1790 r1811 4 4 import static org.openstreetmap.josm.tools.I18n.tr; 5 5 6 import java.io.IOException;7 6 import java.io.InputStream; 8 7 import java.net.HttpURLConnection; … … 14 13 import java.util.logging.Logger; 15 14 16 import org.openstreetmap.josm.Main;17 15 import org.openstreetmap.josm.data.osm.DataSet; 18 16 import org.openstreetmap.josm.data.osm.Node; … … 23 21 import org.openstreetmap.josm.data.osm.Way; 24 22 import org.openstreetmap.josm.data.osm.visitor.MergeVisitor; 25 import org.xml.sax.SAXException; 23 import org.openstreetmap.josm.gui.progress.NullProgressMonitor; 24 import org.openstreetmap.josm.gui.progress.ProgressMonitor; 26 25 27 26 /** 28 27 * Retrieves a set of {@see OsmPrimitive}s from an OSM server using the so called 29 28 * Multi Fetch API. 30 * 29 * 31 30 * Usage: 32 31 * <pre> … … 42 41 * } 43 42 * </pre> 44 * 43 * 45 44 * 46 45 */ … … 53 52 * which should be safe according to the 54 53 * <a href="http://www.boutell.com/newfaq/misc/urllength.html">WWW FAQ</a>. 55 * 54 * 56 55 */ 57 56 static private int MAX_IDS_PER_REQUEST = 200; … … 79 78 * remembers an {@see OsmPrimitive}'s id and its type. The id will 80 79 * later be fetched as part of a Multi Get request. 81 * 80 * 82 81 * Ignore the id if it id <= 0. 83 * 82 * 84 83 * @param id the id 85 84 * @param type the type … … 100 99 * an {@see OsmPrimitive} with id=<code>id</code>. The id will 101 100 * later we fetched as part of a Multi Get request. 102 * 101 * 103 102 * Ignore the id if it id <= 0. 104 * 103 * 105 104 * @param ds the dataset (must not be null) 106 105 * @param id the id … … 123 122 * appends a list of ids to the list of ids which will be fetched from the server. ds must 124 123 * include an {@see OsmPrimitive} for each id in ids. 125 * 124 * 126 125 * id is ignored if id <= 0. 127 * 126 * 128 127 * @param ds the dataset 129 128 * @param ids the list of ids 130 129 * @return this 131 * 130 * 132 131 */ 133 132 public MultiFetchServerObjectReader append(DataSet ds, long ... ids) { … … 142 141 * appends a collection of ids to the list of ids which will be fetched from the server. ds must 143 142 * include an {@see OsmPrimitive} for each id in ids. 144 * 143 * 145 144 * id is ignored if id <= 0. 146 * 145 * 147 146 * @param ds the dataset 148 147 * @param ids the collection of ids 149 148 * @return this 150 * 149 * 151 150 */ 152 151 public MultiFetchServerObjectReader append(DataSet ds, Collection<Long> ids) { … … 163 162 * @param node the node (ignored, if null) 164 163 * @return this 165 * 164 * 166 165 */ 167 166 public MultiFetchServerObjectReader append(Node node) { … … 177 176 * @param way the way (ignored, if null) 178 177 * @return this 179 * 178 * 180 179 */ 181 180 public MultiFetchServerObjectReader append(Way way) { … … 196 195 * @param relation the relation (ignored, if null) 197 196 * @return this 198 * 197 * 199 198 */ 200 199 public MultiFetchServerObjectReader append(Relation relation) { … … 225 224 * @param primitives the list of primitives (ignored, if null) 226 225 * @return this 227 * 226 * 228 227 * @see #append(Node) 229 228 * @see #append(Way) 230 229 * @see #append(Relation) 231 * 230 * 232 231 */ 233 232 public MultiFetchServerObjectReader append(Collection<OsmPrimitive> primitives) { … … 242 241 * extracts a subset of max {@see #MAX_IDS_PER_REQUEST} ids from <code>ids</code> and 243 242 * replies the subset. The extracted subset is removed from <code>ids</code>. 244 * 243 * 245 244 * @param ids a set of ids 246 245 * @return the subset of ids … … 267 266 * builds the Multi Get request string for a set of ids and a given 268 267 * {@see OsmPrimitiveType}. 269 * 268 * 270 269 * @param type the type 271 270 * @param idPackage the package of ids … … 290 289 * builds the Multi Get request string for a single id and a given 291 290 * {@see OsmPrimitiveType}. 292 * 291 * 293 292 * @param type the type 294 293 * @param id the id … … 306 305 * invokes a Multi Get for a set of ids and a given {@see OsmPrimitiveType}. 307 306 * The retrieved primitives are merged to {@see #outputDataSet}. 308 * 307 * 309 308 * @param type the type 310 309 * @param pkg the package of ids 311 310 * @exception OsmTransferException thrown if an error occurs while communicating with the API server 312 * 313 */ 314 protected void multiGetIdPackage(OsmPrimitiveType type, Set<Long> pkg ) throws OsmTransferException {311 * 312 */ 313 protected void multiGetIdPackage(OsmPrimitiveType type, Set<Long> pkg, ProgressMonitor progressMonitor) throws OsmTransferException { 315 314 String request = buildRequestString(type, pkg); 316 final InputStream in = getInputStream(request, Main.pleaseWaitDlg);315 final InputStream in = getInputStream(request, NullProgressMonitor.INSTANCE); 317 316 if (in == null) return; 318 Main.pleaseWaitDlg.currentAction.setText(tr("Downloading OSM data..."));317 progressMonitor.subTask(tr("Downloading OSM data...")); 319 318 try { 320 final OsmReader osm = OsmReader.parseDataSetOsm(in, Main.pleaseWaitDlg);319 final OsmReader osm = OsmReader.parseDataSetOsm(in, progressMonitor.createSubTaskMonitor(ProgressMonitor.ALL_TICKS, false)); 321 320 merge(osm.getDs()); 322 321 } catch(Exception e) { … … 328 327 * invokes a Multi Get for a single id and a given {@see OsmPrimitiveType}. 329 328 * The retrieved primitive is merged to {@see #outputDataSet}. 330 * 329 * 331 330 * @param type the type 332 331 * @param id the id 333 332 * @exception OsmTransferException thrown if an error occurs while communicating with the API server 334 * 335 */ 336 protected void singleGetId(OsmPrimitiveType type, long id ) throws OsmTransferException {333 * 334 */ 335 protected void singleGetId(OsmPrimitiveType type, long id, ProgressMonitor progressMonitor) throws OsmTransferException { 337 336 String request = buildRequestString(type, id); 338 final InputStream in = getInputStream(request, Main.pleaseWaitDlg);337 final InputStream in = getInputStream(request, NullProgressMonitor.INSTANCE); 339 338 if (in == null) 340 339 return; 341 Main.pleaseWaitDlg.currentAction.setText(tr("Downloading OSM data..."));340 progressMonitor.subTask(tr("Downloading OSM data...")); 342 341 try { 343 final OsmReader osm = OsmReader.parseDataSetOsm(in, Main.pleaseWaitDlg);342 final OsmReader osm = OsmReader.parseDataSetOsm(in, progressMonitor.createSubTaskMonitor(ProgressMonitor.ALL_TICKS, false)); 344 343 merge(osm.getDs()); 345 344 } catch(Exception e) { … … 351 350 * invokes a sequence of Multi Gets for individual ids in a set of ids and a given {@see OsmPrimitiveType}. 352 351 * The retrieved primitives are merged to {@see #outputDataSet}. 353 * 352 * 354 353 * This method is used if one of the ids in pkg doesn't exist (the server replies with return code 404). 355 354 * If the set is fetched with this method it is possible to find out which of the ids doesn't exist. 356 355 * Unfortunatelly, the server does not provide an error header or an error body for a 404 reply. 357 * 356 * 358 357 * @param type the type 359 358 * @param pkg the set of ids 360 359 * @exception OsmTransferException thrown if an error occurs while communicating with the API server 361 * 362 */ 363 protected void singleGetIdPackage(OsmPrimitiveType type, Set<Long> pkg ) throws OsmTransferException {360 * 361 */ 362 protected void singleGetIdPackage(OsmPrimitiveType type, Set<Long> pkg, ProgressMonitor progressMonitor) throws OsmTransferException { 364 363 for (long id : pkg) { 365 364 try { 366 singleGetId(type, id );365 singleGetId(type, id, progressMonitor); 367 366 } catch(OsmApiException e) { 368 367 if (e.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) { … … 378 377 /** 379 378 * merges the dataset <code>from</code> to {@see #outputDataSet}. 380 * 379 * 381 380 * @param from the other dataset 382 * 381 * 383 382 */ 384 383 protected void merge(DataSet from) { … … 389 388 /** 390 389 * fetches a set of ids of a given {@see OsmPrimitiveType} from the server 391 * 390 * 392 391 * @param ids the set of ids 393 392 * @param type the type 394 393 * @exception OsmTransferException thrown if an error occurs while communicating with the API server 395 394 */ 396 protected void fetchPrimitives(Set<Long> ids, OsmPrimitiveType type ) throws OsmTransferException{395 protected void fetchPrimitives(Set<Long> ids, OsmPrimitiveType type, ProgressMonitor progressMonitor) throws OsmTransferException{ 397 396 Set<Long> toFetch = new HashSet<Long>(ids); 398 397 toFetch.addAll(ids); … … 400 399 Set<Long> pkg = extractIdPackage(toFetch); 401 400 try { 402 multiGetIdPackage(type, pkg );401 multiGetIdPackage(type, pkg, progressMonitor); 403 402 } catch(OsmApiException e) { 404 403 if (e.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) { 405 404 logger.warning(tr("Server replied with response code 404, retrying with an individual request for each primitive")); 406 singleGetIdPackage(type, pkg );405 singleGetIdPackage(type, pkg, progressMonitor); 407 406 } else 408 407 throw e; … … 417 416 * the latest version of the primitive (if any), even if the primitive is not visible (i.e. if 418 417 * visible==false). 419 * 418 * 420 419 * Invoke {@see #getMissingPrimitives()} to get a list of primitives which have not been 421 420 * found on the server (the server response code was 404) 422 * 421 * 423 422 * Invoke {@see #getSkippedWay()} to get a list of ways which this reader could not build from 424 423 * the fetched data because the ways refer to nodes which don't exist on the server. 425 * 424 * 426 425 * @return the parsed data 427 426 * @exception OsmTransferException thrown if an error occurs while communicating with the API server 428 427 * @see #getMissingPrimitives() 429 428 * @see #getSkippedWays() 430 * 429 * 431 430 432 431 */ 433 432 @Override 434 public DataSet parseOsm() throws OsmTransferException { 435 missingPrimitives = new HashSet<Long>(); 436 437 fetchPrimitives(nodes,OsmPrimitiveType.NODE); 438 fetchPrimitives(ways,OsmPrimitiveType.WAY); 439 fetchPrimitives(relations,OsmPrimitiveType.RELATION); 440 return outputDataSet; 433 public DataSet parseOsm(ProgressMonitor progressMonitor) throws OsmTransferException { 434 progressMonitor.beginTask(""); 435 try { 436 missingPrimitives = new HashSet<Long>(); 437 438 fetchPrimitives(nodes,OsmPrimitiveType.NODE, progressMonitor); 439 fetchPrimitives(ways,OsmPrimitiveType.WAY, progressMonitor); 440 fetchPrimitives(relations,OsmPrimitiveType.RELATION, progressMonitor); 441 return outputDataSet; 442 } finally { 443 progressMonitor.finishTask(); 444 } 441 445 } 442 446 … … 445 449 * server was submitted but which are not available from the server (the server 446 450 * replied a return code of 404) 447 * 451 * 448 452 * @return the set of ids of missing primitives 449 453 */
Note:
See TracChangeset
for help on using the changeset viewer.