Changeset 538
 Timestamp:
 Apr 30, 2007, 11:19:11 AM (14 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

trunk/npemap.org.uk/scripts/gmclondon/FindBestMatch.java
r537 r538 1 import java.util.ArrayList; 2 import java.util.Arrays; 3 import java.util.Comparator; 4 1 5 public class FindBestMatch { 2 6 private static Place[] places; 3 7 static { 4 8 places = new Place[] { 9 new Place("Park Drive", 520896,174936, 646,2487), 5 10 new Place("Hither Green Sta", 539016,174424, 22751,1390), 6 11 new Place("Stratford Broadway", 539000,184488, 22898,13708), 7 12 new Place("Mill Hill Road", 522000,176056, 2105,3856), 8 new Place("Park Drive", 520896,174936, 646,2487),9 13 new Place("Lewisham High St", 538152,175104, 21708,2254), 10 14 new Place("Hither Green", 538152,174928, 21713,2050), … … 26 30 // Then calculate, then loop by scale and find error 27 31 public static void main(String[] args) { 28 for(double slant_base=0.35; slant_base<=1.4; slant_base += 0.01) { 29 for(double slant_ajd_e=0.5; slant_ajd_e<=1.2; slant_ajd_e += 0.01) { 30 for(double slant_ajd_n=0.5; slant_ajd_n<=1.2; slant_ajd_n += 0.01) { 32 // What the unscaled e+n's are 33 double[] e_m = new double[places.length]; 34 double[] n_m = new double[places.length]; 35 36 // Don't reallocate all the time 37 double slant, slant_rad, gmc_e_m, gmc_n_m; 38 double ngr_e_0, ngr_n_0, ngr_e, ngr_n; 39 double error; 40 41 // Our matches 42 ArrayList<Match> matches = new ArrayList<Match>(); 43 44 //for(double slant_base=0.35; slant_base<=1.4; slant_base += 0.01) { 45 for(double slant_base=1.05; slant_base<=1.09; slant_base += 0.01) { 46 System.out.println("The slant is " + slant_base); 47 for(double slant_adj_e=0.5; slant_adj_e<=1.2; slant_adj_e += 0.01) { 48 for(double slant_adj_n=0.5; slant_adj_n<=1.2; slant_adj_n += 0.01) { 31 49 // Calculate for this slant set 50 for(int i=0; i<places.length; i++) { 51 slant = slant_base + 52 slant_adj_e * ((26000.0  places[i].gmc_e)/26000.0) + 53 slant_adj_n * ((17000.0  places[i].gmc_n)/17000.0); 54 slant_rad = slant / 360.0 * 2.0 * Math.PI; 55 56 // Turn the gmc e+n into meters from the start of the map, 57 // but without scaling 58 // (Grid squares were half mile) 59 gmc_e_m = places[i].gmc_e / 2.0 * 1.609344; 60 gmc_n_m = places[i].gmc_n / 2.0 * 1.609344; 61 62 // Apply the slant factor 63 e_m[i] = (Math.sin(slant_rad)*gmc_n_m) + (Math.cos(slant_rad)*gmc_e_m); 64 n_m[i] = (Math.cos(slant_rad)*gmc_n_m) + (Math.sin(slant_rad)*gmc_e_m); 65 } 66 67 // Loop over the scales 68 for(double scale=0.95; scale <= 1.25; scale += 0.01) { 69 // Calculate the left corner e+n using 1st Place 70 // (We know that 0,646 2,487 is 520,896 174,936) 71 ngr_e_0 = 520896  (e_m[0] * scale); 72 ngr_n_0 = 174936  (n_m[0] * scale); 73 74 error = 0.0; 75 for(int i=0; i<places.length; i++) { 76 // Adjust by scale 77 ngr_e = (e_m[i] * scale) + ngr_e_0; 78 ngr_n = (n_m[i] * scale) + ngr_n_0; 79 80 // Find the error 81 error += Math.sqrt( 82 Math.pow(places[i].osgb_e  ngr_e, 2) + 83 Math.pow(places[i].osgb_n  ngr_n, 2) 84 ); 85 } 86 87 // Was the error small enough? If so, save it 88 if(error < 4000) { 89 System.out.println("Slant is " + slant_base + ", " + slant_adj_e + ", " + slant_adj_n + "  Scale is " + scale + "  Error is " + error); 90 matches.add( 91 new Match(slant_base,slant_adj_e,slant_adj_n,scale,error) 92 ); 93 } 94 } 32 95 } 33 96 } 97 } 98 99 // Sort by the error 100 System.out.println("Found " + matches.size() + " potentials"); 101 Match[] m = (Match[])matches.toArray(new Match[matches.size()]); 102 Arrays.sort(m, new MatchSorter()); 103 104 // Print out the best few 105 for(int i=0; i<= 10; i++) { 106 System.out.println("Error was " + m[i].error); 107 System.out.println(" Scale was " + m[i].scale); 108 System.out.println(" slant was " + m[i].slant + ", " + m[i].slant_ajd_e + ", " + m[i].slant_ajd_n); 34 109 } 35 110 } … … 63 138 private double error; 64 139 } 140 public static class MatchSorter implements Comparator { 141 public int compare(Object a,Object b) { 142 Match ma = (Match)a; 143 Match mb = (Match)b; 144 if(ma.error < mb.error) return 1; 145 if(ma.error > mb.error) return 1; 146 return 0; 147 } 148 } 65 149 };
Note: See TracChangeset
for help on using the changeset viewer.