| 1 | |
|---|
| 2 | package atmsdriver.model; |
|---|
| 3 | |
|---|
| 4 | import java.util.ArrayList; |
|---|
| 5 | import java.util.Iterator; |
|---|
| 6 | import java.util.List; |
|---|
| 7 | import java.util.Scanner; |
|---|
| 8 | |
|---|
| 9 | /** |
|---|
| 10 | * PostmileCoords is a collection of Postmiles (id and LatLong Coordinates). |
|---|
| 11 | * @author jdalbey |
|---|
| 12 | */ |
|---|
| 13 | public 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 | // remove .0 for comparing strings |
|---|
| 122 | if (target.endsWith(".0")) |
|---|
| 123 | { |
|---|
| 124 | String truncTarget = target.substring(0,target.length()-2); |
|---|
| 125 | return this.name.equals(truncTarget); |
|---|
| 126 | } |
|---|
| 127 | return this.name.equals(target); |
|---|
| 128 | } |
|---|
| 129 | @Override public boolean equals(Object aThat) |
|---|
| 130 | { |
|---|
| 131 | if (this == aThat) return true; |
|---|
| 132 | if (!(aThat instanceof Postmile)) return false; |
|---|
| 133 | Postmile that = (Postmile) aThat; |
|---|
| 134 | boolean r1 = this.name.equals(that.name); |
|---|
| 135 | boolean r2 = this.latitude.equals(that.latitude); |
|---|
| 136 | boolean r3 = this.longitude.equals(that.longitude); |
|---|
| 137 | boolean r4 = this.street.equals(that.street); |
|---|
| 138 | return r1 && r2 && r3 && r4; |
|---|
| 139 | } |
|---|
| 140 | @Override public String toString() |
|---|
| 141 | { |
|---|
| 142 | return name + ": " + latitude +","+ longitude +","+ street+","+ perpx +","+ perpy; |
|---|
| 143 | } |
|---|
| 144 | public String toJson() |
|---|
| 145 | { |
|---|
| 146 | String pattern = "\n{\n \"type\": \"Feature\",\n" + |
|---|
| 147 | " \"id\": \"%s\",\n" + |
|---|
| 148 | " \"geometry\":\n {\n \"type\": \"Point\",\n" + |
|---|
| 149 | " \"coordinates\": [%s,%s]\n" + |
|---|
| 150 | " },\n" + |
|---|
| 151 | " \"properties\": \n" + |
|---|
| 152 | " {\"street\": \"%s\", " + |
|---|
| 153 | "\"color\": \"desiredcolor\", " + |
|---|
| 154 | "\"perpx\": \"%s\", " + |
|---|
| 155 | "\"perpy\": \"%s\"" + |
|---|
| 156 | "}\n},"; |
|---|
| 157 | |
|---|
| 158 | return String.format(pattern, name, longitude, latitude, street, perpx, perpy); |
|---|
| 159 | } |
|---|
| 160 | } |
|---|
| 161 | } |
|---|