Merge pull request #874 from SNoiraud:B11269
This commit is contained in:
commit
d75d9797c0
@ -1,3 +1,6 @@
|
|||||||
|
"""
|
||||||
|
WithinArea : used to verify if a place is contained in a specific area
|
||||||
|
"""
|
||||||
#
|
#
|
||||||
# Gramps - a GTK+/GNOME based genealogy program
|
# Gramps - a GTK+/GNOME based genealogy program
|
||||||
#
|
#
|
||||||
@ -25,17 +28,21 @@
|
|||||||
# Standard Python modules
|
# Standard Python modules
|
||||||
#
|
#
|
||||||
#-------------------------------------------------------------------------
|
#-------------------------------------------------------------------------
|
||||||
|
|
||||||
from math import pi, cos, hypot
|
from math import pi, cos, hypot
|
||||||
from ....const import GRAMPS_LOCALE as glocale
|
import re
|
||||||
_ = glocale.translation.sgettext
|
|
||||||
|
|
||||||
#-------------------------------------------------------------------------
|
#-------------------------------------------------------------------------
|
||||||
#
|
#
|
||||||
# Gramps modules
|
# Gramps modules
|
||||||
#
|
#
|
||||||
#-------------------------------------------------------------------------
|
#-------------------------------------------------------------------------
|
||||||
|
from ....const import GRAMPS_LOCALE as glocale
|
||||||
from .. import Rule
|
from .. import Rule
|
||||||
from ....utils.location import located_in
|
from ....utils.place import conv_lat_lon
|
||||||
|
from gramps.gen.errors import FilterError
|
||||||
|
|
||||||
|
_ = glocale.translation.sgettext
|
||||||
|
|
||||||
#-------------------------------------------------------------------------
|
#-------------------------------------------------------------------------
|
||||||
#
|
#
|
||||||
@ -51,6 +58,10 @@ class WithinArea(Rule):
|
|||||||
name = _('Places within an area')
|
name = _('Places within an area')
|
||||||
description = _('Matches places within a given distance of another place')
|
description = _('Matches places within a given distance of another place')
|
||||||
category = _('Position filters')
|
category = _('Position filters')
|
||||||
|
handle = None
|
||||||
|
radius = None
|
||||||
|
latitude = None
|
||||||
|
longitude = None
|
||||||
|
|
||||||
def prepare(self, db, user):
|
def prepare(self, db, user):
|
||||||
ref_place = db.get_place_from_gramps_id(self.list[0])
|
ref_place = db.get_place_from_gramps_id(self.list[0])
|
||||||
@ -60,13 +71,25 @@ class WithinArea(Rule):
|
|||||||
self.longitude = None
|
self.longitude = None
|
||||||
if ref_place:
|
if ref_place:
|
||||||
self.handle = ref_place.handle
|
self.handle = ref_place.handle
|
||||||
self.latitude = ref_place.get_latitude()
|
latitude = ref_place.get_latitude()
|
||||||
if self.latitude == "":
|
if latitude == "":
|
||||||
self.latitude = None
|
latitude = None
|
||||||
return
|
return
|
||||||
self.longitude = ref_place.get_longitude()
|
longitude = ref_place.get_longitude()
|
||||||
value = self.list[1]
|
self.latitude, self.longitude = conv_lat_lon(latitude,
|
||||||
unit = self.list[2]
|
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
|
||||||
|
value = int(val)
|
||||||
|
unit = int(self.list[2])
|
||||||
# earth perimeter in kilometers for latitude
|
# earth perimeter in kilometers for latitude
|
||||||
# 2 * pi * (6371 * cos(latitude/180*pi))
|
# 2 * pi * (6371 * cos(latitude/180*pi))
|
||||||
# so 1 degree correspond to the result above / 360
|
# so 1 degree correspond to the result above / 360
|
||||||
@ -79,7 +102,7 @@ class WithinArea(Rule):
|
|||||||
self.radius = float(value)
|
self.radius = float(value)
|
||||||
self.radius = self.radius/2
|
self.radius = self.radius/2
|
||||||
|
|
||||||
def apply(self, db, place):
|
def apply(self, dummy_db, place):
|
||||||
if self.handle is None:
|
if self.handle is None:
|
||||||
return False
|
return False
|
||||||
if self.latitude is None:
|
if self.latitude is None:
|
||||||
@ -90,7 +113,11 @@ class WithinArea(Rule):
|
|||||||
lat = place.get_latitude()
|
lat = place.get_latitude()
|
||||||
lon = place.get_longitude()
|
lon = place.get_longitude()
|
||||||
if lat and lon:
|
if lat and lon:
|
||||||
if (hypot(float(self.latitude)-float(lat),
|
latit, longit = conv_lat_lon(lat, lon, "D.D8")
|
||||||
float(self.longitude)-float(lon)) <= self.radius) == True:
|
if latit is None or longit is None:
|
||||||
|
return False
|
||||||
|
if (hypot(float(self.latitude)-float(latit),
|
||||||
|
float(self.longitude)-float(longit))
|
||||||
|
<= self.radius):
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
Loading…
Reference in New Issue
Block a user