Warning: Can't use blame annotator:
svn blame failed on trunk/src/atmsdriver/GoogleMapAnimator.java: ("Can't find a temporary directory: Internal error", 20014)

source: tmcsimulator/trunk/src/atmsdriver/GoogleMapAnimator.java @ 248

Revision 248, 8.0 KB checked in by jdalbey, 7 years ago (diff)

TrafficModelManager?: multi-file commit to enhance Traffic Mgr to output highway status to json file for viewing in Google Map.

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