gvsig-raster / org.gvsig.raster.tools / trunk / templates / rasterTaskProjectTemplate / alg_with_preview_template / sources / app / ProjectTemplatePreview.java @ 1908
History | View | Annotate | Download (5.4 KB)
1 | 1843 | nbrodin | package org.gvsig.raster.projecttemplate.app; |
---|---|---|---|
2 | |||
3 | import java.awt.Graphics2D; |
||
4 | import java.awt.geom.Rectangle2D; |
||
5 | import java.awt.image.BufferedImage; |
||
6 | import java.util.HashMap; |
||
7 | |||
8 | import org.gvsig.fmap.dal.coverage.RasterLocator; |
||
9 | import org.gvsig.fmap.dal.coverage.dataset.Buffer; |
||
10 | import org.gvsig.fmap.dal.coverage.datastruct.Extent; |
||
11 | import org.gvsig.fmap.dal.coverage.exception.ProcessInterruptedException; |
||
12 | import org.gvsig.fmap.dal.coverage.grid.render.ImageDrawer; |
||
13 | import org.gvsig.gui.beans.imagenavigator.ImageUnavailableException; |
||
14 | import org.gvsig.raster.fmap.layers.FLyrRaster; |
||
15 | import org.gvsig.raster.projecttemplate.algorithm.ProjectTemplateProcess; |
||
16 | import org.gvsig.raster.projecttemplate.app.ProjectTemplateExtension.LayerElement; |
||
17 | import org.gvsig.raster.projecttemplate.swing.ProjectTemplateData; |
||
18 | import org.gvsig.raster.swing.preview.DataSourcePreview; |
||
19 | 1908 | nbrodin | import org.gvsig.raster.algorithm.RasterBaseAlgorithmLibrary; |
20 | import org.gvsig.raster.algorithm.process.ProcessException; |
||
21 | import org.gvsig.raster.algorithm.process.RasterProcess; |
||
22 | 1843 | nbrodin | import org.gvsig.tools.task.Cancellable; |
23 | import org.slf4j.Logger; |
||
24 | import org.slf4j.LoggerFactory; |
||
25 | |||
26 | @SuppressWarnings("deprecation") |
||
27 | public class ProjectTemplatePreview implements DataSourcePreview { |
||
28 | private FLyrRaster inputLyr = null; |
||
29 | private Extent bbox = null; |
||
30 | private ProjectTemplateData data = null; |
||
31 | private static final Logger logger = LoggerFactory.getLogger(ProjectTemplatePreview.class); |
||
32 | |||
33 | |||
34 | public void setData(ProjectTemplateData data) { |
||
35 | this.data = data;
|
||
36 | loadLayers(); |
||
37 | } |
||
38 | |||
39 | private void loadLayers() { |
||
40 | if(data == null) |
||
41 | return;
|
||
42 | Object obj = data.getInputLayer();
|
||
43 | if(obj != null) |
||
44 | inputLyr = (FLyrRaster)((LayerElement)data.getInputLayer()).getLayer(); |
||
45 | if(inputLyr != null) |
||
46 | bbox = inputLyr.getFullRasterExtent(); |
||
47 | } |
||
48 | |||
49 | public void closePreviewLayer() { |
||
50 | |||
51 | } |
||
52 | |||
53 | public double getCellSize() { |
||
54 | if(inputLyr != null) |
||
55 | return inputLyr.getDataStore().getCellSize();
|
||
56 | return 1; |
||
57 | } |
||
58 | |||
59 | public DataSourcePreview cloneDataSourcePreview() {
|
||
60 | return this; |
||
61 | } |
||
62 | |||
63 | public void popStatus() { |
||
64 | } |
||
65 | |||
66 | public void pushStatus() { |
||
67 | } |
||
68 | |||
69 | public void draw(BufferedImage image, Graphics2D g, double[] coords, int w, int h, Cancellable cancel, double scale) throws ImageUnavailableException { |
||
70 | //Posici?n de inicio dibujado dentro del Graphics
|
||
71 | int x = 0; |
||
72 | int y = 0; |
||
73 | //Coordenadas de la petici?n
|
||
74 | double iniX = coords[0]; |
||
75 | double iniY = coords[1]; |
||
76 | double endX = coords[2]; |
||
77 | double endY = coords[3]; |
||
78 | |||
79 | //Se ajustan las coordenadas de petici?n, ancho y alto en coordenadas p?xel,
|
||
80 | //as? como la coordenada de inicio de dibujado
|
||
81 | if(bbox.getULX() > coords[0]) { |
||
82 | iniX = bbox.getULX(); |
||
83 | x = (int)(((iniX - coords[0]) * w) / Math.abs(coords[2] - coords[0])); |
||
84 | } |
||
85 | |||
86 | if(bbox.getULY() < coords[1]) { |
||
87 | iniY = bbox.getULY(); |
||
88 | y = (int)(((coords[1] - iniY) * h) / Math.abs(coords[3] - coords[1])); |
||
89 | } |
||
90 | |||
91 | if(bbox.getLRX() < coords[2]) |
||
92 | endX = bbox.getLRX(); |
||
93 | |||
94 | if(bbox.getLRY() > coords[3]) |
||
95 | endY = bbox.getLRY(); |
||
96 | |||
97 | if(iniX != coords[0] || endX != coords[2]) { |
||
98 | w = (int)((Math.abs(endX - iniX) * w) / Math.abs(coords[2] - coords[0])); |
||
99 | } |
||
100 | |||
101 | if(iniY != coords[1] || endY != coords[3]) { |
||
102 | h = (int)((Math.abs(iniY - endY) * h) / Math.abs(coords[3] - coords[1])); |
||
103 | } |
||
104 | |||
105 | Extent ext = RasterLocator.getManager().getDataStructFactory().createExtent(iniX, iniY, endX, endY); |
||
106 | BufferedImage img = throwProcess(ext, w, h);
|
||
107 | g.drawImage(img, x, y, null);
|
||
108 | } |
||
109 | |||
110 | /**
|
||
111 | * Throws the process sequentially. Since the preview is small,
|
||
112 | * it should not taken a lot of time
|
||
113 | * is small
|
||
114 | * @param ext
|
||
115 | * @param w
|
||
116 | * @param h
|
||
117 | * @return
|
||
118 | */
|
||
119 | @SuppressWarnings("unchecked") |
||
120 | private BufferedImage throwProcess(Extent ext, int w, int h) { |
||
121 | loadLayers(); |
||
122 | |||
123 | RasterProcess task; |
||
124 | try {
|
||
125 | task = RasterBaseAlgorithmLibrary.getManager().createRasterTask("ProjectTemplateProcess");
|
||
126 | } catch (ProcessException e1) {
|
||
127 | e1.printStackTrace(); |
||
128 | return null; |
||
129 | } |
||
130 | task.addParam(ProjectTemplateProcess.RASTER_STORE1, inputLyr.getDataStore()); |
||
131 | task.addParam(ProjectTemplateProcess.TEST_EXTENT, ext); |
||
132 | task.addParam(ProjectTemplateProcess.TEST_WIDTH, w); |
||
133 | task.addParam(ProjectTemplateProcess.TEST_HEIGHT, h); |
||
134 | //......
|
||
135 | |||
136 | task.init(); |
||
137 | try {
|
||
138 | task.process(); |
||
139 | } catch (ProcessInterruptedException e1) {
|
||
140 | return null; |
||
141 | } catch (ProcessException e) {
|
||
142 | logger.debug("Error in process");
|
||
143 | return null; |
||
144 | } |
||
145 | |||
146 | HashMap<String, Object> map = (HashMap<String, Object>)task.getResult(); |
||
147 | Buffer buf = (Buffer)map.get(ProjectTemplateProcess.BUFFER); |
||
148 | buf.setDataExtent(ext.toRectangle2D()); |
||
149 | if(buf.getBandCount() == 1) { |
||
150 | buf.replicateBand(0, 1); |
||
151 | buf.replicateBand(0, 2); |
||
152 | } |
||
153 | ImageDrawer drawer = RasterLocator.getManager().createImageDrawerService(); |
||
154 | drawer.setBuffer(buf); |
||
155 | try {
|
||
156 | //drawer.addEnhanced(inputLyr.getDataStore().getStatistics(), true, 0.2);
|
||
157 | return (BufferedImage)drawer.drawBufferOverImageObject(); |
||
158 | } catch (ProcessInterruptedException e) {
|
||
159 | return null; |
||
160 | } |
||
161 | } |
||
162 | |||
163 | public Object getSource() { |
||
164 | return null; |
||
165 | } |
||
166 | |||
167 | public Rectangle2D getBBoxRectangle() { |
||
168 | if(inputLyr != null) |
||
169 | return inputLyr.getFullRasterExtent().toRectangle2D();
|
||
170 | return new Rectangle2D.Double(); |
||
171 | } |
||
172 | |||
173 | public void addParams(RasterProcess task, ProjectTemplateData data, String path) { |
||
174 | task.addParam(ProjectTemplateProcess.RASTER_STORE1, inputLyr.getDataStore()); |
||
175 | //......
|
||
176 | task.addParam(ProjectTemplateProcess.PATH, path); |
||
177 | } |
||
178 | } |