Changeset 243 in tmcsimulator


Ignore:
Timestamp:
02/07/2019 03:10:15 PM (7 years ago)
Author:
jdalbey
Message:

Highways.java: Added toJson() method.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/atmsdriver/model/Highways.java

    r238 r243  
    55import atmsdriver.model.Station.DIRECTION; 
    66import java.io.File; 
     7import java.io.FileInputStream; 
    78import java.io.FileNotFoundException; 
    89import java.io.IOException; 
     
    522523        return result.toString(); 
    523524    } 
     525    /** Return a json representation of the Highways, readable by Google Maps */ 
     526    public String toJson() 
     527    { 
     528        PostmileCoords pmList = new PostmileCoords(); 
     529        FileInputStream fis = null; 
     530        try 
     531        { 
     532            fis = new FileInputStream("config/vds_data/postmile_coordinates.txt"); 
     533        } 
     534        catch (FileNotFoundException ex) 
     535        { 
     536            Logger.getLogger(Highways.class.getName()).log(Level.SEVERE, null, ex); 
     537        } 
     538        Scanner s = new Scanner(fis).useDelimiter("\\A"); 
     539        pmList.load(s); 
     540        String header = "{\n" + 
     541        "  \"type\": \"FeatureCollection\",\n" + 
     542        "  \"features\": ["; 
     543        StringBuilder result = new StringBuilder(); 
     544        result.append(header); 
     545        for (Highway hwy: highways) 
     546        { 
     547            // Consider each route direction 
     548            //for (DIRECTION dir: DIRECTION.values()) 
     549            // TODO: Needs fixing so proper direction is displayed 
     550            Station.DIRECTION dir = Station.DIRECTION.SOUTH; 
     551            if (hwy.routeNumber == 55) 
     552                dir = Station.DIRECTION.SOUTH; 
     553            if (hwy.routeNumber == 405) 
     554                dir = Station.DIRECTION.NORTH; 
     555            { 
     556                String rowLabel = ""+String.format("%3s ",hwy.routeNumber)+dir.getLetter()+' '; 
     557                StringBuilder lineout = new StringBuilder(); 
     558                // Examine every station on this highway and direction 
     559                for (Station stat: hwy.stations) 
     560                { 
     561                    String pmID = "" + hwy.routeNumber + " " + stat.postmile; 
     562                    PostmileCoords.Postmile currentPM = pmList.find(pmID); 
     563                    if (currentPM != null) 
     564                    {     
     565                    //lineout.append("" + dir.getLetter() + stat.postmile); 
     566                    //lineout.append(stat.getColorByDirection(dir)); 
     567                    String outString = currentPM.toJson(); 
     568                    String colorName=""; 
     569                    switch (stat.getColorByDirection(dir)) 
     570                    { 
     571                        case '@': colorName = "red";break; 
     572                        case '+': colorName = "yellow"; break; 
     573                        case '-': colorName = "green";break; 
     574                        case ' ': colorName = "green";break; 
     575                    } 
     576                    outString = outString.replace("green",colorName); 
     577                    lineout.append(outString); 
     578                    lineout.append("  "); 
     579                    } 
     580                } 
     581                                // See if there were stations for this direction 
     582                String checkMe = lineout.toString().trim(); 
     583                // if any stations were colored, output the line 
     584                if (checkMe.length() > 1) 
     585                { 
     586                    //result.append(rowLabel); 
     587                    result.append(lineout + "\n"); 
     588                } 
     589 
     590            } 
     591        } 
     592        // remove last trailing comma 
     593        result.replace(result.lastIndexOf(","), result.lastIndexOf(",") + 1, " "  ); 
     594 
     595        result.append("  ]\n" +  "}"); 
     596        return result.toString(); 
     597    } 
    524598     
    525599    /** 
  • trunk/test/atmsdriver/model/HighwaysTest.java

    r228 r243  
    9494"  5 S @--"; 
    9595 
     96    public void testToJson() { 
     97        System.out.println("toJson"); 
     98        Highways highways = new Highways( 
     99                "config/vds_data/highways_fullmap.txt", 
     100                "localhost", 8080); 
     101        highways.getHighwayByRouteNumber(5).stations.get(0).loops.get(0).vol = 1; 
     102        String result = highways.toJson(); 
     103        System.out.println(result); 
     104        assertTrue(result.startsWith(expToJson1)); 
     105    } 
     106    String expToJson1 = "";  
    96107    
    97108 
Note: See TracChangeset for help on using the changeset viewer.