surplus: add and use local to full length converter
This commit is contained in:
parent
19c0d9d910
commit
4ca4a7c8a8
|
@ -34,7 +34,7 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
|
@ -45,7 +45,7 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 11,
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
|
@ -54,7 +54,7 @@
|
|||
"Result(value=Latlong(latitude=1.3336875, longitude=103.7746875), error=None)"
|
||||
]
|
||||
},
|
||||
"execution_count": 11,
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
|
@ -63,6 +63,30 @@
|
|||
"PlusCodeQuery(code=\"6PH58QMF+FV\").to_lat_long_coord(geocoder=default_geocoder)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 13,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"Result(value=Latlong(latitude=1.3336875, longitude=103.7746875), error=None)"
|
||||
]
|
||||
},
|
||||
"execution_count": 13,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"plus_code = LocalCodeQuery(code=\"8QMF+FV\", locality=\"Singapore\").to_full_plus_code(\n",
|
||||
" geocoder=default_geocoder\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"PlusCodeQuery(code=plus_code.get()).to_lat_long_coord(geocoder=default_geocoder)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
|
|
45
surplus.py
45
surplus.py
|
@ -242,6 +242,32 @@ class LocalCodeQuery(NamedTuple):
|
|||
code: str
|
||||
locality: str
|
||||
|
||||
def to_full_plus_code(self, geocoder: Callable[[str], Latlong]) -> Result[str]:
|
||||
"""
|
||||
method that returns a full-length Plus Code
|
||||
|
||||
arguments
|
||||
geocoder: typing.Callable[[str], Latlong]
|
||||
name string to location function, must take in a string and return a
|
||||
Latlong. exceptions are handled.
|
||||
|
||||
returns Result[str]
|
||||
"""
|
||||
|
||||
try:
|
||||
locality_location = geocoder(self.locality)
|
||||
|
||||
recovered_pluscode = _PlusCode_recoverNearest(
|
||||
code=self.code,
|
||||
referenceLatitude=locality_location.latitude,
|
||||
referenceLongitude=locality_location.longitude,
|
||||
)
|
||||
|
||||
return Result[str](recovered_pluscode)
|
||||
|
||||
except Exception as err:
|
||||
return Result[str]("", error=err)
|
||||
|
||||
def to_lat_long_coord(self, geocoder: Callable[[str], Latlong]) -> Result[Latlong]:
|
||||
"""
|
||||
method that returns a latitude-longitude coordinate pair
|
||||
|
@ -254,19 +280,16 @@ class LocalCodeQuery(NamedTuple):
|
|||
returns Result[Latlong]
|
||||
"""
|
||||
|
||||
try:
|
||||
locality_location = geocoder(self.locality)
|
||||
recovered_pluscode = self.to_full_plus_code(geocoder=geocoder)
|
||||
|
||||
recovered_pluscode = _PlusCode_recoverNearest(
|
||||
code=self.code,
|
||||
referenceLatitude=locality_location.latitude,
|
||||
referenceLongitude=locality_location.longitude,
|
||||
)
|
||||
if not recovered_pluscode:
|
||||
return Result[Latlong](EMPTY_LATLONG, error=recovered_pluscode.error)
|
||||
|
||||
return PlusCodeQuery(recovered_pluscode).to_lat_long_coord(geocoder=geocoder)
|
||||
|
||||
except Exception as err:
|
||||
return Result[Latlong](EMPTY_LATLONG, error=err)
|
||||
return Result[Latlong](
|
||||
PlusCodeQuery(recovered_pluscode.get())
|
||||
.to_lat_long_coord(geocoder=geocoder)
|
||||
.get() # PlusCodeQuery can get latlong coord offline, so no need to handle
|
||||
)
|
||||
|
||||
|
||||
class LatlongQuery(NamedTuple):
|
||||
|
|
Loading…
Reference in a new issue