1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.dialogs;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
6 |
|
---|
7 | import java.util.Arrays;
|
---|
8 | import java.util.List;
|
---|
9 | import java.util.stream.Collectors;
|
---|
10 | import java.util.stream.Stream;
|
---|
11 |
|
---|
12 | import org.openstreetmap.josm.data.SystemOfMeasurement;
|
---|
13 | import org.openstreetmap.josm.data.conflict.Conflict;
|
---|
14 | import org.openstreetmap.josm.data.coor.ILatLon;
|
---|
15 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
16 | import org.openstreetmap.josm.data.coor.conversion.AbstractCoordinateFormat;
|
---|
17 | import org.openstreetmap.josm.data.coor.conversion.DecimalDegreesCoordinateFormat;
|
---|
18 | import org.openstreetmap.josm.data.coor.conversion.ProjectedCoordinateFormat;
|
---|
19 | import org.openstreetmap.josm.data.osm.BBox;
|
---|
20 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
21 | import org.openstreetmap.josm.data.osm.INode;
|
---|
22 | import org.openstreetmap.josm.data.osm.IPrimitive;
|
---|
23 | import org.openstreetmap.josm.data.osm.IRelation;
|
---|
24 | import org.openstreetmap.josm.data.osm.IRelationMember;
|
---|
25 | import org.openstreetmap.josm.data.osm.IWay;
|
---|
26 | import org.openstreetmap.josm.data.osm.OsmData;
|
---|
27 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
28 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
29 | import org.openstreetmap.josm.data.osm.Way;
|
---|
30 | import org.openstreetmap.josm.data.projection.ProjectionRegistry;
|
---|
31 | import org.openstreetmap.josm.data.projection.proj.TransverseMercator;
|
---|
32 | import org.openstreetmap.josm.data.projection.proj.TransverseMercator.Hemisphere;
|
---|
33 | import org.openstreetmap.josm.tools.Geometry;
|
---|
34 | import org.openstreetmap.josm.tools.Pair;
|
---|
35 | import org.openstreetmap.josm.tools.date.DateUtils;
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * Textual representation of primitive contents, used in {@code InspectPrimitiveDialog}.
|
---|
39 | * @since 10198
|
---|
40 | */
|
---|
41 | public class InspectPrimitiveDataText {
|
---|
42 | private static final String INDENT = " ";
|
---|
43 | private static final char NL = '\n';
|
---|
44 |
|
---|
45 | private final StringBuilder s = new StringBuilder();
|
---|
46 | private final OsmData<?, ?, ?, ?> ds;
|
---|
47 |
|
---|
48 | InspectPrimitiveDataText(OsmData<?, ?, ?, ?> ds) {
|
---|
49 | this.ds = ds;
|
---|
50 | }
|
---|
51 |
|
---|
52 | private InspectPrimitiveDataText add(String title, String... values) {
|
---|
53 | s.append(INDENT).append(title);
|
---|
54 | for (String v : values) {
|
---|
55 | s.append(v);
|
---|
56 | }
|
---|
57 | s.append(NL);
|
---|
58 | return this;
|
---|
59 | }
|
---|
60 |
|
---|
61 | private static String getNameAndId(String name, long id) {
|
---|
62 | if (name != null) {
|
---|
63 | return name + tr(" ({0})", /* sic to avoid thousand separators */ Long.toString(id));
|
---|
64 | } else {
|
---|
65 | return Long.toString(id);
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * Adds a new OSM primitive.
|
---|
71 | * @param o primitive to add
|
---|
72 | */
|
---|
73 | public void addPrimitive(IPrimitive o) {
|
---|
74 |
|
---|
75 | addHeadline(o);
|
---|
76 |
|
---|
77 | if (!(o.getDataSet() != null && o.getDataSet().getPrimitiveById(o) != null)) {
|
---|
78 | s.append(NL).append(INDENT).append(tr("not in data set")).append(NL);
|
---|
79 | return;
|
---|
80 | }
|
---|
81 | if (o.isIncomplete()) {
|
---|
82 | s.append(NL).append(INDENT).append(tr("incomplete")).append(NL);
|
---|
83 | return;
|
---|
84 | }
|
---|
85 | s.append(NL);
|
---|
86 |
|
---|
87 | addState(o);
|
---|
88 | addCommon(o);
|
---|
89 | addAttributes(o);
|
---|
90 | addSpecial(o);
|
---|
91 | addReferrers(s, o);
|
---|
92 | if (o instanceof OsmPrimitive) {
|
---|
93 | addConflicts((OsmPrimitive) o);
|
---|
94 | }
|
---|
95 | s.append(NL);
|
---|
96 | }
|
---|
97 |
|
---|
98 | void addHeadline(IPrimitive o) {
|
---|
99 | addType(o);
|
---|
100 | addNameAndId(o);
|
---|
101 | }
|
---|
102 |
|
---|
103 | void addType(IPrimitive o) {
|
---|
104 | if (o instanceof INode) {
|
---|
105 | s.append(tr("Node: "));
|
---|
106 | } else if (o instanceof IWay) {
|
---|
107 | s.append(tr("Way: "));
|
---|
108 | } else if (o instanceof IRelation) {
|
---|
109 | s.append(tr("Relation: "));
|
---|
110 | }
|
---|
111 | }
|
---|
112 |
|
---|
113 | void addNameAndId(IPrimitive o) {
|
---|
114 | String name = o.get("name");
|
---|
115 | if (name == null) {
|
---|
116 | s.append(o.getUniqueId());
|
---|
117 | } else {
|
---|
118 | s.append(getNameAndId(name, o.getUniqueId()));
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 | void addState(IPrimitive o) {
|
---|
123 | StringBuilder sb = new StringBuilder(INDENT);
|
---|
124 | /* selected state is left out: not interesting as it is always selected */
|
---|
125 | if (o.isDeleted()) {
|
---|
126 | sb.append(tr("deleted")).append(INDENT);
|
---|
127 | }
|
---|
128 | if (!o.isVisible()) {
|
---|
129 | sb.append(tr("deleted-on-server")).append(INDENT);
|
---|
130 | }
|
---|
131 | if (o.isModified()) {
|
---|
132 | sb.append(tr("modified")).append(INDENT);
|
---|
133 | }
|
---|
134 | if (o.isDisabledAndHidden()) {
|
---|
135 | sb.append(tr("filtered/hidden")).append(INDENT);
|
---|
136 | }
|
---|
137 | if (o.isDisabled()) {
|
---|
138 | sb.append(tr("filtered/disabled")).append(INDENT);
|
---|
139 | }
|
---|
140 | if (o.hasDirectionKeys()) {
|
---|
141 | if (o.reversedDirection()) {
|
---|
142 | sb.append(tr("has direction keys (reversed)")).append(INDENT);
|
---|
143 | } else {
|
---|
144 | sb.append(tr("has direction keys")).append(INDENT);
|
---|
145 | }
|
---|
146 | }
|
---|
147 | String state = sb.toString().trim();
|
---|
148 | if (!state.isEmpty()) {
|
---|
149 | add(tr("State: "), sb.toString().trim());
|
---|
150 | }
|
---|
151 | }
|
---|
152 |
|
---|
153 | void addCommon(IPrimitive o) {
|
---|
154 | add(tr("Data Set: "), Integer.toHexString(o.getDataSet().hashCode()));
|
---|
155 | add(tr("Edited at: "), o.isTimestampEmpty() ? tr("<new object>")
|
---|
156 | : DateUtils.fromTimestamp(o.getRawTimestamp()));
|
---|
157 | add(tr("Edited by: "), o.getUser() == null ? tr("<new object>")
|
---|
158 | : getNameAndId(o.getUser().getName(), o.getUser().getId()));
|
---|
159 | add(tr("Version:"), " ", Integer.toString(o.getVersion()));
|
---|
160 | add(tr("In changeset: "), Integer.toString(o.getChangesetId()));
|
---|
161 | }
|
---|
162 |
|
---|
163 | void addAttributes(IPrimitive o) {
|
---|
164 | if (o.hasKeys()) {
|
---|
165 | add(tr("Tags: "));
|
---|
166 | for (String key : o.keySet()) {
|
---|
167 | s.append(INDENT).append(INDENT);
|
---|
168 | s.append(String.format("\"%s\"=\"%s\"%n", key, o.get(key)));
|
---|
169 | }
|
---|
170 | }
|
---|
171 | }
|
---|
172 |
|
---|
173 | void addSpecial(IPrimitive o) {
|
---|
174 | if (o instanceof INode) {
|
---|
175 | addCoordinates((INode) o);
|
---|
176 | } else if (o instanceof IWay) {
|
---|
177 | addBbox(o);
|
---|
178 | add(tr("Centroid: "), toStringCSV(false,
|
---|
179 | ProjectionRegistry.getProjection().eastNorth2latlon(Geometry.getCentroid(((IWay<?>) o).getNodes()))));
|
---|
180 | if (o instanceof Way) {
|
---|
181 | double dist = ((Way) o).getLength();
|
---|
182 | String distText = SystemOfMeasurement.getSystemOfMeasurement().getDistText(dist);
|
---|
183 | add(tr("Length: {0}", distText));
|
---|
184 | }
|
---|
185 | if (o instanceof Way && ((Way) o).concernsArea() && ((Way) o).isClosed()) {
|
---|
186 | double area = Geometry.closedWayArea((Way) o);
|
---|
187 | String areaText = SystemOfMeasurement.getSystemOfMeasurement().getAreaText(area);
|
---|
188 | add(tr("Area: {0}", areaText));
|
---|
189 | }
|
---|
190 | addWayNodes((IWay<?>) o);
|
---|
191 | } else if (o instanceof IRelation) {
|
---|
192 | addBbox(o);
|
---|
193 | if (o instanceof Relation && ((Relation) o).concernsArea()) {
|
---|
194 | double area = Geometry.multipolygonArea(((Relation) o));
|
---|
195 | String areaText = SystemOfMeasurement.getSystemOfMeasurement().getAreaText(area);
|
---|
196 | add(tr("Area: {0}", areaText));
|
---|
197 | }
|
---|
198 | addRelationMembers((IRelation<?>) o);
|
---|
199 | }
|
---|
200 | }
|
---|
201 |
|
---|
202 | void addRelationMembers(IRelation<?> r) {
|
---|
203 | add(trn("{0} Member: ", "{0} Members: ", r.getMembersCount(), r.getMembersCount()));
|
---|
204 | for (IRelationMember<?> m : r.getMembers()) {
|
---|
205 | s.append(INDENT).append(INDENT);
|
---|
206 | addHeadline(m.getMember());
|
---|
207 | s.append(tr(" as \"{0}\"", m.getRole()));
|
---|
208 | s.append(NL);
|
---|
209 | }
|
---|
210 | }
|
---|
211 |
|
---|
212 | void addWayNodes(IWay<?> w) {
|
---|
213 | add(tr("{0} Nodes: ", w.getNodesCount()));
|
---|
214 | for (INode n : w.getNodes()) {
|
---|
215 | s.append(INDENT).append(INDENT);
|
---|
216 | addNameAndId(n);
|
---|
217 | s.append(NL);
|
---|
218 | }
|
---|
219 | }
|
---|
220 |
|
---|
221 | void addBbox(IPrimitive o) {
|
---|
222 | BBox bbox = o.getBBox();
|
---|
223 | if (bbox != null) {
|
---|
224 | final LatLon bottomRight = bbox.getBottomRight();
|
---|
225 | final LatLon topLeft = bbox.getTopLeft();
|
---|
226 | add(tr("Bounding box: "), toStringCSV(false, bottomRight, topLeft));
|
---|
227 | add(tr("Bounding box (projected): "), toStringCSV(true, bottomRight, topLeft));
|
---|
228 | add(tr("Center of bounding box: "), toStringCSV(false, bbox.getCenter()));
|
---|
229 | }
|
---|
230 | }
|
---|
231 |
|
---|
232 | void addCoordinates(INode n) {
|
---|
233 | if (n.isLatLonKnown()) {
|
---|
234 | add(tr("Coordinates:"), " ", toStringCSV(false, n));
|
---|
235 | add(tr("Coordinates (projected): "), toStringCSV(true, n));
|
---|
236 | Pair<Integer, Hemisphere> utmZone = TransverseMercator.locateUtmZone(n.getCoor());
|
---|
237 | String utmLabel = tr("UTM Zone");
|
---|
238 | add(utmLabel, utmLabel.endsWith(":") ? " " : ": ", Integer.toString(utmZone.a), utmZone.b.name().substring(0, 1));
|
---|
239 | }
|
---|
240 | }
|
---|
241 |
|
---|
242 | void addReferrers(StringBuilder s, IPrimitive o) {
|
---|
243 | List<? extends IPrimitive> refs = o.getReferrers();
|
---|
244 | if (!refs.isEmpty()) {
|
---|
245 | add(tr("Part of: "));
|
---|
246 | for (IPrimitive p : refs) {
|
---|
247 | s.append(INDENT).append(INDENT);
|
---|
248 | addHeadline(p);
|
---|
249 | s.append(NL);
|
---|
250 | }
|
---|
251 | }
|
---|
252 | }
|
---|
253 |
|
---|
254 | void addConflicts(OsmPrimitive o) {
|
---|
255 | Conflict<?> c = ((DataSet) ds).getConflicts().getConflictForMy(o);
|
---|
256 | if (c != null) {
|
---|
257 | add(tr("In conflict with: "));
|
---|
258 | addNameAndId(c.getTheir());
|
---|
259 | }
|
---|
260 | }
|
---|
261 |
|
---|
262 | /**
|
---|
263 | * Returns the coordinates in human-readable format.
|
---|
264 | * @param projected whether to use projected coordinates
|
---|
265 | * @param coordinates the coordinates to format
|
---|
266 | * @return String in the format {@code "1.23456, 2.34567"}
|
---|
267 | */
|
---|
268 | private static String toStringCSV(boolean projected, ILatLon... coordinates) {
|
---|
269 | final AbstractCoordinateFormat format = projected
|
---|
270 | ? ProjectedCoordinateFormat.INSTANCE
|
---|
271 | : DecimalDegreesCoordinateFormat.INSTANCE;
|
---|
272 | return Arrays.stream(coordinates)
|
---|
273 | .flatMap(ll -> Stream.of(format.latToString(ll), format.lonToString(ll)))
|
---|
274 | .collect(Collectors.joining(", "));
|
---|
275 | }
|
---|
276 |
|
---|
277 | @Override
|
---|
278 | public String toString() {
|
---|
279 | return s.toString();
|
---|
280 | }
|
---|
281 | }
|
---|