1
0
mirror of https://github.com/django/django.git synced 2024-12-31 21:46:05 +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)
return Country(self._country_or_city(enc_query))
# #### Coordinate retrieval routines ####
def coords(self, query, ordering=("longitude", "latitude")):
cdict = self.city(query)
if cdict is None:
return None
else:
return tuple(cdict[o] for o in ordering)
data = self.city(query)
return tuple(data[o] for o in ordering)
def lon_lat(self, 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):
"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):
"Return a GEOS Point object for the given query."
ll = self.lon_lat(query)
if ll:
# Allows importing and using GeoIP2() when GEOS is not installed.
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)
else:
return None
return Point(self.lon_lat(query), srid=4326)
@classmethod
def open(cls, full_path, cache):