root / branches / v2_0_0_prep / extensions / extGazetteer / src / org / gvsig / gazetteer / loaders / FeatureLoader.java @ 29628
History | View | Annotate | Download (8.64 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*
|
19 |
* For more information, contact:
|
20 |
*
|
21 |
* Generalitat Valenciana
|
22 |
* Conselleria d'Infraestructures i Transport
|
23 |
* Av. Blasco Ib??ez, 50
|
24 |
* 46010 VALENCIA
|
25 |
* SPAIN
|
26 |
*
|
27 |
* +34 963862235
|
28 |
* gvsig@gva.es
|
29 |
* www.gvsig.gva.es
|
30 |
*
|
31 |
* or
|
32 |
*
|
33 |
* IVER T.I. S.A
|
34 |
* Salamanca 50
|
35 |
* 46005 Valencia
|
36 |
* Spain
|
37 |
*
|
38 |
* +34 963163400
|
39 |
* dac@iver.es
|
40 |
*/
|
41 |
package org.gvsig.gazetteer.loaders; |
42 |
|
43 |
|
44 |
import java.awt.Color; |
45 |
import java.awt.Toolkit; |
46 |
import java.awt.geom.Point2D; |
47 |
import java.awt.geom.Rectangle2D; |
48 |
|
49 |
import org.cresques.cts.ICoordTrans; |
50 |
import org.cresques.cts.IProjection; |
51 |
import org.gvsig.andami.PluginServices; |
52 |
import org.gvsig.app.project.documents.view.gui.BaseView; |
53 |
import org.gvsig.fmap.crs.CRSFactory; |
54 |
import org.gvsig.fmap.geom.GeometryLocator; |
55 |
import org.gvsig.fmap.geom.GeometryManager; |
56 |
import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
57 |
import org.gvsig.fmap.geom.exception.CreateEnvelopeException; |
58 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
59 |
import org.gvsig.fmap.geom.primitive.Envelope; |
60 |
import org.gvsig.fmap.geom.primitive.Point; |
61 |
import org.gvsig.fmap.geom.utils.FConstant; |
62 |
import org.gvsig.fmap.mapcontext.MapContext; |
63 |
import org.gvsig.fmap.mapcontext.ViewPort; |
64 |
import org.gvsig.fmap.mapcontext.layers.GraphicLayer; |
65 |
import org.gvsig.fmap.mapcontext.rendering.legend.FGraphicLabel; |
66 |
import org.gvsig.fmap.mapcontext.rendering.symbols.FSymbol; |
67 |
import org.gvsig.gazetteer.DeleteSearchesExtension; |
68 |
import org.gvsig.gazetteer.querys.Feature; |
69 |
import org.gvsig.gazetteer.querys.GazetteerQuery; |
70 |
|
71 |
|
72 |
|
73 |
|
74 |
/**
|
75 |
* This class is used to load a new feature like a layer in gvSIG
|
76 |
*
|
77 |
* @author Jorge Piera Llodra (piera_jor@gva.es)
|
78 |
*/
|
79 |
public class FeatureLoader { |
80 |
private static GeometryManager geometryManager = GeometryLocator.getGeometryManager(); |
81 |
|
82 |
/**
|
83 |
* Coordinates Transformer
|
84 |
*/
|
85 |
private ICoordTrans coordTrans;
|
86 |
|
87 |
/**
|
88 |
* @param projection
|
89 |
* Server projection
|
90 |
*/
|
91 |
public FeatureLoader(String sProjection){ |
92 |
BaseView activeView = |
93 |
(BaseView) PluginServices.getMDIManager().getActiveWindow(); |
94 |
|
95 |
|
96 |
IProjection projection = CRSFactory.getCRS(sProjection); |
97 |
if (projection == null){ |
98 |
projection = activeView.getMapControl().getViewPort().getProjection(); |
99 |
} |
100 |
|
101 |
coordTrans= projection.getCT(activeView.getMapControl().getViewPort().getProjection()); |
102 |
} |
103 |
/**
|
104 |
* It makes a zoom in gvSIG
|
105 |
* @param
|
106 |
* feature
|
107 |
* @param
|
108 |
* query
|
109 |
* Query that contains advanced options to search and
|
110 |
* to show the results
|
111 |
* @return
|
112 |
* true or false if fail
|
113 |
*/
|
114 |
public boolean load(Feature feature,GazetteerQuery query) { |
115 |
addAndDrawLabel(feature, |
116 |
query.getOptions().getAspect().isKeepOld(), |
117 |
query.getOptions().getAspect().isPaintCurrent()); |
118 |
|
119 |
if (query.getOptions().getAspect().isGoTo()){
|
120 |
focusCenter(feature); |
121 |
} |
122 |
|
123 |
return true; |
124 |
} |
125 |
|
126 |
/**
|
127 |
* This method focus the toponim in the center of the view
|
128 |
* @param feature
|
129 |
* Feature that contains the coordinates
|
130 |
*/
|
131 |
private void focusCenter(Feature feature){ |
132 |
BaseView activeView = |
133 |
(BaseView) PluginServices.getMDIManager().getActiveWindow(); |
134 |
|
135 |
IProjection projection = activeView.getProjection(); |
136 |
ViewPort viewPort = activeView.getMapControl().getViewPort(); |
137 |
Point2D point = getReprojectedPoint(feature.getCoordinates());
|
138 |
|
139 |
Rectangle2D zoomExtent = null; |
140 |
if (viewPort.getAdjustedExtent() == null){ |
141 |
|
142 |
}else{
|
143 |
Toolkit kit = Toolkit.getDefaultToolkit(); |
144 |
double dpi = kit.getScreenResolution();
|
145 |
Envelope envelope = viewPort.getAdjustedExtent(); |
146 |
Rectangle2D rectangle = new Rectangle2D.Double(envelope.getLowerCorner().getX(), |
147 |
envelope.getLowerCorner().getY(), |
148 |
envelope.getUpperCorner().getX() - envelope.getLowerCorner().getX(), |
149 |
envelope.getUpperCorner().getY() - envelope.getLowerCorner().getY()); |
150 |
Rectangle2D extent = projection.getExtent(rectangle,
|
151 |
new Double(25000).doubleValue(), |
152 |
new Double(viewPort.getImageWidth()).doubleValue(), |
153 |
new Double(viewPort.getImageHeight()).doubleValue(), |
154 |
MapContext.CHANGE[viewPort.getMapUnits()], |
155 |
MapContext.CHANGE[viewPort.getDistanceUnits()], |
156 |
dpi); |
157 |
if (extent != null){ |
158 |
try {
|
159 |
envelope = geometryManager.createEnvelope(SUBTYPES.GEOM2D); |
160 |
Point loweCorner = geometryManager.createPoint(point.getX() - extent.getWidth()/2, |
161 |
point.getY() - extent.getHeight()/2,
|
162 |
SUBTYPES.GEOM2D); |
163 |
Point upperCorner = geometryManager.createPoint(point.getX() + extent.getWidth()/2, |
164 |
point.getY() + extent.getHeight()/2,
|
165 |
SUBTYPES.GEOM2D); |
166 |
envelope.setLowerCorner(loweCorner); |
167 |
envelope.setUpperCorner(upperCorner); |
168 |
activeView.getMapControl().getMapContext().zoomToEnvelope(envelope); |
169 |
} catch (CreateEnvelopeException e) {
|
170 |
// TODO Auto-generated catch block
|
171 |
e.printStackTrace(); |
172 |
} catch (CreateGeometryException e) {
|
173 |
// TODO Auto-generated catch block
|
174 |
e.printStackTrace(); |
175 |
} |
176 |
} |
177 |
} |
178 |
} |
179 |
|
180 |
private Envelope getEnvelope(Rectangle2D rectangle) throws CreateGeometryException, CreateEnvelopeException{ |
181 |
Envelope envelope = geometryManager.createEnvelope(SUBTYPES.GEOM2D); |
182 |
Point loweCorner = geometryManager.createPoint(rectangle.getMinX(),
|
183 |
rectangle.getMinY(), |
184 |
SUBTYPES.GEOM2D); |
185 |
Point upperCorner = geometryManager.createPoint(rectangle.getMaxX(),
|
186 |
rectangle.getMaxY(), |
187 |
SUBTYPES.GEOM2D); |
188 |
envelope.setLowerCorner(loweCorner); |
189 |
envelope.setUpperCorner(upperCorner); |
190 |
return envelope;
|
191 |
} |
192 |
|
193 |
/**
|
194 |
* It adds a new Label to the current view
|
195 |
* @param feature
|
196 |
* To obtain the coordinates and the toponim name
|
197 |
* @param isRemoveOldClicked
|
198 |
* To remove or keep the old searches
|
199 |
*/
|
200 |
private void addAndDrawLabel(Feature feature,boolean isRemoveOldClicked,boolean isMarkedPlaceClicked){ |
201 |
BaseView activeView = |
202 |
(BaseView) PluginServices.getMDIManager().getActiveWindow(); |
203 |
|
204 |
GraphicLayer lyr = activeView.getMapControl().getMapContext().getGraphicsLayer(); |
205 |
|
206 |
if (isRemoveOldClicked){
|
207 |
lyr.clearAllGraphics(); |
208 |
} |
209 |
|
210 |
if (isMarkedPlaceClicked){
|
211 |
int idSymbol = lyr.addSymbol(getSymbol());
|
212 |
|
213 |
Point2D point2d = getReprojectedPoint(feature.getCoordinates());
|
214 |
Point point;
|
215 |
try {
|
216 |
point = geometryManager.createPoint(point2d.getX(), point2d.getY(), SUBTYPES.GEOM2D); |
217 |
FGraphicLabel theLabel = new FGraphicLabel(point, idSymbol, feature.getName());
|
218 |
lyr.addGraphic(theLabel); |
219 |
|
220 |
DeleteSearchesExtension.setVisible(); |
221 |
PluginServices.getMainFrame().enableControls(); |
222 |
} catch (CreateGeometryException e) {
|
223 |
// TODO Auto-generated catch block
|
224 |
e.printStackTrace(); |
225 |
} |
226 |
} |
227 |
|
228 |
activeView.getMapControl().drawGraphics(); |
229 |
|
230 |
//This line could look stupid, but is necessary because grawGraphics does't
|
231 |
//remove the old Graphics searched
|
232 |
try {
|
233 |
activeView.getMapControl().getViewPort().setEnvelope(getEnvelope(activeView.getMapControl().getViewPort().getExtent())); |
234 |
} catch (CreateGeometryException e) {
|
235 |
// TODO Auto-generated catch block
|
236 |
e.printStackTrace(); |
237 |
} catch (CreateEnvelopeException e) {
|
238 |
// TODO Auto-generated catch block
|
239 |
e.printStackTrace(); |
240 |
} |
241 |
} |
242 |
|
243 |
/**
|
244 |
* Creates a FSymbol
|
245 |
* @return
|
246 |
* FSymbol
|
247 |
*/
|
248 |
private FSymbol getSymbol(){
|
249 |
FSymbol theSymbol = new FSymbol(FConstant.SYMBOL_TYPE_TEXT);
|
250 |
theSymbol.setColor(Color.RED);
|
251 |
theSymbol.setStyle(1);
|
252 |
theSymbol.setFontColor(Color.BLACK);
|
253 |
theSymbol.setSizeInPixels(true);
|
254 |
theSymbol.setSize(5);
|
255 |
return theSymbol;
|
256 |
} |
257 |
|
258 |
// private SimpleTextSymbol getSymbol(String text){
|
259 |
// SimpleTextSymbol theSymbol = new SimpleTextSymbol();
|
260 |
// theSymbol.setTextColor(Color.RED);
|
261 |
// theSymbol.setText(text);
|
262 |
// theSymbol.setFontSize(10);
|
263 |
// return theSymbol;
|
264 |
// }
|
265 |
|
266 |
/**
|
267 |
* Reprojects the new point
|
268 |
* @param ptOrig
|
269 |
* Origin point
|
270 |
* @return
|
271 |
* FPoint2D
|
272 |
*/
|
273 |
private Point2D getReprojectedPoint(Point2D ptOrigin){ |
274 |
Point2D ptDest = null; |
275 |
return getCoordTrans().convert(ptOrigin, ptDest);
|
276 |
} |
277 |
/**
|
278 |
* @return the coordTrans
|
279 |
*/
|
280 |
public ICoordTrans getCoordTrans() {
|
281 |
return coordTrans;
|
282 |
} |
283 |
|
284 |
|
285 |
|
286 |
} |