1

2# -*- coding: utf-8 -*-

3

4 u'''Hausdorff distances.

5

6 Classes L{Hausdorff}, L{HausdorffDegrees}, L{HausdorffRadians},

7 L{HausdorffCosineAndoyerLambert}, L{HausdorffCosineForsytheAndoyerLambert},

8 L{HausdorffCosineLaw}, L{HausdorffDistanceTo}, L{HausdorffEquirectangular},

9 L{HausdorffEuclidean}, L{HausdorffFlatLocal}, L{HausdorffFlatPolar},

10 L{HausdorffHaversine}, L{HausdorffHubeny}, L{HausdorffKarney},

11 L{HausdorffThomas} and L{HausdorffVincentys} to compute U{Hausdorff

12<https://WikiPedia.org/wiki/Hausdorff_distance>} distances between two

13 sets of C{LatLon}, C{NumPy}, C{tuples} or other types of points.

14

15 Only L{HausdorffDistanceTo} -iff used with L{ellipsoidalKarney.LatLon}

16 points- and L{HausdorffKarney} requires installation of I{Charles Karney}'s

17 U{geographiclib<https://PyPI.org/project/geographiclib>}.

18

19 Typical usage is as follows. First, create a C{Hausdorff} calculator

20 from a given set of C{LatLon} points, called the C{model} or C{template}

21 points.

22

23 C{h = HausdorffXyz(point1s, ...)}

24

25 Get the C{directed} or C{symmetric} Hausdorff distance to a second set

26 of C{LatLon} points, named the C{target} points, by using

27

28 C{t6 = h.directed(point2s)}

29

30 respectively

31

32 C{t6 = h.symmetric(point2s)}.

33

34 Or, use function C{hausdorff_} with a proper C{distance} function and

35 optionally a C{point} function passed as keyword arguments as follows

36

37 C{t6 = hausdorff_(point1s, point2s, ..., distance=..., point=...)}.

38

39 In all cases, the returned result C{t6} is a L{Hausdorff6Tuple}.

40

41 For C{(lat, lon, ...)} points in a C{NumPy} array or plain C{tuples},

42 wrap the points in a L{Numpy2LatLon} respectively L{Tuple2LatLon}

43 instance, more details in the documentation thereof.

44

45 For other points, create a L{Hausdorff} sub-class with the appropriate

46 C{distance} method overloading L{Hausdorff.distance} and optionally a

47 C{point} method overriding L{Hausdorff.point} as the next example.

48

49 >>> from pygeodesy import Hausdorff, hypot_

50 >>>

51 >>> class H3D(Hausdorff):

52 >>> """Custom Hausdorff example.

53 >>> """

54 >>> def distance(self, p1, p2):

55 >>> return hypot_(p1.x - p2.x, p1.y - p2.y, p1.z - p2.z)

56 >>>

57 >>> h3D = H3D(xyz1, ..., units="...")

58 >>> d6 = h3D.directed(xyz2)

59

60 Transcribed from the original SciPy U{Directed Hausdorff Code

61<https://GitHub.com/scipy/scipy/blob/master/scipy/spatial/_hausdorff.pyx>}

62 version 0.19.0, Copyright (C) Tyler Reddy, Richard Gowers, and Max Linke,

63 2016, distributed under the same BSD license as SciPy, including C{early

64 breaking} and C{random sampling} as in U{Abdel Aziz Taha, Allan Hanbury

65"An Efficient Algorithm for Calculating the Exact Hausdorff Distance"

66<https://Publik.TUWien.ac.AT/files/PubDat_247739.pdf>}, IEEE Trans. Pattern

67 Analysis Machine Intelligence (PAMI), vol 37, no 11, pp 2153-2163, Nov 2015.

68'''

69

70 from pygeodesy.constants import INF, NINF, _0_0

71 from pygeodesy.datums import _ellipsoidal_datum, _WGS84

72 from pygeodesy.errors import _IsnotError, PointsError, _xattr, _xkwds, _xkwds_get

73 import pygeodesy.formy as _formy

74 from pygeodesy.interns import NN, _i_, _j_, _units_

75# from pygeodesy.iters import points2 # from .points

76 from pygeodesy.lazily import _ALL_LAZY, _FOR_DOCS

77 from pygeodesy.named import _Named, _NamedTuple, notOverloaded, _Pass

78# from pygeodesy.namedTuples import PhiLam2Tuple # from .points

79 from pygeodesy.points import _distanceTo, points2 as _points2, PhiLam2Tuple, radians

80 from pygeodesy.props import Property_RO, property_doc_, property_RO

81 from pygeodesy.units import Float, Number_, _xUnit, _xUnits

82 from pygeodesy.unitsBase import _Str_degrees, _Str_degrees2, _Str_meter, _Str_NN, \

83 _Str_radians, _Str_radians2

84

85# from math import radians # from .points

86 from random import Random

87

88__all__ = _ALL_LAZY.hausdorff

89__version__ = '23.08.06'

90

91

92 class HausdorffError(PointsError):

93 '''Hausdorff issue.

94 '''

95 pass

96

97

98 class Hausdorff(_Named):

99 '''Hausdorff base class, requires method L{Hausdorff.distance} to

100 be overloaded.

101 '''

102 _datum = _WGS84

103 _func = None # formy function

104 _kwds = {} # func_ options

105 _model = ()

106 _seed = None

107 _units = _Str_NN # XXX Str to _Pass and for backward compatibility

108

109 def __init__(self, point1s, seed=None, name=NN, units=NN, **kwds):

110 '''New C{Hausdorff...} calculator.

111

112 @arg point1s: Initial set of points, aka the C{model} or

113 C{template} (C{LatLon}[], C{Numpy2LatLon}[],

114 C{Tuple2LatLon}[] or C{other}[]).

115 @kwarg seed: Random sampling seed (C{any}) or C{None}, C{0}

116 or C{False} for no U{random sampling<https://

117 Publik.TUWien.ac.AT/files/PubDat_247739.pdf>}.

118 @kwarg name: Optional name for this interpolator (C{str}).

119 @kwarg units: Optional, the distance units (C{Unit} or C{str}).

120 @kwarg kwds: Optional keyword argument for distance function,

121 retrievable with property C{kwds}.

122

123 @raise HausdorffError: Insufficient number of B{C{point1s}}

124 or an invalid B{C{point1}}, B{C{seed}}

125 or B{C{units}}.

126 '''

127 _, self._model = self._points2(point1s)

128 if seed:

129 self.seed = seed

130 if name:

131 self.name = name

132 if units: # and not self.units:

133 self.units = units

134 if kwds:

135 self._kwds = kwds

136

137 @Property_RO

138 def adjust(self):

139 '''Get the adjust setting (C{bool} or C{None} if not applicable).

140 '''

141 return _xkwds_get(self._kwds, adjust=None)

142

143 @Property_RO

144 def datum(self):

145 '''Get the datum of this calculator (L{Datum} or C{None} if not applicable).

146 '''

147 return self._datum

148

149 def _datum_setter(self, datum):

150 '''(INTERNAL) Set the datum.

151 '''

152 d = datum or _xattr(self._model[0], datum=datum)

153 if d not in (None, self._datum): # PYCHOK no cover

154 self._datum = _ellipsoidal_datum(d, name=self.name)

155

156 def directed(self, point2s, early=True):

157 '''Compute only the C{forward Hausdorff} distance.

158

159 @arg point2s: Second set of points, aka the C{target} (C{LatLon}[],

160 C{Numpy2LatLon}[], C{Tuple2LatLon}[] or C{other}[]).

161 @kwarg early: Enable or disable U{early breaking<https://

162 Publik.TUWien.ac.AT/files/PubDat_247739.pdf>} (C{bool}).

163

164 @return: A L{Hausdorff6Tuple}C{(hd, i, j, mn, md, units)}.

165

166 @raise HausdorffError: Insufficient number of B{C{point2s}} or

167 an invalid B{C{point2}}.

168

169 @note: See B{C{point2s}} note at L{HausdorffDistanceTo}.

170 '''

171 return self._hausdorff_(point2s, False, early, self.distance)

172

173 def distance(self, point1, point2):

174 '''Return the distance between B{C{point1}} and B{C{point2s}},

175 subject to the supplied optional keyword arguments, see

176 property C{kwds}.

177 '''

178 return self._func(point1.lat, point1.lon,

179 point2.lat, point2.lon, **self._kwds)

180

181 def _hausdorff_(self, point2s, both, early, distance):

182 _, ps2 = self._points2(point2s)

183 return _hausdorff_(self._model, ps2, both, early, self.seed,

184 self.units, distance, self.point)

185

186 @property_RO

187 def kwds(self):

188 '''Get the supplied, optional keyword arguments (C{dict}).

189 '''

190 return self._kwds

191

192 def point(self, point):

193 '''Convert a C{model} or C{target} point for the C{.distance} method.

194 '''

195 return point # pass thru

196

197 def _points2(self, points):

198 '''(INTERNAL) Check a set of points.

199 '''

200 return _points2(points, closed=False, Error=HausdorffError)

201

202 @property_doc_(''' the random sampling seed (C{Random}).''')

203 def seed(self):

204 '''Get the random sampling seed (C{any} or C{None}).

205 '''

206 return self._seed

207

208 @seed.setter # PYCHOK setter!

209 def seed(self, seed):

210 '''Set the random sampling seed (C{Random(seed)}) or

211 C{None}, C{0} or C{False} for no U{random sampling

212 <https://Publik.TUWien.ac.AT/files/PubDat_247739.pdf>}.

213

214 @raise HausdorffError: Invalid B{C{seed}}.

215 '''

216 if seed:

217 try:

218 Random(seed)

219 except (TypeError, ValueError) as x:

220 raise HausdorffError(seed=seed, cause=x)

221 self._seed = seed

222 else:

223 self._seed = None

224

225 def symmetric(self, point2s, early=True):

226 '''Compute the combined C{forward and reverse Hausdorff} distance.

227

228 @arg point2s: Second set of points, aka the C{target} (C{LatLon}[],

229 C{Numpy2LatLon}[], C{Tuple2LatLon}[] or C{other}[]).

230 @kwarg early: Enable or disable U{early breaking<https://

231 Publik.TUWien.ac.AT/files/PubDat_247739.pdf>} (C{bool}).

232

233 @return: A L{Hausdorff6Tuple}C{(hd, i, j, mn, md, units)}.

234

235 @raise HausdorffError: Insufficient number of B{C{point2s}} or

236 an invalid B{C{point2}}.

237

238 @note: See B{C{point2s}} note at L{HausdorffDistanceTo}.

239 '''

240 return self._hausdorff_(point2s, True, early, self.distance)

241

242 @property_doc_(''' the distance units (C{Unit} or C{str}).''')

243 def units(self):

244 '''Get the distance units (C{Unit} or C{str}).

245 '''

246 return self._units

247

248 @units.setter # PYCHOK setter!

249 def units(self, units):

250 '''Set the distance units (C{Unit} or C{str}).

251

252 @raise TypeError: Invalid B{C{units}}.

253 '''

254 self._units = _xUnits(units, Base=Float)

255

256 @Property_RO

257 def wrap(self):

258 '''Get the wrap setting (C{bool} or C{None} if not applicable).

259 '''

260 return _xkwds_get(self._kwds, adjust=None)

261

262

263 class HausdorffDegrees(Hausdorff):

264 '''L{Hausdorff} base class for distances from C{LatLon}

265 points in C{degrees}.

266 '''

267 _units = _Str_degrees

268

269 if _FOR_DOCS:

270 __init__ = Hausdorff.__init__

271 directed = Hausdorff.directed

272 symmetric = Hausdorff.symmetric

273

274 def distance(self, point1, point2): # PYCHOK no cover

275 '''I{Must be overloaded} to return the distance between

276 B{C{point1}} and B{C{point2}} in C{degrees}.

277 '''

278 notOverloaded(self, point1, point2)

279

280

281 class HausdorffRadians(Hausdorff):

282 '''L{Hausdorff} base class for distances from C{LatLon}

283 points converted from C{degrees} to C{radians}.

284 '''

285 _units = _Str_radians

286

287 if _FOR_DOCS:

288 __init__ = Hausdorff.__init__

289 directed = Hausdorff.directed

290 symmetric = Hausdorff.symmetric

291

292 def distance(self, point1, point2): # PYCHOK no cover

293 '''I{Must be overloaded} to return the distance between

294 B{C{point1}} and B{C{point2}} in C{radians}.

295 '''

296 notOverloaded(self, point1, point2)

297

298 def point(self, point):

299 '''Return B{C{point}} as L{PhiLam2Tuple} to maintain

300 I{backward compatibility} of L{HausdorffRadians}.

301

302 @return: A L{PhiLam2Tuple}C{(phi, lam)}.

303 '''

304 try:

305 return point.philam

306 except AttributeError:

307 return PhiLam2Tuple(radians(point.lat), radians(point.lon))

308

309

310 class _HausdorffMeterRadians(Hausdorff):

311 '''(INTERNAL) Returning C{meter} or C{radians} depending on

312 the optional keyword arguments supplied at instantiation

313 of the C{Hausdorff*} sub-class.

314 '''

315 _units = _Str_meter

316 _units_ = _Str_radians

317

318 def directed(self, point2s, early=True):

319 '''Overloaded method L{Hausdorff.directed} to determine

320 the distance function and units from the optional

321 keyword arguments given at this instantiation, see

322 property C{kwds}.

323

324 @see: L{Hausdorff.directed} for other details.

325 '''

326 return self._hausdorff_(point2s, False, early, _formy._radistance(self))

327

328 def symmetric(self, point2s, early=True):

329 '''Overloaded method L{Hausdorff.symmetric} to determine

330 the distance function and units from the optional

331 keyword arguments given at this instantiation, see

332 property C{kwds}.

333

334 @see: L{Hausdorff.symmetric} for other details.

335 '''

336 return self._hausdorff_(point2s, True, early, _formy._radistance(self))

337

338 def _func_(self, *args, **kwds): # PYCHOK no cover

339 notOverloaded(self, *args, **kwds)

340

341

342 class HausdorffCosineAndoyerLambert(_HausdorffMeterRadians):

343 '''Compute the C{Hausdorff} distance based on the I{angular} distance

344 in C{radians} from function L{pygeodesy.cosineAndoyerLambert}.

345 '''

346 def __init__(self, point1s, seed=None, name=NN, **datum_wrap):

347 '''New L{HausdorffCosineAndoyerLambert} calculator.

348

349 @see: L{Hausdorff.__init__} for details about B{C{point1s}},

350 B{C{seed}}, B{C{name}} and other exceptions.

351

352 @kwarg datum_wrap: Optional keyword arguments for function

353 L{pygeodesy.cosineAndoyerLambert}.

354 '''

355 Hausdorff.__init__(self, point1s, seed=seed, name=name,

356 **datum_wrap)

357 self._func = _formy.cosineAndoyerLambert

358 self._func_ = _formy.cosineAndoyerLambert_

359

360 if _FOR_DOCS:

361 directed = Hausdorff.directed

362 symmetric = Hausdorff.symmetric

363

364

365 class HausdorffCosineForsytheAndoyerLambert(_HausdorffMeterRadians):

366 '''Compute the C{Hausdorff} distance based on the I{angular} distance

367 in C{radians} from function L{pygeodesy.cosineForsytheAndoyerLambert}.

368 '''

369 def __init__(self, point1s, seed=None, name=NN, **datum_wrap):

370 '''New L{HausdorffCosineForsytheAndoyerLambert} calculator.

371

372 @see: L{Hausdorff.__init__} for details about B{C{point1s}},

373 B{C{seed}}, B{C{name}} and other exceptions.

374

375 @kwarg datum_wrap: Optional keyword arguments for function

376 L{pygeodesy.cosineAndoyerLambert}.

377 '''

378 Hausdorff.__init__(self, point1s, seed=seed, name=name,

379 **datum_wrap)

380 self._func = _formy.cosineForsytheAndoyerLambert

381 self._func_ = _formy.cosineForsytheAndoyerLambert_

382

383 if _FOR_DOCS:

384 directed = Hausdorff.directed

385 symmetric = Hausdorff.symmetric

386

387

388 class HausdorffCosineLaw(_HausdorffMeterRadians):

389 '''Compute the C{Hausdorff} distance based on the I{angular}

390 distance in C{radians} from function L{pygeodesy.cosineLaw_}.

391

392 @note: See note at function L{pygeodesy.vincentys_}.

393 '''

394 def __init__(self, point1s, seed=None, name=NN, **radius_wrap):

395 '''New L{HausdorffCosineLaw} calculator.

396

397 @kwarg radius_wrap: Optional keyword arguments for function

398 L{pygeodesy.cosineLaw}.

399

400 @see: L{Hausdorff.__init__} for details about B{C{point1s}},

401 B{C{seed}}, B{C{name}} and other exceptions.

402 '''

403 Hausdorff.__init__(self, point1s, seed=seed, name=name,

404 **radius_wrap)

405 self._func = _formy.cosineLaw

406 self._func_ = _formy.cosineLaw_

407

408 if _FOR_DOCS:

409 directed = Hausdorff.directed

410 symmetric = Hausdorff.symmetric

411

412

413 class HausdorffDistanceTo(Hausdorff):

414 '''Compute the C{Hausdorff} distance based on the distance from the

415 points' C{LatLon.distanceTo} method, conventionally in C{meter}.

416 '''

417 _units = _Str_meter

418

419 def __init__(self, point1s, seed=None, name=NN, **distanceTo_kwds):

420 '''New L{HausdorffDistanceTo} calculator.

421

422 @kwarg distanceTo_kwds: Optional keyword arguments for each

423 B{C{point1s}}' C{LatLon.distanceTo}

424 method.

425

426 @see: L{Hausdorff.__init__} for details about B{C{point1s}},

427 B{C{seed}}, B{C{name}} and other exceptions.

428

429 @note: All C{model}, C{template} and C{target} B{C{points}}

430 I{must} be instances of the same ellipsoidal or

431 spherical C{LatLon} class.

432 '''

433 Hausdorff.__init__(self, point1s, seed=seed, name=name,

434 **distanceTo_kwds)

435

436 if _FOR_DOCS:

437 directed = Hausdorff.directed

438 symmetric = Hausdorff.symmetric

439

440 def distance(self, p1, p2):

441 '''Return the distance in C{meter}.

442 '''

443 return p1.distanceTo(p2, **self._kwds)

444

445 def _points2(self, points):

446 '''(INTERNAL) Check a set of points.

447 '''

448 np, ps = Hausdorff._points2(self, points)

449 return np, _distanceTo(HausdorffError, points=ps)

450

451

452 class HausdorffEquirectangular(Hausdorff):

453 '''Compute the C{Hausdorff} distance based on the C{equirectangular} distance

454 in C{radians squared} like function L{pygeodesy.equirectangular_}.

455 '''

456 _units = _Str_degrees2

457

458 def __init__(self, point1s, seed=None, name=NN, **adjust_limit_wrap):

459 '''New L{HausdorffEquirectangular} calculator.

460

461 @kwarg adjust_limit_wrap: Optional keyword arguments for function

462 L{pygeodesy.equirectangular_} I{with default}

463 C{B{limit}=0} for I{backward compatibility}.

464

465 @see: L{Hausdorff.__init__} for details about B{C{point1s}},

466 B{C{seed}}, B{C{name}} and other exceptions.

467 '''

468 adjust_limit_wrap = _xkwds(adjust_limit_wrap, limit=0)

469 Hausdorff.__init__(self, point1s, seed=seed, name=name,

470 **adjust_limit_wrap)

471 self._func = _formy._equirectangular # helper

472

473 if _FOR_DOCS:

474 directed = Hausdorff.directed

475 symmetric = Hausdorff.symmetric

476

477

478 class HausdorffEuclidean(_HausdorffMeterRadians):

479 '''Compute the C{Hausdorff} distance based on the C{Euclidean}

480 distance in C{radians} from function L{pygeodesy.euclidean_}.

481 '''

482 def __init__(self, point1s, seed=None, name=NN, **adjust_wrap):

483 '''New L{HausdorffEuclidean} calculator.

484

485 @kwarg adjust_radius_wrap: Optional keyword arguments for

486 function L{pygeodesy.euclidean}.

487

488 @see: L{Hausdorff.__init__} for details about B{C{point1s}},

489 B{C{seed}}, B{C{name}} and other exceptions.

490 '''

491 Hausdorff.__init__(self, point1s, seed=seed, name=name,

492 **adjust_wrap)

493 self._func = _formy.euclidean

494 self._func_ = _formy.euclidean_

495

496 if _FOR_DOCS:

497 directed = Hausdorff.directed

498 symmetric = Hausdorff.symmetric

499

500

501 class HausdorffExact(Hausdorff):

502 '''Compute the C{Hausdorff} distance based on the I{angular}

503 distance in C{degrees} from method L{GeodesicExact}C{.Inverse}.

504 '''

505 _units = _Str_degrees

506

507 def __init__(self, point1s, seed=None, name=NN, datum=None, **wrap):

508 '''New L{HausdorffKarney} calculator.

509

510 @kwarg datum: Datum to override the default C{Datums.WGS84} and

511 first B{C{point1s}}' datum (L{Datum}, L{Ellipsoid},

512 L{Ellipsoid2} or L{a_f2Tuple}).

513 @kwarg wrap: Optional keyword argument for method C{Inverse1}

514 of class L{geodesicx.GeodesicExact}.

515

516 @see: L{Hausdorff.__init__} for details about B{C{point1s}},

517 B{C{seed}}, B{C{name}} and other exceptions.

518

519 @raise TypeError: Invalid B{C{datum}}.

520 '''

521 Hausdorff.__init__(self, point1s, seed=seed, name=name,

522 **wrap)

523 self._datum_setter(datum)

524 self._func = self.datum.ellipsoid.geodesicx.Inverse1 # note -x

525

526 if _FOR_DOCS:

527 directed = Hausdorff.directed

528 symmetric = Hausdorff.symmetric

529

530

531 class HausdorffFlatLocal(_HausdorffMeterRadians):

532 '''Compute the C{Hausdorff} distance based on the I{angular} distance in

533 C{radians squared} like function L{pygeodesy.flatLocal_}/L{pygeodesy.hubeny_}.

534 '''

535 _units = _Str_radians2

536

537 def __init__(self, point1s, seed=None, name=NN, **datum_scaled_wrap):

538 '''New L{HausdorffFlatLocal}/L{HausdorffHubeny} calculator.

539

540 @kwarg datum_scaled_wrap: Optional keyword arguments for

541 function L{pygeodesy.flatLocal}.

542

543 @see: L{Hausdorff.__init__} for details about B{C{point1s}},

544 B{C{seed}}, B{C{name}} and other exceptions.

545

546 @note: The distance C{units} are C{radians squared}, not C{radians}.

547 '''

548 Hausdorff.__init__(self, point1s, seed=seed, name=name,

549 **datum_scaled_wrap)

550 self._func = _formy.flatLocal

551 self._func_ = self.datum.ellipsoid._hubeny_2

552

553 if _FOR_DOCS:

554 directed = Hausdorff.directed

555 symmetric = Hausdorff.symmetric

556

557

558 class HausdorffFlatPolar(_HausdorffMeterRadians):

559 '''Compute the C{Hausdorff} distance based on the I{angular}

560 distance in C{radians} from function L{pygeodesy.flatPolar_}.

561 '''

562 _wrap = False

563

564 def __init__(self, points, seed=None, name=NN, **radius_wrap):

565 '''New L{HausdorffFlatPolar} calculator.

566

567 @kwarg radius_wrap: Optional keyword arguments for function

568 L{pygeodesy.flatPolar}.

569

570 @see: L{Hausdorff.__init__} for details about B{C{point1s}},

571 B{C{seed}}, B{C{name}} and other exceptions.

572 '''

573 Hausdorff.__init__(self, points, seed=seed, name=name,

574 **radius_wrap)

575 self._func = _formy.flatPolar

576 self._func_ = _formy.flatPolar_

577

578 if _FOR_DOCS:

579 directed = Hausdorff.directed

580 symmetric = Hausdorff.symmetric

581

582

583 class HausdorffHaversine(_HausdorffMeterRadians):

584 '''Compute the C{Hausdorff} distance based on the I{angular}

585 distance in C{radians} from function L{pygeodesy.haversine_}.

586

587 @note: See note under L{HausdorffVincentys}.

588 '''

589 _wrap = False

590

591 def __init__(self, points, seed=None, name=NN, **radius_wrap):

592 '''New L{HausdorffHaversine} calculator.

593

594 @kwarg radius_wrap: Optional keyword arguments for function

595 L{pygeodesy.haversine}.

596

597 @see: L{Hausdorff.__init__} for details about B{C{point1s}},

598 B{C{seed}}, B{C{name}} and other exceptions.

599 '''

600 Hausdorff.__init__(self, points, seed=seed, name=name,

601 **radius_wrap)

602 self._func = _formy.haversine

603 self._func_ = _formy.haversine_

604

605 if _FOR_DOCS:

606 directed = Hausdorff.directed

607 symmetric = Hausdorff.symmetric

608

609

610 class HausdorffHubeny(HausdorffFlatLocal): # for Karl Hubeny

611 if _FOR_DOCS:

612 __doc__ = HausdorffFlatLocal.__doc__

613 __init__ = HausdorffFlatLocal.__init__

614 directed = HausdorffFlatLocal.directed

615 distance = HausdorffFlatLocal.distance

616 symmetric = HausdorffFlatLocal.symmetric

617

618

619 class HausdorffKarney(Hausdorff):

620 '''Compute the C{Hausdorff} distance based on the I{angular}

621 distance in C{degrees} from I{Karney}'s U{geographiclib

622 <https://PyPI.org/project/geographiclib>} U{Geodesic

623 <https://GeographicLib.SourceForge.io/Python/doc/code.html>}

624 Inverse method.

625 '''

626 _units = _Str_degrees

627

628 def __init__(self, point1s, datum=None, seed=None, name=NN, **wrap):

629 '''New L{HausdorffKarney} calculator.

630

631 @kwarg datum: Datum to override the default C{Datums.WGS84} and

632 first B{C{knots}}' datum (L{Datum}, L{Ellipsoid},

633 L{Ellipsoid2} or L{a_f2Tuple}).

634 @kwarg wrap: Optional keyword argument for method C{Inverse1}

635 of class L{geodesicw.Geodesic}.

636

637 @raise ImportError: Package U{geographiclib

638 <https://PyPI.org/project/geographiclib>} missing.

639

640 @raise TypeError: Invalid B{C{datum}}.

641

642 @see: L{Hausdorff.__init__} for details about B{C{point1s}},

643 B{C{seed}}, B{C{name}} and other exceptions.

644 '''

645 Hausdorff.__init__(self, point1s, seed=seed, name=name,

646 **wrap)

647 self._datum_setter(datum)

648 self._func = self.datum.ellipsoid.geodesic.Inverse1

649

650

651 class HausdorffThomas(_HausdorffMeterRadians):

652 '''Compute the C{Hausdorff} distance based on the I{angular}

653 distance in C{radians} from function L{pygeodesy.thomas_}.

654 '''

655 def __init__(self, point1s, seed=None, name=NN, **datum_wrap):

656 '''New L{HausdorffThomas} calculator.

657

658 @kwarg datum_wrap: Optional keyword argument for function

659 L{pygeodesy.thomas}.

660

661 @see: L{Hausdorff.__init__} for details about B{C{point1s}},

662 B{C{seed}}, B{C{name}} and other exceptions.

663 '''

664 Hausdorff.__init__(self, point1s, seed=seed, name=name,

665 **datum_wrap)

666 self._func = _formy.thomas

667 self._func_ = _formy.thomas_

668

669 if _FOR_DOCS:

670 directed = Hausdorff.directed

671 symmetric = Hausdorff.symmetric

672

673

674 class HausdorffVincentys(_HausdorffMeterRadians):

675 '''Compute the C{Hausdorff} distance based on the I{angular}

676 distance in C{radians} from function L{pygeodesy.vincentys_}.

677

678 @note: See note at function L{pygeodesy.vincentys_}.

679 '''

680 _wrap = False

681

682 def __init__(self, point1s, seed=None, name=NN, **radius_wrap):

683 '''New L{HausdorffVincentys} calculator.

684

685 @kwarg radius_wrap: Optional keyword arguments for function

686 L{pygeodesy.vincentys}.

687

688 @see: L{Hausdorff.__init__} for details about B{C{point1s}},

689 B{C{seed}}, B{C{name}} and other exceptions.

690 '''

691 Hausdorff.__init__(self, point1s, seed=seed, name=name,

692 **radius_wrap)

693 self._func = _formy.vincentys

694 self._func_ = _formy.vincentys_

695

696 if _FOR_DOCS:

697 directed = Hausdorff.directed

698 symmetric = Hausdorff.symmetric

699

700

701 def _hausdorff_(ps1, ps2, both, early, seed, units, distance, point):

702 '''(INTERNAL) Core of function L{hausdorff_} and methods C{directed}

703 and C{symmetric} of classes C{hausdorff.Hausdorff...}.

704 '''

705 # shuffling the points generally increases the

706 # chance of an early break in the inner j loop

707 rr = randomrangenerator(seed) if seed else range

708

709 hd = NINF

710 hi = hj = m = mn = 0

711 md = _0_0

712

713 # forward or forward and backward

714 for fb in range(2 if both else 1):

715 n = len(ps2)

716 for i in rr(len(ps1)):

717 p1 = point(ps1[i])

718 dh, dj = INF, 0

719 for j in rr(n):

720 p2 = point(ps2[j])

721 d = distance(p1, p2)

722 if early and d < hd:

723 break # early

724 elif d < dh:

725 dh, dj = d, j

726 else: # no early break

727 if hd < dh:

728 hd = dh

729 if fb:

730 hi, hj = dj, i

731 else:

732 hi, hj = i, dj

733 md += dh

734 mn += 1

735 m += 1

736 # swap model and target

737 ps1, ps2 = ps2, ps1

738

739 md = None if mn < m else (md / float(m))

740 return Hausdorff6Tuple(hd, hi, hj, m, md, units)

741

742

743 def _point(p):

744 '''Default B{C{point}} callable for function L{hausdorff_}.

745

746 @arg p: The original C{model} or C{target} point (C{any}).

747

748 @return: The point, suitable for the L{hausdorff_}

749 B{C{distance}} callable.

750 '''

751 return p

752

753

754 def hausdorff_(model, target, both=False, early=True, seed=None, units=NN,

755 distance=None, point=_point):

756 '''Compute the C{directed} or C{symmetric} U{Hausdorff

757 <https://WikiPedia.org/wiki/Hausdorff_distance>} distance between 2 sets of points

758 with or without U{early breaking<https://Publik.TUWien.ac.AT/files/PubDat_247739.pdf>}

759 and U{random sampling<https://Publik.TUWien.ac.AT/files/PubDat_247739.pdf>}.

760

761 @arg model: First set of points (C{LatLon}[], C{Numpy2LatLon}[],

762 C{Tuple2LatLon}[] or C{other}[]).

763 @arg target: Second set of points (C{LatLon}[], C{Numpy2LatLon}[],

764 C{Tuple2LatLon}[] or C{other}[]).

765 @kwarg both: Return the C{directed} (forward only) or the C{symmetric}

766 (combined forward and reverse) C{Hausdorff} distance (C{bool}).

767 @kwarg early: Enable or disable U{early breaking<https://Publik.TUWien.ac.AT/

768 files/PubDat_247739.pdf>} (C{bool}).

769 @kwarg seed: Random sampling seed (C{any}) or C{None}, C{0} or C{False} for no

770 U{random sampling<https://Publik.TUWien.ac.AT/files/PubDat_247739.pdf>}.

771 @kwarg units: Optional, the distance units (C{Unit} or C{str}).

772 @kwarg distance: Callable returning the distance between a B{C{model}}

773 and B{C{target}} point (signature C{(point1, point2)}).

774 @kwarg point: Callable returning the B{C{model}} or B{C{target}} point

775 suitable for B{C{distance}} (signature C{(point)}).

776

777 @return: A L{Hausdorff6Tuple}C{(hd, i, j, mn, md, units)}.

778

779 @raise HausdorffError: Insufficient number of B{C{model}} or B{C{target}} points.

780

781 @raise TypeError: If B{C{distance}} or B{C{point}} is not callable.

782 '''

783 if not callable(distance):

784 raise _IsnotError(callable.__name__, distance=distance)

785 if not callable(point):

786 raise _IsnotError(callable.__name__, point=point)

787

788 _, ps1 = _points2(model, closed=False, Error=HausdorffError) # PYCHOK non-sequence

789 _, ps2 = _points2(target, closed=False, Error=HausdorffError) # PYCHOK non-sequence

790 return _hausdorff_(ps1, ps2, both, early, seed, units, distance, point)

791

792

793 class Hausdorff6Tuple(_NamedTuple):

794 '''6-Tuple C{(hd, i, j, mn, md, units)} with the U{Hausdorff

795 <https://WikiPedia.org/wiki/Hausdorff_distance>} distance C{hd},

796 indices C{i} and C{j}, the total count C{mn}, the C{I{mean}

797 Hausdorff} distance C{md} and the class or name of both distance

798 C{units}.

799

800 For C{directed Hausdorff} distances, count C{mn} is the number

801 of model points considered. For C{symmetric Hausdorff} distances

802 count C{mn} twice that.

803

804 Indices C{i} and C{j} are the C{model} respectively C{target}

805 point with the C{hd} distance.

806

807 Mean distance C{md} is C{None} if an C{early break} occurred and

808 U{early breaking<https://Publik.TUWien.ac.AT/files/PubDat_247739.pdf>}

809 was enabled by keyword argument C{early=True}.

810 '''

811 _Names_ = ('hd', _i_, _j_, 'mn', 'md', _units_)

812 _Units_ = (_Pass, Number_, Number_, Number_, _Pass, _Pass)

813

814 def toUnits(self, **Error): # PYCHOK expected

815 '''Overloaded C{_NamedTuple.toUnits} for C{hd} and C{md} units.

816 '''

817 U = _xUnit(self.units, Float) # PYCHOK expected

818 M = _Pass if self.md is None else U # PYCHOK expected

819 self._Units_ = (U,) + Hausdorff6Tuple._Units_[1:4] \

820 + (M,) + Hausdorff6Tuple._Units_[5:]

821 return _NamedTuple.toUnits(self, **Error)

822

823

824 def randomrangenerator(seed):

825 '''Return a C{seed}ed random range function generator.

826

827 @arg seed: Initial, internal L{Random} state (C{hashable}

828 or C{None}).

829

830 @note: L{Random} with C{B{seed} is None} seeds from the

831 current time or from a platform-specific randomness

832 source, if available.

833

834 @return: A function to generate random ranges.

835

836 @example:

837

838 >>> rrange = randomrangenerator('R')

839 >>> for r in rrange(n):

840 >>> ... # r is random in 0..n-1

841 '''

842 R = Random(seed)

843

844 def _range(n, *stop_step):

845 '''Like standard L{range}C{start, stop=..., step=...)},

846 except the returned values are in random order.

847

848 @note: Especially C{range(n)} behaves like standard

849 L{Random.sample}C{(range(n), n)} but avoids

850 creating a tuple with the entire C{population}

851 and a list containing all sample values (for

852 large C{n}).

853 '''

854 if stop_step:

855 s = range(n, *stop_step)

856

857 elif n > 32:

858 r = R.randrange # Random._randbelow

859 s = set()

860 for _ in range(n - 32):

861 i = r(n)

862 while i in s:

863 i = r(n)

864 s.add(i)

865 yield i

866 s = set(range(n)) - s # [i for i in range(n) if i not in s]

867 else:

868 s = range(n)

869

870 s = list(s)

871 R.shuffle(s)

872 while s:

873 yield s.pop(0)

874

875 return _range

876

877# **) MIT License

878#

879# Copyright (C) 2016-2023 -- mrJean1 at Gmail -- All Rights Reserved.

880#

881# Permission is hereby granted, free of charge, to any person obtaining a

882# copy of this software and associated documentation files (the "Software"),

883# to deal in the Software without restriction, including without limitation

884# the rights to use, copy, modify, merge, publish, distribute, sublicense,

885# and/or sell copies of the Software, and to permit persons to whom the

886# Software is furnished to do so, subject to the following conditions:

887#

888# The above copyright notice and this permission notice shall be included

889# in all copies or substantial portions of the Software.

890#

891# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS

892# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,

893# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL

894# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR

895# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,

896# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR

897# OTHER DEALINGS IN THE SOFTWARE.