Skip to content

Commit

Permalink
cluster TVS plot
Browse files Browse the repository at this point in the history
  • Loading branch information
wyq committed Jan 4, 2015
1 parent a80f96d commit 898d495
Show file tree
Hide file tree
Showing 7 changed files with 342 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/trajstat/forms/FrmAbout.form
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@
</Component>
<Component class="javax.swing.JLabel" name="jLabel7">
<Properties>
<Property name="text" type="java.lang.String" value="1.4.2"/>
<Property name="text" type="java.lang.String" value="1.4.3"/>
</Properties>
</Component>
<Component class="javax.swing.JLabel" name="jLabel8">
Expand Down
2 changes: 1 addition & 1 deletion src/trajstat/forms/FrmAbout.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ private void initComponents() {

jLabel6.setText("Yaqiang Wang");

jLabel7.setText("1.4.2");
jLabel7.setText("1.4.3");

jLabel8.setFont(new java.awt.Font("宋体", 1, 15)); // NOI18N
jLabel8.setText("Email:");
Expand Down
2 changes: 2 additions & 0 deletions src/trajstat/forms/FrmCWT.java
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,8 @@ private void jButton_WeightCWTActionPerformed(java.awt.event.ActionEvent evt) {/
aLayer.editCellValue("WCWT", i, wcwt);
}
aLayer.getAttributeTable().save();
aLayer.updateLegendIndexes();
this.app.getMapView().paintLayers();

//---- Hide progressbar
this.setCursor(Cursor.getDefaultCursor());
Expand Down
28 changes: 19 additions & 9 deletions src/trajstat/forms/FrmClusterCal.form
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@
<Group type="103" groupAlignment="0" max="-2" attributes="0">
<Component id="jPanel1" pref="0" max="32767" attributes="0"/>
<Group type="102" attributes="0">
<Component id="jButton_Calculate" min="-2" pref="108" max="-2" attributes="0"/>
<Group type="103" groupAlignment="0" attributes="0">
<Component id="jButton_Calculate" min="-2" pref="108" max="-2" attributes="0"/>
<Component id="jButton_viewTSV" alignment="0" min="-2" pref="108" max="-2" attributes="0"/>
</Group>
<EmptySpace type="separate" max="-2" attributes="0"/>
<Component id="jPanel2" min="-2" max="-2" attributes="0"/>
</Group>
Expand All @@ -43,18 +46,16 @@
<Group type="102" alignment="0" attributes="0">
<EmptySpace max="-2" attributes="0"/>
<Component id="jPanel1" min="-2" max="-2" attributes="0"/>
<Group type="103" groupAlignment="0" attributes="0">
<EmptySpace max="32767" attributes="0"/>
<Group type="103" groupAlignment="0" max="-2" attributes="0">
<Component id="jPanel2" min="-2" max="-2" attributes="0"/>
<Group type="102" attributes="0">
<Component id="jButton_Calculate" min="-2" pref="32" max="-2" attributes="0"/>
<EmptySpace max="32767" attributes="0"/>
<Component id="jPanel2" min="-2" max="-2" attributes="0"/>
<EmptySpace min="-2" pref="12" max="-2" attributes="0"/>
</Group>
<Group type="102" alignment="0" attributes="0">
<EmptySpace type="separate" max="-2" attributes="0"/>
<Component id="jButton_Calculate" min="-2" pref="47" max="-2" attributes="0"/>
<EmptySpace max="32767" attributes="0"/>
<Component id="jButton_viewTSV" min="-2" pref="32" max="-2" attributes="0"/>
</Group>
</Group>
<EmptySpace min="-2" pref="12" max="-2" attributes="0"/>
</Group>
</Group>
</DimensionLayout>
Expand Down Expand Up @@ -392,5 +393,14 @@
</Component>
</SubComponents>
</Container>
<Component class="javax.swing.JButton" name="jButton_viewTSV">
<Properties>
<Property name="text" type="java.lang.String" value="View TSV"/>
<Property name="horizontalTextPosition" type="int" value="0"/>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="jButton_viewTSVActionPerformed"/>
</Events>
</Component>
</SubComponents>
</Form>
155 changes: 144 additions & 11 deletions src/trajstat/forms/FrmClusterCal.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
*/
package trajstat.forms;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
Expand All @@ -27,11 +29,17 @@
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.DefaultListModel;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingWorker;
import javax.swing.WindowConstants;
import org.meteoinfo.chart.Chart;
import org.meteoinfo.chart.ChartPanel;
import org.meteoinfo.chart.plot.XY1DPlot;
import org.meteoinfo.data.XYListDataset;
import org.meteoinfo.geoprocess.analysis.Clustering;
import org.meteoinfo.ui.CheckBoxListEntry;
import org.meteoinfo.layer.VectorLayer;
Expand All @@ -45,9 +53,11 @@
import org.meteoinfo.layer.LayerDrawType;
import org.meteoinfo.legend.LegendManage;
import org.meteoinfo.legend.LegendScheme;
import org.meteoinfo.legend.PolylineBreak;
import org.meteoinfo.shape.PointZ;
import org.meteoinfo.shape.ShapeTypes;
import trajstat.Main;
import trajstat.trajectory.TrajUtil;

/**
*
Expand All @@ -59,6 +69,9 @@ public class FrmClusterCal extends javax.swing.JDialog {

/**
* Creates new form FrmClusterCal
*
* @param parent
* @param modal
*/
public FrmClusterCal(java.awt.Frame parent, boolean modal) {
super(parent, modal);
Expand All @@ -85,9 +98,10 @@ public FrmClusterCal(java.awt.Frame parent, boolean modal) {
this.jComboBox_Distance.addItem(DistanceType.ANGLE);

this.jComboBox_MaxClusterNum.removeAllItems();
for (int i = 9; i <= 20; i++) {
for (int i = 9; i <= 30; i++) {
this.jComboBox_MaxClusterNum.addItem(i);
}
this.jComboBox_MaxClusterNum.setSelectedItem(30);
}

/**
Expand Down Expand Up @@ -124,6 +138,7 @@ private void initComponents() {
jComboBox_ClusterNum = new javax.swing.JComboBox();
jButton_MeanTraj = new javax.swing.JButton();
jButton_AddToTraj = new javax.swing.JButton();
jButton_viewTSV = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setResizable(false);
Expand Down Expand Up @@ -327,6 +342,14 @@ public void actionPerformed(java.awt.event.ActionEvent evt) {
.addContainerGap())
);

jButton_viewTSV.setText("View TSV");
jButton_viewTSV.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
jButton_viewTSV.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton_viewTSVActionPerformed(evt);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
Expand All @@ -336,7 +359,9 @@ public void actionPerformed(java.awt.event.ActionEvent evt) {
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton_Calculate, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton_Calculate, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton_viewTSV, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
Expand All @@ -346,15 +371,14 @@ public void actionPerformed(java.awt.event.ActionEvent evt) {
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton_Calculate, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(12, 12, 12))
.addGroup(layout.createSequentialGroup()
.addGap(18, 18, 18)
.addComponent(jButton_Calculate, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
.addComponent(jButton_viewTSV, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(12, 12, 12))
);

pack();
Expand Down Expand Up @@ -560,7 +584,6 @@ private void jButton_MeanTrajActionPerformed(java.awt.event.ActionEvent evt) {//
shpfn = shpfn + "." + extent;
}


VectorLayer aLayer = new VectorLayer(ShapeTypes.PolylineZ);
aLayer.editAddField("Cluster", DataTypes.String);
aLayer.editAddField("Traj_Num", DataTypes.Integer);
Expand Down Expand Up @@ -678,6 +701,115 @@ private void jButton_AddToTrajActionPerformed(java.awt.event.ActionEvent evt) {/
}
}//GEN-LAST:event_jButton_AddToTrajActionPerformed

private void jButton_viewTSVActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_viewTSVActionPerformed
BufferedReader src = null;
try {
// TODO add your handling code here:
String clusterFile = this.jTextField_OutputFile.getText();
if (!new File(clusterFile).exists()) {
JOptionPane.showMessageDialog(null, "File does not exist!"
+ System.getProperty("line.separator") + clusterFile);
return;
}
this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
DefaultListModel listModel = (DefaultListModel) this.checkBoxList_Data.getModel();
List<VectorLayer> layers = new ArrayList<VectorLayer>();
int i;
for (i = 0; i < listModel.getSize(); i++) {
if (((CheckBoxListEntry) listModel.get(i)).isSelected()) {
layers.add((VectorLayer) ((CheckBoxListEntry) listModel.get(i)).getValue());
}
}
int maxClusterNum = (Integer) this.jComboBox_MaxClusterNum.getSelectedItem();
DistanceType disType = (DistanceType) this.jComboBox_Distance.getSelectedItem();
List<List<Integer>> clusters = new ArrayList<List<Integer>>();
for (i = 2; i <= maxClusterNum; i++) {
clusters.add(new ArrayList<Integer>());
}
int pointNum = Integer.parseInt(this.jLabel_PointNumValue.getText());
int cluster;
src = new BufferedReader(new FileReader(new File(clusterFile)));
src.readLine();
String[] dArray;
String line = src.readLine();
while (line != null) {
dArray = line.split(",");
if (dArray.length >= maxClusterNum) {
for (i = 2; i <= maxClusterNum; i++) {
cluster = Integer.parseInt(dArray[i]);
clusters.get(i - 2).add(cluster);
}
}
line = src.readLine();
}
src.close();
List<Double> tsvrs = new ArrayList<Double>();
double tsv1 = 0.0, tsv2, r;
for (i = 2; i <= maxClusterNum; i++) {
List<Integer> cls = clusters.get(i - 2);
tsv2 = TrajUtil.calTSV(cls, i, pointNum, layers, disType);
if (i == 2) {
tsv1 = tsv2;
} else {
r = Math.abs(tsv2 - tsv1) * 100 / tsv1;
tsvrs.add(r);
tsv1 = tsv2;
}
}

//Plot
XYListDataset dataset = new XYListDataset();
int n = maxClusterNum - 2;
double[] xvs = new double[n];
double[] yvs = new double[n];
List<Double> xTickValues = new ArrayList<Double>();
xTickValues.add(Double.valueOf(0));
xTickValues.add(Double.valueOf(1));
for (i = 0; i < n; i++) {
xvs[i] = i + 2;
xTickValues.add(Double.valueOf(i + 2));
yvs[i] = tsvrs.get(i);
}
xTickValues.add(Double.valueOf(n + 2));
dataset.addSeries("S_1", xvs, yvs);
XY1DPlot plot = new XY1DPlot(dataset);
((PolylineBreak) plot.getLegendBreak(0)).setDrawSymbol(true);
plot.getGridLine().setDrawXLine(true);
plot.getGridLine().setDrawYLine(true);
plot.getXAxis().setInverse(true);
plot.getXAxis().setLabel("Number of clusters");
plot.getXAxis().setDrawLabel(true);
plot.getXAxis().setTickValues(xTickValues);
plot.getYAxis().setLabel("Percent change in TVS (%)");
plot.getYAxis().setDrawLabel(true);
Chart chart = new Chart(plot);
ChartPanel chartPanel = new ChartPanel(chart);
JFrame frame = new JFrame();
BufferedImage image = null;
try {
image = ImageIO.read(this.getClass().getResource("/trajstat/resources/TrajStat_Logo.png"));
} catch (Exception e) {
}
frame.setIconImage(image);
frame.getContentPane().add(chartPanel, BorderLayout.CENTER);
frame.setSize(600, 400);
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
frame.setLocationRelativeTo(this);
frame.setVisible(true);
} catch (FileNotFoundException ex) {
Logger.getLogger(FrmClusterCal.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(FrmClusterCal.class.getName()).log(Level.SEVERE, null, ex);
} finally {
this.setCursor(Cursor.getDefaultCursor());
try {
src.close();
} catch (IOException ex) {
Logger.getLogger(FrmClusterCal.class.getName()).log(Level.SEVERE, null, ex);
}
}
}//GEN-LAST:event_jButton_viewTSVActionPerformed

private void addClusterToLayers(final List<String[]> cDataArray, final List<VectorLayer> layers) {
SwingWorker worker = new SwingWorker<String, String>() {
@Override
Expand Down Expand Up @@ -732,7 +864,7 @@ protected void done() {
FrmClusterCal.this.setCursor(Cursor.getDefaultCursor());
}
};

worker.execute();
}

Expand Down Expand Up @@ -784,6 +916,7 @@ public void windowClosing(java.awt.event.WindowEvent e) {
private javax.swing.JButton jButton_MeanTraj;
private javax.swing.JButton jButton_OutputFile;
private javax.swing.JButton jButton_SelAll;
private javax.swing.JButton jButton_viewTSV;
private javax.swing.JCheckBox jCheckBox_IgnoreDataLines;
private javax.swing.JComboBox jComboBox_ClusterNum;
private javax.swing.JComboBox jComboBox_Distance;
Expand Down
2 changes: 2 additions & 0 deletions src/trajstat/forms/FrmPSCF.java
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,8 @@ private void jButton_WeightPSCFActionPerformed(java.awt.event.ActionEvent evt) {
aLayer.editCellValue("WPSCF", i, wpscf);
}
aLayer.getAttributeTable().save();
aLayer.updateLegendIndexes();
this.app.getMapView().paintLayers();

//---- Hide progressbar
this.setCursor(Cursor.getDefaultCursor());
Expand Down
Loading

0 comments on commit 898d495

Please sign in to comment.