| 1 | package tmcsim.highwaymodel; |
|---|
| 2 | |
|---|
| 3 | import tmcsim.highwaymodel.Highways; |
|---|
| 4 | import tmcsim.highwaymodel.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 | final String CONFIG_FILE_NAME = "traffic_model_config.properties"; |
|---|
| 71 | String propertiesFile = "config" + System.getProperty("file.separator") |
|---|
| 72 | + CONFIG_FILE_NAME; |
|---|
| 73 | Properties props = TrafficModelManager.loadProperties(propertiesFile); |
|---|
| 74 | events_file = props.getProperty("Events_File"); |
|---|
| 75 | FileInputStream fis = null; |
|---|
| 76 | try |
|---|
| 77 | { |
|---|
| 78 | fis = new FileInputStream(events_file); |
|---|
| 79 | } catch (FileNotFoundException ex) |
|---|
| 80 | { |
|---|
| 81 | Logger.getLogger(TrafficModelManager.class.getName()).log(Level.SEVERE, null, |
|---|
| 82 | "Missing Traffic Events file " + events_file); |
|---|
| 83 | System.exit(-1); |
|---|
| 84 | } |
|---|
| 85 | Scanner fileScanner = new Scanner(fis); |
|---|
| 86 | // Read all lines from the file of events and put in a queue |
|---|
| 87 | eventQueue = TrafficModelManager.readBatchFile(fileScanner); |
|---|
| 88 | } |
|---|
| 89 | /** Load the traffic events into a history buffer */ |
|---|
| 90 | public void load() |
|---|
| 91 | { |
|---|
| 92 | txtDisplay.setText("Loading traffic events from: " + events_file); |
|---|
| 93 | history = new ArrayList<String>(); |
|---|
| 94 | // If we have any events left to process |
|---|
| 95 | while (!eventQueue.isEmpty()) |
|---|
| 96 | { |
|---|
| 97 | // Get next event |
|---|
| 98 | TrafficEvent nextEvent = eventQueue.peek(); |
|---|
| 99 | System.out.println(nextEvent.eventTime); |
|---|
| 100 | // apply colorization to highways |
|---|
| 101 | highways.applyColorToHighwayStretch(nextEvent.routeNumber, nextEvent.dir, |
|---|
| 102 | nextEvent.postmile, nextEvent.range, nextEvent.color); |
|---|
| 103 | //history.add(highways.toString()+"\n"+nextEvent.eventTime); |
|---|
| 104 | |
|---|
| 105 | // Output the highway model |
|---|
| 106 | String geojson = highways.toJson(); |
|---|
| 107 | //System.out.println(geojson); // diagnostic |
|---|
| 108 | PrintWriter out; |
|---|
| 109 | try |
|---|
| 110 | { |
|---|
| 111 | out = new PrintWriter("highways.json"); |
|---|
| 112 | out.print(geojson); |
|---|
| 113 | out.close(); |
|---|
| 114 | } |
|---|
| 115 | catch (FileNotFoundException ex) |
|---|
| 116 | { |
|---|
| 117 | Logger.getLogger(GoogleMapAnimator.class.getName()).log(Level.SEVERE, null, ex); |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | // Sleep 10 sec for the javascript page to load the file |
|---|
| 121 | try |
|---|
| 122 | { |
|---|
| 123 | Thread.sleep(10000); // pause 10 seconds |
|---|
| 124 | } |
|---|
| 125 | catch (InterruptedException ex) |
|---|
| 126 | { |
|---|
| 127 | Logger.getLogger(GoogleMapAnimator.class.getName()).log(Level.SEVERE, null, ex); |
|---|
| 128 | } |
|---|
| 129 | // Remove this event from the queue, we're done with it. |
|---|
| 130 | eventQueue.remove(); |
|---|
| 131 | } |
|---|
| 132 | scrollBar.setMaximum(history.size()); |
|---|
| 133 | // display the first item from the history |
|---|
| 134 | txtDisplay.setText(history.get(0)); |
|---|
| 135 | } |
|---|
| 136 | /** |
|---|
| 137 | * This method is called from within the constructor to initialize the form. |
|---|
| 138 | * WARNING: Do NOT modify this code. The content of this method is always |
|---|
| 139 | * regenerated by the Form Editor. |
|---|
| 140 | */ |
|---|
| 141 | @SuppressWarnings("unchecked") |
|---|
| 142 | // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents |
|---|
| 143 | private void initComponents() { |
|---|
| 144 | |
|---|
| 145 | scrollBar = new javax.swing.JScrollBar(); |
|---|
| 146 | jScrollPane1 = new javax.swing.JScrollPane(); |
|---|
| 147 | txtDisplay = new javax.swing.JTextArea(); |
|---|
| 148 | |
|---|
| 149 | setPreferredSize(new java.awt.Dimension(151, 92)); |
|---|
| 150 | |
|---|
| 151 | scrollBar.addAdjustmentListener(new java.awt.event.AdjustmentListener() { |
|---|
| 152 | public void adjustmentValueChanged(java.awt.event.AdjustmentEvent evt) { |
|---|
| 153 | scrollbarValueChanged(evt); |
|---|
| 154 | } |
|---|
| 155 | }); |
|---|
| 156 | |
|---|
| 157 | txtDisplay.setColumns(20); |
|---|
| 158 | txtDisplay.setFont(new java.awt.Font("Courier New", 0, 15)); // NOI18N |
|---|
| 159 | txtDisplay.setRows(5); |
|---|
| 160 | jScrollPane1.setViewportView(txtDisplay); |
|---|
| 161 | |
|---|
| 162 | javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); |
|---|
| 163 | this.setLayout(layout); |
|---|
| 164 | layout.setHorizontalGroup( |
|---|
| 165 | layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) |
|---|
| 166 | .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() |
|---|
| 167 | .addComponent(scrollBar, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE) |
|---|
| 168 | .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) |
|---|
| 169 | .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 421, Short.MAX_VALUE)) |
|---|
| 170 | ); |
|---|
| 171 | layout.setVerticalGroup( |
|---|
| 172 | layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) |
|---|
| 173 | .addComponent(scrollBar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) |
|---|
| 174 | .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) |
|---|
| 175 | ); |
|---|
| 176 | }// </editor-fold>//GEN-END:initComponents |
|---|
| 177 | |
|---|
| 178 | private void scrollbarValueChanged(java.awt.event.AdjustmentEvent evt)//GEN-FIRST:event_scrollbarValueChanged |
|---|
| 179 | {//GEN-HEADEREND:event_scrollbarValueChanged |
|---|
| 180 | // when the scroll bar is moved show the corresponding snapshot from history |
|---|
| 181 | txtDisplay.setText(history.get(evt.getValue()) ); |
|---|
| 182 | repaint(); |
|---|
| 183 | }//GEN-LAST:event_scrollbarValueChanged |
|---|
| 184 | |
|---|
| 185 | /** Entry point for the application. Builds a surrounding JFrame and |
|---|
| 186 | * runs the application. |
|---|
| 187 | * @param args |
|---|
| 188 | * @throws RemoteException |
|---|
| 189 | * @throws SimulationException |
|---|
| 190 | */ |
|---|
| 191 | public static void main(String[] args) throws RemoteException, SimulationException |
|---|
| 192 | { |
|---|
| 193 | JFrame frame = new JFrame("Google Map Animator"); |
|---|
| 194 | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
|---|
| 195 | frame.setSize(1050,450); |
|---|
| 196 | GoogleMapAnimator display = new GoogleMapAnimator(); |
|---|
| 197 | frame.add(display); |
|---|
| 198 | frame.setVisible(true); |
|---|
| 199 | display.load(); |
|---|
| 200 | } |
|---|
| 201 | |
|---|
| 202 | // Variables declaration - do not modify//GEN-BEGIN:variables |
|---|
| 203 | private javax.swing.JScrollPane jScrollPane1; |
|---|
| 204 | private javax.swing.JScrollBar scrollBar; |
|---|
| 205 | private javax.swing.JTextArea txtDisplay; |
|---|
| 206 | // End of variables declaration//GEN-END:variables |
|---|
| 207 | } |
|---|