[2512] | 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;
|
---|
[2852] | 7 | import java.text.MessageFormat;
|
---|
[2512] | 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;
|
---|
[2852] | 19 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
[2512] | 20 |
|
---|
| 21 | /**
|
---|
| 22 | * OsmServerBackreferenceReader fetches the primitives from the OSM server which
|
---|
[5874] | 23 | * refer to a specific primitive. For a {@link org.openstreetmap.josm.data.osm.Node Node}, ways and relations are retrieved
|
---|
[7092] | 24 | * which refer to the node. For a {@link Way} or a {@link Relation}, only relations are read.
|
---|
[2512] | 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 | *
|
---|
[5266] | 30 | * If you set {@link #setReadFull(boolean)} to true this reader uses a {@link MultiFetchServerObjectReader}
|
---|
[2512] | 31 | * to complete incomplete primitives.
|
---|
| 32 | *
|
---|
[7092] | 33 | * @since 1806
|
---|
[2512] | 34 | */
|
---|
| 35 | public 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 | *
|
---|
[6830] | 47 | * @param primitive the primitive to be read. Must not be null. primitive.id > 0 expected
|
---|
[2512] | 48 | *
|
---|
[8291] | 49 | * @throws IllegalArgumentException if primitive is null
|
---|
| 50 | * @throws IllegalArgumentException if primitive.id <= 0
|
---|
[2512] | 51 | */
|
---|
[8291] | 52 | public OsmServerBackreferenceReader(OsmPrimitive primitive) {
|
---|
[2852] | 53 | CheckParameterUtil.ensureValidPrimitiveId(primitive, "primitive");
|
---|
[2512] | 54 | this.id = primitive.getId();
|
---|
| 55 | this.primitiveType = OsmPrimitiveType.from(primitive);
|
---|
| 56 | this.readFull = false;
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | /**
|
---|
| 60 | * constructor
|
---|
| 61 | *
|
---|
[6830] | 62 | * @param id the id of the primitive. > 0 expected
|
---|
[2512] | 63 | * @param type the type of the primitive. Must not be null.
|
---|
| 64 | *
|
---|
[8291] | 65 | * @throws IllegalArgumentException if id <= 0
|
---|
| 66 | * @throws IllegalArgumentException if type is null
|
---|
[2512] | 67 | */
|
---|
[8291] | 68 | public OsmServerBackreferenceReader(long id, OsmPrimitiveType type) {
|
---|
[2512] | 69 | if (id <= 0)
|
---|
[2852] | 70 | throw new IllegalArgumentException(MessageFormat.format("Parameter ''{0}'' > 0 expected. Got ''{1}''.", "id", id));
|
---|
| 71 | CheckParameterUtil.ensureParameterNotNull(type, "type");
|
---|
[2512] | 72 | this.id = id;
|
---|
| 73 | this.primitiveType = type;
|
---|
| 74 | this.readFull = false;
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | /**
|
---|
[5881] | 78 | * Creates a back reference reader for given primitive
|
---|
[2512] | 79 | *
|
---|
[5881] | 80 | * @param primitive the primitive
|
---|
| 81 | * @param readFull <code>true</code>, if referers should be read fully (i.e. including their immediate children)
|
---|
[2512] | 82 | *
|
---|
| 83 | */
|
---|
| 84 | public OsmServerBackreferenceReader(OsmPrimitive primitive, boolean readFull) {
|
---|
| 85 | this(primitive);
|
---|
| 86 | this.readFull = readFull;
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | /**
|
---|
[5881] | 90 | * Creates a back reference reader for given primitive id
|
---|
[2512] | 91 | *
|
---|
[5881] | 92 | * @param id the id of the primitive whose referers are to be read
|
---|
| 93 | * @param type the type of the primitive
|
---|
[2512] | 94 | * @param readFull true, if referers should be read fully (i.e. including their immediate children)
|
---|
| 95 | *
|
---|
[8291] | 96 | * @throws IllegalArgumentException if id <= 0
|
---|
| 97 | * @throws IllegalArgumentException if type is null
|
---|
[2512] | 98 | */
|
---|
[8291] | 99 | public OsmServerBackreferenceReader(long id, OsmPrimitiveType type, boolean readFull) {
|
---|
[2512] | 100 | this(id, type);
|
---|
[6361] | 101 | this.readFull = readFull;
|
---|
[2512] | 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 |
|
---|
[7092] | 122 | private DataSet getReferringPrimitives(ProgressMonitor progressMonitor, String type, String message) throws OsmTransferException {
|
---|
[2512] | 123 | progressMonitor.beginTask(null, 2);
|
---|
| 124 | try {
|
---|
[7092] | 125 | progressMonitor.subTask(tr("Contacting OSM Server..."));
|
---|
[6822] | 126 | StringBuilder sb = new StringBuilder();
|
---|
[8390] | 127 | sb.append(primitiveType.getAPIName()).append('/').append(id).append(type);
|
---|
[2512] | 128 |
|
---|
[7033] | 129 | try (InputStream in = getInputStream(sb.toString(), progressMonitor.createSubTaskMonitor(1, true))) {
|
---|
| 130 | if (in == null)
|
---|
| 131 | return null;
|
---|
[7092] | 132 | progressMonitor.subTask(message);
|
---|
[7033] | 133 | return OsmReader.parseDataSet(in, progressMonitor.createSubTaskMonitor(1, true));
|
---|
| 134 | }
|
---|
[8510] | 135 | } catch (OsmTransferException e) {
|
---|
[2512] | 136 | throw e;
|
---|
| 137 | } catch (Exception e) {
|
---|
| 138 | if (cancel)
|
---|
| 139 | return null;
|
---|
| 140 | throw new OsmTransferException(e);
|
---|
| 141 | } finally {
|
---|
| 142 | progressMonitor.finishTask();
|
---|
[5874] | 143 | activeConnection = null;
|
---|
[2512] | 144 | }
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | /**
|
---|
[7092] | 148 | * Reads referring ways from the API server and replies them in a {@link DataSet}
|
---|
| 149 | *
|
---|
[9231] | 150 | * @param progressMonitor progress monitor
|
---|
[7092] | 151 | * @return the data set
|
---|
[8470] | 152 | * @throws OsmTransferException if any error occurs during dialog with OSM API
|
---|
[7092] | 153 | */
|
---|
| 154 | protected DataSet getReferringWays(ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
| 155 | return getReferringPrimitives(progressMonitor, "/ways", tr("Downloading referring ways ..."));
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | /**
|
---|
[5266] | 159 | * Reads referring relations from the API server and replies them in a {@link DataSet}
|
---|
[2512] | 160 | *
|
---|
| 161 | * @param progressMonitor the progress monitor
|
---|
| 162 | * @return the data set
|
---|
[8470] | 163 | * @throws OsmTransferException if any error occurs during dialog with OSM API
|
---|
[2512] | 164 | */
|
---|
| 165 | protected DataSet getReferringRelations(ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
[7092] | 166 | return getReferringPrimitives(progressMonitor, "/relations", tr("Downloading referring relations ..."));
|
---|
[2512] | 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>
|
---|
[5881] | 175 | * <li>if this reader reads referers for a {@link org.openstreetmap.josm.data.osm.Node}, referring ways are always
|
---|
[2512] | 176 | * read individually from the server</li>
|
---|
[5266] | 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>
|
---|
[2512] | 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
|
---|
[8291] | 186 | * @throws OsmTransferException if an exception occurs.
|
---|
[2512] | 187 | */
|
---|
| 188 | protected DataSet readIncompletePrimitives(DataSet ds, ProgressMonitor progressMonitor) throws OsmTransferException {
|
---|
| 189 | progressMonitor.beginTask(null, 2);
|
---|
| 190 | try {
|
---|
[7005] | 191 | Collection<Way> waysToCheck = new ArrayList<>(ds.getWays());
|
---|
[8510] | 192 | if (isReadFull() || primitiveType.equals(OsmPrimitiveType.NODE)) {
|
---|
[2512] | 193 | for (Way way: waysToCheck) {
|
---|
[5251] | 194 | if (!way.isNew() && way.hasIncompleteNodes()) {
|
---|
[2512] | 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()) {
|
---|
[7005] | 203 | Collection<Relation> relationsToCheck = new ArrayList<>(ds.getRelations());
|
---|
[2512] | 204 | for (Relation relation: relationsToCheck) {
|
---|
[5251] | 205 | if (!relation.isNew() && relation.hasIncompleteMembers()) {
|
---|
[8509] | 206 | OsmServerObjectReader reader = new OsmServerObjectReader(relation.getId(), OsmPrimitiveType.from(relation), true);
|
---|
[2512] | 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
|
---|
[5266] | 221 | * replies them as {@link DataSet}
|
---|
[2512] | 222 | *
|
---|
[5266] | 223 | * @param progressMonitor the progress monitor. Set to {@link NullProgressMonitor#INSTANCE} if null.
|
---|
[2512] | 224 | * @return the dataset with the referring primitives
|
---|
[8291] | 225 | * @throws OsmTransferException if an error occurs while communicating with the server
|
---|
[2512] | 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));
|
---|
[8510] | 237 | DataSetMerger visitor = new DataSetMerger(ret, ds);
|
---|
[2512] | 238 | visitor.merge();
|
---|
| 239 | ret = visitor.getTargetDataSet();
|
---|
| 240 | }
|
---|
| 241 | DataSet ds = getReferringRelations(progressMonitor.createSubTaskMonitor(1, false));
|
---|
[8510] | 242 | DataSetMerger visitor = new DataSetMerger(ret, ds);
|
---|
[2512] | 243 | visitor.merge();
|
---|
| 244 | ret = visitor.getTargetDataSet();
|
---|
[3362] | 245 | if (ret != null) {
|
---|
[7050] | 246 | readIncompletePrimitives(ret, progressMonitor.createSubTaskMonitor(1, false));
|
---|
[3362] | 247 | ret.deleteInvisible();
|
---|
| 248 | }
|
---|
[2512] | 249 | return ret;
|
---|
| 250 | } finally {
|
---|
| 251 | progressMonitor.finishTask();
|
---|
| 252 | }
|
---|
| 253 | }
|
---|
| 254 | }
|
---|