Debian Codegraph @

src/dr/geo/GTOPO30Tile.java

raw symbols SCIP
1 /*
2 * GTOPO30Tile.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.geo;
27
28 import dr.app.gui.ColorFunction;
29
30 import javax.imageio.ImageIO;
31 import javax.imageio.stream.ImageInputStream;
32 import javax.swing.*;
33 import java.awt.*;
34 import java.awt.geom.Rectangle2D;
35 import java.awt.image.BufferedImage;
36 import java.io.*;
37 import java.nio.ByteOrder;
38 import java.util.HashMap;
39 import java.util.Map;
40 import java.util.StringTokenizer;
41
42 /**
43 * @author Alexei Drummond
44 * @author Remco Bouckaert
45 */
46 public class GTOPO30Tile extends JComponent {
47
48 static final int NROWS = 6000;
49 static final int NCOLS = 4800;
50 static final int NODATA = -9999;
51
52 short[][] height;
53 BufferedImage image;
54 ColorFunction colorFunction;
55
56 double ulxmap, ulymap, xdim, ydim;
57
58 Rectangle2D bounds;
59
60 boolean createImage = true;
61
62
63 public GTOPO30Tile(String filename, ColorFunction function) throws IOException {
64
65 String headerFileName = filename.substring(0, filename.lastIndexOf('.')) + ".HDR";
66
67 File headerFile = new File(headerFileName);
68
69 if (headerFile.isFile()) {
70 try {
71 readHeader(headerFile);
72 } catch (FileNotFoundException e) {
73 System.err.println("No header file named " + headerFile.toString() + " found");
74 }
75 }
76
77 height = new short[NROWS][NCOLS];
78
79 colorFunction = function;
80
81 read(filename);
82 }
83
84 private void readHeader(File headerFile) throws IOException {
85
86 BufferedReader reader = new BufferedReader(new FileReader(headerFile));
87 Map<String, String> properties = new HashMap<String, String>();
88
89 String line = reader.readLine();
90 while (line != null) {
91 StringTokenizer tokens = new StringTokenizer(line);
92 if (tokens.countTokens() == 2) {
93 properties.put(tokens.nextToken(), tokens.nextToken());
94 }
95 line = reader.readLine();
96 }
97 ulxmap = Double.parseDouble(properties.get("ULXMAP"));
98 ulymap = Double.parseDouble(properties.get("ULYMAP"));
99 xdim = Double.parseDouble(properties.get("XDIM"));
100 ydim = Double.parseDouble(properties.get("YDIM"));
101
102 bounds = new Rectangle2D.Double(ulxmap - xdim / 2.0, ulymap + ydim / 2.0 - ydim * NROWS, xdim * NCOLS, ydim * NROWS);
103
104 if (NROWS != Integer.parseInt(properties.get("NROWS"))) {
105 throw new RuntimeException("Expected NROWS to be " + NROWS + " in header file " + headerFile);
106 }
107 if (NCOLS != Integer.parseInt(properties.get("NCOLS"))) {
108 throw new RuntimeException("Expected NCOLS to be " + NCOLS + " in header file " + headerFile);
109 }
110 }
111
112 void read(String sFile) {
113
114 if (createImage) {
115 image = new BufferedImage(NCOLS, NROWS, BufferedImage.TYPE_INT_RGB);
116 }
117
118 try {
119 ImageInputStream iis = ImageIO.createImageInputStream(new File(sFile));
120 iis.setByteOrder(ByteOrder.BIG_ENDIAN);
121
122 for (int y = 0; y < NROWS; y++) {
123 iis.readFully(height[y], 0, NCOLS);
124
125 if (createImage) {
126
127 for (int x = 0; x < NCOLS; x++) {
128 int color = height[y][x];//iis.readShort();
129 if (color == -9999) {
130 image.setRGB(x, y, Color.blue.darker().getRGB());
131 } else {
132 image.setRGB(x, y, colorFunction.getColor((float) color).getRGB());
133 }
134 }
135 }
136 }
137 } catch (Exception e) {
138 e.printStackTrace();
139 }
140 System.err.println();
141 } // read
142
143 protected void paintComponent(Graphics g) {
144 int nW = getWidth();
145 int nH = getHeight();
146 g.drawImage(image, 0, 0, nW, nH, 0, 0, NCOLS, NROWS, null);
147 }
148
149 /**
150 * @param y latitudinal pixel, increasing south from north edge
151 * @param x longitudinal pixel index, increasing east from west edge
152 * @return the height of the pixel at y,x
153 */
154 public short getHeight(int y, int x) {
155 return height[y][x];
156 }
157
158 boolean contains(double longitude, double latitude) {
159 return bounds.contains(longitude, latitude);
160 }
161
162 public int getMinLongitude() {
163 return (int) Math.round(bounds.getMinX());
164 }
165
166 public int getMaxLongitude() {
167 return (int) Math.round(bounds.getMaxX());
168 }
169
170 public int getMinLatitude() {
171 return (int) Math.round(bounds.getMinY());
172 }
173
174 public int getMaxLatitude() {
175 return (int) Math.round(bounds.getMaxY());
176 }
177 }