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