Skip to content

Commit

Permalink
Refactor: Ameliorate the performance when we validate the french GPS …
Browse files Browse the repository at this point in the history
…coordinates (#20)

* Following the issue #19 , this PR aims to ameliorate the performance
when we validate the French GPS coordinates.

---------

Co-authored-by: Pierre Camilleri <[email protected]>
  • Loading branch information
Sarrabah and pierrecamilleri authored Sep 25, 2024
1 parent 5733c74 commit c0f7def
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 23 deletions.
49 changes: 27 additions & 22 deletions src/frformat/geo/coordonnees_gps_francaises.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,11 @@
from shapely.geometry import Point, shape
from shapely.geometry.base import BaseGeometry

from frformat import Metadata

name = "Coordonnées GPS françaises"
description = """Check that GPS coordinates are in a bounding box approximating France (including DOM)"""


class CoordonneesGPSFrancaises:
metadata = Metadata(name, description)

@classmethod
def is_valid(cls, lon: float, lat: float) -> bool:
return is_point_in_france((lon, lat))


def is_point_in_france(coordonnees_xy: tuple[float, float]) -> bool:
"""Returns True if the point is in metropolitan France, Guadeloupe, Martinique,
Guyane, la Réunion or Mayotte. As we are using bounding boxes (with a small margin),
locations outside of France but quite close by may return True.
"""
p = Point(*coordonnees_xy)

# Create a Polygon
geoms = [region["geometry"] for region in FRANCE_BOUNDING_BOXES]
polys = [shape(geom) for geom in geoms]
return any([p.within(poly) for poly in polys])


FRANCE_BOUNDING_BOXES = [
{
"nom": "Guadeloupe",
Expand Down Expand Up @@ -149,3 +128,29 @@ def is_point_in_france(coordonnees_xy: tuple[float, float]) -> bool:
},
},
]


def create_polygons() -> list[BaseGeometry]:
geoms = [region["geometry"] for region in FRANCE_BOUNDING_BOXES]
polys = [shape(geom) for geom in geoms]
return polys


POLYGONS: list[BaseGeometry] = create_polygons()


def is_point_in_france(coordonnees_xy: tuple[float, float]) -> bool:
"""Returns True if the point is in metropolitan France, Guadeloupe, Martinique,
Guyane, la Réunion or Mayotte. As we are using bounding boxes (with a small margin),
locations outside of France but quite close by may return True.
"""
p = Point(*coordonnees_xy)
return any(p.within(poly) for poly in POLYGONS)


class CoordonneesGPSFrancaises:
metadata = Metadata(name, description)

@classmethod
def is_valid(cls, lon: float, lat: float) -> bool:
return is_point_in_france((lon, lat))
1 change: 0 additions & 1 deletion src/tests/test_coordonnees_gps_francaises.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ class TestCase:
TestCase("Berlin", (13.3982663, 52.5075445), False),
TestCase("Valence", (-0.4439106, 39.4078888), False),
]

for tc in test_cases:
assert (
CoordonneesGPSFrancaises.is_valid(*tc.coordinates) == tc.is_in_france
Expand Down

0 comments on commit c0f7def

Please sign in to comment.