source: tmcsimulator/trunk/src/atmsdriver/model/PostmileCoords.java @ 269

Revision 269, 5.2 KB checked in by jdalbey, 7 years ago (diff)

PostmileCoords?.java: Add new fields for perpendicular vector (x and y components). This enables adjusting the dot position along line perpendicular to the highway when the map is zoomed.

Line 
1
2package atmsdriver.model;
3
4import java.util.ArrayList;
5import java.util.Iterator;
6import java.util.List;
7import java.util.Scanner;
8
9/**
10 * PostmileCoords is a collection of Postmiles (id and LatLong Coordinates).
11 * @author jdalbey
12 */
13public class PostmileCoords implements Iterable
14{
15    private List<Postmile> postmileList;
16    public PostmileCoords()
17    {
18        postmileList = new ArrayList<Postmile>();
19    }
20    /** Accessor to value at given index */
21    public Postmile get(int idx)
22    {
23        return postmileList.get(idx);
24    }
25    /** Return the size */
26    public int size()
27    {
28        return postmileList.size();
29    }
30    /** Find the postmile with the given name */
31    public Postmile find(String target)
32    {
33        int idx = 0;
34        while (idx < postmileList.size() && !postmileList.get(idx).nameEquals(target))
35        {
36            idx++;
37        }
38        if (idx < postmileList.size())
39        {
40            return postmileList.get(idx);
41        }
42        else
43        {
44            return null;
45        }
46    }
47    /** Load the postmile list from external file */
48    public void load(Scanner scan)
49    {
50        try
51        {
52        String out = scan.next();
53        //  split into an array
54        String[] results = out.split("\\n");
55        for (String item : results)
56        {
57            String[] fields = item.split(",");
58            String[] nameparts = fields[0].split(" ");
59            String statepm = nameparts[2];
60            // some postmiles come with a prefix, which we ignore
61            if (Character.isLetter(statepm.charAt(0)))
62            {
63                statepm = statepm.substring(1);
64            } 
65            String revisedpm = nameparts[0] + " " + nameparts[1] + " " + statepm;
66            Postmile pm;
67            // If the file has 6 fields per line, include the perpx,y values
68            if (fields.length == 6)
69            {
70                pm = new Postmile(revisedpm,fields[1],fields[2],fields[3],fields[4],fields[5]);
71            }
72            else // otherwise assume the file has just 4 fields
73            {
74                pm = new Postmile(revisedpm,fields[1],fields[2],fields[3]);
75            }
76            postmileList.add(pm);
77        }
78        }
79        catch (Exception ex)  // probably badly formatted file
80        {
81            ex.printStackTrace();
82        }
83    }
84
85    @Override
86    public Iterator iterator()
87    {
88        return postmileList.iterator();
89    }
90    /**
91     *  Postmile is a postmile id and its LatLong Coordinates.
92     */
93    final static class Postmile 
94    {
95        String name;  // the postmile name (Route,Direction,State postmile, e.g., 5 N 6.2)
96        String latitude; // the latitude coordinate for this postmile
97        String longitude; // the longitude coordinate for this postmile
98        String street; // cross street name
99        /* These fields are used by the map to adjust position of dot when zoomed */
100        String perpx="0";  // perpendicular vector, x-component (default value)
101        String perpy="0";  // perpendicular vector, y-component (default value)
102        public Postmile(String name, String lat, String longitude, String street)
103        {
104            this.name = name.trim();
105            this.latitude = lat.trim();
106            this.longitude = longitude.trim();
107            this.street = street.trim();
108        }
109        // This constructor is used if the file contains data for perpendicular vectors
110        public Postmile(String name, String lat, String longitude, String street, String perpx, String perpy)
111        {
112            this.name = name.trim();
113            this.latitude = lat.trim();
114            this.longitude = longitude.trim();
115            this.street = street.trim();
116            this.perpx = perpx.trim();
117            this.perpy = perpy.trim();
118        }
119        public boolean nameEquals(String target)
120        {
121            return this.name.equals(target);
122        }
123        @Override public boolean equals(Object aThat) 
124        {
125            if (this == aThat) return true;
126            if (!(aThat instanceof Postmile)) return false;
127            Postmile that = (Postmile) aThat;
128            boolean r1 = this.name.equals(that.name);
129            boolean r2 = this.latitude.equals(that.latitude);
130            boolean r3 = this.longitude.equals(that.longitude);
131            boolean r4 = this.street.equals(that.street);
132            return r1 && r2 && r3 && r4;
133        }
134        @Override public String toString()
135        {
136            return name + ": " + latitude +","+ longitude +","+ street+","+ perpx +","+ perpy;
137        }
138        public String toJson()
139        {
140            String pattern =  "\n{\n   \"type\": \"Feature\",\n" +
141                    "   \"id\": \"%s\",\n" +
142                    "   \"geometry\":\n       {\n        \"type\": \"Point\",\n" +
143                    "        \"coordinates\": [%s,%s]\n" +
144                    "       },\n" +
145                    "   \"properties\": \n" +         
146                    "       {\"street\": \"%s\", " +
147                    "\"color\": \"desiredcolor\", " +
148                    "\"perpx\": \"%s\", " +
149                    "\"perpy\": \"%s\"" +
150                    "}\n},";
151
152            return String.format(pattern, name, longitude, latitude, street, perpx, perpy);
153        }
154    }
155}
Note: See TracBrowser for help on using the repository browser.