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_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 test_grid_formatting(): assert osgb.format_grid(osgb.parse_grid('NM975948')) == 'NM 975 948' assert osgb.format_grid(osgb.parse_grid('NH073060')) == 'NH 073 060' assert osgb.format_grid(osgb.parse_grid('SX700682')) == 'SX 700 682' assert osgb.format_grid(osgb.parse_grid('TQ103606')) == 'TQ 103 606' assert osgb.format_grid(osgb.parse_grid('HY 554 300')) == 'HY 554 300' assert osgb.format_grid(osgb.parse_grid('40/975948')) == 'NM 975 948'
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)
def test_greenwich(): assert osgb.format_grid(osgb.ll_to_grid(lat=51.5, lon=0, model='OSGB36')) == 'TQ 388 798'
'''This script shows all the points with "all zero" grid references that are on a map sheet. For background, look at the WP entry for "Beast Cliff", which is point OV 000 000. The only argument is "series" which defines what map series to use. The default is A which means "OS Landranger" only. If you are planning on a visit to any of them, beware that some of them are in the sea. ''' import argparse import osgb if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument("--series", default='A', help='Which map series?') args = parser.parse_args() for n in range(13): for e in range(8): easting = e * 100000 northing = n * 100000 maps = osgb.sheet_keys(easting, northing, series=args.series) if maps: gr = osgb.format_grid(easting, northing) for m in maps: s = osgb.get_sheet(m) main_title = s.title.split('(')[0].strip() print(gr, 'on', m, main_title)
if i > 0: print('label.bot("{:d}", (t, 0)) withcolor .5 white;'.format( i * 100), file=plotter) for j in range(13): n = j * 100000 if i == 0: print('t:={:g};draw (0, t) -- (x0, t);'.format(n / scale), file=plotter) if j > 0: print('label.lft("{:d}", (0, t)) withcolor .5 white;'. format(j * 100), file=plotter) if i < 7 and j < 12: sq = osgb.format_grid(e, n, form='SS') mid = '({:.1f}, {:.1f})'.format((e + 50000) / scale, (n + 50000) / scale) print( 'label("{}" infont "phvr8r" scaled {}, {}) withcolor .8 white;' .format(sq, 3600 / scale, mid), file=plotter) # add HP as well mid = '({:.1f}, {:.1f})'.format(450000 / scale, 1250000 / scale) print('label("{}" infont "phvr8r" scaled {}, {}) withcolor .8 white;'. format("HP", 3600 / scale, mid), file=plotter) if not args.nocoast: coast_shapes = pkgutil.get_data('osgb', 'gb_coastline.shapes') if coast_shapes:
print('label.llft("0", origin) withcolor .5 white;', file=plotter) for i in range(8): e = i*100000 print('t:={:g};draw (t, 0) -- (t, y0);'.format(e/scale), file=plotter) if i > 0: print('label.bot("{:d}", (t, 0)) withcolor .5 white;'.format(i*100), file=plotter) for j in range(13): n = j*100000 if i == 0: print('t:={:g};draw (0, t) -- (x0, t);'.format(n/scale), file=plotter) if j > 0: print('label.lft("{:d}", (0, t)) withcolor .5 white;'.format(j*100), file=plotter) if i < 7 and j < 12: sq = osgb.format_grid(e, n, form='SS') mid = '({:.1f}, {:.1f})'.format((e+50000)/scale, (n+50000)/scale) print('label("{}" infont "phvr8r" scaled {}, {}) withcolor .8 white;'.format(sq, 3600/scale, mid), file=plotter) # add HP as well mid = '({:.1f}, {:.1f})'.format(450000/scale, 1250000/scale) print('label("{}" infont "phvr8r" scaled {}, {}) withcolor .8 white;'.format("HP", 3600/scale, mid), file=plotter) if not args.nocoast: coast_shapes = pkgutil.get_data('osgb', 'gb_coastline.shapes') if coast_shapes: print("drawoptions(withpen pencircle scaled 0.2 withcolor (0, 172/255, 226/255));", file=plotter) poly_path = list() for line in coast_shapes.split(b'\n'): if line.startswith(b'#'): print('draw ' + '--'.join(poly_path) + ';', file=plotter) del poly_path[:]
parser.add_argument("--sexagesimal", action="store_true", help="Show degrees in sexagesimal") parser.add_argument("grid_or_ll_element", type=str, nargs='*', default=['SP', '101', '203'], help="A grid reference string or lat/lon pair.") args = parser.parse_args() if args.random: map = random.choice( list(S for S in osgb.map_locker.values() if S.series == 'A')) e = random.randint(map.bbox[0][0], map.bbox[1][0]) n = random.randint(map.bbox[0][1], map.bbox[1][1]) agenda = osgb.format_grid(e, n) else: agenda = ' '.join(args.grid_or_ll_element) alphabet = ' 1234567890-.+/:ABCDEFGHJKLMNOPQRSTUVWXYZabcdef' # there is no I in this agenda = agenda.replace(',', ' ') # treat commas as spaces agenda = ''.join(x for x in agenda if x in alphabet) # and remove unwanted characters m = re.match( r'([456]\d) ([012345]?\d) ([012345]?\d) N (\d) ([012345]?\d) ([012345]?\d) ([EW])', agenda) if m is not None: (ha, ma, sa, hb, mb, sb, hemi) = m.groups() latlon = [ int(ha) + int(ma) / 60 + int(sa) / 3600,
def test_too_far(): with pytest.raises(osgb.gridder.FarFarAwayError): _ = osgb.format_grid(14000000, 800234)
def test_duff_form(): with pytest.raises(osgb.gridder.FaultyFormError): _ = osgb.format_grid(200000, 100000, form="TRD")