-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNDVILineGraph.java
executable file
·582 lines (524 loc) · 21.8 KB
/
NDVILineGraph.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
// NDVILineGraph.java implements a class that reads and displays the
// ndvi data on a line graph.
//------------------------------------------------------------------------
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.AffineTransform;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.net.URL;
import java.text.DecimalFormat;
import java.util.Vector;
import java.util.NoSuchElementException;
import java.util.StringTokenizer;
import java.util.zip.GZIPInputStream;
import javax.swing.JButton;
import javax.swing.JPanel;
public class NDVILineGraph extends JPanel implements MouseListener, Printable
{
private imgViewer applet; // reference to applet
private MosaicData md; // reference to the mosaicData class
private LineGraph lineGraph;// reference to the lineGraph class
private NDVIGraphDialog ndviGraphDialog; // reference to the dialog box
private String[] landcoverNames; // array of Landcover Names
private int gridCol; // grid column of landcover data
private int gridRow; // grid row of landcover data
private int displayYear = 0;// year currently displayed
private int topMargin; // graph top margin
private int bottomMargin; // graph bottom margin
private int leftMargin; // graph left margin
private int rightMargin; // graph right margin
private Metadata currentScene; // current selected scene
private Sensor currSensor; // current selected sensor
private Landcover landcover;// class to hold the ndvi values
private Vector availLandcover; // vector that holds the landcover class
private int firstYear; // first year of landcover data available
private DecimalFormat threeDigitFormat; // three digit path/row formatter
private static String[] XAxisBottomValues =
{"Jan 14","Jan 28","Feb 11","Feb 25","Mar 11", "Mar 25","Apr 8",
"Apr 22","May 6","May 20","Jun 3","Jun 17","Jul 1","Jul 15",
"Jul 29","Aug 12","Aug 26","Sep 9","Sep 23","Oct 7","Oct 21",
"Nov 4","Nov 18","Dec 2","Dec 16","Dec 31"};
private static String[] XAxisTopValues =
{"14","28","42","56","70", "84","98",
"112","126","140","154","168","182","196",
"210","224","238","252","266","280","294",
"308","322","336","350","365"};
private boolean dataAvailable; //flag to indicate if data is available
// class to store the ndvi data
class Landcover
{
String name;
int year;
int count;
double[] ndviValues;
}
// Constructor for the NDVI Line graph
//--------------------------------------
public NDVILineGraph(imgViewer applet, MosaicData md,
NDVIGraphDialog dialogBox, String[] landcoverNames )
{
this.applet = applet;
this.md = md;
ndviGraphDialog = dialogBox;
this.landcoverNames = landcoverNames;
availLandcover = new Vector();
setBackground(Color.WHITE);
addMouseListener(this);
// graph line colors
// must match number and order of elements in landcoverNames array
Color[] spectrum = new Color[20];
spectrum[0] = new Color(0,0,0); // black - DECIDUOUS FOREST
spectrum[1] = new Color(255,0,255); // pink - EVERGREEN FOREST
spectrum[2] = new Color(0,135,0); // green - HERB. GRASSLANDS
spectrum[3] = new Color(255,0,0); // red - HERB. WETLANDS
spectrum[4] = new Color(0,0,252); // blue - MIXED FOREST
spectrum[5] = new Color(156,85,0); // brown - PASTURE HAY
spectrum[6] = new Color(145,0,205); // purple - SHRUBLAND
spectrum[7] = new Color(255,111,0); // orange - ROW CROPS
spectrum[8] = new Color(153,153,153); // grey - SMALL GRAINS
spectrum[9] = new Color(0,153,153); // greenish - TRANSITIONAL
spectrum[10] = new Color(204,204,0); // yellowish - WOODY WETLANDS
spectrum[11] = new Color(255,111,0); // orange - Cultivated Crops
spectrum[12] = new Color(0,0,0); // black - Deciduous Forest
spectrum[13] = new Color(255,0,255); // pink - Evergreen Forest
spectrum[14] = new Color(0,135,0); // green - Herb. Grasslands
spectrum[15] = new Color(255,0,0); // red - Herb. Wetlands
spectrum[16] = new Color(0,0,252); // blue - Mixed Forest
spectrum[17] = new Color(156,85,0); // brown - Pasture/Hay
spectrum[18] = new Color(145,0,205); // purple - Shrub/Scrub
spectrum[19] = new Color(204,204,0); // yellowish - Woody Wetlands
// graph layout settings
topMargin = 50;
bottomMargin = 110;
leftMargin = 50;
rightMargin = 30;
int padding = 20;
double yScaleMax = 1;
double yScaleMin = 0;
int yRange = 10;
int numXSteps = 26; // # elements in XAxisTopValues, XAxisBottomValues
double yAxisStep = 0.1;
boolean decimalFormat = true;
lineGraph = new LineGraph(spectrum, topMargin, bottomMargin,
leftMargin, rightMargin, padding,
yScaleMax, yScaleMin, numXSteps,
yAxisStep, yRange, decimalFormat);
threeDigitFormat = new DecimalFormat("000");
}
// get the current scene the graph is drawn to
//--------------------------------------------
public Metadata getScene()
{
return currentScene;
}
// get the current sensor the graph is drawn for
//----------------------------------------------
public Sensor getSensor()
{
return currSensor;
}
// check to see if data was available for the last selected scene
//---------------------------------------------------------------
public void checkAvailability(double[][] values)
{
dataAvailable = false;
// detect when no data is available for display
for (int i = 0; i < values.length; i++)
{
if (values[i] != null)
{
dataAvailable = true;
break;
}
}
}
// check if data is available
//-----------------------------------
public boolean isDataAvailable()
{
return dataAvailable;
}
// read the ndvi file.
//-------------------------------
private void read()
{
// Get the current sensor and scene.
currSensor = applet.sensorMenu.getCurrentSensor();
TOC currentCell = md.getCurrentCell();
gridCol = currentCell.gridCol;
gridRow = currentCell.gridRow;
currentScene = md.getCurrentScene();
if (currentScene == null)
{
availLandcover.removeAllElements();
return;
}
displayYear = currentScene.year;
// get the current scenes column, row to find the
// right ndvi file and data.
BufferedReader data = null;
// clear out the available landcovers vector.
availLandcover.removeAllElements();
int numLines = 0;
// Note: the landcovers are sorted in the order of NDVI counts
// which is the largest ndvi count to the smallest.
try
{
// open the NDVI file
URL ndviURL = new URL(applet.getCodeBase(),"NDVI/p"
+ threeDigitFormat.format(gridCol)
+ "/r" + threeDigitFormat.format(gridRow)
+ "/NDVI.gz");
InputStream is = ndviURL.openStream();
data = new BufferedReader(new InputStreamReader(
new GZIPInputStream(is)));
String dataLine = data.readLine();
if (dataLine == null)
{
System.out.println(
"Error reading NDVI Data file for gridCol/gridRow "
+ gridCol + "/" + gridRow);
data.close();
return;
}
try
{
// parse the data line read in
StringTokenizer st = new StringTokenizer(dataLine,",");
int tempVal = Integer.parseInt(st.nextToken());
if (tempVal != gridCol)
{
System.out.println(
"Error in NDVI Data file -- incorrect Path specified.");
System.out.println(" " + tempVal + " " + gridCol + "\n");
data.close();
return;
}
tempVal = Integer.parseInt(st.nextToken());
if (tempVal != gridRow)
{
System.out.println(
"Error in NDVI Data file -- incorrect Row specified.");
data.close();
return;
}
numLines = Integer.parseInt(st.nextToken());
}
catch (NoSuchElementException e)
{
System.out.println("Exception: " + e.getMessage());
data.close();
return;
}
catch (NumberFormatException e)
{
System.out.println("Exception: " + e.getMessage());
data.close();
return;
}
firstYear = -1;
// for each line store the values in the landcover class and
// add them to the availLandcover vector.
for (int i = 0; i < numLines; i++)
{
dataLine = data.readLine();
StringTokenizer st = new StringTokenizer(dataLine,",");
int tempYear = Integer.parseInt(st.nextToken());
if (firstYear == -1)
firstYear = tempYear;
if (dataLine == null)
{
System.out.println(
"Error reading NDVI Data file for gridCol/gridRow "
+ gridCol + "/" + gridRow);
data.close();
return;
}
try
{
Landcover landcover = new NDVILineGraph.Landcover();
// get the landcover name, count and string of ndvi
// data values.
String [] temp = null;
String landcoverName = "";
double[] dataValues = new double[26];
String landcoverCount = "";
temp = dataLine.split(",");
double d = 0;
for (int j = 0 ; j < temp.length ; j++)
{
if (j == 1)
{
landcoverCount = temp[j];
}
else if (j == 2)
{
landcoverName = temp[j];
}
else if (j >= 3)
{
int value = Integer.parseInt(temp[j]);
// convert to NDVI scale
d = (value - 100);
d = d/100;
dataValues[j - 3] = d;
}
}
// do a little tweaking of the long class names
if (landcoverName.equals("Grassland/Herbaceous"))
{
landcoverName = "Herb. Grasslands";
}
else if (landcoverName.equals("Emergent Herbaceous Wetlands"))
{
landcoverName = "Herb. Wetlands";
}
// assign the values to the landcover class
landcover.ndviValues = dataValues;
landcover.year = tempYear;
landcover.name = landcoverName;
landcover.count = Integer.parseInt(landcoverCount);
// populate the vector
availLandcover.addElement(landcover);
}
catch (NoSuchElementException e)
{
System.out.println("Exception: "+ e.getMessage());
data.close();
return;
}
catch (NumberFormatException e)
{
System.out.println("Exception: "+ e.getMessage());
data.close();
return;
}
}
}
catch(IOException e)
{
// if the data file was opened, close it and issue a message.
// if it was never successfully opened, skip issuing a message
// since the file probably just doesn't exist.
if (data != null)
{
try {data.close();} catch (Exception e1){};
System.out.println("Exception: "+e.getMessage());
}
}
}
// method to draw the all parts of the line graph
//-----------------------------------------------
public void paintComponent(Graphics g)
{
Dimension displaySize = getSize();
String title = "NDVI Data for ";
if (currentScene != null)
title += "Year " + currentScene.year;
title += " Path " + gridCol + " Row " + gridRow;
String xAxisLabel = ""; // label not needed
String yAxisLabel = "NDVI Values";
Vector values = new Vector();
Vector names = new Vector();
Vector colors = new Vector();
int count = 0;
// get the values, names and colors in an array to be passed to the
// line graph class, setting the checkbox for the dialog.
int numAvailable = availLandcover.size();
for (int i = 0; i < numAvailable; i++)
{
Landcover landcover = (Landcover)availLandcover.elementAt(i);
if (landcover.year == currentScene.year)
{
if (ndviGraphDialog.setLandcover(landcover.name + ": ("
+ landcover.count + ")"))
{
values.addElement(landcover.ndviValues);
names.addElement(landcover.name);
colors.addElement(ndviGraphDialog.getColors(landcover.name
+ ": (" + landcover.count + ")"));
count++;
}
}
}
// legacy functions expect arrays, not Vectors
double[][] valuesArray = new double[values.size()][];
values.toArray(valuesArray);
String[] namesArray = new String[names.size()];
names.toArray(namesArray);
Integer[] colorsArray = new Integer[colors.size()];
colors.toArray(colorsArray);
// check if data is available
checkAvailability(valuesArray);
// draw the layout of the linegraph.
lineGraph.drawGraphLayout(g,XAxisTopValues,XAxisBottomValues,
displaySize,title,xAxisLabel,yAxisLabel);
// get the currently displayed scenes date
if ((currentScene != null) && (isDataAvailable()))
{
int daysInScale = getDaysInScale();
// the scale starts at January 14, so adjust accordingly
float percentage = ((float)currentScene.jDate - 14) / daysInScale;
lineGraph.drawSelectedDataRange(g, displaySize, percentage);
}
// draw the line graph points and legend.
lineGraph.drawPoints(g,displaySize,valuesArray,colorsArray);
lineGraph.drawLegend(g,displaySize,namesArray,colorsArray);
}
// set the default landcover
//--------------------------
// FIXME - not sure this method has much value
public void setDefaultLandcover()
{
Sensor currSensor = applet.sensorMenu.getCurrentSensor();
if (currSensor.hasNdviLineGraph)
performAction();
}
// read and set all of the landcover dialog box checkboxes
//--------------------------------------------------------
public void performAction()
{
boolean setDefault = true;
Metadata scene = md.getCurrentScene();
// read the ndvi data
if ((scene == null) || ((gridCol != scene.gridCol)
|| (gridRow != scene.gridRow))
|| (currSensor != applet.sensorMenu.getCurrentSensor()))
{
read();
}
currentScene = scene;
// check if no checkboxes are selected to know if we need to set the
// default landcover values.
if (ndviGraphDialog.isLandcoverSelected()
&& (scene != null && displayYear == scene.year))
{
setDefault = false;
}
if (scene != null)
{
displayYear = scene.year;
}
int defaultLandcoverCount = 0;
DecimalFormat formatNumber = new DecimalFormat("###,###.##");
int year = firstYear;
if (scene != null)
year = scene.year;
int numAvailable = availLandcover.size();
// set and enable/disable the dialog box checkbox
for (int i = 0; i < numAvailable; i++)
{
Landcover landcover = (Landcover)availLandcover.elementAt(i);
if (landcover.year == year)
{
// add the count to the label
String landcoverLabel = landcover.name + ": ("
+ formatNumber.format(landcover.count) + ")";
// find the landcovers that are available and if we need to
// set the default landcovers
if (setDefault && (defaultLandcoverCount <= 4))
{
ndviGraphDialog.setDefault(landcoverLabel);
}
else if (setDefault)
{
if(!ndviGraphDialog.setLandcover(landcoverLabel))
{
ndviGraphDialog.setAvailLandcover(landcoverLabel);
}
}
else
{
ndviGraphDialog.setAvailLandcover(landcoverLabel);
}
defaultLandcoverCount++;
}
}
// set the enable/disable of the set default button
if (ndviGraphDialog.isLandcoverSelected())
ndviGraphDialog.enableButtons();
else
ndviGraphDialog.disableButtons();
}
// method to return the number of days on the x-axis
//--------------------------------------------------
private int getDaysInScale()
{
int year = currentScene.year;
int daysInScale = 351;
if (((year % 4) != 0) || (((year % 100) == 0) && ((year % 400) != 0)))
{
daysInScale = 352;
}
return daysInScale;
}
// action performed event handler to display the scene that is in the
// date range the user clicked on
//-------------------------------------------------------------------
public void mouseClicked(MouseEvent event)
{
Dimension displaySize = getSize();
// get x & y coordinate of the click.
int clickedX = event.getX();
int clickedY = event.getY();
// get min and max ranges.
int minX = leftMargin;
int maxX = displaySize.width - rightMargin;
int minY = topMargin;
int maxY = displaySize.height - bottomMargin;;
// draw line if clicked in the graph area.
if (((clickedX > minX) && (clickedX < maxX)) &&
((clickedY > minY) && (clickedY < maxY)))
{
if ((currentScene != null) && (isDataAvailable()))
{
float percentage = lineGraph.getXPercentage(clickedX);
int daysInScale = getDaysInScale();
// the scale starts at January 14, so adjust accordingly
int targetDate = (int)(daysInScale * percentage + 0.5F) + 14;
md.setSceneToClosestDate(currentScene, targetDate,
currentScene.year);
}
}
}
// dummy event handlers for the unused events needed for a MouseListener
//----------------------------------------------------------------------
public void mousePressed(MouseEvent event){}
public void mouseEntered(MouseEvent event){}
public void mouseExited(MouseEvent event){}
public void mouseReleased(MouseEvent event){}
// printable interface method to print the graph
//----------------------------------------------
public int print(Graphics g, PageFormat pf, int pageIndex)
throws PrinterException
{
// only a single page is available to print, so if anything past the
// 0 index, return that the page doesn't exist
if (pageIndex >= 1)
return Printable.NO_SUCH_PAGE;
// determine how much the image needs to be scaled to fit on the page
Dimension size = getSize();
double scale = pf.getImageableWidth() / size.width;
double hScale = pf.getImageableHeight() / size.height;
if (hScale < scale)
scale = hScale;
// set the location to start printing on the page and the correct scale
Graphics2D g2 = (Graphics2D) g;
g2.translate(pf.getImageableX(), pf.getImageableY());
g2.scale(scale, scale);
// paint the graph on the printed page
paintComponent(g2);
// indicate the page exists
return Printable.PAGE_EXISTS;
}
}