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

source: tmcsimulator/trunk/src/tmcsim/utilities/BuildHighwayFile.java @ 422

Revision 422, 12.5 KB checked in by jdalbey, 7 years ago (diff)

Remove ATMS functionality. Reworked and simplified the Highway model to use only VDS data from PeMS. Updated all unit tests.

RevLine 
1package tmcsim.utilities;
2
3import java.io.File;
4import java.io.FileNotFoundException;
5import java.io.PrintWriter;
6import java.util.ArrayList;
7import java.util.HashMap;
8import java.util.HashSet;
9import java.util.List;
10import java.util.Map;
11import java.util.Scanner;
12import java.util.Set;
13import java.util.logging.Level;
14import java.util.logging.Logger;
15
16/**
17 * OBSOLETE since deprecating ATMS functionality. No longer used.
18 * This utility program is used to create the highway map file used as part
19 * of the configuration files needed by the simulator.
20 * It merges info from three input files to create highway map file.
21 * Input: 1. VDS file is the district VDS metadata file downloaded from the PeMS
22 *      Data Clearinghouse.  It gives identifying info about each VDS.
23 *      2. Loop detector (lane) file that we found on the ATMS server.
24 *      It lists all the lanes associated with each VDS.
25 *      3. LDS file that has the fep line number for each LDS.
26 * Output: 1. The highway map file that lists VDS info and the lanes it governs,
27 *      in a format similar to what FEP simulator uses.
28 * @author jdalbey, jtorres
29 */
30public class BuildHighwayFile
31{
32    final public static String dirpath = "config/vds_data/old_vds_data/";
33    final public static String vdsFileName = "d12_vds_meta.txt";
34    final public static String loopFileName = "cleaned_chu_atms_loop_data.txt";
35    final public static String ldsFileName = "cleaned_chu_atms_lds_data.txt";
36    final public static String highwayFile = "highways_complete.txt";
37    /** A dictionary of ldsIDs to VDSids */
38    Map<String,StationAddr> ldsDict;
39    /** A dictionary of all VDSids linked to a given FEP line number */
40    Map<String,List<String>> feplines;
41    /** A dictionary to lookup VDS data given vdsID */
42    Map<String,VehicleDetectionStation> vdsDict;
43    /** A list of used VDS ids - FOR DEBUGGING*/
44    Set<String> vdsUsed = new HashSet<String>();
45    int ignored = 0; // count of ignored VDS ids
46    /** IDs Reported missing during lookup */
47    Set<String> missingVDS = new HashSet<String>();
48    Set<String> missingLDS = new HashSet<String>();
49   
50    /** Once all the input data has been read and the lookup dictionaries
51     * created we can build the highway file.
52     */
53    public void createHighwayFile()
54    {
55        try
56        {
57            PrintWriter hwyWriter = new PrintWriter(new File(dirpath+highwayFile));
58            hwyWriter.print(feplines.size()+"\n");
59            // for each fep line
60            for (String fep_line: feplines.keySet()) 
61            {
62                hwyWriter.print(fep_line+" 0 ");
63                List<String> vdsList = feplines.get(fep_line);
64                hwyWriter.print(vdsList.size()+"\n");
65                //    output the vds info
66                for (String vds: vdsList)
67                {
68                    VehicleDetectionStation vdsItem = vdsDict.get(vds);
69                    if (vdsItem == null)
70                    {
71                        System.out.println(vds + " not found in vdsDict");
72                    }
73                    else
74                    {
75                        // Output the VDS identifying info
76                        hwyWriter.print(vdsItem.id + " ");
77                        hwyWriter.print(vdsItem.toString() + " ");
78                        hwyWriter.print(vdsItem.getLaneList().size() + " ");
79                        hwyWriter.print(vdsItem.street+"\n");
80                        List<String> laneList = vdsItem.getLaneList();
81                        //    output all the lanes
82                        for (String lane: laneList)
83                        {
84                            hwyWriter.print(lane);
85                            hwyWriter.print("\n");
86                        }
87                    }
88                }
89            }
90            hwyWriter.close();
91        }catch (FileNotFoundException ex)
92        {
93            Logger.getLogger(BuildHighwayFile.class.getName()).log(Level.SEVERE, null, ex);
94        } catch (Exception ex)
95        {
96            ex.printStackTrace();
97            Logger.getLogger(BuildHighwayFile.class.getName()).log(Level.SEVERE, null, ex);
98        }
99
100    }
101   
102    /** Read the loop file to create a dictionary to lookup all the lanes
103     * for a given VDS.
104     */
105    public void createLanelookup()
106    {   
107        feplines = new HashMap<String,List<String>> ();
108           
109        try
110        {
111            Scanner loopScanner = new Scanner(new File(dirpath+loopFileName));
112           
113            loopScanner.nextLine();  // Skip the column headers
114
115            // Read all the lines in the loop file
116            while(loopScanner.hasNextLine())
117            {
118                String line = loopScanner.nextLine();
119                Scanner lineScanner = new Scanner(line);
120               
121                Integer fwy = lineScanner.nextInt();
122                String dir = lineScanner.next();
123                String postmile = lineScanner.next();
124                String ldsID = lineScanner.next();
125                String vdsID = lineScanner.next();
126                String loopID = lineScanner.next();
127                String shortLoc = lineScanner.next();
128                Integer laneNum = lineScanner.nextInt();
129                String loop_name = lineScanner.nextLine().trim();
130//              String loop_name = line.substring(73).trim(); // grab rest of line
131                loop_name = loop_name.replace(" ", "_");
132               
133                lineScanner.close();
134               
135                // Combine fields for one lane description
136                String laneDesc = loopID + " " + shortLoc + " " + loop_name;
137                // Add the lane to the lookup table
138                if (vdsDict.containsKey(vdsID))
139                {
140                    vdsDict.get(vdsID).addLane(laneDesc);
141                    vdsUsed.add(vdsID);
142                }
143                else
144                {
145                    // If we're missing a VDS id record it
146                    // but we're only interested in two lane types.
147                    boolean desiredType = shortLoc.equals("ML") || shortLoc.equals("OS");
148                    if (desiredType && !missingVDS.contains(vdsID))
149                    {
150                        System.out.println("createLaneLookup(): vdsID: "+vdsID+
151                                " of type "+shortLoc+" not found. "+ String.format("%3s %s %5s",fwy,dir,postmile));
152                        missingVDS.add(vdsID);
153                    }
154                }
155
156                // Also Add this vdsID to the list associated with a fepline
157                // lookup which fepline to use for this VDS (using ldsid as
158                //  intermediate key.
159                StationAddr sa = ldsDict.get(ldsID);
160                if (sa == null)
161                {
162                    if (!missingLDS.contains(ldsID))
163                    {
164                        System.out.println("missing ldsID in Station Addr lookup: "+ldsID+" "+ String.format("%3s %s %5s",fwy,dir,postmile));
165                        missingLDS.add(ldsID);
166                    }
167                }
168                else
169                {
170                    String fep_line = sa.line_num;
171                    // Fetch the info about this VDS
172                    VehicleDetectionStation curr = vdsDict.get(vdsID);
173                    // Some VDS were ignored because they weren't Mainline
174                    if (curr != null)
175                    {
176                        // Assign the station address for this VDS
177                        curr.setStaAddr(ldsDict.get(ldsID).station_address);
178
179                        if (fep_line == null)
180                        {
181                            System.out.println("No fepline for ldsID "+ldsID+ " :vdsid "
182                                    +vdsID+"  "+fwy+dir+postmile);
183                        }
184                        else
185                        {
186                            // Add vdsID to list of feplines
187                            if (feplines.containsKey(fep_line))
188                            {
189                                List<String> vdsList = feplines.get(fep_line);
190                                // only add it if it isn't already there
191                                if (!vdsList.contains(vdsID))
192                                {
193                                    feplines.get(fep_line).add(vdsID);
194                                }
195                            }
196                            else // Create a new one
197                            {
198                                List<String> arraylist1 = new ArrayList<String>();
199                                arraylist1.add(vdsID);
200                                feplines.put(fep_line, arraylist1);
201                            }
202                        }
203                    }
204                }
205            }
206           
207            loopScanner.close();
208           
209        } catch (FileNotFoundException ex)
210        {
211            Logger.getLogger(BuildHighwayFile.class.getName()).log(Level.SEVERE, null, ex);
212        } 
213    }
214    /** Create a dictionary of VehicleDetectionStations */
215    public void createVDSdict()
216    {   
217        vdsDict = new HashMap<String,VehicleDetectionStation>(); 
218        try
219        {
220            Scanner vdsScanner = new Scanner(new File(dirpath+vdsFileName));
221           
222            // Read the tab-delimited vds file
223            while(vdsScanner.hasNextLine())
224            {
225                String line = vdsScanner.nextLine();
226                Scanner lineScanner = new Scanner(line).useDelimiter("\t");
227                VehicleDetectionStation vds = new VehicleDetectionStation(lineScanner);
228                // We only want stations that are mainline, ignore offramps, etc.
229                if (vds.type.equals("ML"))
230                {
231                    vdsDict.put(vds.id, vds);
232                }
233                else
234                {
235                    ignored++;
236                }
237                lineScanner.close();
238            }
239            vdsScanner.close();
240            System.out.println("Ignored "+ignored+ " non-Mainline VDS's");
241           
242        } catch (FileNotFoundException ex)
243        {
244            Logger.getLogger(BuildHighwayFile.class.getName()).log(Level.SEVERE, null, ex);
245        } 
246    }
247    /** Read the lds file to create a dictionary to lookup the FEP line number.
248     */
249    public void createLDSdict()
250    {   
251        ldsDict = new HashMap<String,StationAddr> ();
252           
253        try
254        {
255            Scanner ldsScanner = new Scanner(new File(dirpath+ldsFileName));
256           
257            ldsScanner.nextLine();  // Skip the column headers
258
259            // Read all the lines in the loop file
260            while(ldsScanner.hasNextLine())
261            {
262                String line = ldsScanner.nextLine();
263                Scanner lineScanner = new Scanner(line);
264               
265                String ldsID = lineScanner.next();
266                String line_num = lineScanner.next();
267                String stn_address = lineScanner.next();
268                // Save the fep linenum and station address for this LDS
269                StationAddr sa = new StationAddr(line_num,stn_address);
270                lineScanner.close();
271                // Add the ldsID to the dict
272                ldsDict.put(ldsID, sa);
273            }
274            ldsScanner.close();
275        } catch (FileNotFoundException ex)
276        {
277            Logger.getLogger(BuildHighwayFile.class.getName()).log(Level.SEVERE, null, ex);
278        } 
279    }
280
281    /** Display the set of VDS in the dict that didn't get used (weren't found
282     * in loop file.
283     */
284    public void showLeftoverVDS()
285    {
286        Set<String> vdsItems = vdsDict.keySet(); // get all the VDS ids
287        vdsItems.removeAll(vdsUsed); // remove the ones that were used
288        System.out.println("Here are the unused VDS ids:");
289        // list the remaining items
290        for (String vdsitem: vdsItems)
291        {
292            System.out.print(vdsitem+"  ");
293        }
294        System.out.println("");           
295    }
296    /**
297     * @param args the command line arguments
298     */
299    public static void main(String[] args)
300    {
301        BuildHighwayFile app = new BuildHighwayFile();
302        app.createLDSdict();
303        app.createVDSdict();
304        app.createLanelookup();
305        app.createHighwayFile();
306        System.out.println("Build complete, output file: "+highwayFile);
307    }
308
309    /** A record for a fep line_num  and Station Address pair */
310    final class StationAddr
311    {
312        public final String line_num;
313        public final String station_address;
314        public StationAddr(String line, String addr)
315        {
316            this.line_num = line;
317            this.station_address = addr;
318        }
319    }
320}
321
Note: See TracBrowser for help on using the repository browser.