001: package org.geotools.demo;
002:
003: import java.awt.Color;
004: import java.awt.Dimension;
005: import java.awt.Graphics;
006: import java.awt.Graphics2D;
007: import java.awt.Point;
008: import java.awt.Rectangle;
009: import java.awt.event.MouseEvent;
010: import java.awt.event.MouseListener;
011: import java.awt.event.MouseMotionListener;
012: import java.io.File;
013: import java.io.FileNotFoundException;
014:
015: import javax.swing.JComponent;
016: import javax.swing.JFileChooser;
017: import javax.swing.JFrame;
018: import javax.swing.JPanel;
019: import javax.swing.WindowConstants;
020: import javax.swing.filechooser.FileFilter;
021:
022: import org.geotools.coverage.grid.GridCoverage2D;
023: import org.geotools.gce.image.WorldImageReader;
024: import org.geotools.geometry.DirectPosition2D;
025: import org.geotools.geometry.jts.ReferencedEnvelope;
026: import org.geotools.renderer.lite.gridcoverage2d.GridCoverageRenderer;
027: import org.geotools.styling.RasterSymbolizer;
028: import org.geotools.styling.RasterSymbolizerImpl;
029: import org.opengis.geometry.DirectPosition;
030:
031: /**
032: * This class is my first cut at rendering an image, please
033: * use ImageLab2 as a good example.
034: *
035: * @author Jody Garnett
036: * @depreacted Please use ImageLab2 as an example
037: */
038: public class ImageDisplay extends JFrame {
039:
040: /**
041: *
042: */
043: private static final long serialVersionUID = 5611889483925986034L;
044: /** Original coverage we are working on */
045: GridCoverage2D coverage;
046: CoveragePanel panel;
047: Zoom zoom;
048: RasterSymbolizer style = new RasterSymbolizerImpl();
049: ReferencedEnvelope bbox;
050:
051: /**
052: * Explore the functionality of the provided GridCoverage (think BufferedImage + CRS).
053: * <p>
054: * A GridCoverage literally a set of features that "covers"
055: * an area without gaps; in the case of grid coverage the area
056: * is covered by an regular grid.
057: * <p>
058: * Coverage work by letting you call a "sample" operation in order
059: * to retrieve a Record of the data at the location. A grid coverage
060: * lets you express the location using row and column.
061: * <p>
062: * @param coverage
063: */
064: public ImageDisplay(GridCoverage2D coverage) {
065: setResizable(false);
066: setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
067: setTitle("ImageLab");
068: this .coverage = coverage;
069: this .bbox = new ReferencedEnvelope(coverage.getEnvelope());
070: this .panel = new CoveragePanel();
071: add(panel);
072:
073: this .zoom = new Zoom();
074:
075: setGlassPane(zoom);
076: zoom.setVisible(true);
077:
078: pack();
079: }
080:
081: /**
082: * Take the clicks and update the bbox; implemented as a glass pane
083: * so we do not have to redraw the image.
084: */
085: class Zoom extends JComponent implements MouseListener,
086: MouseMotionListener {
087: /**
088: *
089: */
090: private static final long serialVersionUID = 7936937113781962010L;
091: Point point1;
092: Point point2;
093:
094: Zoom() {
095: addMouseListener(this );
096: addMouseMotionListener(this );
097: }
098:
099: public void mouseClicked(MouseEvent squeek) {
100: if (squeek.getClickCount() == 2) {
101: bbox = new ReferencedEnvelope(coverage.getEnvelope2D());
102: point1 = null;
103: point2 = null;
104: }
105: }
106:
107: public void mouseEntered(MouseEvent squeek) {
108: }
109:
110: public void mouseExited(MouseEvent squeek) {
111: }
112:
113: public void mousePressed(MouseEvent squeek) {
114: point1 = squeek.getPoint();
115: repaint();
116: }
117:
118: public void mouseReleased(MouseEvent squeek) {
119: if (point1 != null && point2 != null
120: && point1.distance(point2) > 5) {
121: DirectPosition2D location1 = toLocation(point1);
122: DirectPosition2D location2 = toLocation(point2);
123: if (location1 == null || location2 == null)
124: return;
125:
126: bbox = aspectRatioFix(location1, location2);
127: panel.repaint();
128: }
129: point1 = null;
130: point2 = null;
131: repaint();
132: }
133:
134: public void mouseDragged(MouseEvent squeek) {
135: if (point1 != null) {
136: point2 = squeek.getPoint();
137: repaint();
138: }
139: }
140:
141: public void mouseMoved(MouseEvent squeek) {
142:
143: }
144:
145: protected void paintComponent(Graphics g) {
146: if (point1 != null && point2 != null) {
147: Rectangle feedback = new Rectangle(point1);
148: feedback.add(point2);
149:
150: g.setColor(Color.BLUE);
151: g.drawRect(feedback.x, feedback.y, feedback.width,
152: feedback.height);
153:
154: g.setColor(new Color(0.0f, 0.0f, 1.0f, 0.5f));
155: g.fillRect(feedback.x, feedback.y, feedback.width,
156: feedback.height);
157: }
158: }
159:
160: public DirectPosition2D toLocation(Point point) {
161: double w = (double) getWidth();
162: double h = (double) getHeight();
163: double rx = ((double) point.x) / w;
164: double ry = (h - (double) point.y) / h;
165:
166: DirectPosition start = bbox.getLowerCorner();
167: DirectPosition2D location = new DirectPosition2D(coverage
168: .getCoordinateReferenceSystem());
169: location.x = start.getOrdinate(0) + rx * bbox.getWidth();
170: location.y = start.getOrdinate(1) + ry * bbox.getHeight();
171:
172: return location;
173: }
174:
175: public ReferencedEnvelope aspectRatioFix(DirectPosition start,
176: DirectPosition end) {
177: ReferencedEnvelope request = new ReferencedEnvelope(
178: coverage.getCoordinateReferenceSystem());
179: request.include(start.getOrdinate(0), start.getOrdinate(1));
180: request.include(end.getOrdinate(0), end.getOrdinate(1));
181: start = request.getLowerCorner();
182: end = request.getUpperCorner();
183:
184: ReferencedEnvelope world = new ReferencedEnvelope(coverage
185: .getEnvelope());
186: double ratio = world.getHeight() / world.getWidth();
187:
188: double w = request.getWidth();
189: double h = request.getWidth() * ratio;
190:
191: ReferencedEnvelope envelope = new ReferencedEnvelope(
192: coverage.getCoordinateReferenceSystem());
193: envelope
194: .include(start.getOrdinate(0), start.getOrdinate(1));
195: envelope.include(start.getOrdinate(0) + w, start
196: .getOrdinate(1)
197: + h);
198: return envelope;
199: }
200: }
201:
202: class CoveragePanel extends JPanel {
203: /**
204: *
205: */
206: private static final long serialVersionUID = 3956983132547799557L;
207:
208: CoveragePanel() {
209: setBackground(Color.WHITE);
210: }
211:
212: public Dimension getPreferredSize() {
213: ReferencedEnvelope world = new ReferencedEnvelope(coverage
214: .getEnvelope());
215: double ratio = world.getHeight() / world.getWidth();
216: double w = 640.0;
217: double h = 640.0 * ratio;
218: return new Dimension((int) w, (int) h);
219: }
220:
221: public void paintComponent(Graphics graphics) {
222: super .paintComponents(graphics);
223: Graphics2D g = (Graphics2D) graphics;
224: GridCoverageRenderer renderer;
225: try {
226: renderer = new GridCoverageRenderer(coverage
227: .getCoordinateReferenceSystem(), bbox,
228: getBounds());
229: renderer.paint(g, coverage, style);
230: } catch (Exception e) {
231: g.drawString(e.getLocalizedMessage(), 0,
232: getHeight() / 2);
233: }
234: }
235: }
236:
237: /**
238: * Prompt the user for a file and open up ImageLab.
239: * @param args filename of image
240: */
241: public static void main(String[] args) throws Exception {
242: File file = getImageFile(args);
243: WorldImageReader reader = new WorldImageReader(file);
244: GridCoverage2D coverage = (GridCoverage2D) reader.read(null);
245:
246: ImageDisplay imageDisplay = new ImageDisplay(coverage);
247: imageDisplay.setVisible(true);
248: }
249:
250: private static File getImageFile(String[] args)
251: throws FileNotFoundException {
252: File file;
253: if (args.length == 0) {
254: JFileChooser chooser = new JFileChooser();
255: chooser.setDialogTitle("Open Image file");
256: chooser.setFileFilter(new FileFilter() {
257: public boolean accept(File f) {
258: return f.isDirectory()
259: || f.getPath().endsWith("jpg")
260: || f.getPath().endsWith("JPG");
261: }
262:
263: public String getDescription() {
264: return "JPEG";
265: }
266: });
267: int returnVal = chooser.showOpenDialog(null);
268:
269: if (returnVal != JFileChooser.APPROVE_OPTION) {
270: System.exit(0);
271: }
272: file = chooser.getSelectedFile();
273:
274: System.out.println("Opening Image file: " + file.getName());
275: } else {
276: file = new File(args[0]);
277: }
278: if (!file.exists()) {
279: throw new FileNotFoundException(file.getAbsolutePath());
280: }
281: return file;
282: }
283: }
|