def test_reading_from_explorer(): ''' In the sea near Coquet Island, SE corner of Explorer 332 ''' gr = osgb.format_grid(osgb.ll_to_grid(lat=55 + 19 / 60, lon=-1.5, model='WGS84'), form='GPS') assert gr == 'NU 31831 02623'
def test_glendessary(): ''' # and now a boggy path just north of Glendessary in Lochaber # OS Sheet 40 topright corner. A graticule intersection at # 57N 5o20W is marked. GR measured from the map. ''' assert osgb.ll_to_grid(lat=57, lon=-320 / 60, model='OSGB36') == (197573.181, 794792.843)
def test_all(chatty=False): for k in sorted(test_input): gr = osgb.ll_to_grid(*test_input[k]) if chatty: print("Exp:", expected_output[k]) print("Got:", gr) print() assert gr == expected_output[k]
def test_reading_from_landranger(): ''' Hills above Loch Achall, OS Sheet 20 ''' gr = osgb.format_grid(osgb.ll_to_grid(lat=57 + 55 / 60, lon=-305 / 60, model='OSGB36'), form='GPS') assert gr == 'NH 17379 96054'
def lonlat_to_osgb(lon, lat, digits=3, formatted=True, model='OSGB36'): """Convert a longitude and latitude to Ordnance Survey grid reference. :Parameters: lon Longitude, presumed to be in OSG36 degrees (unless you set model='WGS84'). lat Latitude, ditto. digits The number of digits to use for each direction in the final grid reference. 3 by default, grid references are up to 6. formatted Should the OSGB reference be nicely formatted (with whitespace)? By default true. model OSGB36 or WGS84, default OSGB36 :Returns: A string giving a formatted OSGB reference. For example:: >>> print(lonlat_to_osgb (1.088978, 52.129892)) TM 114 525 >>> print(lonlat_to_osgb (1.088978, 52.129892, formatted=False)) TM114525 >>> print(lonlat_to_osgb (1.088978, 52.129892, 5)) TM 11400 52500 In the re-implemented version you can reverse arguments if you want to... >>> print(lonlat_to_osgb(52.129892, 1.088978, 5)) TM 11400 52500 """ east, north = osgb.ll_to_grid(lat, lon, model=model) if formatted: format_spec = ' '.join(['SS', 'E' * digits, 'N' * digits]) else: format_spec = ''.join(['SS', 'E' * digits, 'N' * digits]) return osgb.format_grid(east, north, form=format_spec)
def test_ten_metre_test_with_osgb(): ''' the sw corner of OS Explorer sheet 161 (Cobham, Surrey) ------------------------------------------------------- check we are within 10m (easily the limit of my ability to measure on a 1:25000 map) Note that here we are using OSGB36 through out, because the LL printed on OS maps is not based on WGS84 ''' (e, n) = osgb.ll_to_grid(lat=51.3333333333, lon=-0.416666666667, model='OSGB36') assert (e, n) == (510290.252, 160605.816) gr = osgb.format_grid(510290, 160606) assert gr == 'TQ 102 606'
def lonlat_to_osgb(lon, lat, digits=3, formatted=True, model='OSGB36'): """Convert a longitude and latitude to Ordnance Survey grid reference. :Parameters: lon Longitude, assumed to be in OSGB36 degrees (unless you set model='WGS84'). lat Latitude, ditto. digits The number of digits to use for each direction in the final grid reference. Default 3. formatted Should the OSGB reference be nicely formatted (with whitespace)? Default true. model 'OSGB36' or 'WGS84', default 'OSGB36' :Returns: A string giving a formatted OSGB reference. For example:: >>> print(lonlat_to_osgb(1.088978, 52.129892)) TM 114 525 >>> print(lonlat_to_osgb(1.088978, 52.129892, formatted=False)) TM114525 >>> print(lonlat_to_osgb(1.088978, 52.129892, 5)) TM 11400 52500 In the re-implemented version you can reverse arguments if you want to... >>> print(lonlat_to_osgb(52.129892, 1.088978, 5)) TM 11400 52500 """ east, north = osgb.ll_to_grid(lat, lon, model=model) if formatted: format_spec = ' '.join(['SS', 'E' * digits, 'N' * digits]) else: format_spec = ''.join(['SS', 'E' * digits, 'N' * digits]) return osgb.format_grid(east, north, form=format_spec)
points = list() for track in gpx.tracks: for segment in track.segments: for point in segment.points: points.append(point) for point in gpx.waypoints: points.append(point) for route in gpx.routes: for point in route.points: points.append(point) maps = list() for p in points: maps.extend(osgb.sheet_keys(*osgb.ll_to_grid(p.latitude, p.longitude), series=args.series)) c = collections.Counter(maps) for map_key in sorted(c): out = list() if args.name: k, sheet = map_key.split(':') out.append('{} {}'.format(osgb.name_for_map_series[k], sheet)) else: out.append(map_key) if args.title: out.append('"' + osgb.map_locker[map_key].title + '"') if args.coverage: out.append("({}%)".format(int(100*c[map_key]/len(points)))) print(' '.join(out))
def test_greenwich(): assert osgb.format_grid(osgb.ll_to_grid(lat=51.5, lon=0, model='OSGB36')) == 'TQ 388 798'
points = list() for track in gpx.tracks: for segment in track.segments: for point in segment.points: points.append(point) for point in gpx.waypoints: points.append(point) for route in gpx.routes: for point in route.points: points.append(point) maps = list() for p in points: maps.extend(osgb.sheet_list(*osgb.ll_to_grid(p.latitude, p.longitude), series=args.series)) c = collections.Counter(maps) for map_key in sorted(c): out = list() if args.name: k, sheet = map_key.split(':') out.append('{} {}'.format(osgb.mapping.name_for_map_series[k], sheet)) else: out.append(map_key) if args.title: out.append('"' + osgb.mapping.map_locker[map_key]['title'] + '"') if args.coverage: out.append("({}%)".format(int(100*c[map_key]/len(points)))) print(' '.join(out))
# Starting making the MP file print( 'prologues := 3; outputtemplate := "%j.eps"; beginfig(1); defaultfont := "phvr8r";', file=plotter) # sides and insets will have anything in if we chose one or more series for k in sides + insets: print("fill {} withcolor (0.98, 0.906, 0.71);".format(path_for[k]), file=plotter) if args.error: for x in range(70): e = x * 10000 + 5000 for y in range(125): n = y * 10000 + 5000 (ee, nn) = osgb.ll_to_grid(*osgb.grid_to_ll(e, n)) h = math.sqrt((e - ee)**2 + (n - nn)**2) if h > 0: print( 'drawdot ({:.1f}, {:.1f}) withpen pencircle scaled 4 withcolor {:.2f}[white, red];' .format(e / scale, n / scale, h * 100), file=plotter) print( 'label.rt("Round trip error (mm)" infont defaultfont scaled 0.6, ({:.1f}, {:.1f}));' .format(-176500 / scale, 1255000 / scale), file=plotter) for i in range(6): e = -120000 - i * 10000 n = 1245000 print(
# open a tempory file for MP plotter = tempfile.NamedTemporaryFile(mode='wt', prefix='plot_maps_', suffix='.mp', dir='.', delete=False) # Starting making the MP file print('prologues := 3; outputtemplate := "%j.eps"; beginfig(1); defaultfont := "phvr8r";', file=plotter) # sides and insets will have anything in if we chose one or more series for k in sides + insets: print("fill {} withcolor (0.98, 0.906, 0.71);".format(path_for[k]), file=plotter) if args.error: for x in range(70): e = x * 10000 + 5000 for y in range(125): n = y * 10000 + 5000 (ee, nn) = osgb.ll_to_grid(*osgb.grid_to_ll(e, n)) h = math.sqrt((e-ee)**2+(n-nn)**2) if h > 0: print('drawdot ({:.1f}, {:.1f}) withpen pencircle scaled 4 withcolor {:.2f}[white, red];'.format(e/scale, n/scale, h*100), file=plotter) print('label.rt("Round trip error (mm)" infont defaultfont scaled 0.6, ({:.1f}, {:.1f}));'.format(-176500/scale, 1255000/scale), file=plotter) for i in range(6): e = -120000 - i * 10000 n = 1245000 print('drawdot ({:.1f}, {:.1f}) withpen pencircle scaled 4 withcolor {:.2f}[white, red];'.format(e/scale, n/scale, i/5), file=plotter) print('label.bot("{}" infont defaultfont scaled 0.6, ({:.1f}, {:.1f}));'.format(2*i, e/scale, n/scale-2), file=plotter) if not args.nograt: print("drawoptions(withpen pencircle scaled 0.4);", file=plotter) for lon in range(-10, 3): points = []
if m is not None: (ha, ma, sa, hb, mb, sb, hemi) = m.groups() latlon = [ int(ha) + int(ma) / 60 + int(sa) / 3600, (int(hb) + int(mb) / 60 + int(sb) / 3600) ] if hemi == 'W': latlon[1] = -latlon[1] else: latlon = list(get_likely_lon_lat(x) for x in agenda.split()) # if the arguments consists of just two likely looking numbers... # You can't just say "all(floats)" because 0 is a valid Longitude if len(latlon) == 2 and all(x is not None for x in latlon): (lon, lat) = sorted(latlon) # Lon is always less than Lat in OSGB (e, n) = osgb.ll_to_grid(lat, lon) (olat, olon) = (lat, lon) (oe, on) = osgb.ll_to_grid(lat, lon, 'OSGB36') else: print('>>', agenda) (e, n) = osgb.parse_grid(agenda) (lat, lon) = osgb.grid_to_ll(e, n) (oe, on) = (e, n) (olat, olon) = osgb.grid_to_ll(e, n, 'OSGB36') try: grid = osgb.format_grid(e, n) maps = osgb.sheet_keys(e, n) except osgb.gridder.Error: grid = '??'
if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument("--putsomethinghere") args = parser.parse_args() print(''' prologues := 3; outputtemplate := "%j%c.eps"; beginfig(1); ''') for n in range(1200500, 500, -10000): for e in range(500, 700000, 10000): lat, lon = osgb.grid_to_ll(e, n) ee, nn = osgb.ll_to_grid(lat, lon) de = ee-e dn = nn-n hh = math.sqrt(de*de + dn*dn) #print('{:.3f}'.format(hh*100), end=' ') if hh > 0: print('drawdot ({}, {}) withpen pencircle scaled 4 withcolor {}[white, red];'.format(e/1000, n/1000, hh*100)) print() for lat in range(50, 61): print('draw') for lon in range(-9, 2): (e, n) = osgb.ll_to_grid(lat, lon) print('({}, {})..'.format(e/1000, n/1000))
from __future__ import print_function, division import argparse import osgb if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument("-d", "--diff", type=int) parser.add_argument("-a", "--lat", action='store_true') args = parser.parse_args() d = 10 ** -args.diff if args.diff else 0.1; print("{:g}".format(d)) for lat in range(60,49,-1): print(lat, end = ':') for lon in range(-8, 2): (e, n) = osgb.ll_to_grid(lat, lon) (ee, nn) = osgb.ll_to_grid(lat+d, lon+d) if args.lat: print('{:.3f}'.format(nn-n), end= ' ') else: print('{:.3f}'.format(ee-e), end= ' ') print() # Approx answers: 5 places 0.00001 lat = 1m, long < 1m # 8 places 0.00000001 lat = 1mm, lat < 1mm
from __future__ import print_function, division import argparse import osgb if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument("-d", "--diff", type=int) parser.add_argument("-a", "--lat", action='store_true') args = parser.parse_args() d = 10**-args.diff if args.diff else 0.1 print( "{:g} difference in lat+lon makes this difference in metres...".format( d)) for lat in range(60, 49, -1): print(lat, end=':') for lon in range(-8, 2): (e, n) = osgb.ll_to_grid(lat, lon) (ee, nn) = osgb.ll_to_grid(lat + d, lon + d) if args.lat: print('{:.3f}'.format(nn - n), end=' ') else: print('{:.3f}'.format(ee - e), end=' ') print() # Approx answers: 5 places 0.00001 lat = 1m, long < 1m # 8 places 0.00000001 lat = 1mm, lat < 1mm