1
0
mirror of https://github.com/django/django.git synced 2025-01-18 14:24:39 +00:00

Simplified coordinate retrieval routines for GeoIP2.

Also removed dead code checking for ``None`` as ``GeoIP2.city()` cannot
return ``None``.
This commit is contained in:
Nick Pope 2021-04-06 00:10:01 +01:00 committed by Mariusz Felisiak
parent be06c39abe
commit 1311f82a6a

View File

@ -198,32 +198,26 @@ class GeoIP2:
enc_query = self._check_query(query, city_or_country=True) enc_query = self._check_query(query, city_or_country=True)
return Country(self._country_or_city(enc_query)) return Country(self._country_or_city(enc_query))
# #### Coordinate retrieval routines ####
def coords(self, query, ordering=("longitude", "latitude")): def coords(self, query, ordering=("longitude", "latitude")):
cdict = self.city(query) data = self.city(query)
if cdict is None: return tuple(data[o] for o in ordering)
return None
else:
return tuple(cdict[o] for o in ordering)
def lon_lat(self, query): def lon_lat(self, query):
"Return a tuple of the (longitude, latitude) for the given query." "Return a tuple of the (longitude, latitude) for the given query."
return self.coords(query) data = self.city(query)
return data["longitude"], data["latitude"]
def lat_lon(self, query): def lat_lon(self, query):
"Return a tuple of the (latitude, longitude) for the given query." "Return a tuple of the (latitude, longitude) for the given query."
return self.coords(query, ("latitude", "longitude")) data = self.city(query)
return data["latitude"], data["longitude"]
def geos(self, query): def geos(self, query):
"Return a GEOS Point object for the given query." "Return a GEOS Point object for the given query."
ll = self.lon_lat(query) # Allows importing and using GeoIP2() when GEOS is not installed.
if ll: from django.contrib.gis.geos import Point
# Allows importing and using GeoIP2() when GEOS is not installed.
from django.contrib.gis.geos import Point
return Point(ll, srid=4326) return Point(self.lon_lat(query), srid=4326)
else:
return None
@classmethod @classmethod
def open(cls, full_path, cache): def open(cls, full_path, cache):