org.openstreetmap.josm.io
Class MultiFetchServerObjectReader

java.lang.Object
  extended by org.openstreetmap.josm.io.OsmConnection
      extended by org.openstreetmap.josm.io.OsmServerReader
          extended by org.openstreetmap.josm.io.MultiFetchServerObjectReader

public class MultiFetchServerObjectReader
extends OsmServerReader

Retrieves a set of OsmPrimitives from an OSM server using the so called Multi Fetch API. Usage:

    MultiFetchServerObjectReader reader = MultiFetchServerObjectReader()
         .append(2345,2334,4444)
         .append(new Node(72343));
    reader.parseOsm();
    if (!reader.getMissingPrimitives().isEmpty()) {
        System.out.println("There are missing primitives: " + reader.getMissingPrimitives());
    }
    if (!reader.getSkippedWays().isEmpty()) {
       System.out.println("There are skipped ways: " + reader.getMissingPrimitives());
    }
 


Nested Class Summary
protected static class MultiFetchServerObjectReader.Fetcher
          The class that actually download data from OSM API.
protected static class MultiFetchServerObjectReader.FetchResult
          The class holding the results given by MultiFetchServerObjectReader.Fetcher.
 
Field Summary
private static int MAX_IDS_PER_REQUEST
          the max.
private  java.util.Set<PrimitiveId> missingPrimitives
           
private  java.util.Set<java.lang.Long> nodes
           
private  DataSet outputDataSet
           
private  java.util.Set<java.lang.Long> relations
           
private  java.util.Set<java.lang.Long> ways
           
 
Fields inherited from class org.openstreetmap.josm.io.OsmConnection
activeConnection, cancel, oauthParameters
 
Constructor Summary
MultiFetchServerObjectReader()
          Constructs a MultiFetchServerObjectReader.
 
Method Summary
 MultiFetchServerObjectReader append(java.util.Collection<? extends OsmPrimitive> primitives)
          appends a list of OsmPrimitive to the list of ids which will be fetched from the server.
 MultiFetchServerObjectReader append(DataSet ds, long id, OsmPrimitiveType type)
          appends a OsmPrimitive id to the list of ids which will be fetched from the server.
 MultiFetchServerObjectReader append(OsmPrimitive primitive)
          appends an OsmPrimitive to the list of ids which will be fetched from the server.
 MultiFetchServerObjectReader appendNode(Node node)
          appends a Node id to the list of ids which will be fetched from the server.
protected  MultiFetchServerObjectReader appendRelation(Relation relation)
          appends a Relation id to the list of ids which will be fetched from the server.
 MultiFetchServerObjectReader appendWay(Way way)
          appends a Way id and the list of ids of nodes the way refers to the list of ids which will be fetched from the server.
protected static java.lang.String buildRequestString(OsmPrimitiveType type, long id)
          builds the Multi Get request string for a single id and a given OsmPrimitiveType.
protected static java.lang.String buildRequestString(OsmPrimitiveType type, java.util.Set<java.lang.Long> idPackage)
          builds the Multi Get request string for a set of ids and a given OsmPrimitiveType.
protected  java.util.Set<java.lang.Long> extractIdPackage(java.util.Set<java.lang.Long> ids)
          extracts a subset of max MAX_IDS_PER_REQUEST ids from ids and replies the subset.
protected  void fetchPrimitives(java.util.Set<java.lang.Long> ids, OsmPrimitiveType type, ProgressMonitor progressMonitor)
          fetches a set of ids of a given OsmPrimitiveType from the server
 java.util.Set<PrimitiveId> getMissingPrimitives()
          replies the set of ids of all primitives for which a fetch request to the server was submitted but which are not available from the server (the server replied a return code of 404)
protected  void merge(DataSet from)
          merges the dataset from to outputDataSet.
 DataSet parseOsm(ProgressMonitor progressMonitor)
          invokes one or more Multi Gets to fetch the OsmPrimitives and replies the dataset of retrieved primitives.
protected  void remember(DataSet ds, long id, OsmPrimitiveType type)
          remembers an OsmPrimitive's id.
protected  void remember(PrimitiveId id)
          Remembers an OsmPrimitive's id.
protected  void rememberNodesOfIncompleteWaysToLoad(DataSet from)
           
 
Methods inherited from class org.openstreetmap.josm.io.OsmServerReader
getBaseUrl, getInputStream, getInputStreamRaw, isDoAuthenticate, parseOsmBzip2, parseOsmChange, parseOsmChangeBzip2, parseOsmChangeGzip, parseOsmGzip, parseRawGps, setDoAuthenticate
 
Methods inherited from class org.openstreetmap.josm.io.OsmConnection
addAuth, addBasicAuthorizationHeader, addOAuthAuthorizationHeader, cancel, isCanceled
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MAX_IDS_PER_REQUEST

private static int MAX_IDS_PER_REQUEST
the max. number of primitives retrieved in one step. Assuming IDs with 7 digits, this leads to a max. request URL of ~ 1600 Bytes ((7 digits + 1 Separator) * 200), which should be safe according to the WWW FAQ.


nodes

private java.util.Set<java.lang.Long> nodes

ways

private java.util.Set<java.lang.Long> ways

relations

private java.util.Set<java.lang.Long> relations

missingPrimitives

private java.util.Set<PrimitiveId> missingPrimitives

outputDataSet

private DataSet outputDataSet
Constructor Detail

MultiFetchServerObjectReader

public MultiFetchServerObjectReader()
Constructs a MultiFetchServerObjectReader.

Method Detail

remember

protected void remember(PrimitiveId id)
Remembers an OsmPrimitive's id. The id will later be fetched as part of a Multi Get request. Ignore the id if it represents a new primitives.

Parameters:
id - the id

remember

protected void remember(DataSet ds,
                        long id,
                        OsmPrimitiveType type)
                 throws java.lang.IllegalArgumentException,
                        java.util.NoSuchElementException
remembers an OsmPrimitive's id. ds must include an OsmPrimitive with id=id. The id will later we fetched as part of a Multi Get request. Ignore the id if it id <= 0.

Parameters:
ds - the dataset (must not be null)
id - the primitive id
type - The primitive type. Must be one of NODE, WAY, RELATION
Throws:
java.lang.IllegalArgumentException - if ds is null
java.util.NoSuchElementException - if ds does not include an OsmPrimitive with id=id

append

public MultiFetchServerObjectReader append(DataSet ds,
                                           long id,
                                           OsmPrimitiveType type)
appends a OsmPrimitive id to the list of ids which will be fetched from the server.

Parameters:
ds - the DataSet to which the primitive belongs
id - the primitive id
type - The primitive type. Must be one of NODE, WAY, RELATION
Returns:
this

appendNode

public MultiFetchServerObjectReader appendNode(Node node)
appends a Node id to the list of ids which will be fetched from the server.

Parameters:
node - the node (ignored, if null)
Returns:
this

appendWay

public MultiFetchServerObjectReader appendWay(Way way)
appends a Way id and the list of ids of nodes the way refers to the list of ids which will be fetched from the server.

Parameters:
way - the way (ignored, if null)
Returns:
this

appendRelation

protected MultiFetchServerObjectReader appendRelation(Relation relation)
appends a Relation id to the list of ids which will be fetched from the server.

Parameters:
relation - the relation (ignored, if null)
Returns:
this

append

public MultiFetchServerObjectReader append(OsmPrimitive primitive)
appends an OsmPrimitive to the list of ids which will be fetched from the server.

Parameters:
primitive - the primitive
Returns:
this

append

public MultiFetchServerObjectReader append(java.util.Collection<? extends OsmPrimitive> primitives)
appends a list of OsmPrimitive to the list of ids which will be fetched from the server.

Parameters:
primitives - the list of primitives (ignored, if null)
Returns:
this
See Also:
append(OsmPrimitive)

extractIdPackage

protected java.util.Set<java.lang.Long> extractIdPackage(java.util.Set<java.lang.Long> ids)
extracts a subset of max MAX_IDS_PER_REQUEST ids from ids and replies the subset. The extracted subset is removed from ids.

Parameters:
ids - a set of ids
Returns:
the subset of ids

buildRequestString

protected static java.lang.String buildRequestString(OsmPrimitiveType type,
                                                     java.util.Set<java.lang.Long> idPackage)
builds the Multi Get request string for a set of ids and a given OsmPrimitiveType.

Parameters:
type - The primitive type. Must be one of NODE, WAY, RELATION
idPackage - the package of ids
Returns:
the request string

buildRequestString

protected static java.lang.String buildRequestString(OsmPrimitiveType type,
                                                     long id)
builds the Multi Get request string for a single id and a given OsmPrimitiveType.

Parameters:
type - The primitive type. Must be one of NODE, WAY, RELATION
id - the id
Returns:
the request string

rememberNodesOfIncompleteWaysToLoad

protected void rememberNodesOfIncompleteWaysToLoad(DataSet from)

merge

protected void merge(DataSet from)
merges the dataset from to outputDataSet.

Parameters:
from - the other dataset

fetchPrimitives

protected void fetchPrimitives(java.util.Set<java.lang.Long> ids,
                               OsmPrimitiveType type,
                               ProgressMonitor progressMonitor)
                        throws OsmTransferException
fetches a set of ids of a given OsmPrimitiveType from the server

Parameters:
ids - the set of ids
type - The primitive type. Must be one of NODE, WAY, RELATION
Throws:
OsmTransferException - if an error occurs while communicating with the API server

parseOsm

public DataSet parseOsm(ProgressMonitor progressMonitor)
                 throws OsmTransferException
invokes one or more Multi Gets to fetch the OsmPrimitives and replies the dataset of retrieved primitives. Note that the dataset includes non visible primitives too! In contrast to a simple Get for a node, a way, or a relation, a Multi Get always replies the latest version of the primitive (if any), even if the primitive is not visible (i.e. if visible==false). Invoke getMissingPrimitives() to get a list of primitives which have not been found on the server (the server response code was 404)

Specified by:
parseOsm in class OsmServerReader
Returns:
the parsed data
Throws:
OsmTransferException - if an error occurs while communicating with the API server
See Also:
getMissingPrimitives()

getMissingPrimitives

public java.util.Set<PrimitiveId> getMissingPrimitives()
replies the set of ids of all primitives for which a fetch request to the server was submitted but which are not available from the server (the server replied a return code of 404)

Returns:
the set of ids of missing primitives


JOSM