001: package com.vividsolutions.jump.workbench.imagery.geotiff;
002:
003: /*
004: * The Unified Mapping Platform (JUMP) is an extensible, interactive GUI
005: * for visualizing and manipulating spatial features with geometry and attributes.
006: *
007: * Copyright (C) 2003 Vivid Solutions
008: *
009: * This program is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU General Public License
011: * as published by the Free Software Foundation; either version 2
012: * of the License, or (at your option) any later version.
013: *
014: * This program is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
017: * GNU General Public License for more details.
018: *
019: * You should have received a copy of the GNU General Public License
020: * along with this program; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
022: *
023: * For more information, contact:
024: *
025: * Vivid Solutions
026: * Suite #1A
027: * 2328 Government Street
028: * Victoria BC V8T 5G5
029: * Canada
030: *
031: * (250)385-6040
032: * www.vividsolutions.com
033: */
034: import java.awt.geom.AffineTransform;
035: import java.awt.geom.Point2D;
036: import java.awt.image.renderable.ParameterBlock;
037:
038: import javax.media.jai.JAI;
039: import javax.media.jai.RenderedOp;
040:
041: import com.vividsolutions.jts.geom.Coordinate;
042: import com.vividsolutions.jts.geom.Envelope;
043:
044: public abstract class GeoReferencedRaster {
045: protected String imageFileLocation;
046: protected RenderedOp src = null;
047: Envelope envModel_image;
048:
049: // Image enhancement
050: double[] min;
051: double[] max;
052:
053: Coordinate coorRasterTiff_tiepointLT;
054: Coordinate coorModel_tiepointLT;
055:
056: private double dblModelUnitsPerRasterUnit_X;
057: private double dblModelUnitsPerRasterUnit_Y;
058:
059: public GeoReferencedRaster(String imageFileLocation)
060: throws Exception {
061: this .imageFileLocation = imageFileLocation;
062: }
063:
064: /**
065: * Basic fetchRasters retrieves a raster from a file. To get a raster from
066: * somewhere else, override this method in subclasses.
067: */
068: protected void fetchRaster() throws Exception {
069: src = JAI.create("fileload", imageFileLocation);
070: }
071:
072: protected void readRasterfile() throws Exception {
073: // ===========================
074: // Load the image, any format.
075: // ===========================
076: fetchRaster();
077:
078: // ======================================
079: // Image can be distorted, make it square
080: // in modelspace.
081: // ======================================
082: normalize(src);
083: }
084:
085: /**
086: * Convert a coordinate from rasterspace to modelspace.
087: *
088: * @param coorRaster
089: * coordinate in rasterspace
090: * @return coordinate in modelspace
091: */
092: private Coordinate rasterToModelSpace(Coordinate coorRaster) {
093: Coordinate coorModel = new Coordinate();
094:
095: coorModel.x = coorModel_tiepointLT.x
096: + (coorRaster.x - coorRasterTiff_tiepointLT.x)
097: * dblModelUnitsPerRasterUnit_X;
098: coorModel.y = coorModel_tiepointLT.y
099: - (coorRaster.y + coorRasterTiff_tiepointLT.y)
100: * dblModelUnitsPerRasterUnit_Y;
101: coorModel.z = 0;
102:
103: return coorModel;
104: }
105:
106: /**
107: * This method must be overridden if an image is not a square image in
108: * modelspace. It should be transformed to make it a square image in
109: * modelspace.
110: *
111: * @param image
112: */
113: protected void normalize(RenderedOp image) {
114: }
115:
116: /**
117: * @return
118: */
119: public Coordinate getCoorModel_tiepointLT() {
120: return coorModel_tiepointLT;
121: }
122:
123: /**
124: * @return
125: */
126: public Coordinate getCoorRasterTiff_tiepointLT() {
127: return coorRasterTiff_tiepointLT;
128: }
129:
130: /**
131: * @return
132: */
133: public double getDblModelUnitsPerRasterUnit_X() {
134: return dblModelUnitsPerRasterUnit_X;
135: }
136:
137: /**
138: * @return
139: */
140: public double getDblModelUnitsPerRasterUnit_Y() {
141: return dblModelUnitsPerRasterUnit_Y;
142: }
143:
144: public RenderedOp getImage() throws Exception {
145: if (src == null)
146: readRasterfile();
147: return src;
148: }
149:
150: void setEnvelope() {
151: // try
152: // {
153: Coordinate coorRaster_imageLB = new Coordinate(
154: coorRasterTiff_tiepointLT.x, src.getHeight() - 1, 0);
155: Coordinate coorRaster_imageRT = new Coordinate(
156: src.getWidth() - 1, 0, 0);
157: Coordinate coorModel_imageLB = rasterToModelSpace(coorRaster_imageLB);
158: Coordinate coorModel_imageRT = rasterToModelSpace(coorRaster_imageRT);
159:
160: envModel_image = new Envelope(coorModel_imageLB,
161: coorModel_imageRT);
162: }
163:
164: /**
165: * @param coordinate
166: */
167: public void setCoorModel_tiepointLT(Coordinate coordinate) {
168: coorModel_tiepointLT = coordinate;
169: setEnvelope();
170: }
171:
172: /**
173: * @param coordinate
174: */
175: public void setCoorRasterTiff_tiepointLT(Coordinate coordinate) {
176: coorRasterTiff_tiepointLT = coordinate;
177: //setEnvelope();
178: }
179:
180: /**
181: * @param d
182: */
183: public void setDblModelUnitsPerRasterUnit_X(double d) {
184: dblModelUnitsPerRasterUnit_X = d;
185: //setEnvelope();
186: }
187:
188: /**
189: * @param d
190: */
191: public void setDblModelUnitsPerRasterUnit_Y(double d) {
192: dblModelUnitsPerRasterUnit_Y = d;
193: setEnvelope();
194: }
195:
196: public void setAffineTransformation(AffineTransform transform) {
197: double scaleX = Math.abs(transform.getScaleX());
198: double scaleY = Math.abs(transform.getScaleY());
199:
200: setDblModelUnitsPerRasterUnit_X(scaleX);
201: setDblModelUnitsPerRasterUnit_Y(scaleY);
202:
203: Point2D rasterLT = new Point2D.Double(src.getMinX(), src
204: .getMinY());
205: Point2D modelLT = new Point2D.Double();
206: transform.transform(rasterLT, modelLT);
207:
208: setCoorRasterTiff_tiepointLT(new Coordinate(rasterLT.getX(),
209: rasterLT.getY()));
210: setCoorModel_tiepointLT(new Coordinate(modelLT.getX(), modelLT
211: .getY()));
212: }
213:
214: public RenderedOp fullContrast() {
215: int bands = src.getNumBands();
216: double[] constants = new double[bands];
217: double[] offsets = new double[bands];
218: for (int i = 0; i < bands; i++) {
219: constants[i] = 1.2 * 255 / (max[i] - min[i]);
220: offsets[i] = 255 * min[i] / (min[i] - max[i]);
221: }
222:
223: ParameterBlock pb = new ParameterBlock();
224: pb.addSource(src);
225: pb.add(constants);
226: pb.add(offsets);
227: return JAI.create("rescale", pb, null);
228: }
229:
230: public Envelope getEnvelope() {
231: return envModel_image;
232: }
233:
234: public double[] getMinimumExtreme() {
235: return min;
236: }
237:
238: public double[] getMaximumExtreme() {
239: return max;
240: }
241: }
|