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

Revision 272, 12.4 KB checked in by jdalbey, 7 years ago (diff)

new utility added: tmcsim/utilities/BuildHighwayFile.java for creating the highways map file from original lds,loop,and vds data files.

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