def test2(self, pts, npt, psxy=False): clas_ = classname(pts) + '.' def _test(name, *args, **kwds): # prefix class to test name self.test(clas_ + name, *args, **kwds) if Sequence: # check abstact base class conformance _test('ABC', isinstance(pts, Sequence), True) e = pts.epsilon _test('epsilon', e, EPS) pts.epsilon = 0 _test('epsilon', pts.epsilon, 0.0) pts.epsilon = e n = len(pts) // 6 # 0 < some number < len(pts) _test('len', len(pts), len(npt)) _test('iter', len(tuple(iter(pts))), len(npt)) if hasattr(npt, 'shape'): _test('shape', npt.shape, pts.shape) _test('slice1', len(pts[:n]), n) _test('slice2', type(pts[1:n:2]), type(pts)) _test('slice3', pts[1:n][0], pts[1]) _test('strepr', str(pts), repr(pts)) if hasattr(pts, 'subset'): _test('subset', type(pts.subset(range(n))), type(npt)) # , nt=1) for i, p in ((10, (52.224006, -0.707747)), (20, (52.232688, -0.714608)), (30, (52.234375, -0.714348)), (40, (52.237239, -0.712557)), (50, (52.24023, -0.709919)), (60, (52.240745, -0.707042))): if psxy: _test('find LL', pts.find(LatLon_(*p)), i) # coverage p = tuple(reversed(p)) # flip to x, y tuple _test('find LL', pts.find(LatLon_(*p)), -1) # coverage _test('count', pts.count(p), 1) _test('index', pts.index(p), i) _test('rfind', pts.rfind(p), i) _test('in', p in pts, True) p = tuple(reversed(p)) _test('count', pts.count(p), 0) _test('find', pts.find(p), -1) _test('rfind', pts.rfind(p), -1) _test('not in', p not in pts, True) pts = pts[::6] for i, p in enumerate(pts): _test('enumerate[%s]' % (i, ), p, pts[i]) i = len(pts) for p in reversed(pts): i -= 1 _test('reversed[%s]' % (i, ), p, pts[i])
def testGeoid(self, G, grid, llh3, crop=None, eps=None, kind=None): # MCCABE 13 # test geoid G(grid) for (lat, lon, height, eps) if scipy or G is GeoidKarney: e_max = 0 eps = eps or self._epsHeight f = self.failed - self.known g = G(grid, kind=kind or self._kind, crop=crop) s = '%s.height(%%s) kind %s' % (g, g.kind) try: for lat, lon, expected in llh3: t = s % (fStr((lat, lon), prec=3),) try: h = g.height(lat, lon) e = abs(h - expected) if e_max < e: e_max = e self.test(t, h, expected, fmt='%.3f', known=e < eps) except GeoidError as x: self.test(t, str(x), '%.3f' % (expected,), known=G not in (GeoidKarney, GeoidPGM)) except RangeError as x: self.test(t, str(x), '%.3f' % (expected,), known=bool(crop)) except KeyboardInterrupt as x: self.printf(repr(x), nl=1) f = self.failed - self.known - f if f > 0 or e_max > 0: h = '' if g.hits is None else ', hits %s' % (g.hits,) t = '%s.height() kind %s%s, eps max (in %s FAILED)' % (g, g.kind, h, f) x = eps if f > 0 else e_max self.test(t, e_max, x, fmt='%.3f', known=e_max < eps, nl=1, nt=1) # print('%r\n\n%r' % (g, getattr(g, 'pgm', None))) if coverage: for a in ('highest', 'lowerleft', 'lowerright', 'lowest', 'upperleft', 'upperright'): t = fStr(getattr(g, a)(), prec=3) self.test('%s.%s()' % (g, a), t, t, known=True) for p in ('dtype', 'knots', 'mean', 'nBytes', 'scipy', 'smooth', 'stdev'): # , 'pgm' t = ''.join(reprs((getattr(g, p),), prec=3)) self.test('%s.%s' % (g, p), t, t, known=True) for a in ('_g2ll2', '_ll2g2'): t = getattr(g, a)(180, 360) self.test('%s.%s(180, 360)' % (g, a), t, t, known=True) for t in ((LatLon_(-10, -100), LatLon_(10, 100)), ( (-10, -100), (10, 100))): t = g._swne(t) self.test('%s.%s' % (g, '_swne'), t, '(-10.0, -100.0, 10.0, 100.0)') t = g.toStr() self.test('%s.%s' % (g, 'toStr'), t, t, known=True, nt=1) self.testCopy(g) else: n, _ = len2(llh3) self.skip('no scipy', n=n)
def testStreprs(self): self.test('anstr', anstr('a-b?_'), 'a-b__') self.test('fstr', fstr(0.123, prec=-6), '0.123000') self.test('fstr', fstr(0.123, prec=+6), '0.123') self.test('fstr', fstr((0.123, 456.789), prec=+6), '0.123, 456.789') self.test('fstr', fstr(0.123, prec=-5, fmt='%.*e'), '1.23000e-01') self.test('fstr', fstr(0.123, prec=+5, fmt='%.*e'), '1.23e-01') try: # coverage self.test('fstr', fstr(1, fmt='X'), ValueError.__name__) except ValueError as x: from pygeodesy.streprs import _Fspec_ self.test('fstr', str(x), "fmt ('X'): not %r" % (_Fspec_, )) for f, x in ( (1, '1.0'), (1.0, '1.0'), (-1, '-1.0'), # (1e300, '10000<290>40160.'), (1e1234, 'INF'), # == INF (INF, 'INF'), (NAN, 'NAN'), (NEG0, '-0.0'), (0.0, '0.0')): self.test('fstr(%F)' % (f, ), fstr(f), x) for f, x in (('0.0', '0.0'), ('0.00', '0.0'), ('0.000', '0.0'), ('00.0', '00.0'), ('000.00', '000.0'), ('0.000', '0.0'), ('0.010', '0.01'), ('0.0200', '0.02'), ('0.0e+01', '0.0e+01'), ('0.00e+02', '0.0e+02'), ('0.000e+03', '0.0e+03'), ('00.0e+00', '00.0e+00'), ('000.00e+01', '000.0e+01'), ('0.000e+02', '0.0e+02'), ('0.010e+03', '0.01e+03'), ('0.0200e+00', '0.02e+00')): self.test('fstrzs(%s)' % (f, ), fstrzs(f), x) for f, x in (('0', '0.0'), ('0.0', '0.0'), ('0.', '0.'), ('1e10', '1.0e10'), ('2E+2', '2.0E+2'), ('3.E3', '3.E3')): self.test('fstrzs(%s, ap1z=True)' % (f, ), fstrzs(f, ap1z=True), x) ll = LatLon_(45, 90, height=1.2) self.test('instr', ll.toStr2(), 'LatLon_(45.0°N, 090.0°E, +1.20)') self.test('instr', instr(ll, 45, 90, h=1.2), 'LatLon_(45, 90, h=1.2)') self.test('unstr', unstr('f', 1.1, 2.2), 'f(1.1, 2.2)') self.test('unstr', unstr('f', y=2.2, x=1.1), 'f(x=1.1, y=2.2)')
def testEcefCartesian(self): self.test(EcefCartesian.__name__, '...', '...', nl=1) # <https://GeographicLib.SourceForge.io/html/CartConvert.1.html> c = EcefCartesian(33, 44, 20, name='Test') self.test('name', c.name, 'Test') t = c.toStr2() self.test('toStr', t, c.classname, known=True) self.testCopy(c) t = c.forward(33.3, 44.4, 6000) self.test('forward', fstr(t[3:6], prec=1), '33.3, 44.4, 6000.0') self.test('forward', fstr(t[0:3], prec=2), '37288.97, 33374.29, 5783.65') # 5783.64 self.test('name', c.name, 'Test') t = c.reverse(37288.97, 33374.29, 5783.65) self.test('reverse', fstr(t[3:6], prec=2), '33.3, 44.4, 6000.0') self.test('name', c.name, 'Test') # <https://SourceForge.net/p/geographiclib/code/ci/release/tree/examples/example-LocalCartesian.cpp> c.reset(48 + 50 / 60.0, 2 + 20 / 60.0, name='Paris') self.test('name', c.name, 'Paris') self.test(c.name, fstr((c.lat0, c.lon0, c.height0), prec=3), '48.833, 2.333, 0.0') t = c.forward(LatLon_(50.9, 1.8, name='Calais')) self.test('forward', fstr(t[3:6], prec=1), '50.9, 1.8, 0.0') self.test('forward', fstr(t[0:3], prec=2), '-37518.64, 229949.65, -4260.43') self.test('name', t.name, 'Calais') t = c.reverse(-37518.64, 229949.65, -4260.43) self.test('reverse', fstr(t[3:6], prec=2), '50.9, 1.8, -0.0', know=True) self.test('name', t.name, 'Paris') t = c.reverse(-38e3, 230e3, -4e3) self.test('reverse', fstr(t[0:3], prec=1), '4028834.2, 126130.9, 4926765.2') self.test('reverse', fstr(t[3:6], prec=2), '50.9, 1.79, 264.92') t = c.forward(50.9, 1.79, 264.92) self.test('forward', fstr(t[0:3], prec=1), '-38000.0, 230000.0, -4000.0', known=True)
def testGeodCalc(self): self.subtitle(karney, 'GeodCalc') # <https://GeographicLib.sourceforge.io/scripts/geod-calc.html#area> p = [ LatLon_(*t) for t in ((-63.1, -58), (-72.9, -74), (-71.9, -102), (-74.9, -102), (-74.3, -131), (-77.5, -163), (-77.4, 163), (-71.7, 172), (-65.9, 140), (-65.7, 113), (-66.6, 88), (-66.9, 59), (-69.8, 25), (-70.0, -4), (-71.0, -14), (-77.3, -33), (-77.9, -46), (-74.7, -61)) ] self.test('area', ellipsoidalKarney.areaOf(p), 13662703680020.125, fmt='%.0f') self.test('perimeter', ellipsoidalKarney.perimeterOf(p, closed=True), 16831067.89279071, fmt='%.6f')
# -*- coding: utf-8 -*- # Test the Frechet distances. __all__ = ('Tests', ) __version__ = '19.08.30' from base import isPython3, isWindows, TestsBase from pygeodesy import fStr, LatLon_, randomrangenerator _rr = randomrangenerator('R') _ms = [ LatLon_(*_ll) for _ll in zip( _rr(-90, 90, 2), # 90 _rr(-180, 180, 4)) ] _ps = [ LatLon_(*_ll) for _ll in zip( _rr(-89, 90, 3), # 60 _rr(-179, 180, 6)) ] class Tests(TestsBase): def test2(self, Frechet, x, y, **kwds): def _tstr(t): s = list(t[:5]) s[0] = fStr(t.fd, prec=5) return '(%s)' % (', '.join(map(str, s)), )
self.test('areaOf', areaOf(p, radius=R_MA), '8.811228e+09', fmt='%.6e') self.test('isclockwise', isclockwise(p), False) p = LatLon_(0, 0), LatLon_(1, 0), LatLon_(0, 1) self.test('areaOf', areaOf(p, radius=R_MA), '7.09e+09', fmt='%.2e') self.test('isclockwise', isclockwise(p), True) if __name__ == '__main__': # PYCHOK internal error? from testRoutes import PtsFFI t = Tests(__file__, __version__, points) try: t.test('LatLon_', LatLon_(0, 0).__dict__, 'AttributeError') except AttributeError as x: t.test('LatLon_', x, "'LatLon_' object has no attribute '__dict__'") pts = LatLon2psxy(PtsFFI, wrap=False) t.test2(pts, PtsFFI, True) if numpy: t.test('numpy.__version__', numpy.__version__, numpy.__version__) npy = numpy.array([(ll.lon, 0, ll.lat, 0) for ll in PtsFFI], dtype=float) pts = Numpy2LatLon(npy, ilat=2, ilon=0) t.test2(pts, npy, False) else: # check abstact base class conformance t.test('no', 'numpy', 'numpy')
dy, dx = abs(p1.phi - p2.phi), abs(p1.lam - p2.lam) if dx < dy: dx, dy = dy, dx return dx + dy * 0.5 def _tstr(t): s = list(t[:5]) s[0] = fStr(t.hd, prec=5) s[4] = None if t.md is None else fStr(t.md, prec=5) return '(%s)' % (', '.join(map(str, s)),) _rr = randomrangenerator('R') # like <https://GitHub.com/mavillan/py-hausdorff> Example _ms = [LatLon_(*_ll) for _ll in zip(_rr( -90, 90, 2), # 90 _rr(-180, 180, 4))] _ps = [LatLon_(*_ll) for _ll in zip(_rr( -89, 90, 3), # 60 _rr(-179, 180, 6))] class Tests(TestsBase): def test4(self, Hausdorff, *tests, **kwds): for s, e, x, y in tests: h = Hausdorff(_ms, seed=s, **kwds) t = _tstr(h.directed(_ps, early=e)) self.test(h.named, t, x) # + (h.units,)
def toLatLon(self, **unused): lat, lon, _, _ = _ETM.reverse(self.easting, self.northing) return LatLon_(lat, lon)
# for comparison, the RDPgr results should be less than a few points different t.test2(simplifyRDPgr, Ptsn, _ms({160: 111, 80: 161, 40: 256, 20: 387, 10: 542, 1: 1267}), adjust=False, shortest=True) # different points t.test2(simplifyVW, PtsFFI, _ms({1000: 3, 100: 12, 10: 48, 1: 69}), adjust=False) t.test2(simplifyRDP, PtsFFI, _ms({1000: 3, 100: 7, 10: 18, 1: 50}), adjust=False, shortest=False) # XXX len(RdpFFI) = 7 # for comparison, the RDPgr results should be less than a few points different t.test2(simplifyRDPfw, PtsFFI, _ms({1000: 3, 100: 7, 10: 18, 1: 50}), adjust=False, shortest=False) # for comparison, the RDPgr results should be less than a few points different t.test2(simplifyRDPgr, PtsFFI, _ms({1000: 3, 100: 7, 10: 18, 1: 50}), adjust=False, shortest=True) t.test2(simplifyRDPgr, PtsFFI, _ms({1000: 2, 100: 7, 10: 15, 1: 45}), adjust=True, shortest=True) # <https://docs.RS/geo/0.8.3/geo/algorithm/simplify/trait.Simplify.html> # t.test2(simplifyRDP, [LatLon_(*ll) for ll in ((0.0, 0.0), (5.0, 4.0), (11.0, 5.5), (17.3, 3.2), (27.8, 0.1))], # _ms({1: 5}), adjust=False, shortest=True) # (0.0, 0.0), (5.0, 4.0), (11.0, 5.5), (27.8, 0.1) t.test2(simplifyRDP, [LatLon_(*ll) for ll in ((0.0, 0.0), (5.0, 4.0), (11.0, 5.5), (17.3, 3.2), (EPS, EPS))], _ms({1: 5}), adjust=False, shortest=True) # coverage d2ih # <https://docs.RS/geo/0.8.3/geo/algorithm/simplifyvw/trait.SimplifyVW.html> # t.test2(simplifyVW, [LatLon_(*ll) for ll in ((5.0, 2.0), (3.0, 8.0), (6.0, 20.0), (7.0, 25.0), (10.0, 10.0))], # _ms({30: 3}), adjust=False) # (5.0, 2.0), (7.0, 25.0), (10.0, 10.0) t.test2(simplifyVW, [LatLon_(*ll) for ll in ((5.0, 2.0), (3.0, 8.0), (6.0, 20.0), (7.0, 25.0), (10.0, 10.0))], _ms({30: 5}), adjust=False, attr='name') # coverage attr, 3 set to 5 if numpy: if 'numpy' in _Simplifys: _Simplifys.remove('numpy') t.test('numpy.__version__', numpy.__version__, numpy.__version__) npy = numpy.array([(ll.lon, 0, ll.lat, 0) for ll in PtsFFI], dtype=float)
latlonre = re.compile( r"""(-*\d+\.\d+)""" ) # Extract the two pairs of coords from the rest of the text mykml = untangle.parse(inputFilename) # Parse the XML input file coords = [] # Prepare to store coordinates for folder in mykml.kml.Document.Folder.Placemark: # For each 'folder'... tmpcoordpair = latlonre.findall( folder.LineString.coordinates.cdata ) # Find all of the Regular Expression string matches for i in range(2): # Since there are '2' pairs do the following... coord = LatLon_(tmpcoordpair[i * 2], tmpcoordpair[i * 2 + 1]) coords.append({ "folder": folder.name.cdata, "coord_deg": str(latlonDMS(coord, form='-d')).replace('°', ''), "coord_degmin": str(latlonDMS(coord, form='-dm')).replace('°', ' ').replace('′', ''), "coord_degminsec": str(latlonDMS(coord, form='-dms')).replace('°', ' ').replace( '′', ' ').replace('″', ''), }) for coord in coords: print(coord) # For debugging really...
t.test2(simplifyRDPgr, PtsFFI, _ms({ 1000: 2, 100: 7, 10: 15, 1: 45 }), adjust=True, shortest=True) # <https://docs.RS/geo/0.8.3/geo/algorithm/simplify/trait.Simplify.html> # t.test2(simplifyRDP, [LatLon_(*ll) for ll in ((0.0, 0.0), (5.0, 4.0), (11.0, 5.5), (17.3, 3.2), (27.8, 0.1))], # _ms({1: 5}), adjust=False, shortest=True) # (0.0, 0.0), (5.0, 4.0), (11.0, 5.5), (27.8, 0.1) t.test2(simplifyRDP, [ LatLon_(*ll) for ll in ((0.0, 0.0), (5.0, 4.0), (11.0, 5.5), (17.3, 3.2), (EPS, EPS)) ], _ms({1: 5}), adjust=False, shortest=True) # coverahe d2ih # <https://docs.RS/geo/0.8.3/geo/algorithm/simplifyvw/trait.SimplifyVW.html> # t.test2(simplifyVW, [LatLon_(*ll) for ll in ((5.0, 2.0), (3.0, 8.0), (6.0, 20.0), (7.0, 25.0), (10.0, 10.0))], # _ms({30: 3}), adjust=False) # (5.0, 2.0), (7.0, 25.0), (10.0, 10.0) if numpy: if 'numpy' in _Simplifys: _Simplifys.remove('numpy') t.test('numpy.__version__', numpy.__version__, numpy.__version__)