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

Last change on this file since 7033 was 7033, checked in by Don-vip, 10 years ago

see #8465 - global use of try-with-resources, according to

  • Property svn:eol-style set to native
File size: 11.2 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
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 {@link #setReadFull(boolean)} to true this reader uses a {@link MultiFetchServerObjectReader}
32 * to complete incomplete primitives.
33 *
34 *
35 */
36public 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 &gt; 0 expected
49 *
50 * @exception IllegalArgumentException thrown if primitive is null
51 * @exception IllegalArgumentException thrown if primitive.id &lt;= 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. &gt; 0 expected
64 * @param type the type of the primitive. Must not be null.
65 *
66 * @exception IllegalArgumentException thrown if id &lt;= 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 * Creates a back reference reader for given primitive
81 *
82 * @param primitive the primitive
83 * @param readFull <code>true</code>, 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 * Creates a back reference reader for given primitive id
93 *
94 * @param id the id of the primitive whose referers are to be read
95 * @param type the type of the primitive
96 * @param readFull true, if referers should be read fully (i.e. including their immediate children)
97 *
98 * @exception IllegalArgumentException thrown if id &lt;= 0
99 * @exception IllegalArgumentException thrown if type is null
100 *
101 */
102 public OsmServerBackreferenceReader(long id, OsmPrimitiveType type, boolean readFull) throws IllegalArgumentException {
103 this(id, type);
104 this.readFull = readFull;
105 }
106
107 /**
108 * Replies true if this reader also reads immediate children of referring primitives
109 *
110 * @return true if this reader also reads immediate children of referring primitives
111 */
112 public boolean isReadFull() {
113 return readFull;
114 }
115
116 /**
117 * Set true if this reader should reads immediate children of referring primitives too. False, otherweise.
118 *
119 * @param readFull true if this reader should reads immediate children of referring primitives too. False, otherweise.
120 */
121 public void setReadFull(boolean readFull) {
122 this.readFull = readFull;
123 }
124
125 /**
126 * Reads referring ways from the API server and replies them in a {@link DataSet}
127 *
128 * @return the data set
129 * @throws OsmTransferException
130 */
131 protected DataSet getReferringWays(ProgressMonitor progressMonitor) throws OsmTransferException {
132 progressMonitor.beginTask(null, 2);
133 try {
134 progressMonitor.indeterminateSubTask(tr("Downloading from OSM Server..."));
135 StringBuilder sb = new StringBuilder();
136 sb.append(primitiveType.getAPIName())
137 .append("/").append(id).append("/ways");
138
139 try (InputStream 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 }
145 } catch(OsmTransferException e) {
146 throw e;
147 } catch (Exception e) {
148 if (cancel)
149 return null;
150 throw new OsmTransferException(e);
151 } finally {
152 progressMonitor.finishTask();
153 activeConnection = null;
154 }
155 }
156
157 /**
158 * Reads referring relations from the API server and replies them in a {@link DataSet}
159 *
160 * @param progressMonitor the progress monitor
161 * @return the data set
162 * @throws OsmTransferException
163 */
164 protected DataSet getReferringRelations(ProgressMonitor progressMonitor) throws OsmTransferException {
165 progressMonitor.beginTask(null, 2);
166 try {
167 progressMonitor.subTask(tr("Contacting OSM Server..."));
168 StringBuilder sb = new StringBuilder();
169 sb.append(primitiveType.getAPIName())
170 .append("/").append(id).append("/relations");
171
172 try (InputStream in = getInputStream(sb.toString(), progressMonitor.createSubTaskMonitor(1, true))) {
173 if (in == null)
174 return null;
175 progressMonitor.subTask(tr("Downloading referring relations ..."));
176 return OsmReader.parseDataSet(in, progressMonitor.createSubTaskMonitor(1, true));
177 }
178 } catch(OsmTransferException e) {
179 throw e;
180 } catch (Exception e) {
181 if (cancel)
182 return null;
183 throw new OsmTransferException(e);
184 } finally {
185 progressMonitor.finishTask();
186 activeConnection = null;
187 }
188 }
189
190 /**
191 * Scans a dataset for incomplete primitives. Depending on the configuration of this reader
192 * incomplete primitives are read from the server with an individual <tt>/api/0.6/[way,relation]/#id/full</tt>
193 * request.
194 *
195 * <ul>
196 * <li>if this reader reads referers for a {@link org.openstreetmap.josm.data.osm.Node}, referring ways are always
197 * read individually from the server</li>
198 * <li>if this reader reads referers for an {@link Way} or a {@link Relation}, referring relations
199 * are only read fully if {@link #setReadFull(boolean)} is set to true.</li>
200 * </ul>
201 *
202 * The method replies the modified dataset.
203 *
204 * @param ds the original dataset
205 * @param progressMonitor the progress monitor
206 * @return the modified dataset
207 * @throws OsmTransferException thrown if an exception occurs.
208 */
209 protected DataSet readIncompletePrimitives(DataSet ds, ProgressMonitor progressMonitor) throws OsmTransferException {
210 progressMonitor.beginTask(null, 2);
211 try {
212 Collection<Way> waysToCheck = new ArrayList<>(ds.getWays());
213 if (isReadFull() ||primitiveType.equals(OsmPrimitiveType.NODE)) {
214 for (Way way: waysToCheck) {
215 if (!way.isNew() && way.hasIncompleteNodes()) {
216 OsmServerObjectReader reader = new OsmServerObjectReader(way.getId(), OsmPrimitiveType.from(way), true /* read full */);
217 DataSet wayDs = reader.parseOsm(progressMonitor.createSubTaskMonitor(1, false));
218 DataSetMerger visitor = new DataSetMerger(ds, wayDs);
219 visitor.merge();
220 }
221 }
222 }
223 if (isReadFull()) {
224 Collection<Relation> relationsToCheck = new ArrayList<>(ds.getRelations());
225 for (Relation relation: relationsToCheck) {
226 if (!relation.isNew() && relation.hasIncompleteMembers()) {
227 OsmServerObjectReader reader = new OsmServerObjectReader(relation.getId(), OsmPrimitiveType.from(relation), 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 return ds;
235 } finally {
236 progressMonitor.finishTask();
237 }
238 }
239
240 /**
241 * Reads the referring primitives from the OSM server, parses them and
242 * replies them as {@link DataSet}
243 *
244 * @param progressMonitor the progress monitor. Set to {@link NullProgressMonitor#INSTANCE} if null.
245 * @return the dataset with the referring primitives
246 * @exception OsmTransferException thrown if an error occurs while communicating with the server
247 */
248 @Override
249 public DataSet parseOsm(ProgressMonitor progressMonitor) throws OsmTransferException {
250 if (progressMonitor == null) {
251 progressMonitor = NullProgressMonitor.INSTANCE;
252 }
253 try {
254 progressMonitor.beginTask(null, 3);
255 DataSet ret = new DataSet();
256 if (primitiveType.equals(OsmPrimitiveType.NODE)) {
257 DataSet ds = getReferringWays(progressMonitor.createSubTaskMonitor(1, false));
258 DataSetMerger visitor = new DataSetMerger(ret,ds);
259 visitor.merge();
260 ret = visitor.getTargetDataSet();
261 }
262 DataSet ds = getReferringRelations(progressMonitor.createSubTaskMonitor(1, false));
263 DataSetMerger visitor = new DataSetMerger(ret,ds);
264 visitor.merge();
265 ret = visitor.getTargetDataSet();
266 readIncompletePrimitives(ret, progressMonitor.createSubTaskMonitor(1, false));
267 if (ret != null) {
268 ret.deleteInvisible();
269 }
270 return ret;
271 } finally {
272 progressMonitor.finishTask();
273 }
274 }
275}
Note: See TracBrowser for help on using the repository browser.