Package sabx10 :: Package oxm :: Module utils
[hide private]
[frames] | no frames]

Module utils

source code

Various constants and utility functions used by the SABX library.

Classes [hide private]
  VersionException
This exception is raised to indicate an incorrect SABX file version was encoutered.
Functions [hide private]
float
calculate_distance(lat1, lon1, lat2, lon2)
Calculate the distance between the two points (lat1,lon1) and (lat2,lon2) in statute miles.
source code
(float,float)
pt_dist_from_pt(lat1, lon1, distance, true_course)
Calculate a point (lat,lon) that is distance statute miles from the point (lat1,lon1) along a course true_course degrees.
source code
boolean
check_version(xml_tree)
Check the version of the XML tree.
source code
Element
get_from_id(elements, id)
Find an ElementTree element with the given id in a list of elements.
source code
Element
parse_no_def_namespaces(source)
Read an XML file using ElementTree tree, stripping default namespace strings from all elements.
source code
Variables [hide private]
float rad_to_nm = 3437.74677078
multiply a radian value by this to convert it to nautical miles
float nm_to_rad = 0.000290888208666
multiply a nautical mile value by this to convert it to radians
float nm_to_statute = 1.15077944802
multiply a nautical mile value by this to convert it to statute miles
float statute_to_nm = 0.868976241901
multiply a statute mile value by this to convert it to nautical miles
float meter_feet = 3.28083989501
number of feet in a meter
float mile_feet = 5280.0
number of feet in a mile
  kilometer_miles = 0.621371192
string cur_version = '1.0'
current version of the SABX file format (always "1.0" for the SABX10 library)
  __package__ = 'sabx10.oxm'
  e = 2.71828182846
  pi = 3.14159265359
Function Details [hide private]

calculate_distance(lat1, lon1, lat2, lon2)

source code 

Calculate the distance between the two points (lat1,lon1) and (lat2,lon2) in statute miles.

Parameters:
  • lat1 (float) - decimal degrees
  • lon1 (float) - decimal degrees (west is negative)
  • lat2 (float) - decimal degrees
  • lon2 (float) - decimal degrees (west is negative)
Returns: float
distance in statute miles

pt_dist_from_pt(lat1, lon1, distance, true_course)

source code 

Calculate a point (lat,lon) that is distance statute miles from the point (lat1,lon1) along a course true_course degrees.

Some useful true_course values:

  • 0.0 - north
  • 90.0 - east
  • 180.0 - south
  • 270.0 - west
Parameters:
  • lat1 (float) - decimal degrees
  • lon1 (float) - decimal degrees (west is negative)
  • distance (float) - distance from the point in statute miles
  • true_course (float) - true course from the point in degrees
Returns: (float,float)
(lat,lon) representing the calculated point

check_version(xml_tree)

source code 

Check the version of the XML tree.

Parameters:
  • xml_tree (Element) - an ElementTree Element that's the root of an SABX XML tree
Returns: boolean
True to indicate good version
Raises:
  • VersionException - when the given tree is not of the current version, this exception is raised

get_from_id(elements, id)

source code 

Find an ElementTree element with the given id in a list of elements.

I can't believe element tree doesn't provide something like this. Actually, it looks like element tree 1.3 may provide something like this with their XPath support - I need to check this out.

Parameters:
  • elements (list) - list of ElementTree Elements
  • id (string) - id of element to find
Returns: Element
Eement found or None

parse_no_def_namespaces(source)

source code 

Read an XML file using ElementTree tree, stripping default namespace strings from all elements.

This routine is a little bit precarious. Its aim is to strip the default namespace {uri} from tags so that you don't have to put all that text into find, findall, findtext, etc... when searching the XML tree using the ElementTree stuff.

This should work most of the time. That is, when there is only one default namespace declared, in the root of the XML tree, then it will be just fine. In other instances, where the default namespace changes throughout the XML document, the behavior can be unexpected.

Actually, the behavior of this function isn't unexpected. It will work just fine. It's the behaviour of anything using it that will be unexpected. If you change default namespaces, and you have tags that have duplicate local portions that are differentiated by the namespace, then they will all be retrieved by the find... calls. This may not be what you want. Luckily, in the limited world that this routine operates in (so far), this isn't a problem. GPX, TCX, and SABX files all have one default namespace, declared in the root element. Let's hope.

Parameters:
  • source (string or open file handle) - XML file to read
Returns: Element
tree root Element (NOT an ElementTree object)