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.OsmPrimitive;
|
---|
12 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
---|
13 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
14 | import org.openstreetmap.josm.data.osm.Way;
|
---|
15 | import org.openstreetmap.josm.data.osm.visitor.MergeVisitor;
|
---|
16 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * OsmServerBackreferenceReader fetches the primitives from the OSM server which
|
---|
20 | * refer to a specific primitive. For a {@see Node}, ways and relations are retrieved
|
---|
21 | * which refer to the node. For a {@see Way} or a {@see Relation}, only relations are
|
---|
22 | * read.
|
---|
23 | *
|
---|
24 | * OsmServerBackreferenceReader uses the API calls <code>[node|way|relation]/#id/relations</code>
|
---|
25 | * and <code>node/#id/ways</code> to retrieve the referring primitives. The default behaviour
|
---|
26 | * of these calls is to reply incomplete primitives only.
|
---|
27 | *
|
---|
28 | * If you set {@see #setReadFull(boolean)} to true this reader uses a {@see MultiFetchServerObjectReader}
|
---|
29 | * to complete incomplete primitives.
|
---|
30 | *
|
---|
31 | *
|
---|
32 | */
|
---|
33 | public class OsmServerBackreferenceReader extends OsmServerReader {
|
---|
34 |
|
---|
35 | /** the id of the primitive whose referrers are to be read */
|
---|
36 | private long id;
|
---|
37 | /** the type of the primitive */
|
---|
38 | private OsmPrimitiveType primitiveType;
|
---|
39 | /** true if this reader should complete incomplete primitives */
|
---|
40 | private boolean readFull;
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * constructor
|
---|
44 | *
|
---|
45 | * @param primitive the primitive to be read. Must not be null. primitive.id > 0 expected
|
---|
46 | *
|
---|
47 | * @exception IllegalArgumentException thrown if primitive is null
|
---|
48 | * @exception IllegalArgumentException thrown if primitive.id <= 0
|
---|
49 | */
|
---|
50 | public OsmServerBackreferenceReader(OsmPrimitive primitive) throws IllegalArgumentException {
|
---|
51 | if (primitive == null)
|
---|
52 | throw new IllegalArgumentException(tr("parameter ''{0}'' must not be null", "primitive"));
|
---|
53 | if (primitive.id == 0)
|
---|
54 | throw new IllegalArgumentException(tr("id parameter ''{0}'' > 0 required. Got {1}", "primitive", primitive.id));
|
---|
55 | this.id = primitive.id;
|
---|
56 | this.primitiveType = OsmPrimitiveType.from(primitive);
|
---|
57 | this.readFull = false;
|
---|
58 | }
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * constructor
|
---|
62 | *
|
---|
63 | * @param id the id of the primitive. > 0 expected
|
---|
64 | * @param type the type of the primitive. Must not be null.
|
---|
65 | *
|
---|
66 | * @exception IllegalArgumentException thrown if id <= 0
|
---|
67 | * @exception IllegalArgumentException thrown if type is null
|
---|
68 | *
|
---|
69 | */
|
---|
70 | public OsmServerBackreferenceReader(long id, OsmPrimitiveType type) throws IllegalArgumentException {
|
---|
71 | if (id <= 0)
|
---|
72 | throw new IllegalArgumentException(tr("parameter ''{0}'' > 0 required. Got {1}", "id", id));
|
---|
73 | if (type == null)
|
---|
74 | throw new IllegalArgumentException(tr("parameter ''{0}'' must not be null", "type"));
|
---|
75 | this.id = id;
|
---|
76 | this.primitiveType = type;
|
---|
77 | this.readFull = false;
|
---|
78 | }
|
---|
79 |
|
---|
80 | /**
|
---|
81 | * constructor
|
---|
82 | *
|
---|
83 | * @param id the id of the primitive. > 0 expected
|
---|
84 | * @param type the type of the primitive. Must not be null.
|
---|
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("Contacting 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 | * @return the data set
|
---|
167 | * @throws OsmTransferException
|
---|
168 | */
|
---|
169 | protected DataSet getReferringRelations(ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
170 | InputStream in = null;
|
---|
171 | progressMonitor.beginTask(null, 2);
|
---|
172 | try {
|
---|
173 | progressMonitor.subTask(tr("Contacting OSM Server..."));
|
---|
174 | StringBuffer sb = new StringBuffer();
|
---|
175 | sb.append(primitiveType.getAPIName())
|
---|
176 | .append("/").append(id).append("/relations");
|
---|
177 |
|
---|
178 | in = getInputStream(sb.toString(), progressMonitor.createSubTaskMonitor(1, true));
|
---|
179 | if (in == null)
|
---|
180 | return null;
|
---|
181 | progressMonitor.subTask(tr("Downloading referring relations ..."));
|
---|
182 | return OsmReader.parseDataSet(in, progressMonitor.createSubTaskMonitor(1, true));
|
---|
183 | } catch(OsmTransferException e) {
|
---|
184 | throw e;
|
---|
185 | } catch (Exception e) {
|
---|
186 | if (cancel)
|
---|
187 | return null;
|
---|
188 | throw new OsmTransferException(e);
|
---|
189 | } finally {
|
---|
190 | progressMonitor.finishTask();
|
---|
191 | if (in != null) {
|
---|
192 | try {
|
---|
193 | in.close();
|
---|
194 | } catch(Exception e) {}
|
---|
195 | activeConnection = null;
|
---|
196 | }
|
---|
197 | }
|
---|
198 | }
|
---|
199 |
|
---|
200 | /**
|
---|
201 | * Scans a dataset for incomplete primitives. Depending on the configuration of this reader
|
---|
202 | * incomplete primitives are read from the server with an individual <tt>/api/0.6/[way,relation]/#id/full</tt>
|
---|
203 | * request.
|
---|
204 | *
|
---|
205 | * <ul>
|
---|
206 | * <li>if this reader reads referers for an {@see Node}, referring ways are always
|
---|
207 | * read individually from the server</li>
|
---|
208 | * <li>if this reader reads referers for an {@see Way} or a {@see Relation}, referring relations
|
---|
209 | * are only read fully if {@see #setReadFull(boolean)} is set to true.</li>
|
---|
210 | * </ul>
|
---|
211 | *
|
---|
212 | * The method replies the modified dataset.
|
---|
213 | *
|
---|
214 | * @param ds the original dataset
|
---|
215 | * @return the modified dataset
|
---|
216 | * @throws OsmTransferException thrown if an exception occurs.
|
---|
217 | */
|
---|
218 | protected DataSet readIncompletePrimitives(DataSet ds, ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
219 | progressMonitor.beginTask(null, 2);
|
---|
220 | try {
|
---|
221 | Collection<Way> waysToCheck = new ArrayList<Way>(ds.ways);
|
---|
222 | if (isReadFull() ||primitiveType.equals(OsmPrimitiveType.NODE)) {
|
---|
223 | for (Way way: waysToCheck) {
|
---|
224 | if (way.id > 0 && way.incomplete) {
|
---|
225 | OsmServerObjectReader reader = new OsmServerObjectReader(way.id, OsmPrimitiveType.from(way), true /* read full */);
|
---|
226 | DataSet wayDs = reader.parseOsm(progressMonitor.createSubTaskMonitor(1, false));
|
---|
227 | MergeVisitor visitor = new MergeVisitor(ds, wayDs);
|
---|
228 | visitor.merge();
|
---|
229 | }
|
---|
230 | }
|
---|
231 | }
|
---|
232 | if (isReadFull()) {
|
---|
233 | Collection<Relation> relationsToCheck = new ArrayList<Relation>(ds.relations);
|
---|
234 | for (Relation relation: relationsToCheck) {
|
---|
235 | if (relation.id > 0 && relation.incomplete) {
|
---|
236 | OsmServerObjectReader reader = new OsmServerObjectReader(relation.id, OsmPrimitiveType.from(relation), true /* read full */);
|
---|
237 | DataSet wayDs = reader.parseOsm(progressMonitor.createSubTaskMonitor(1, false));
|
---|
238 | MergeVisitor visitor = new MergeVisitor(ds, wayDs);
|
---|
239 | visitor.merge();
|
---|
240 | }
|
---|
241 | }
|
---|
242 | }
|
---|
243 | return ds;
|
---|
244 | } finally {
|
---|
245 | progressMonitor.finishTask();
|
---|
246 | }
|
---|
247 | }
|
---|
248 |
|
---|
249 | /**
|
---|
250 | * Reads the referring primitives from the OSM server, parses them and
|
---|
251 | * replies them as {@see DataSet}
|
---|
252 | *
|
---|
253 | * @return the dataset with the referring primitives
|
---|
254 | * @exception OsmTransferException thrown if an error occurs while communicating with the server
|
---|
255 | */
|
---|
256 | @Override
|
---|
257 | public DataSet parseOsm(ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
258 | progressMonitor.beginTask(null, 3);
|
---|
259 | try {
|
---|
260 | DataSet ret = new DataSet();
|
---|
261 | if (primitiveType.equals(OsmPrimitiveType.NODE)) {
|
---|
262 | DataSet ds = getReferringWays(progressMonitor.createSubTaskMonitor(1, false));
|
---|
263 | MergeVisitor visitor = new MergeVisitor(ret,ds);
|
---|
264 | visitor.merge();
|
---|
265 | ret = visitor.getMyDataSet();
|
---|
266 | }
|
---|
267 | DataSet ds = getReferringRelations(progressMonitor.createSubTaskMonitor(1, false));
|
---|
268 | MergeVisitor visitor = new MergeVisitor(ret,ds);
|
---|
269 | visitor.merge();
|
---|
270 | ret = visitor.getMyDataSet();
|
---|
271 | readIncompletePrimitives(ret, progressMonitor.createSubTaskMonitor(1, false));
|
---|
272 | return ret;
|
---|
273 | } finally {
|
---|
274 | progressMonitor.finishTask();
|
---|
275 | }
|
---|
276 | }
|
---|
277 | }
|
---|