1 | package org.openstreetmap.josm.data.osm;
|
---|
2 |
|
---|
3 | import java.util.Collection;
|
---|
4 | import java.util.Collections;
|
---|
5 | import java.util.HashSet;
|
---|
6 | import java.util.LinkedList;
|
---|
7 |
|
---|
8 | import org.openstreetmap.josm.data.Bounds;
|
---|
9 | import org.openstreetmap.josm.data.SelectionTracker;
|
---|
10 |
|
---|
11 | /**
|
---|
12 | * DataSet is the data behind one window in the application. It can consist of only a few
|
---|
13 | * points up to the whole osm database. DataSet's can be merged together, split up into
|
---|
14 | * several different ones, saved, (up/down/disk)loaded etc.
|
---|
15 | *
|
---|
16 | * Note, that DataSet is not an osm-primitive, so it has no key association but a few
|
---|
17 | * members to store some information.
|
---|
18 | *
|
---|
19 | * @author imi
|
---|
20 | */
|
---|
21 | public class DataSet extends SelectionTracker implements Cloneable {
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * All nodes goes here, even when included in other data (tracks etc).
|
---|
25 | * This enables the instant conversion of the whole DataSet by iterating over
|
---|
26 | * this data structure.
|
---|
27 | */
|
---|
28 | public Collection<Node> nodes = new LinkedList<Node>();
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * All pending line segments goes here. Pending line segments are those, that
|
---|
32 | * are in this list but are in no track.
|
---|
33 | */
|
---|
34 | private Collection<LineSegment> pendingLineSegments = new LinkedList<LineSegment>();
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * All tracks (Streets etc.) in the DataSet.
|
---|
38 | *
|
---|
39 | * The nodes of the track segments of this track must be objects from
|
---|
40 | * the nodes list, however the track segments are stored only in the
|
---|
41 | * track list.
|
---|
42 | */
|
---|
43 | private Collection<Track> tracks = new LinkedList<Track>();
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * Add the track to the tracklist.
|
---|
47 | */
|
---|
48 | public void addTrack(Track t) {
|
---|
49 | tracks.add(t);
|
---|
50 | }
|
---|
51 | /**
|
---|
52 | * Remove the track from the tracklist.
|
---|
53 | */
|
---|
54 | public void removeTrack(Track t) {
|
---|
55 | t.destroy();
|
---|
56 | tracks.remove(t);
|
---|
57 | }
|
---|
58 | /**
|
---|
59 | * Return a read-only collection of all tracks
|
---|
60 | */
|
---|
61 | public Collection<Track> tracks() {
|
---|
62 | return Collections.unmodifiableCollection(tracks);
|
---|
63 | }
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * Add a newly created line segment to the pending lines list.
|
---|
67 | */
|
---|
68 | public void addPendingLineSegment(LineSegment ls) {
|
---|
69 | pendingLineSegments.add(ls);
|
---|
70 | }
|
---|
71 | /**
|
---|
72 | * Remove a line segment from the pending lines list, because it has been
|
---|
73 | * assigned to the track.
|
---|
74 | * @param ls The line segment from the pending list
|
---|
75 | * @param t The track, that will hold the line segment
|
---|
76 | * @param end <code>true</code> to attach on the end. <code>false</code>
|
---|
77 | * to attach on the beginning.
|
---|
78 | */
|
---|
79 | public void assignPendingLineSegment(LineSegment ls, Track t, boolean end) {
|
---|
80 | pendingLineSegments.remove(ls);
|
---|
81 | if (end)
|
---|
82 | t.add(ls);
|
---|
83 | else
|
---|
84 | t.addStart(ls);
|
---|
85 | }
|
---|
86 | /**
|
---|
87 | * Delete the pending line segment without moving it anywhere.
|
---|
88 | */
|
---|
89 | public void destroyPendingLineSegment(LineSegment ls) {
|
---|
90 | pendingLineSegments.remove(ls);
|
---|
91 | ls.destroy();
|
---|
92 | }
|
---|
93 | /**
|
---|
94 | * Return an read-only iterator over all pending line segments.
|
---|
95 | */
|
---|
96 | public Collection<LineSegment> pendingLineSegments() {
|
---|
97 | return Collections.unmodifiableCollection(pendingLineSegments);
|
---|
98 | }
|
---|
99 |
|
---|
100 | /**
|
---|
101 | * Return the bounds of this DataSet, depending on X/Y values.
|
---|
102 | * The min of the return value is the upper left GeoPoint, the max the lower
|
---|
103 | * down GeoPoint, regarding to the X/Y values.
|
---|
104 | *
|
---|
105 | * Return null, if any point not converted yet or if there are no points at all.
|
---|
106 | *
|
---|
107 | * @return Bounding coordinate structure.
|
---|
108 | */
|
---|
109 | public Bounds getBoundsXY() {
|
---|
110 | if (nodes.isEmpty())
|
---|
111 | return null;
|
---|
112 |
|
---|
113 | Node first = nodes.iterator().next();
|
---|
114 | Bounds b = new Bounds(first.coor.clone(), first.coor.clone());
|
---|
115 | for (Node n : nodes)
|
---|
116 | {
|
---|
117 | if (Double.isNaN(n.coor.x) || Double.isNaN(n.coor.y))
|
---|
118 | return null;
|
---|
119 | if (n.coor.x < b.min.x)
|
---|
120 | b.min.x = n.coor.x;
|
---|
121 | if (n.coor.y < b.min.y)
|
---|
122 | b.min.y = n.coor.y;
|
---|
123 | if (n.coor.x > b.max.x)
|
---|
124 | b.max.x = n.coor.x;
|
---|
125 | if (n.coor.y > b.max.y)
|
---|
126 | b.max.y = n.coor.y;
|
---|
127 | }
|
---|
128 | return b;
|
---|
129 | }
|
---|
130 |
|
---|
131 | /**
|
---|
132 | * Return the bounds of this DataSet, depending on lat/lon values.
|
---|
133 | * The min of the return value is the upper left GeoPoint, the max the lower
|
---|
134 | * down GeoPoint.
|
---|
135 | *
|
---|
136 | * Return null, if any point does not have lat/lon or if there are no
|
---|
137 | * points at all.
|
---|
138 | *
|
---|
139 | * @return Bounding coordinate structure.
|
---|
140 | */
|
---|
141 | public Bounds getBoundsLatLon() {
|
---|
142 | if (nodes.isEmpty())
|
---|
143 | return null;
|
---|
144 |
|
---|
145 | Node first = nodes.iterator().next();
|
---|
146 | Bounds b = new Bounds(first.coor.clone(), first.coor.clone());
|
---|
147 | for (Node w : nodes)
|
---|
148 | {
|
---|
149 | if (Double.isNaN(w.coor.lat) || Double.isNaN(w.coor.lon))
|
---|
150 | return null;
|
---|
151 | if (w.coor.lat < b.min.lat)
|
---|
152 | b.min.lat = w.coor.lat;
|
---|
153 | if (w.coor.lon < b.min.lon)
|
---|
154 | b.min.lon = w.coor.lon;
|
---|
155 | if (w.coor.lat > b.max.lat)
|
---|
156 | b.max.lat = w.coor.lat;
|
---|
157 | if (w.coor.lon > b.max.lon)
|
---|
158 | b.max.lon = w.coor.lon;
|
---|
159 | }
|
---|
160 | return b;
|
---|
161 | }
|
---|
162 |
|
---|
163 | /**
|
---|
164 | * Remove the selection of the whole dataset.
|
---|
165 | */
|
---|
166 | public void clearSelection() {
|
---|
167 | clearSelection(nodes);
|
---|
168 | clearSelection(tracks);
|
---|
169 | for (Track t : tracks)
|
---|
170 | clearSelection(t.segments());
|
---|
171 | }
|
---|
172 |
|
---|
173 | /**
|
---|
174 | * Return a list of all selected objects. Even keys are returned.
|
---|
175 | * @return List of all selected objects.
|
---|
176 | */
|
---|
177 | @Override
|
---|
178 | public Collection<OsmPrimitive> getSelected() {
|
---|
179 | Collection<OsmPrimitive> sel = getSelected(nodes);
|
---|
180 | sel.addAll(getSelected(pendingLineSegments));
|
---|
181 | sel.addAll(getSelected(tracks));
|
---|
182 | for (Track t : tracks)
|
---|
183 | sel.addAll(getSelected(t.segments()));
|
---|
184 | return sel;
|
---|
185 | }
|
---|
186 |
|
---|
187 | /**
|
---|
188 | * Remove the selection from every value in the collection.
|
---|
189 | * @param list The collection to remove the selection from.
|
---|
190 | */
|
---|
191 | private void clearSelection(Collection<? extends OsmPrimitive> list) {
|
---|
192 | if (list == null)
|
---|
193 | return;
|
---|
194 | for (OsmPrimitive osm : list) {
|
---|
195 | osm.setSelected(false, this);
|
---|
196 | if (osm.keys != null)
|
---|
197 | clearSelection(osm.keys.keySet());
|
---|
198 | }
|
---|
199 | }
|
---|
200 |
|
---|
201 | /**
|
---|
202 | * Return all selected items in the collection.
|
---|
203 | * @param list The collection from which the selected items are returned.
|
---|
204 | */
|
---|
205 | private Collection<OsmPrimitive> getSelected(Collection<? extends OsmPrimitive> list) {
|
---|
206 | Collection<OsmPrimitive> sel = new HashSet<OsmPrimitive>();
|
---|
207 | if (list == null)
|
---|
208 | return sel;
|
---|
209 | for (OsmPrimitive osm : list) {
|
---|
210 | if (osm.isSelected())
|
---|
211 | sel.add(osm);
|
---|
212 | if (osm.keys != null)
|
---|
213 | sel.addAll(getSelected(osm.keys.keySet()));
|
---|
214 | }
|
---|
215 | return sel;
|
---|
216 | }
|
---|
217 |
|
---|
218 |
|
---|
219 | @Override
|
---|
220 | public DataSet clone() {
|
---|
221 | try {return (DataSet)super.clone();} catch (CloneNotSupportedException e) {}
|
---|
222 | return null;
|
---|
223 | }
|
---|
224 | }
|
---|