jenkins-bot has submitted this change and it was merged.
Change subject: Implement Coordinate.precisionToDim
......................................................................
Implement Coordinate.precisionToDim
Enable precision to dim conversion by implementing Coordinate.precisionToDim
Add corresponding tests in wikibase_tests
Bug: T89670
Change-Id: I2f1432603218f0ede2b0f703ef3c5046928440b3
---
M pywikibot/__init__.py
M tests/wikibase_tests.py
2 files changed, 43 insertions(+), 2 deletions(-)
Approvals:
John Vandenberg: Looks good to me, approved
jenkins-bot: Verified
diff --git a/pywikibot/__init__.py b/pywikibot/__init__.py
index 4fe6801..f9e750c 100644
--- a/pywikibot/__init__.py
+++ b/pywikibot/__init__.py
@@ -354,6 +354,8 @@
@rtype: float or None
"""
+ if self._dim is None and self._precision is None:
+ raise ValueError('No values set for dim or precision')
if self._precision is None and self._dim is not None:
radius = 6378137 # TODO: Support other globes
self._precision = math.degrees(
@@ -365,8 +367,32 @@
self._precision = value
def precisionToDim(self):
- """Convert precision from Wikibase to GeoData's
dim."""
- raise NotImplementedError
+ """Convert precision from Wikibase to GeoData's dim and return
the latter.
+
+ dim is calculated if the Coordinate doesn't have a dimension, and precision
is set.
+ When neither dim nor precision are set, ValueError is thrown.
+
+ Carrying on from the earlier derivation of precision, since
+ precision = math.degrees(dim/(radius*math.cos(math.radians(self.lat)))), we get
+ dim = math.radians(precision)*radius*math.cos(math.radians(self.lat))
+ But this is not valid, since it returns a float value for dim which is an
integer.
+ We must round it off to the nearest integer.
+
+ Therefore::
+ dim =
int(round(math.radians(precision)*radius*math.cos(math.radians(self.lat))))
+
+ @rtype: int or None
+ """
+ if self._dim is None and self._precision is None:
+ raise ValueError('No values set for dim or precision')
+ if self._dim is None and self._precision is not None:
+ radius = 6378137
+ self._dim = int(
+ round(
+ math.radians(self._precision) * radius *
math.cos(math.radians(self.lat))
+ )
+ )
+ return self._dim
class WbTime(_WbRepresentation):
diff --git a/tests/wikibase_tests.py b/tests/wikibase_tests.py
index ed3cd7e..a0d8265 100644
--- a/tests/wikibase_tests.py
+++ b/tests/wikibase_tests.py
@@ -144,6 +144,21 @@
dry = True
+ def test_Coordinate_dim(self):
+ """Test Coordinate."""
+ repo = self.get_repo()
+ x = pywikibot.Coordinate(site=repo, lat=12.0, lon=13.0, precision=5.0)
+ self.assertEqual(x.precisionToDim(), 544434)
+ self.assertIsInstance(x.precisionToDim(), int)
+ y = pywikibot.Coordinate(site=repo, lat=12.0, lon=13.0, dim=54444)
+ self.assertEqual(y.precision, 0.500005084017101)
+ self.assertIsInstance(y.precision, float)
+ z = pywikibot.Coordinate(site=repo, lat=12.0, lon=13.0)
+ with self.assertRaises(ValueError):
+ z.precision
+ with self.assertRaises(ValueError):
+ z.precisionToDim()
+
def test_WbTime(self):
"""Test WbTime."""
repo = self.get_repo()
--
To view, visit
https://gerrit.wikimedia.org/r/275112
To unsubscribe, visit
https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I2f1432603218f0ede2b0f703ef3c5046928440b3
Gerrit-PatchSet: 15
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Darthbhyrava <hbhyrava(a)gmail.com>
Gerrit-Reviewer: Darthbhyrava <hbhyrava(a)gmail.com>
Gerrit-Reviewer: John Vandenberg <jayvdb(a)gmail.com>
Gerrit-Reviewer: Merlijn van Deen <valhallasw(a)arctus.nl>
Gerrit-Reviewer: Mpaa <mpaa.wiki(a)gmail.com>
Gerrit-Reviewer: Polybuildr <v.a.ghaisas(a)gmail.com>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: jenkins-bot <>