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