ddtdanilo / pynmea

Automatically exported from code.google.com/p/pynmea
Other
0 stars 0 forks source link

Adding support for calculating decimal longitudes and latitudes #4

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Hi,

this is not the cleanest hack but I thought I'd share it. This creates 
longitude_decimal and latitude_decimal properties if it finds the longitude and 
latitude properties in a sentence while parsing it. I use this to allow easy 
mapping using Google Maps.
This is inserted at the end of the parse function in the NMEASentence class 
(line 60):

        #Calculate the decimal value of the longitude and latitude if they exist in the current sentence
        if self.longitude:
            self.longitude_decimal = float(float(self.longitude[3:])/60)+float(self.longitude[:3])
        if self.latitude:
            self.latitude_decimal = float(float(self.latitude[2:])/60)+float(self.latitude[:2])

I also noticed the init.py still shows version as being 0.3.0.

Best,
Charles.

Original issue reported on code.google.com by charlesf...@gmail.com on 30 Apr 2013 at 6:55

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
Ok, nevermind that above, it doesn't work. I wrote better code to be inserted 
at the same spot. Now it checks in the parse_map to see if there are the 
longitude and latitude attributes that were parsed. If so, it will dynamically 
grab the name of the attribute, get the value and create a corresponding 
'_decimal' attribute with the calculated value.

        #Calculate the decimal value of the longitude and latitude if the attribute is present in the sentence
        for attribute in self.parse_map:
            if attribute[0].lower() == 'longitude':
                #Get the longitude attribute dynamically using the attribute name from parse_map
                longitude = getattr(self,attribute[1])
                #Add failsafe for empty values from invalid sentences
                if longitude != '':
                    self.longitude_decimal = float(longitude[3:])/60+float(longitude[:3])
                #To be consistent, create empty attribute, prevent AttributeError in downstream code
                else:
                    self.longitude_decimal = ''

            if attribute[0].lower() == 'latitude':
                #Get the latitude attribute dynamically using the attribute name from parse_map
                latitude = getattr(self,attribute[1])
                #Add failsafe for empty values from invalid sentences
                if latitude != '':
                    self.latitude_decimal = float(latitude[2:])/60+float(latitude[:2])
                #To be consistent, create empty attribute, prevent AttributeError in downstream code
                else:
                    self.latitude_decimal = ''

Sorry for the stupid first post.
Charles.

Original comment by charlesf...@gmail.com on 30 Apr 2013 at 7:56