1 |
package skrueger.geotools; |
2 |
|
3 |
import java.awt.Color; |
4 |
import java.awt.Point; |
5 |
import java.awt.event.MouseEvent; |
6 |
import java.awt.event.MouseWheelEvent; |
7 |
import java.awt.geom.Point2D; |
8 |
import java.util.Enumeration; |
9 |
import java.util.Hashtable; |
10 |
import java.util.Iterator; |
11 |
|
12 |
import org.geotools.data.memory.MemoryFeatureCollection; |
13 |
import org.geotools.feature.FeatureCollection; |
14 |
import org.geotools.map.MapLayer; |
15 |
import org.opengis.feature.simple.SimpleFeature; |
16 |
import org.opengis.feature.simple.SimpleFeatureType; |
17 |
import org.opengis.geometry.DirectPosition; |
18 |
|
19 |
import schmitzm.geotools.gui.SelectableXMapPane; |
20 |
import schmitzm.geotools.map.event.FeatureSelectedEvent; |
21 |
import skrueger.geotools.GeomFilterGenerator.PointFilterGenerator; |
22 |
|
23 |
import com.vividsolutions.jts.geom.Coordinate; |
24 |
import com.vividsolutions.jts.geom.Envelope; |
25 |
import com.vividsolutions.jts.geom.Geometry; |
26 |
import com.vividsolutions.jts.geom.GeometryFactory; |
27 |
|
28 |
public abstract class XMapPaneAction_Select implements XMapPaneAction { |
29 |
// |
30 |
// /** |
31 |
// * Flag fuer Modus "SimpleFeature-Auswahl auf allen (sichtbaren) Layern". |
32 |
// * |
33 |
// * @see #setState(int) |
34 |
// * @see #setState(int) |
35 |
// */ |
36 |
// public static enum SelMode { |
37 |
// SELECT_ALL |
38 |
// } |
39 |
|
40 |
public void performClick(XMapPane xMapPane, MouseEvent ev, |
41 |
DirectPosition geoCoord) { |
42 |
|
43 |
SelectableXMapPane sexMapPane = (SelectableXMapPane) xMapPane; |
44 |
|
45 |
// calculate a circle radius around the mouse position |
46 |
double radius = DEFAULT_DISTANCE_FRACTION |
47 |
* (xMapPane.getMapArea().getWidth() + xMapPane.getMapArea() |
48 |
.getHeight()) / 2; |
49 |
|
50 |
// Fenster-Koordinate in Geo-Koordinate umwandelt |
51 |
Point2D geoPoint = xMapPane.getScreenToWorld().transform(ev.getPoint(), |
52 |
null); |
53 |
|
54 |
PointFilterGenerator filterGenerator = new PointFilterGenerator( |
55 |
geoCoord, radius); |
56 |
Hashtable<MapLayer, FeatureCollection<SimpleFeatureType, SimpleFeature>> result = sexMapPane |
57 |
.findVisibleFeatures(filterGenerator, getSelectionMode(), |
58 |
new Envelope(geoPoint.getX(), geoPoint.getX(), geoPoint |
59 |
.getY(), geoPoint.getY())); |
60 |
|
61 |
// Ein Event auslösen für das jeweils nächste Feature pro Layer |
62 |
for (Enumeration<MapLayer> element = result.keys(); element |
63 |
.hasMoreElements();) { |
64 |
|
65 |
MapLayer layer = element.nextElement(); |
66 |
FeatureCollection<SimpleFeatureType, SimpleFeature> fc = result |
67 |
.get(layer); |
68 |
FeatureCollection<SimpleFeatureType, SimpleFeature> fcOne; |
69 |
|
70 |
if (fc != null && !fc.isEmpty()) { |
71 |
|
72 |
if (fc.size() > 1) { |
73 |
// If the result contains more that one feature, we only |
74 |
// return the nearest one. |
75 |
|
76 |
SimpleFeature nearestFeature = null; |
77 |
Double nearestDist = 0.0; |
78 |
|
79 |
Iterator<SimpleFeature> fcIt = fc.iterator(); |
80 |
try { |
81 |
com.vividsolutions.jts.geom.Point mousePoint = new GeometryFactory() |
82 |
.createPoint(new Coordinate(geoPoint.getX(), |
83 |
geoPoint.getY())); |
84 |
|
85 |
while (fcIt.hasNext()) { |
86 |
SimpleFeature f = fcIt.next(); |
87 |
Geometry obj = (Geometry) f |
88 |
.getDefaultGeometryProperty().getValue(); |
89 |
|
90 |
// TODO Hier muss doch irgendwie noch CRS |
91 |
// umgewrechnet werden... |
92 |
|
93 |
Geometry featureGeometry = (Geometry) obj; |
94 |
|
95 |
double distance = featureGeometry |
96 |
.distance(mousePoint); |
97 |
|
98 |
if ((nearestFeature == null) |
99 |
|| (distance < nearestDist)) { |
100 |
nearestFeature = f; |
101 |
nearestDist = distance; |
102 |
} |
103 |
|
104 |
} |
105 |
|
106 |
} finally { |
107 |
fc.close(fcIt); |
108 |
} |
109 |
|
110 |
fcOne = new MemoryFeatureCollection(fc.getSchema()); |
111 |
fc.clear(); |
112 |
fcOne.add(nearestFeature); |
113 |
} else { |
114 |
fcOne = fc; |
115 |
} |
116 |
|
117 |
// Fire the event with the one selected feature |
118 |
sexMapPane.fireMapPaneEvent(new FeatureSelectedEvent( |
119 |
sexMapPane, layer, fcOne.getBounds(), fcOne)); |
120 |
} |
121 |
} |
122 |
|
123 |
// If no vector features were found, or we are in SELECT_ALL mode, we |
124 |
// check the raster layers now |
125 |
if (getSelectionMode() == SelectableXMapPane.SELECT_ALL || result.isEmpty()) { |
126 |
sexMapPane.findGridCoverageValuesAndFireEvents(geoPoint, |
127 |
getSelectionMode()); |
128 |
} |
129 |
} |
130 |
|
131 |
/** |
132 |
* Does nothing if initiated by keyboard. |
133 |
* |
134 |
* TODO select all features should be assignable to Strg-A |
135 |
*/ |
136 |
@Override |
137 |
public void performKeyboard(XMapPane mapPane, Object param) { |
138 |
} |
139 |
|
140 |
/** |
141 |
* Default distance fraction used with line and point features. When the |
142 |
* user clicks on the map, this tool searches for features within a |
143 |
* rectangle of width w centered on the mouse location, where w is the |
144 |
* average map side length multiplied by the value of this constant. |
145 |
*/ |
146 |
public static final double DEFAULT_DISTANCE_FRACTION = 0.01d; |
147 |
|
148 |
// @Override |
149 |
// public void performClick(XMapPane xMapPane, MouseEvent ev, |
150 |
// DirectPosition geoCoord) { |
151 |
// |
152 |
// performSelectionClick(xMapPane, ev, geoCoord, XMapPane.SELECT_ALL); |
153 |
// } |
154 |
|
155 |
@Override |
156 |
public void performDragged(XMapPane mapPane, MouseEvent ev, |
157 |
Point dragStartPos, Point dragLastPos, DirectPosition startCoord, |
158 |
DirectPosition endCoord) { |
159 |
|
160 |
SelectableXMapPane sexMapPane = (SelectableXMapPane) mapPane; |
161 |
|
162 |
Point mp = ev.getPoint(); |
163 |
|
164 |
// Ist es eine wirkliche Selektion, oder etwas nur ein Klick? |
165 |
if (dragStartPos.x != mp.x && dragStartPos.y != mp.y) { |
166 |
sexMapPane.drawRectangle(sexMapPane.getGraphics(), dragStartPos, ev |
167 |
.getPoint(), Color.BLUE.brighter(), true); |
168 |
} |
169 |
|
170 |
// SELECTION! |
171 |
sexMapPane.performSelectionEvent(dragStartPos.x, dragStartPos.y, mp.x, |
172 |
mp.y, getSelectionMode()); |
173 |
} |
174 |
|
175 |
abstract int getSelectionMode(); |
176 |
|
177 |
@Override |
178 |
public void performDragging(XMapPane mapPane, MouseEvent ev, |
179 |
Point dragStartPos, Point dragLastPos, DirectPosition startCoord, |
180 |
DirectPosition endCoord) { |
181 |
if (dragLastPos != null) |
182 |
mapPane.drawRectangle(mapPane.getGraphics(), dragStartPos, |
183 |
dragLastPos, Color.BLUE.brighter(), true); |
184 |
|
185 |
mapPane.drawRectangle(mapPane.getGraphics(), dragStartPos, ev |
186 |
.getPoint(), Color.BLUE.brighter(), true); |
187 |
} |
188 |
|
189 |
@Override |
190 |
public void performWheel(XMapPane mapPane, MouseWheelEvent ev, |
191 |
DirectPosition coord) { |
192 |
} |
193 |
|
194 |
public static class All extends XMapPaneAction_Select { |
195 |
|
196 |
int getSelectionMode() { |
197 |
return SelectableXMapPane.SELECT_ALL; |
198 |
} |
199 |
} |
200 |
|
201 |
public static class OneFromTop extends XMapPaneAction_Select { |
202 |
|
203 |
int getSelectionMode() { |
204 |
return SelectableXMapPane.SELECT_ONE_FROM_TOP; |
205 |
} |
206 |
} |
207 |
|
208 |
public static class Top extends XMapPaneAction_Select { |
209 |
|
210 |
int getSelectionMode() { |
211 |
return SelectableXMapPane.SELECT_TOP; |
212 |
} |
213 |
} |
214 |
|
215 |
} |