surplus: add and use local to full length converter

This commit is contained in:
Mark Joshwel 2023-08-31 20:50:46 +00:00
parent 19c0d9d910
commit 4ca4a7c8a8
2 changed files with 61 additions and 14 deletions

View file

@ -34,7 +34,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 4, "execution_count": 3,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@ -45,7 +45,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 11, "execution_count": 4,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -54,7 +54,7 @@
"Result(value=Latlong(latitude=1.3336875, longitude=103.7746875), error=None)" "Result(value=Latlong(latitude=1.3336875, longitude=103.7746875), error=None)"
] ]
}, },
"execution_count": 11, "execution_count": 4,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@ -63,6 +63,30 @@
"PlusCodeQuery(code=\"6PH58QMF+FV\").to_lat_long_coord(geocoder=default_geocoder)" "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", "cell_type": "code",
"execution_count": 6, "execution_count": 6,

View file

@ -242,6 +242,32 @@ class LocalCodeQuery(NamedTuple):
code: str code: str
locality: 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]: def to_lat_long_coord(self, geocoder: Callable[[str], Latlong]) -> Result[Latlong]:
""" """
method that returns a latitude-longitude coordinate pair method that returns a latitude-longitude coordinate pair
@ -254,20 +280,17 @@ class LocalCodeQuery(NamedTuple):
returns Result[Latlong] returns Result[Latlong]
""" """
try: recovered_pluscode = self.to_full_plus_code(geocoder=geocoder)
locality_location = geocoder(self.locality)
recovered_pluscode = _PlusCode_recoverNearest( if not recovered_pluscode:
code=self.code, return Result[Latlong](EMPTY_LATLONG, error=recovered_pluscode.error)
referenceLatitude=locality_location.latitude,
referenceLongitude=locality_location.longitude, 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
) )
return PlusCodeQuery(recovered_pluscode).to_lat_long_coord(geocoder=geocoder)
except Exception as err:
return Result[Latlong](EMPTY_LATLONG, error=err)
class LatlongQuery(NamedTuple): class LatlongQuery(NamedTuple):
""" """