gvsig-3d / 2.1 / trunk / org.gvsig.view3d / org.gvsig.view3d.swing / org.gvsig.view3d.swing.impl / src / main / java / org / gvsig / view3d / swing / impl / data / GvSIGLayerDataRaster.java @ 446
History | View | Annotate | Download (9.04 KB)
1 | 446 | llmarques | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | *
|
||
4 | * Copyright ? 2007-2015 gvSIG Association
|
||
5 | *
|
||
6 | * This program is free software; you can redistribute it and/or
|
||
7 | * modify it under the terms of the GNU General Public License
|
||
8 | * as published by the Free Software Foundation; either version 2
|
||
9 | * of the License, or (at your option) any later version.
|
||
10 | *
|
||
11 | * This program is distributed in the hope that it will be useful,
|
||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
14 | * GNU General Public License for more details.
|
||
15 | *
|
||
16 | * You should have received a copy of the GNU General Public License
|
||
17 | * along with this program; if not, write to the Free Software
|
||
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||
19 | * MA 02110-1301, USA.
|
||
20 | *
|
||
21 | * For any additional information, do not hesitate to contact us
|
||
22 | * at info AT gvsig.com, or visit our website www.gvsig.com.
|
||
23 | */
|
||
24 | |||
25 | package org.gvsig.view3d.swing.impl.data; |
||
26 | |||
27 | import gov.nasa.worldwind.WWObjectImpl; |
||
28 | import gov.nasa.worldwind.avlist.AVKey; |
||
29 | import gov.nasa.worldwind.avlist.AVList; |
||
30 | import gov.nasa.worldwind.data.BufferedImageRaster; |
||
31 | import gov.nasa.worldwind.data.DataRaster; |
||
32 | import gov.nasa.worldwind.geom.Sector; |
||
33 | |||
34 | import java.awt.Graphics2D; |
||
35 | import java.awt.Rectangle; |
||
36 | import java.awt.geom.AffineTransform; |
||
37 | import java.awt.geom.Point2D; |
||
38 | import java.awt.image.BufferedImage; |
||
39 | |||
40 | import org.cresques.cts.ICoordTrans; |
||
41 | import org.cresques.cts.IProjection; |
||
42 | |||
43 | import org.gvsig.fmap.crs.CRSFactory; |
||
44 | import org.gvsig.fmap.dal.coverage.store.RasterDataStore; |
||
45 | import org.gvsig.fmap.dal.exception.ReadException; |
||
46 | import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
||
47 | import org.gvsig.fmap.geom.GeometryLocator; |
||
48 | import org.gvsig.fmap.geom.GeometryManager; |
||
49 | import org.gvsig.fmap.geom.exception.CreateEnvelopeException; |
||
50 | import org.gvsig.fmap.geom.primitive.Envelope; |
||
51 | import org.gvsig.fmap.mapcontext.MapContext; |
||
52 | import org.gvsig.fmap.mapcontext.ViewPort; |
||
53 | import org.gvsig.fmap.mapcontext.layers.FLayer; |
||
54 | import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
||
55 | import org.gvsig.raster.fmap.layers.FLyrRaster; |
||
56 | import org.gvsig.tools.task.Cancellable; |
||
57 | |||
58 | /**
|
||
59 | * @author <a href="mailto:lmarques@disid.com">Lluis Marques</a>
|
||
60 | *
|
||
61 | */
|
||
62 | @SuppressWarnings("deprecation") |
||
63 | public class GvSIGLayerDataRaster extends WWObjectImpl implements DataRaster { |
||
64 | |||
65 | private MapContext mapContext;
|
||
66 | private FLayer layer;
|
||
67 | |||
68 | /**
|
||
69 | *
|
||
70 | */
|
||
71 | public GvSIGLayerDataRaster(MapContext mapContext, FLayer theLayer,
|
||
72 | AVList params) { |
||
73 | |||
74 | if (params == null || mapContext == null || theLayer == null) { |
||
75 | throw new IllegalArgumentException(); |
||
76 | } |
||
77 | |||
78 | this.layer = theLayer;
|
||
79 | this.mapContext = mapContext;
|
||
80 | this.setValues(params.copy());
|
||
81 | } |
||
82 | |||
83 | public void dispose() { |
||
84 | |||
85 | } |
||
86 | |||
87 | public int getWidth() { |
||
88 | |||
89 | if (this.getValue(AVKey.WIDTH) == null) { |
||
90 | |||
91 | if (layer instanceof FLyrRaster) { |
||
92 | FLyrRaster rasterLayer = (FLyrRaster) layer; |
||
93 | RasterDataStore dataStore = rasterLayer.getDataStore(); |
||
94 | return (int) dataStore.getWidth(); |
||
95 | } |
||
96 | |||
97 | if (layer instanceof FLyrVect) { |
||
98 | // TODO
|
||
99 | } |
||
100 | } |
||
101 | |||
102 | return (Integer) this.getValue(AVKey.WIDTH); |
||
103 | } |
||
104 | |||
105 | public int getHeight() { |
||
106 | |||
107 | if (this.getValue(AVKey.HEIGHT) == null) { |
||
108 | |||
109 | if (layer instanceof FLyrRaster) { |
||
110 | FLyrRaster rasterLayer = (FLyrRaster) layer; |
||
111 | RasterDataStore dataStore = rasterLayer.getDataStore(); |
||
112 | return (int) dataStore.getHeight(); |
||
113 | } |
||
114 | |||
115 | if (layer instanceof FLyrVect) { |
||
116 | // TODO
|
||
117 | } |
||
118 | } |
||
119 | |||
120 | return (Integer) this.getValue(AVKey.HEIGHT); |
||
121 | } |
||
122 | |||
123 | public Sector getSector() {
|
||
124 | |||
125 | return (Sector) this.getValue(AVKey.SECTOR); |
||
126 | |||
127 | } |
||
128 | |||
129 | public void drawOnTo(DataRaster canvas) { |
||
130 | |||
131 | if (canvas == null) { |
||
132 | throw new IllegalArgumentException(); |
||
133 | } |
||
134 | |||
135 | Sector overlap = this.getSector().intersection(canvas.getSector());
|
||
136 | |||
137 | java.awt.Rectangle clipRect = this.computeClipRect(overlap, this); |
||
138 | if (null == clipRect || clipRect.width == 0 || clipRect.height == 0) { |
||
139 | return;
|
||
140 | } |
||
141 | |||
142 | ViewPort viewPort = new ViewPort(mapContext.getProjection());
|
||
143 | GeometryManager geoManager = GeometryLocator.getGeometryManager(); |
||
144 | |||
145 | Envelope envelope = null;
|
||
146 | try {
|
||
147 | |||
148 | double[] points = overlap.asDegreesArray(); |
||
149 | |||
150 | IProjection projection = layer.getProjection(); |
||
151 | |||
152 | if (projection == null) { |
||
153 | projection = mapContext.getProjection(); |
||
154 | } |
||
155 | |||
156 | ICoordTrans ct = CRSFactory.getCRS("EPSG:4326").getCT(projection);
|
||
157 | Point2D p1 =
|
||
158 | ct.convert(new Point2D.Double(points[2], points[0]), null); |
||
159 | Point2D p2 =
|
||
160 | ct.convert(new Point2D.Double(points[3], points[1]), null); |
||
161 | |||
162 | envelope = |
||
163 | geoManager.createEnvelope(Math.min(p1.getX(), p2.getX()),
|
||
164 | Math.min(p1.getY(), p2.getY()),
|
||
165 | Math.max(p1.getX(), p2.getX()),
|
||
166 | Math.max(p1.getY(), p2.getY()), SUBTYPES.GEOM2D);
|
||
167 | |||
168 | } catch (CreateEnvelopeException e) {
|
||
169 | // TODO Auto-generated catch block
|
||
170 | e.printStackTrace(); |
||
171 | } |
||
172 | |||
173 | viewPort.setEnvelope(envelope); |
||
174 | viewPort.setImageSize(clipRect.getSize()); |
||
175 | |||
176 | BufferedImage image =
|
||
177 | new BufferedImage((int) clipRect.getWidth(), |
||
178 | (int) clipRect.getHeight(), BufferedImage.TYPE_INT_ARGB); |
||
179 | |||
180 | try {
|
||
181 | |||
182 | layer.draw(image, (Graphics2D) image.getGraphics(), viewPort,
|
||
183 | new Cancellable() {
|
||
184 | |||
185 | public void setCanceled(boolean canceled) { |
||
186 | // TODO Auto-generated method stub
|
||
187 | } |
||
188 | |||
189 | public boolean isCanceled() { |
||
190 | // TODO Auto-generated method stub
|
||
191 | return false; |
||
192 | } |
||
193 | }, getScale(viewPort)); |
||
194 | |||
195 | } catch (ReadException e) {
|
||
196 | // TODO Auto-generated catch block
|
||
197 | e.printStackTrace(); |
||
198 | } |
||
199 | |||
200 | BufferedImageRaster bufferedImageRaster = |
||
201 | new BufferedImageRaster(overlap, image);
|
||
202 | bufferedImageRaster.drawOnTo(canvas); |
||
203 | |||
204 | } |
||
205 | |||
206 | private double getScale(ViewPort viewPort) { |
||
207 | |||
208 | MapContext mapContextCloned = mapContext.cloneFMap(); |
||
209 | mapContextCloned.setViewPort(viewPort); |
||
210 | double scale = mapContextCloned.getScaleView();
|
||
211 | mapContextCloned.dispose(); |
||
212 | |||
213 | return scale;
|
||
214 | } |
||
215 | |||
216 | private Rectangle computeClipRect(Sector clipSector, |
||
217 | DataRaster clippedRaster) { |
||
218 | |||
219 | AffineTransform geographicToRaster =
|
||
220 | this.computeGeographicToRasterTransform(clippedRaster.getWidth(),
|
||
221 | clippedRaster.getHeight(), clippedRaster.getSector()); |
||
222 | |||
223 | java.awt.geom.Point2D geoPoint = new java.awt.geom.Point2D.Double();
|
||
224 | java.awt.geom.Point2D ul = new java.awt.geom.Point2D.Double();
|
||
225 | java.awt.geom.Point2D lr = new java.awt.geom.Point2D.Double();
|
||
226 | |||
227 | geoPoint.setLocation(clipSector.getMinLongitude().degrees, |
||
228 | clipSector.getMaxLatitude().degrees); |
||
229 | geographicToRaster.transform(geoPoint, ul); |
||
230 | |||
231 | geoPoint.setLocation(clipSector.getMaxLongitude().degrees, |
||
232 | clipSector.getMinLatitude().degrees); |
||
233 | geographicToRaster.transform(geoPoint, lr); |
||
234 | |||
235 | int x = (int) Math.floor(ul.getX()); |
||
236 | int y = (int) Math.floor(ul.getY()); |
||
237 | int width = (int) Math.ceil(lr.getX() - ul.getX()); |
||
238 | int height = (int) Math.ceil(lr.getY() - ul.getY()); |
||
239 | |||
240 | return new Rectangle(x, y, width, height); |
||
241 | } |
||
242 | |||
243 | private java.awt.geom.AffineTransform computeGeographicToRasterTransform(
|
||
244 | int width, int height, Sector sector) { |
||
245 | // Compute the the transform from geographic to raster coordinates. In
|
||
246 | // this computation a pixel is assumed
|
||
247 | // to cover a finite area.
|
||
248 | |||
249 | double ty = -sector.getMaxLatitude().degrees;
|
||
250 | double tx = -sector.getMinLongitude().degrees;
|
||
251 | |||
252 | double sy = -(height / sector.getDeltaLatDegrees());
|
||
253 | double sx = (width / sector.getDeltaLonDegrees());
|
||
254 | |||
255 | java.awt.geom.AffineTransform transform = |
||
256 | new java.awt.geom.AffineTransform();
|
||
257 | transform.scale(sx, sy); |
||
258 | transform.translate(tx, ty); |
||
259 | return transform;
|
||
260 | } |
||
261 | |||
262 | public DataRaster getSubRaster(AVList params) {
|
||
263 | int width = (Integer) params.getValue(AVKey.WIDTH); |
||
264 | int height = (Integer) params.getValue(AVKey.HEIGHT); |
||
265 | Sector sector = (Sector) params.getValue(AVKey.SECTOR); |
||
266 | |||
267 | return this.getSubRaster(width, height, sector, params); |
||
268 | } |
||
269 | |||
270 | public DataRaster getSubRaster(int width, int height, Sector sector, |
||
271 | AVList params) { |
||
272 | |||
273 | params.setValue(AVKey.WIDTH, width); |
||
274 | params.setValue(AVKey.HEIGHT, height); |
||
275 | params.setValue(AVKey.SECTOR, sector); |
||
276 | |||
277 | GvSIGLayerDataRaster subRaster = |
||
278 | new GvSIGLayerDataRaster(mapContext, layer, params);
|
||
279 | return subRaster;
|
||
280 | } |
||
281 | } |