Package: latloncalc
FeaturesMethods for representing geographic coordinates (latitude and longitude) including the ability to:Convert lat/lon strings from almost any format into aLatLonobject (analogous to the datetime
library’sstptimemethod)Automatically store decimal degrees, decimal minutes, and degree, minute, second information in
aLatLonobjectOutput lat/lon information into a formatted string (analogous to the datetime library’sstrftimemethod)Project lat/lon coordinates into some other proj projectionCalculate distance and heading between lat/lon pairs using either the FAI or WGS84 approximationCreate a newLatLonobject by offsetting an initial coordinate by a distance and headingSubtracting oneLatLonobject from another creates aGeoVectorobject with distance and heading
attributes (analogous to the datetime library’stimedeltaobject)Adding or subtracting aLatlonobject and aGeoVectorobject creates a newLatLonobject with
the coordinates adjusted by theGeoVectorobject’s distance and headingGeoVectorobjects can be added, subtracted, multiplied or dividedNotesThe package latloncalc is a fork of the original package LatLon written by Gen Del Raye.latloncalc is essentially the same, except that Python 3 support was included and Python 2 support was
dropped.A new name was created because LatLon was abandoned and the author does not respond on pull
requests anymore.Another copy of the original packages was made by Jiho Persy Lee with  the name LatLon3.
This package is also essentially the same as the original package; also Python 3 support was
included without dropping the Python 2 support. However, in this version, unit tests and
documentation were dropped.If you are interested in a Python 3 only version of LatLon with Unit testing and documentation,
use latloncalc, otherwise LatLon3 can be used.Installationlatloncalchas been tested in Python 3.6, 3.7, 3.8, 3.9, 3.10, and 3.11Installation through pip:$ pip install latloncalcRequires the following non-standard libraries:pyprojnumpyUsage NotesUsage oflatloncalcis primarily through the classLatLon, which is designed to hold a single
pair ofLatitudeandLongitudeobjects. Strings can be converted toLatLonobjects using the
methodstring2latlon, and toLatitudeorLongitudeobjects usingstring2geocoord.
Alternatively, a LatLon object can be constructed by subtracting twoLatLonobjects, or adding or
subtracting aLatlonobject and aGeoVectorobject.Latitude or Longitude ConstructionLatitude of longitude construction is through the classesLatitudeandLongitude, respectively.
You can pass a latitude or longitude coordinate in any combination of decimal degrees, degrees and
minutes, or degrees minutes and seconds. Alternatively, you can pass a formatted string using the
functionstring2geocoordfor a string containing a single latitude or longitude, orstring2latlonfor a pair of strings representing the latitude and longitude.String formatting:string2latlonandstring2geocoordboth take aformatterstring which is loosely modeled on theformatkeyword used indatetime’sstrftimefunction.
Indicator characters (e.g.HorD) are placed between a specific separator character (%) to
specify the way in which a coordinate string is formatted.
Possible values are as follows:His a hemisphere identifier (e.g. N, S, E or W)Dis a coordinate in decimal degrees notation (e.g. 5.833)dis a coordinate in degrees notation (e.g. 5)Mis a coordinate in decimal minutes notation (e.g. 54.35)mis a coordinate in minutes notation (e.g. 54)Sis a coordinate in seconds notation (e.g. 28.93)Any other characters (e.g. ‘ ‘ or ‘, ‘) will be treated as a separator between the above components.All components should be separated by the%character. For example, if the coord_str is ‘5, 52,
59.88_N’, the format_str would be ‘d%, %m%, %S%_%H’ImportantOne format that will not currently work is one where the hemisphere identifier and a degree or
decimal degree are not separated by any characters. For example  ‘5 52 59.88 N’ is valid whereas
‘5 52 59.88N’ is not.String output:BothLatLonandLatitudeandLongitudeobjects include ato_string()method for outputting
a formatted coordinate.Projection:UseLatLon.projectto transform geographical coordinates into a chosen projection. Requires that
you pass it apyprojorbasemapprojection.Distance and Heading Calculation:LatLonobjects have adistance()method which accepts a 2ndLatLonobject as an argument.distance()will calculate the great-circle distance between the two coordinates using the WGS84
ellipsoid by default.
To use the more approximate FAI sphere, setellipseto ‘sphere’. Initial and reverse headings
(in degrees) can be calculated in a similar way using theheading_initial()andheading_reverse()methods.
Alternatively, subtracting oneLatLonobject from another will return aGeoVectorobject with
the attributes heading and distance.Creating a New LatLon Object by Offset from Another One:Use theoffset()method ofLatLonobjects, which takes an initial heading (in degrees) and
distance (in km) to return a newLatLonobject at the offset coordinates.
Also, you can perform the same operation by adding or subtracting a LatLon object with a GeoVector
object.ExamplesImport the classes:>> from latloncalc.latlon import LatLon, Latitude, LongitudeCreate aLatLonobject from coordinates:>> palmyra = LatLon(Latitude(5.8833), Longitude(-162.0833)) # Location of Palmyra Atoll in decimal degrees
>> palmyra = LatLon(5.8833, -162.0833) # Same thing but simpler!
>> palmyra = LatLon(Latitude(degree = 5, minute = 52, second = 59.88),
>>                  Longitude(degree = -162, minute = -4.998) # or more complicated!
>> print palmyra.to_string('d% %m% %S% %H') # Print coordinates to degree minute second
('5 52 59.88 N', '162 4 59.88 W')Create aLatlonobject from a formatted string:>> palmyra = string2latlon('5 52 59.88 N', '162 4 59.88 W', 'd% %m% %S% %H')
>> print palmyra.to_string('d%_%M') # Print coordinates as degree minutes separated by underscore
('5_52.998', '-162_4.998')Perform some calculations:>> palmyra = LatLon(Latitude(5.8833), Longitude(-162.0833)) # Location of Palmyra Atoll
>> honolulu = LatLon(Latitude(21.3), Longitude(-157.8167)) # Location of Honolulu, HI
>> distance = palmyra.distance(honolulu) # WGS84 distance in km
>> print distance
1766.69130376
>> print palmyra.distance(honolulu, ellipse = 'sphere') # FAI distance in km
1774.77188181
>> initial_heading = palmyra.heading_initial(honolulu) # Heading from Palmyra to Honolulu on WGS84 ellipsoid
>> print initial_heading
14.6907922022
>> hnl = palmyra.offset(initial_heading, distance) # Reconstruct Honolulu based on offset from Palmyra
>> print hnl.to_string('D') # Coordinates of Honolulu
('21.3', '-157.8167')ManipulateLatLonobjects usingGeoVectors:>> vector = (honolulu - palmyra) * 2 # A GeoVector with 2x the magnitude of a vector from palmyra to honolulu
>> print vector # Print heading and magnitude
14.6907922022 3533.38260751
print palmyra + (vector/2.0) # Recreate the coordinates of Honolulu by adding half of vector to palmyra
21.3, -157.8167Publication NotesThis project has been set up using PyScaffold 4.2.1. For details and usage information on
PyScaffold seehttp://pyscaffold.readthedocs.org/The following steps were done for publishing the package to PyPi:> tox -e build                          # build the package locally
> tox -e publish                        # publish the package to TesPyPi
> tox -e docs                           # build the documentation
> tox -e publish -- --repository pypi   # Publish the package to PyPi