From 7617891faf929bf9ada45e7efad5fd85fcd61465 Mon Sep 17 00:00:00 2001 From: SNoiraud Date: Fri, 23 Aug 2019 19:32:05 +0200 Subject: [PATCH 1/3] Could not convert string to float by using withinarea filter rule + difference between sidebar filter and filter rule + some pylint improvements --- gramps/gen/filters/rules/place/_withinarea.py | 38 +++++++++++++------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/gramps/gen/filters/rules/place/_withinarea.py b/gramps/gen/filters/rules/place/_withinarea.py index 62f76164e..2e3ea4b35 100644 --- a/gramps/gen/filters/rules/place/_withinarea.py +++ b/gramps/gen/filters/rules/place/_withinarea.py @@ -1,3 +1,6 @@ +""" + WithinArea : used to verify if a place is contained in a specific area +""" # # Gramps - a GTK+/GNOME based genealogy program # @@ -25,17 +28,19 @@ # Standard Python modules # #------------------------------------------------------------------------- + from math import pi, cos, hypot -from ....const import GRAMPS_LOCALE as glocale -_ = glocale.translation.sgettext #------------------------------------------------------------------------- # # Gramps modules # #------------------------------------------------------------------------- +from ....const import GRAMPS_LOCALE as glocale from .. import Rule -from ....utils.location import located_in +from ....utils.place import conv_lat_lon + +_ = glocale.translation.sgettext #------------------------------------------------------------------------- # @@ -51,6 +56,10 @@ class WithinArea(Rule): name = _('Places within an area') description = _('Matches places within a given distance of another place') category = _('Position filters') + handle = None + radius = None + latitude = None + longitude = None def prepare(self, db, user): ref_place = db.get_place_from_gramps_id(self.list[0]) @@ -60,13 +69,16 @@ class WithinArea(Rule): self.longitude = None if ref_place: self.handle = ref_place.handle - self.latitude = ref_place.get_latitude() - if self.latitude == "": - self.latitude = None + latitude = ref_place.get_latitude() + if latitude == "": + latitude = None return - self.longitude = ref_place.get_longitude() - value = self.list[1] - unit = self.list[2] + longitude = ref_place.get_longitude() + self.latitude, self.longitude = conv_lat_lon(latitude, + longitude, + "D.D8") + value = int(self.list[1]) + unit = int(self.list[2]) # earth perimeter in kilometers for latitude # 2 * pi * (6371 * cos(latitude/180*pi)) # so 1 degree correspond to the result above / 360 @@ -79,7 +91,7 @@ class WithinArea(Rule): self.radius = float(value) self.radius = self.radius/2 - def apply(self, db, place): + def apply(self, dummy_db, place): if self.handle is None: return False if self.latitude is None: @@ -90,7 +102,9 @@ class WithinArea(Rule): lat = place.get_latitude() lon = place.get_longitude() if lat and lon: - if (hypot(float(self.latitude)-float(lat), - float(self.longitude)-float(lon)) <= self.radius) == True: + latit, longit = conv_lat_lon(lat, lon, "D.D8") + if (hypot(float(self.latitude)-float(latit), + float(self.longitude)-float(longit)) + <= self.radius): return True return False From d397b83dc88a515420665b178e9260b592ee905a Mon Sep 17 00:00:00 2001 From: SNoiraud Date: Wed, 28 Aug 2019 15:50:02 +0200 Subject: [PATCH 2/3] Avoid alphabetic characters in filter rules --- gramps/gen/filters/rules/place/_withinarea.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/gramps/gen/filters/rules/place/_withinarea.py b/gramps/gen/filters/rules/place/_withinarea.py index 2e3ea4b35..31358d426 100644 --- a/gramps/gen/filters/rules/place/_withinarea.py +++ b/gramps/gen/filters/rules/place/_withinarea.py @@ -30,6 +30,7 @@ #------------------------------------------------------------------------- from math import pi, cos, hypot +import re #------------------------------------------------------------------------- # @@ -77,7 +78,10 @@ class WithinArea(Rule): self.latitude, self.longitude = conv_lat_lon(latitude, longitude, "D.D8") - value = int(self.list[1]) + val = self.list[1] + if isinstance(val, str): + val = re.sub("\D", "", val) # suppress all alpha characters + value = int(val) unit = int(self.list[2]) # earth perimeter in kilometers for latitude # 2 * pi * (6371 * cos(latitude/180*pi)) From 5767b9c3c857d7f8da4e044303a8d16358a935ab Mon Sep 17 00:00:00 2001 From: SNoiraud Date: Sun, 1 Sep 2019 10:30:44 +0200 Subject: [PATCH 3/3] Avoid bad coordinates in the ref place --- gramps/gen/filters/rules/place/_withinarea.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/gramps/gen/filters/rules/place/_withinarea.py b/gramps/gen/filters/rules/place/_withinarea.py index 31358d426..37c0f0b1c 100644 --- a/gramps/gen/filters/rules/place/_withinarea.py +++ b/gramps/gen/filters/rules/place/_withinarea.py @@ -40,6 +40,7 @@ import re from ....const import GRAMPS_LOCALE as glocale from .. import Rule from ....utils.place import conv_lat_lon +from gramps.gen.errors import FilterError _ = glocale.translation.sgettext @@ -78,6 +79,12 @@ class WithinArea(Rule): self.latitude, self.longitude = conv_lat_lon(latitude, longitude, "D.D8") + if self.latitude is None or self.longitude is None: + raise FilterError(_("Cannot use the filter 'within area'"), + _("The place you selected contains bad coordinates. " + "Please, run the tool 'clean input data'")) + return + val = self.list[1] if isinstance(val, str): val = re.sub("\D", "", val) # suppress all alpha characters @@ -107,6 +114,8 @@ class WithinArea(Rule): lon = place.get_longitude() if lat and lon: latit, longit = conv_lat_lon(lat, lon, "D.D8") + if latit is None or longit is None: + return False if (hypot(float(self.latitude)-float(latit), float(self.longitude)-float(longit)) <= self.radius):