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