source: josm/trunk/src/org/openstreetmap/josm/io/OsmServerBackreferenceReader.java@ 9461

Last change on this file since 9461 was 9231, checked in by Don-vip, 8 years ago

javadoc update

  • Property svn:eol-style set to native
File size: 10.5 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.io;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.io.InputStream;
7import java.text.MessageFormat;
8import java.util.ArrayList;
9import java.util.Collection;
10
11import org.openstreetmap.josm.data.osm.DataSet;
12import org.openstreetmap.josm.data.osm.DataSetMerger;
13import org.openstreetmap.josm.data.osm.OsmPrimitive;
14import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
15import org.openstreetmap.josm.data.osm.Relation;
16import org.openstreetmap.josm.data.osm.Way;
17import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
18import org.openstreetmap.josm.gui.progress.ProgressMonitor;
19import org.openstreetmap.josm.tools.CheckParameterUtil;
20
21/**
22 * OsmServerBackreferenceReader fetches the primitives from the OSM server which
23 * refer to a specific primitive. For a {@link org.openstreetmap.josm.data.osm.Node Node}, ways and relations are retrieved
24 * which refer to the node. For a {@link Way} or a {@link Relation}, only relations are read.
25 *
26 * OsmServerBackreferenceReader uses the API calls <code>[node|way|relation]/#id/relations</code>
27 * and <code>node/#id/ways</code> to retrieve the referring primitives. The default behaviour
28 * of these calls is to reply incomplete primitives only.
29 *
30 * If you set {@link #setReadFull(boolean)} to true this reader uses a {@link MultiFetchServerObjectReader}
31 * to complete incomplete primitives.
32 *
33 * @since 1806
34 */
35public class OsmServerBackreferenceReader extends OsmServerReader {
36
37 /** the id of the primitive whose referrers are to be read */
38 private long id;
39 /** the type of the primitive */
40 private OsmPrimitiveType primitiveType;
41 /** true if this reader should complete incomplete primitives */
42 private boolean readFull;
43
44 /**
45 * constructor
46 *
47 * @param primitive the primitive to be read. Must not be null. primitive.id &gt; 0 expected
48 *
49 * @throws IllegalArgumentException if primitive is null
50 * @throws IllegalArgumentException if primitive.id &lt;= 0
51 */
52 public OsmServerBackreferenceReader(OsmPrimitive primitive) {
53 CheckParameterUtil.ensureValidPrimitiveId(primitive, "primitive");
54 this.id = primitive.getId();
55 this.primitiveType = OsmPrimitiveType.from(primitive);
56 this.readFull = false;
57 }
58
59 /**
60 * constructor
61 *
62 * @param id the id of the primitive. &gt; 0 expected
63 * @param type the type of the primitive. Must not be null.
64 *
65 * @throws IllegalArgumentException if id &lt;= 0
66 * @throws IllegalArgumentException if type is null
67 */
68 public OsmServerBackreferenceReader(long id, OsmPrimitiveType type) {
69 if (id <= 0)
70 throw new IllegalArgumentException(MessageFormat.format("Parameter ''{0}'' > 0 expected. Got ''{1}''.", "id", id));
71 CheckParameterUtil.ensureParameterNotNull(type, "type");
72 this.id = id;
73 this.primitiveType = type;
74 this.readFull = false;
75 }
76
77 /**
78 * Creates a back reference reader for given primitive
79 *
80 * @param primitive the primitive
81 * @param readFull <code>true</code>, if referers should be read fully (i.e. including their immediate children)
82 *
83 */
84 public OsmServerBackreferenceReader(OsmPrimitive primitive, boolean readFull) {
85 this(primitive);
86 this.readFull = readFull;
87 }
88
89 /**
90 * Creates a back reference reader for given primitive id
91 *
92 * @param id the id of the primitive whose referers are to be read
93 * @param type the type of the primitive
94 * @param readFull true, if referers should be read fully (i.e. including their immediate children)
95 *
96 * @throws IllegalArgumentException if id &lt;= 0
97 * @throws IllegalArgumentException if type is null
98 */
99 public OsmServerBackreferenceReader(long id, OsmPrimitiveType type, boolean readFull) {
100 this(id, type);
101 this.readFull = readFull;
102 }
103
104 /**
105 * Replies true if this reader also reads immediate children of referring primitives
106 *
107 * @return true if this reader also reads immediate children of referring primitives
108 */
109 public boolean isReadFull() {
110 return readFull;
111 }
112
113 /**
114 * Set true if this reader should reads immediate children of referring primitives too. False, otherweise.
115 *
116 * @param readFull true if this reader should reads immediate children of referring primitives too. False, otherweise.
117 */
118 public void setReadFull(boolean readFull) {
119 this.readFull = readFull;
120 }
121
122 private DataSet getReferringPrimitives(ProgressMonitor progressMonitor, String type, String message) throws OsmTransferException {
123 progressMonitor.beginTask(null, 2);
124 try {
125 progressMonitor.subTask(tr("Contacting OSM Server..."));
126 StringBuilder sb = new StringBuilder();
127 sb.append(primitiveType.getAPIName()).append('/').append(id).append(type);
128
129 try (InputStream in = getInputStream(sb.toString(), progressMonitor.createSubTaskMonitor(1, true))) {
130 if (in == null)
131 return null;
132 progressMonitor.subTask(message);
133 return OsmReader.parseDataSet(in, progressMonitor.createSubTaskMonitor(1, true));
134 }
135 } catch (OsmTransferException e) {
136 throw e;
137 } catch (Exception e) {
138 if (cancel)
139 return null;
140 throw new OsmTransferException(e);
141 } finally {
142 progressMonitor.finishTask();
143 activeConnection = null;
144 }
145 }
146
147 /**
148 * Reads referring ways from the API server and replies them in a {@link DataSet}
149 *
150 * @param progressMonitor progress monitor
151 * @return the data set
152 * @throws OsmTransferException if any error occurs during dialog with OSM API
153 */
154 protected DataSet getReferringWays(ProgressMonitor progressMonitor) throws OsmTransferException {
155 return getReferringPrimitives(progressMonitor, "/ways", tr("Downloading referring ways ..."));
156 }
157
158 /**
159 * Reads referring relations from the API server and replies them in a {@link DataSet}
160 *
161 * @param progressMonitor the progress monitor
162 * @return the data set
163 * @throws OsmTransferException if any error occurs during dialog with OSM API
164 */
165 protected DataSet getReferringRelations(ProgressMonitor progressMonitor) throws OsmTransferException {
166 return getReferringPrimitives(progressMonitor, "/relations", tr("Downloading referring relations ..."));
167 }
168
169 /**
170 * Scans a dataset for incomplete primitives. Depending on the configuration of this reader
171 * incomplete primitives are read from the server with an individual <tt>/api/0.6/[way,relation]/#id/full</tt>
172 * request.
173 *
174 * <ul>
175 * <li>if this reader reads referers for a {@link org.openstreetmap.josm.data.osm.Node}, referring ways are always
176 * read individually from the server</li>
177 * <li>if this reader reads referers for an {@link Way} or a {@link Relation}, referring relations
178 * are only read fully if {@link #setReadFull(boolean)} is set to true.</li>
179 * </ul>
180 *
181 * The method replies the modified dataset.
182 *
183 * @param ds the original dataset
184 * @param progressMonitor the progress monitor
185 * @return the modified dataset
186 * @throws OsmTransferException if an exception occurs.
187 */
188 protected DataSet readIncompletePrimitives(DataSet ds, ProgressMonitor progressMonitor) throws OsmTransferException {
189 progressMonitor.beginTask(null, 2);
190 try {
191 Collection<Way> waysToCheck = new ArrayList<>(ds.getWays());
192 if (isReadFull() || primitiveType.equals(OsmPrimitiveType.NODE)) {
193 for (Way way: waysToCheck) {
194 if (!way.isNew() && way.hasIncompleteNodes()) {
195 OsmServerObjectReader reader = new OsmServerObjectReader(way.getId(), OsmPrimitiveType.from(way), true /* read full */);
196 DataSet wayDs = reader.parseOsm(progressMonitor.createSubTaskMonitor(1, false));
197 DataSetMerger visitor = new DataSetMerger(ds, wayDs);
198 visitor.merge();
199 }
200 }
201 }
202 if (isReadFull()) {
203 Collection<Relation> relationsToCheck = new ArrayList<>(ds.getRelations());
204 for (Relation relation: relationsToCheck) {
205 if (!relation.isNew() && relation.hasIncompleteMembers()) {
206 OsmServerObjectReader reader = new OsmServerObjectReader(relation.getId(), OsmPrimitiveType.from(relation), true);
207 DataSet wayDs = reader.parseOsm(progressMonitor.createSubTaskMonitor(1, false));
208 DataSetMerger visitor = new DataSetMerger(ds, wayDs);
209 visitor.merge();
210 }
211 }
212 }
213 return ds;
214 } finally {
215 progressMonitor.finishTask();
216 }
217 }
218
219 /**
220 * Reads the referring primitives from the OSM server, parses them and
221 * replies them as {@link DataSet}
222 *
223 * @param progressMonitor the progress monitor. Set to {@link NullProgressMonitor#INSTANCE} if null.
224 * @return the dataset with the referring primitives
225 * @throws OsmTransferException if an error occurs while communicating with the server
226 */
227 @Override
228 public DataSet parseOsm(ProgressMonitor progressMonitor) throws OsmTransferException {
229 if (progressMonitor == null) {
230 progressMonitor = NullProgressMonitor.INSTANCE;
231 }
232 try {
233 progressMonitor.beginTask(null, 3);
234 DataSet ret = new DataSet();
235 if (primitiveType.equals(OsmPrimitiveType.NODE)) {
236 DataSet ds = getReferringWays(progressMonitor.createSubTaskMonitor(1, false));
237 DataSetMerger visitor = new DataSetMerger(ret, ds);
238 visitor.merge();
239 ret = visitor.getTargetDataSet();
240 }
241 DataSet ds = getReferringRelations(progressMonitor.createSubTaskMonitor(1, false));
242 DataSetMerger visitor = new DataSetMerger(ret, ds);
243 visitor.merge();
244 ret = visitor.getTargetDataSet();
245 if (ret != null) {
246 readIncompletePrimitives(ret, progressMonitor.createSubTaskMonitor(1, false));
247 ret.deleteInvisible();
248 }
249 return ret;
250 } finally {
251 progressMonitor.finishTask();
252 }
253 }
254}
Note: See TracBrowser for help on using the repository browser.