| 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 = new Postmile(revisedpm,fields[1],fields[2],fields[3]); |
|---|
| 67 | postmileList.add(pm); |
|---|
| 68 | } |
|---|
| 69 | } |
|---|
| 70 | catch (Exception ex) |
|---|
| 71 | { |
|---|
| 72 | ex.printStackTrace(); |
|---|
| 73 | } |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | @Override |
|---|
| 77 | public Iterator iterator() |
|---|
| 78 | { |
|---|
| 79 | return postmileList.iterator(); |
|---|
| 80 | } |
|---|
| 81 | /** |
|---|
| 82 | * Postmile is a postmile id and its LatLong Coordinates. |
|---|
| 83 | */ |
|---|
| 84 | final static class Postmile |
|---|
| 85 | { |
|---|
| 86 | String name; // the postmile name (Route,Direction,State postmile, e.g., 5 N 6.2) |
|---|
| 87 | String latitude; // the latitude coordinate for this postmile |
|---|
| 88 | String longitude; // the longitude coordinate for this postmile |
|---|
| 89 | String street; // cross street name |
|---|
| 90 | public Postmile(String name, String lat, String longitude, String street) |
|---|
| 91 | { |
|---|
| 92 | this.name = name.trim(); |
|---|
| 93 | this.latitude = lat.trim(); |
|---|
| 94 | this.longitude = longitude.trim(); |
|---|
| 95 | this.street = street.trim(); |
|---|
| 96 | } |
|---|
| 97 | public boolean nameEquals(String target) |
|---|
| 98 | { |
|---|
| 99 | return this.name.equals(target); |
|---|
| 100 | } |
|---|
| 101 | @Override public boolean equals(Object aThat) |
|---|
| 102 | { |
|---|
| 103 | if (this == aThat) return true; |
|---|
| 104 | if (!(aThat instanceof Postmile)) return false; |
|---|
| 105 | Postmile that = (Postmile) aThat; |
|---|
| 106 | boolean r1 = this.name.equals(that.name); |
|---|
| 107 | boolean r2 = this.latitude.equals(that.latitude); |
|---|
| 108 | boolean r3 = this.longitude.equals(that.longitude); |
|---|
| 109 | boolean r4 = this.street.equals(that.street); |
|---|
| 110 | return r1 && r2 && r3 && r4; |
|---|
| 111 | } |
|---|
| 112 | @Override public String toString() |
|---|
| 113 | { |
|---|
| 114 | return name + ": " + latitude +","+ longitude +","+ street; |
|---|
| 115 | } |
|---|
| 116 | public String toJson() |
|---|
| 117 | { |
|---|
| 118 | String pattern = "\n{\n \"type\": \"Feature\",\n" |
|---|
| 119 | + " \"geometry\":\n {\n \"type\": \"Point\",\n" + |
|---|
| 120 | " \"coordinates\": [%s,%s]\n" + |
|---|
| 121 | " },\n" + |
|---|
| 122 | " \"properties\": \n" + |
|---|
| 123 | " {\"id\": \"%s\", " + |
|---|
| 124 | "\"street\": \"%s\", " + |
|---|
| 125 | "\"color\": \"desiredcolor\"" + |
|---|
| 126 | "}\n},"; |
|---|
| 127 | |
|---|
| 128 | return String.format(pattern, longitude, latitude, name, street); |
|---|
| 129 | } |
|---|
| 130 | } |
|---|
| 131 | } |
|---|