gvsig-gazetteer / org.gvsig.gazetteer / trunk / org.gvsig.gazetteer / org.gvsig.gazetteer.extension / src / main / java / org / gvsig / gazetteer / loaders / FeatureLoader.java @ 102
History | View | Annotate | Download (8.63 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.slf4j.Logger; |
52 |
import org.slf4j.LoggerFactory; |
53 |
import org.gvsig.andami.PluginServices; |
54 |
import org.gvsig.app.project.documents.view.gui.AbstractViewPanel; |
55 |
import org.gvsig.fmap.crs.CRSFactory; |
56 |
import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
57 |
import org.gvsig.fmap.geom.GeometryLocator; |
58 |
import org.gvsig.fmap.geom.GeometryManager; |
59 |
import org.gvsig.fmap.geom.exception.CreateEnvelopeException; |
60 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
61 |
import org.gvsig.fmap.geom.primitive.Envelope; |
62 |
import org.gvsig.fmap.geom.primitive.Point; |
63 |
import org.gvsig.fmap.mapcontext.MapContext; |
64 |
import org.gvsig.fmap.mapcontext.MapContextLocator; |
65 |
import org.gvsig.fmap.mapcontext.MapContextManager; |
66 |
import org.gvsig.fmap.mapcontext.ViewPort; |
67 |
import org.gvsig.fmap.mapcontext.layers.vectorial.GraphicLayer; |
68 |
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol; |
69 |
import org.gvsig.fmap.mapcontext.rendering.symbols.ITextSymbol; |
70 |
import org.gvsig.gazetteer.DeleteSearchesExtension; |
71 |
import org.gvsig.gazetteer.querys.Feature; |
72 |
import org.gvsig.gazetteer.querys.GazetteerQuery; |
73 |
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.IMarkerSymbol; |
74 |
|
75 |
|
76 |
|
77 |
|
78 |
/**
|
79 |
* This class is used to load a new feature like a layer in gvSIG
|
80 |
*
|
81 |
* @author Jorge Piera Llodra (piera_jor@gva.es)
|
82 |
*/
|
83 |
public class FeatureLoader { |
84 |
private static final Logger LOG = |
85 |
LoggerFactory.getLogger(FeatureLoader.class); |
86 |
|
87 |
private static GeometryManager geometryManager = GeometryLocator.getGeometryManager(); |
88 |
private static MapContextManager mapContextManager = MapContextLocator.getMapContextManager(); |
89 |
|
90 |
/**
|
91 |
* Coordinates Transformer
|
92 |
*/
|
93 |
private ICoordTrans coordTrans;
|
94 |
|
95 |
/**
|
96 |
* @param projection
|
97 |
* Server projection
|
98 |
*/
|
99 |
public FeatureLoader(String sProjection){ |
100 |
AbstractViewPanel activeView = |
101 |
(AbstractViewPanel) PluginServices.getMDIManager().getActiveWindow(); |
102 |
|
103 |
|
104 |
IProjection projection = CRSFactory.getCRS(sProjection); |
105 |
if (projection == null){ |
106 |
projection = activeView.getMapControl().getViewPort().getProjection(); |
107 |
} |
108 |
|
109 |
coordTrans= projection.getCT(activeView.getMapControl().getViewPort().getProjection()); |
110 |
} |
111 |
/**
|
112 |
* It makes a zoom in gvSIG
|
113 |
* @param
|
114 |
* feature
|
115 |
* @param
|
116 |
* query
|
117 |
* Query that contains advanced options to search and
|
118 |
* to show the results
|
119 |
* @return
|
120 |
* true or false if fail
|
121 |
*/
|
122 |
public boolean load(Feature feature,GazetteerQuery query) { |
123 |
addAndDrawLabel(feature, |
124 |
query.getOptions().getAspect().isKeepOld(), |
125 |
query.getOptions().getAspect().isPaintCurrent()); |
126 |
|
127 |
if (query.getOptions().getAspect().isGoTo()){
|
128 |
focusCenter(feature); |
129 |
} |
130 |
|
131 |
return true; |
132 |
} |
133 |
|
134 |
/**
|
135 |
* This method focus the toponim in the center of the view
|
136 |
* @param feature
|
137 |
* Feature that contains the coordinates
|
138 |
*/
|
139 |
private void focusCenter(Feature feature){ |
140 |
AbstractViewPanel activeView = |
141 |
(AbstractViewPanel) PluginServices.getMDIManager().getActiveWindow(); |
142 |
|
143 |
IProjection projection = activeView.getProjection(); |
144 |
ViewPort viewPort = activeView.getMapControl().getViewPort(); |
145 |
MapContext mapContext = activeView.getMapControl().getMapContext(); |
146 |
Point2D point = getReprojectedPoint(feature.getCoordinates());
|
147 |
|
148 |
if (viewPort.getAdjustedEnvelope() != null){ |
149 |
Toolkit kit = Toolkit.getDefaultToolkit(); |
150 |
double dpi = kit.getScreenResolution();
|
151 |
|
152 |
Envelope envelope = viewPort.getAdjustedEnvelope(); |
153 |
Rectangle2D rectangle = new Rectangle2D.Double(envelope.getLowerCorner().getX(), |
154 |
envelope.getLowerCorner().getY(), |
155 |
envelope.getUpperCorner().getX() - envelope.getLowerCorner().getX(), |
156 |
envelope.getUpperCorner().getY() - envelope.getLowerCorner().getY()); |
157 |
|
158 |
Rectangle2D extent = projection.getExtent(rectangle,
|
159 |
new Double(25000).doubleValue(), |
160 |
new Double(viewPort.getImageWidth()).doubleValue(), |
161 |
new Double(viewPort.getImageHeight()).doubleValue(), |
162 |
viewPort.getMapUnits(), |
163 |
viewPort.getDistanceUnits(), |
164 |
dpi); |
165 |
if (extent != null){ |
166 |
try {
|
167 |
envelope = geometryManager.createEnvelope(SUBTYPES.GEOM2D); |
168 |
Point loweCorner = geometryManager.createPoint(point.getX() - extent.getWidth()/2, |
169 |
point.getY() - extent.getHeight()/2,
|
170 |
SUBTYPES.GEOM2D); |
171 |
Point upperCorner = geometryManager.createPoint(point.getX() + extent.getWidth()/2, |
172 |
point.getY() + extent.getHeight()/2,
|
173 |
SUBTYPES.GEOM2D); |
174 |
envelope.setLowerCorner(loweCorner); |
175 |
envelope.setUpperCorner(upperCorner); |
176 |
mapContext.zoomToEnvelope(envelope); |
177 |
} catch (CreateEnvelopeException e) {
|
178 |
LOG.error("Error creating the envelope", e);
|
179 |
} catch (CreateGeometryException e) {
|
180 |
LOG.error("Error creating the envelope", e);
|
181 |
} |
182 |
} |
183 |
} |
184 |
} |
185 |
|
186 |
/**
|
187 |
* It adds a new Label to the current view
|
188 |
* @param feature
|
189 |
* To obtain the coordinates and the toponim name
|
190 |
* @param isRemoveOldClicked
|
191 |
* To remove or keep the old searches
|
192 |
*/
|
193 |
private void addAndDrawLabel(Feature feature,boolean isRemoveOldClicked,boolean isMarkedPlaceClicked){ |
194 |
AbstractViewPanel activeView = |
195 |
(AbstractViewPanel) PluginServices.getMDIManager().getActiveWindow(); |
196 |
MapContext mc = activeView.getMapControl().getMapContext(); |
197 |
GraphicLayer lyr = mc.getGraphicsLayer(); |
198 |
|
199 |
if (isRemoveOldClicked){
|
200 |
lyr.clearAllGraphics(); |
201 |
} |
202 |
|
203 |
if (isMarkedPlaceClicked){
|
204 |
int pointSymbol = lyr.addSymbol(getSymbol());
|
205 |
int textSymbol = lyr.addSymbol(getTextSymbol(feature.getName()));
|
206 |
|
207 |
|
208 |
Point2D point2d = getReprojectedPoint(feature.getCoordinates());
|
209 |
feature.setCoordinates(point2d); |
210 |
Point point;
|
211 |
try {
|
212 |
point = geometryManager.createPoint(point2d.getX(), point2d.getY(), SUBTYPES.GEOM2D); |
213 |
lyr.setCoordTrans(getCoordTrans()); |
214 |
lyr.addGraphic("gazetteer", point, pointSymbol, null); |
215 |
lyr.addGraphic("gazetteer", point, textSymbol, feature.getName());
|
216 |
DeleteSearchesExtension.setVisible(); |
217 |
PluginServices.getMainFrame().enableControls(); |
218 |
} catch (CreateGeometryException e) {
|
219 |
LOG.error("Error creating the point", e);
|
220 |
} |
221 |
} |
222 |
|
223 |
mc.invalidate(); |
224 |
} |
225 |
|
226 |
/**
|
227 |
* Creates a FSymbol
|
228 |
* @return
|
229 |
* FSymbol
|
230 |
*/
|
231 |
private ISymbol getTextSymbol(String text){ |
232 |
ITextSymbol theSymbol = |
233 |
(ITextSymbol) mapContextManager.getSymbolManager() |
234 |
.createSymbol(ITextSymbol.SYMBOL_NAME); |
235 |
theSymbol.setColor(Color.RED);
|
236 |
theSymbol.setTextColor(Color.BLACK);
|
237 |
theSymbol.setAutoresizeEnabled(false);
|
238 |
theSymbol.setFontSize(16);
|
239 |
theSymbol.setText(text); |
240 |
return theSymbol;
|
241 |
} |
242 |
|
243 |
/**
|
244 |
* Creates a FSymbol
|
245 |
* @return
|
246 |
* FSymbol
|
247 |
*/
|
248 |
private ISymbol getSymbol(){
|
249 |
IMarkerSymbol theSymbol = |
250 |
(IMarkerSymbol) mapContextManager.getSymbolManager() |
251 |
.createSymbol(IMarkerSymbol.CIRCLE_STYLE); |
252 |
theSymbol.setColor(Color.RED);
|
253 |
theSymbol.setSize(4);
|
254 |
return theSymbol;
|
255 |
} |
256 |
|
257 |
/**
|
258 |
* Reprojects the new point
|
259 |
* @param ptOrig
|
260 |
* Origin point
|
261 |
* @return
|
262 |
* FPoint2D
|
263 |
*/
|
264 |
private Point2D getReprojectedPoint(Point2D ptOrigin){ |
265 |
Point2D ptDest = null; |
266 |
ICoordTrans ct = getCoordTrans(); |
267 |
if(ct != null) { |
268 |
return ct.convert(ptOrigin, ptDest);
|
269 |
} |
270 |
return ptOrigin;
|
271 |
} |
272 |
/**
|
273 |
* @return the coordTrans
|
274 |
*/
|
275 |
public ICoordTrans getCoordTrans() {
|
276 |
return coordTrans;
|
277 |
} |
278 |
} |