| 1 | package atmsdriver; |
|---|
| 2 | |
|---|
| 3 | import atmsdriver.model.Highways; |
|---|
| 4 | import atmsdriver.model.TrafficEvent; |
|---|
| 5 | import java.io.FileInputStream; |
|---|
| 6 | import java.io.FileNotFoundException; |
|---|
| 7 | import java.io.PrintWriter; |
|---|
| 8 | import java.rmi.RemoteException; |
|---|
| 9 | import java.util.ArrayList; |
|---|
| 10 | import java.util.HashMap; |
|---|
| 11 | import java.util.LinkedList; |
|---|
| 12 | import java.util.List; |
|---|
| 13 | import java.util.Map; |
|---|
| 14 | import java.util.Properties; |
|---|
| 15 | import java.util.Scanner; |
|---|
| 16 | import java.util.logging.Level; |
|---|
| 17 | import java.util.logging.Logger; |
|---|
| 18 | import javax.swing.JFrame; |
|---|
| 19 | import tmcsim.cadsimulator.managers.TrafficModelManager; |
|---|
| 20 | import tmcsim.common.SimulationException; |
|---|
| 21 | |
|---|
| 22 | /** |
|---|
| 23 | * Read and process all Traffic Events in a file and output |
|---|
| 24 | * a snapshot of the highway model after each event to a json file. |
|---|
| 25 | * This file can be read by Google maps. |
|---|
| 26 | * This application is used by Traffic Event authors to assist |
|---|
| 27 | * in verifying the correctness of their data file. |
|---|
| 28 | * @author jdalbey |
|---|
| 29 | * @version 3/20/2019 Incomplete, low priority. |
|---|
| 30 | */ |
|---|
| 31 | public class GoogleMapAnimator extends javax.swing.JPanel |
|---|
| 32 | { |
|---|
| 33 | /** |
|---|
| 34 | * Error logger. |
|---|
| 35 | */ |
|---|
| 36 | private final static Logger logger = Logger.getLogger("mapanimator"); |
|---|
| 37 | |
|---|
| 38 | /** |
|---|
| 39 | * Highways in traffic network |
|---|
| 40 | */ |
|---|
| 41 | final private Highways highways; |
|---|
| 42 | |
|---|
| 43 | /** |
|---|
| 44 | * LinkedList of batch events |
|---|
| 45 | */ |
|---|
| 46 | private final LinkedList<TrafficEvent> eventQueue; |
|---|
| 47 | /** |
|---|
| 48 | * Map of incidents to events |
|---|
| 49 | */ |
|---|
| 50 | private Map<String, List<TrafficEvent>> incidents; |
|---|
| 51 | /** |
|---|
| 52 | * Highway history |
|---|
| 53 | */ |
|---|
| 54 | private List<String> history; |
|---|
| 55 | /** |
|---|
| 56 | * name of events file obtained from properties |
|---|
| 57 | */ |
|---|
| 58 | private String events_file = ""; |
|---|
| 59 | /** |
|---|
| 60 | * Creates the JPanel and builds the highways and events. |
|---|
| 61 | */ |
|---|
| 62 | public GoogleMapAnimator() throws RemoteException, SimulationException |
|---|
| 63 | { |
|---|
| 64 | initComponents(); |
|---|
| 65 | |
|---|
| 66 | // Initialize the highway model |
|---|
| 67 | incidents = new HashMap<String, List<TrafficEvent>>(); |
|---|
| 68 | highways = new Highways( |
|---|
| 69 | "config/vds_data/highways_fullmap.txt", |
|---|
| 70 | // following aren't used by this application |
|---|
| 71 | "localhost", 8080); |
|---|
| 72 | final String CONFIG_FILE_NAME = "traffic_model_config.properties"; |
|---|
| 73 | String propertiesFile = "config" + System.getProperty("file.separator") |
|---|
| 74 | + CONFIG_FILE_NAME; |
|---|
| 75 | Properties props = TrafficModelManager.loadProperties(propertiesFile); |
|---|
| 76 | events_file = props.getProperty("Events_File"); |
|---|
| 77 | FileInputStream fis = null; |
|---|
| 78 | try |
|---|
| 79 | { |
|---|
| 80 | fis = new FileInputStream(events_file); |
|---|
| 81 | } catch (FileNotFoundException ex) |
|---|
| 82 | { |
|---|
| 83 | Logger.getLogger(TrafficModelManager.class.getName()).log(Level.SEVERE, null, |
|---|
| 84 | "Missing Traffic Events file " + events_file); |
|---|
| 85 | System.exit(-1); |
|---|
| 86 | } |
|---|
| 87 | Scanner fileScanner = new Scanner(fis); |
|---|
| 88 | // Read all lines from the file of events and put in a queue |
|---|
| 89 | eventQueue = TrafficModelManager.readBatchFile(fileScanner); |
|---|
| 90 | } |
|---|
| 91 | /** Load the traffic events into a history buffer */ |
|---|
| 92 | public void load() |
|---|
| 93 | { |
|---|
| 94 | txtDisplay.setText("Loading traffic events from: " + events_file); |
|---|
| 95 | history = new ArrayList<String>(); |
|---|
| 96 | // If we have any events left to process |
|---|
| 97 | while (!eventQueue.isEmpty()) |
|---|
| 98 | { |
|---|
| 99 | // Get next event |
|---|
| 100 | TrafficEvent nextEvent = eventQueue.peek(); |
|---|
| 101 | System.out.println(nextEvent.eventTime); |
|---|
| 102 | // apply colorization to highways |
|---|
| 103 | highways.applyColorToHighwayStretch(nextEvent.routeNumber, nextEvent.dir, |
|---|
| 104 | nextEvent.postmile, nextEvent.range, nextEvent.color); |
|---|
| 105 | //history.add(highways.toString()+"\n"+nextEvent.eventTime); |
|---|
| 106 | |
|---|
| 107 | // Output the highway model |
|---|
| 108 | String geojson = highways.toJson(); |
|---|
| 109 | //System.out.println(geojson); // diagnostic |
|---|
| 110 | PrintWriter out; |
|---|
| 111 | try |
|---|
| 112 | { |
|---|
| 113 | out = new PrintWriter("highways.json"); |
|---|
| 114 | out.print(geojson); |
|---|
| 115 | out.close(); |
|---|
| 116 | } |
|---|
| 117 | catch (FileNotFoundException ex) |
|---|
| 118 | { |
|---|
| 119 | Logger.getLogger(GoogleMapAnimator.class.getName()).log(Level.SEVERE, null, ex); |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | // Sleep 10 sec for the javascript page to load the file |
|---|
| 123 | try |
|---|
| 124 | { |
|---|
| 125 | Thread.sleep(10000); // pause 10 seconds |
|---|
| 126 | } |
|---|
| 127 | catch (InterruptedException ex) |
|---|
| 128 | { |
|---|
| 129 | Logger.getLogger(GoogleMapAnimator.class.getName()).log(Level.SEVERE, null, ex); |
|---|
| 130 | } |
|---|
| 131 | // Remove this event from the queue, we're done with it. |
|---|
| 132 | eventQueue.remove(); |
|---|
| 133 | } |
|---|
| 134 | scrollBar.setMaximum(history.size()); |
|---|
| 135 | // display the first item from the history |
|---|
| 136 | txtDisplay.setText(history.get(0)); |
|---|
| 137 | } |
|---|
| 138 | /** |
|---|
| 139 | * This method is called from within the constructor to initialize the form. |
|---|
| 140 | * WARNING: Do NOT modify this code. The content of this method is always |
|---|
| 141 | * regenerated by the Form Editor. |
|---|
| 142 | */ |
|---|
| 143 | @SuppressWarnings("unchecked") |
|---|
| 144 | // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents |
|---|
| 145 | private void initComponents() |
|---|
| 146 | { |
|---|
| 147 | |
|---|
| 148 | scrollBar = new javax.swing.JScrollBar(); |
|---|
| 149 | jScrollPane1 = new javax.swing.JScrollPane(); |
|---|
| 150 | txtDisplay = new javax.swing.JTextArea(); |
|---|
| 151 | |
|---|
| 152 | setPreferredSize(new java.awt.Dimension(151, 92)); |
|---|
| 153 | |
|---|
| 154 | scrollBar.addAdjustmentListener(new java.awt.event.AdjustmentListener() |
|---|
| 155 | { |
|---|
| 156 | public void adjustmentValueChanged(java.awt.event.AdjustmentEvent evt) |
|---|
| 157 | { |
|---|
| 158 | scrollbarValueChanged(evt); |
|---|
| 159 | } |
|---|
| 160 | }); |
|---|
| 161 | |
|---|
| 162 | txtDisplay.setColumns(20); |
|---|
| 163 | txtDisplay.setFont(new java.awt.Font("Courier New", 0, 15)); // NOI18N |
|---|
| 164 | txtDisplay.setRows(5); |
|---|
| 165 | jScrollPane1.setViewportView(txtDisplay); |
|---|
| 166 | |
|---|
| 167 | javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); |
|---|
| 168 | this.setLayout(layout); |
|---|
| 169 | layout.setHorizontalGroup( |
|---|
| 170 | layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) |
|---|
| 171 | .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() |
|---|
| 172 | .addComponent(scrollBar, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE) |
|---|
| 173 | .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) |
|---|
| 174 | .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 421, Short.MAX_VALUE)) |
|---|
| 175 | ); |
|---|
| 176 | layout.setVerticalGroup( |
|---|
| 177 | layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) |
|---|
| 178 | .addComponent(scrollBar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) |
|---|
| 179 | .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) |
|---|
| 180 | ); |
|---|
| 181 | }// </editor-fold>//GEN-END:initComponents |
|---|
| 182 | |
|---|
| 183 | private void scrollbarValueChanged(java.awt.event.AdjustmentEvent evt)//GEN-FIRST:event_scrollbarValueChanged |
|---|
| 184 | {//GEN-HEADEREND:event_scrollbarValueChanged |
|---|
| 185 | // when the scroll bar is moved show the corresponding snapshot from history |
|---|
| 186 | txtDisplay.setText(history.get(evt.getValue()) ); |
|---|
| 187 | repaint(); |
|---|
| 188 | }//GEN-LAST:event_scrollbarValueChanged |
|---|
| 189 | |
|---|
| 190 | /** Entry point for the application. Builds a surrounding JFrame and |
|---|
| 191 | * runs the application. |
|---|
| 192 | * @param args |
|---|
| 193 | * @throws RemoteException |
|---|
| 194 | * @throws SimulationException |
|---|
| 195 | */ |
|---|
| 196 | public static void main(String[] args) throws RemoteException, SimulationException |
|---|
| 197 | { |
|---|
| 198 | JFrame frame = new JFrame("Google Map Animator"); |
|---|
| 199 | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
|---|
| 200 | frame.setSize(1050,450); |
|---|
| 201 | GoogleMapAnimator display = new GoogleMapAnimator(); |
|---|
| 202 | frame.add(display); |
|---|
| 203 | frame.setVisible(true); |
|---|
| 204 | display.load(); |
|---|
| 205 | } |
|---|
| 206 | |
|---|
| 207 | // Variables declaration - do not modify//GEN-BEGIN:variables |
|---|
| 208 | private javax.swing.JScrollPane jScrollPane1; |
|---|
| 209 | private javax.swing.JScrollBar scrollBar; |
|---|
| 210 | private javax.swing.JTextArea txtDisplay; |
|---|
| 211 | // End of variables declaration//GEN-END:variables |
|---|
| 212 | } |
|---|