def readInEvents(): ''' Read in the event data stored by the text parser and calculate decimal values of latitude and longitude for data visualization rendering ''' events = [] for line in jsonEvents: dataArr = line.split(": ") e = NewsEvent() e.setGps(dataArr[1][0:-5]) e.setUrl(dataArr[2][0:-1]) coordArr = e.getGps().split(", ") print(coordArr) coordArr[0] = coordArr[0].replace('m','').replace('s','') coordArr[1] = coordArr[1].replace('m','').replace('s','') x = coordArr[0] y = coordArr[1] print(type(LatLon.string2latlon(x, y, 'd% %m% %S% %H' ))) lat, lon = LatLon.string2latlon(x, y, 'd% %m% %S% %H' ).to_string() e.setlat(lat) e.setlon(lon) events.append(e) return events
def assemble_gps(cls, item): """Assemble lat/lon into a format we can work with.""" latlon = {} try: lat, lon = ArfcnCorrelator.assemble_latlon(item) ll = LatLon.string2latlon(lat, lon, "d% %m% %S% %H") latlon["lat"] = ll.to_string('D%')[0] latlon["lon"] = ll.to_string('D%')[1] except: print("ArfcnCorrelator: Unable to compose lat/lon from:") print(str(item)) return latlon
def latlon_trans_fcc(cls, row): """returns dict with lat, lon""" latlon = {} lat_pre = Template( '$LOC_LAT_DEG $LOC_LAT_MIN $LOC_LAT_SEC $LOC_LAT_DIR').substitute( row) # NOQA lon_pre = Template( '$LOC_LONG_DEG $LOC_LONG_MIN $LOC_LONG_SEC $LOC_LONG_DIR' ).substitute(row) # NOQA ll = LatLon.string2latlon(lat_pre, lon_pre, "d% %m% %S% %H") latlon["lat"] = ll.to_string('D%')[0] latlon["lon"] = ll.to_string('D%')[1] return latlon
def lat_lon(lat, emis, lon, emis2): lat_deg = lat[:2] lat_min = lat[2:] lon_deg = lon[:3] lon_min = lon[3:] coords = LatLon.string2latlon(lat_deg + ' ' + lat_min + ' ' + emis, lon_deg + ' ' + lon_min + ' ' + emis2, 'd% %m% %H') res = coords.to_string('D%') res_lat = int(float(res[0]) * 600000) res_lon = int(float(res[1]) * 600000) print(str(res_lat) + ' ' + str(res_lon)) res_coords = res_lat, res_lon return res_coords
def latitude_dms_to_dd(src): src = src.encode('utf-8').replace(' ', '').replace(',', '.').upper() if src.startswith("0°"): return None for the_format, zero_value in zip(["d%°%m%'%H", "d%°%S%''%H"], ["0°0'E", "0°0''E"]): try: lat = LatLon.string2latlon(src, zero_value, the_format).lat break except ValueError: pass try: return float(repr(lat).replace('Latitude ', '')) except UnboundLocalError: return None
def convert_coords_string(x): # string2latlon('5 52 59.88 N', '162 4 59.88 W', 'd% %m% %S% %H') if '"' in x: latlon_filter = 'd% %m% %S% %H' else: latlon_filter = 'd% %m% %H' x = re.sub('[^a-zA-Z0-9 \n\.,]', ' ', x) x = x.split(',') x[1] = x[1].lstrip() x = LatLon.string2latlon(x[0], x[1], latlon_filter) return str(x)
def convert_lat_lon(lat, lon): """ Convert from degrees decimal minutes format to decimal degrees format. References: https://pypi.python.org/pypi/LatLon/1.0.2 http://en.wikipedia.org/wiki/Geographic_coordinate_conversion @param lat: Latitude (e.g. 'N 59 16.58'). @param lon: Longitude (e.g. 'E 18 11.22'). @return: Latitude and longitude on decimal format (e.g. 59.202 and 18.26825). >>> convert_lat_lon('N 59 12.120', 'E 18 16.095') (59.202, 18.26825) >>> convert_lat_lon('N 59 11.772', 'E 17 4.744') (59.1962, 17.079066666666666) """ obj = LatLon.string2latlon(lat, lon, 'H% d% %M') return obj.lat.decimal_degree, obj.lon.decimal_degree
os.path.join('python', 'post_processing')) import constants import flopy from flopy.utils.gridgen import Gridgen with open('latlon.json') as f: data = json.load(f) with open('keys.json') as f: key = json.load(f) for i, key in enumerate(data['latlon']): print i, key latlon_str = LatLon.string2latlon(data['latlon'][key]['lat'], data['latlon'][key]['lon'], 'd% %m% %S% %H').to_string() data['latlon'][key]['lat_str'] = latlon_str[0] data['latlon'][key]['lon_str'] = latlon_str[1] data['latlon'][key]['lat_float'] = float(latlon_str[0]) data['latlon'][key]['lon_float'] = float(latlon_str[1]) lat_base = data['latlon']['b1']['lat_float'] lon_base = data['latlon']['b1']['lon_float'] lat_dx = (data['latlon']['b3']['lat_float'] - data['latlon']['b1']['lat_float'] ) / (data['no_lat_x_discretisation'] - 1) lon_dx = (data['latlon']['b3']['lon_float'] - data['latlon']['b1']['lon_float'] ) / (data['no_lon_y_discretisation'] - 1) #https://stackoverflow.com/questions/19412462/getting-distance-between-two-points-based-on-latitude-longitude cds1 = (data['latlon']['b1']['lat_float'], data['latlon']['b1']['lon_float'])
def main(data): for x, y in coordinate_format(data): new = LatLon.string2latlon(x, y, 'd% %M% %H') z = new.to_string('d%_%M') b = [str.encode("utf-8").replace('_', u"\u00b0") for str in z] print b
def findreferences(page, sqlfile): pagetitle = page.find(ns + 'title').text pagetext = page.find(ns + 'revision/' + ns + 'text').text # find all coords via regex try: coordmatch = coordpattern.search(pagetext) except Exception: logging.info("Exception caught during coord search. Empty page text?") return if coordmatch: # turn into a list and remove empty segments: latgroup = filter(None, coordmatch.groups()[0].split('/')) # turn back into string with blanks as separators, removing any extra blanks latstring = " ".join([s.strip() for s in latgroup]) longroup = filter(None, coordmatch.groups()[1].split('/')) lonstring = " ".join([s.strip() for s in longroup]) try: if len(latgroup) == 1: # decimal degrees coords = LatLon.string2latlon(latstring, lonstring, 'D') if len(latgroup) == 2: # degrees + heading try: coords = LatLon.string2latlon(latstring, lonstring, 'd% %H') except: # if this goes wrong, we usually have degrees and minutes, # but the headings are missing. Sometimes people seem to assume # we are in N / E hemispheres, so we'll do the same: latgroup.extend(["N"]) latstring = " ".join([s.strip() for s in latgroup]) longroup.extend(["E"]) lonstring = " ".join([s.strip() for s in longroup]) # ... then this will be taken care of in the next case: if len(latgroup) == 3: # degrees, decimal minutes + heading try: coords = LatLon.string2latlon(latstring, lonstring, 'd% %M% %H') except: # if this goes wrong, we usually have degrees, minutes, seconds, # but the headings are missing. Sometimes people seem to assume # we are in N / E hemispheres, so we'll do the same: latgroup.extend(["N"]) latstring = " ".join([s.strip() for s in latgroup]) longroup.extend(["E"]) lonstring = " ".join([s.strip() for s in longroup]) # ... then this will be taken care of in the next case: if len(latgroup) == 4: # degrees, decimal minutes + heading coords = LatLon.string2latlon(latstring, lonstring, 'd% %m% %S% %H') addInsert(pagetitle, coords.to_string('D')[0], coords.to_string('D')[1], sqlfile) except Exception as e: logging.error(pagetitle) logging.error(latgroup) logging.error(longroup) logging.error(e)
def converter_lon(lon): # Longitude in degrees:minutes:seconds (+: East, -: West) lon = re.sub('\+','E ',lon) lon = re.sub('\-','W ',lon) return round(LatLon.string2latlon('N 00:00:00',lon,'H% %d%:%m%:%S').lon.decimal_degree,3)
def converter_lat(lat): # Latitude in degrees:minutes:seconds (+: North, -: South) lat = re.sub('\+','N ',lat) lat = re.sub('\-','S ',lat) return round(LatLon.string2latlon(lat,'E 00:00:00','H% %d%:%m%:%S').lat.decimal_degree,3)
def create_ppt(row): Application = win32com.client.Dispatch("PowerPoint.Application") Presentation = Application.Presentations.Open( os.path.join(os.getcwd(), row['type'] + '-template.ppt'), False, True, False) print os.path.join(os.getcwd(), row['type'] + '.ppt') slide = Presentation.Slides(1) try: print row['main_title'] # change the main_title table_0_shape = slide.Shapes('TABLE_0') table_0_shape.Table.Rows(1).Cells.Item(1).Shape.TextFrame.TextRange.Text = row['main_title'] table_1_shape = slide.Shapes('TABLE_1') table_1_shape.Table.Rows(1).Cells.Item(1).Shape.TextFrame.TextRange.Text = row['main_title'] table_2_shape = slide.Shapes('TABLE_2') table_2_shape.Table.Rows(1).Cells.Item(1).Shape.TextFrame.TextRange.Text = row['main_title'] except: print 'Error for main_title' try: print row['valid'] # change the valid table_0_shape.Table.Rows(2).Cells.Item(1).Shape.TextFrame.TextRange.Text = 'Valid: ' + row['valid'] except: print 'Error for valid' try: print row['issue'] # change the issue table_0_shape.Table.Rows(2).Cells.Item(2).Shape.TextFrame.TextRange.Text = 'Issued: ' + row['issue'] except: print 'Error for issue' # change the area of interest try: sw_corner = LatLon.string2latlon(row['sw lat'], row['sw lon'], 'd% %m% %H') ne_corner = LatLon.string2latlon(row['ne lat'], row['ne lon'], 'd% %m% %H') except: print 'Error with sw lat, sw lon, ne lat, or ne lon' def pLAT(L): return "".join((str(abs(int(L.lat.to_string('d')))).zfill(2), unichr(176), str(abs(int(L.lat.to_string('%m')))).zfill(2), "'", L.lat.to_string('%H'))) def pLON(L): return "".join((str(abs(int(L.lon.to_string('d')))).zfill(3), unichr(176), str(abs(int(L.lon.to_string('%m')))).zfill(2), "'", L.lon.to_string('%H'))) try: temp = "".join((pLAT(sw_corner), ' ', '-', ' ', pLAT(ne_corner), '; ', pLON(sw_corner), ' ', '-', ' ', pLON(ne_corner))) print temp table_1_shape = slide.Shapes('TABLE_1') table_1_shape.Table.Rows(2).Cells.Item(2).Shape.TextFrame.TextRange.Text = temp except: print 'Error inserting area of coverage' try: alt_create_map(sw_corner, ne_corner, row['file_code']+'.png') except: print 'Error with file_code or creating map image' try: # insert image into ppt shp = slide.Shapes.AddPicture( os.path.join(os.getcwd(), row['file_code']+'.png'), False, True, 50, 80) shp.Name = 'map' shp.ZOrder(msoSendBackward) except: print 'Error inserting map image into ppt' try: temp = row['file_code'] + '_' + row['type'] + '_' + row['DisplayDays'] + '_' + row['AreaNameKey'] + '.ppt' Presentation.SaveCopyAs(os.path.join(os.getcwd(), temp)) print os.path.join(os.getcwd(),temp) except: print 'Error with DisplayDays, AreaNameKey. Error saving the powerpoint file.' try: # close the powerpoint file Presentation.Close() Application.Quit() print '\n' except: print 'Error closing the powerpoint file'
def findreferences(page, sqlfile): pagetitle = page.find(ns+'title').text pagetext = page.find(ns+'revision/'+ns+'text').text # find all coords via regex try: coordmatch = coordpattern.search(pagetext) except Exception: logme("Exception caught during coord search. Empty page text?") return #sqlfile.write(pagetitle.encode('utf8')+"\n") if coordmatch: #sqlfile.write(u" --- coords: " + str(coordmatch.group()) + "\n"); # figure out if the string contains heading directions and if it does, # where they are: i = 1 lath = "N" lathindex = 0 longh = "W" longhindex = 0 for part in coordmatch.groups(): if part == "N" or part == "S": lathindex = i if part == "W" or part == "E": longhindex = i i = i+1 #sqlfile.write("lathindex: " + str(lathindex) + ", longhindex: " + str(longhindex) + "\n") # first handle the cases where we don't have headings if lathindex == 0 and longhindex == 0: addInsert(pagetitle, str(float(coordmatch.group(1))), str(float(coordmatch.group(2))), sqlfile) # if we have headings, we have three different cases: # 1. just degrees if lathindex == 2 and longhindex == 4: # continue here! latstring = str(coordmatch.group(1)).strip() + " " + str(coordmatch.group(2)).strip() longstring = str(coordmatch.group(3)).strip() + " " + str(coordmatch.group(4)).strip() # just to check: # sqlfile.write(latstring +" "+ longstring+ "\n") coords = LatLon.string2latlon(latstring, longstring, 'd% %H') # finally... addInsert(pagetitle, coords.to_string('D')[0], coords.to_string('D')[1], sqlfile) return # 2. degress + minutes if lathindex == 3 and longhindex == 6: # continue here! latstring = str(coordmatch.group(1)).strip() + " " + str(float(coordmatch.group(2))).strip() + " " + str(coordmatch.group(3)).strip() longstring = str(coordmatch.group(4)).strip() + " " + str(float(coordmatch.group(5))).strip() + " " + str(coordmatch.group(6)).strip() # just to check: # sqlfile.write(latstring +" "+ longstring+ "\n") coords = LatLon.string2latlon(latstring, longstring, 'd% %m% %H') # finally... addInsert(pagetitle, coords.to_string('D')[0], coords.to_string('D')[1], sqlfile) # 3. degrees + minutes + seconds if lathindex == 4 and longhindex == 8: # continue here! latstring = str(coordmatch.group(1)).strip() + " " + str(replaceEmptyFloat(coordmatch.group(2))) + " " + str(replaceEmptyFloat(coordmatch.group(3))) + " "+ str(coordmatch.group(4)).strip() longstring = str(coordmatch.group(5)).strip() + " " + str(replaceEmptyFloat(coordmatch.group(6))) + " " + str(replaceEmptyFloat(coordmatch.group(7))) + " "+ str(coordmatch.group(8)).strip() coords = LatLon.string2latlon(latstring, longstring, 'd% %m% %S% %H') # finally... addInsert(pagetitle, coords.to_string('D')[0], coords.to_string('D')[1], sqlfile) return latcompletematch = latcompletepattern.search(pagetext) longcompletematch = longcompletepattern.search(pagetext) if latcompletematch and longcompletematch: # if the seconds in the coords are empty: if latcompletematch.group(3) == '' or longcompletematch.group(3) == '': latstring = latcompletematch.group(1) + " " + latcompletematch.group(2) + " " + latcompletematch.group(4) longstring = longcompletematch.group(1) + " " + longcompletematch.group(2) + " " + longcompletematch.group(4) coords = LatLon.string2latlon(latstring, longstring, 'd% %m% %H') else: # if we do have seconds: latstring = latcompletematch.group(1) + " " + latcompletematch.group(2) + " " +latcompletematch.group(3) + " " + latcompletematch.group(4) longstring = longcompletematch.group(1) + " " + longcompletematch.group(2) + " " +longcompletematch.group(3) + " " + longcompletematch.group(4) coords = LatLon.string2latlon(latstring, longstring, 'd% %m% %S% %H') addInsert(pagetitle, coords.to_string('D')[0], coords.to_string('D')[1], sqlfile) return # try other patterns: latdmatch = latdpattern.search(pagetext) longdmatch = longdpattern.search(pagetext) if latdmatch and longdmatch: # skip if the coordinates are empty: if len(latdmatch.group(1).strip()) == 0 or len(longdmatch.group(1).strip()) == 0: return latd = float(latdmatch.group(1)); longd = float(longdmatch.group(1)); # do we heave a heading? lathmatch = lathpattern.search(pagetext) longhmatch = longhpattern.search(pagetext) if(lathmatch and longhmatch): lath = str(lathmatch.group(1)) lonh = str(longhmatch.group(1)) else: # print "no heading found for ", pagetitle if str(latd)[0] == "-": lath = "S" else: lath = "N" if str(longd)[0] == "-": lonh = "W" else: lonh = "E" # if they are already decimals, we are done: if isDecimal(latd) and isDecimal(longd): latstring = str(latd) + " " + lath longstring = str(longd) + " " + lonh coords = LatLon.string2latlon(latstring, longstring, 'd% %H') # finally... addInsert(pagetitle, coords.to_string('D')[0], coords.to_string('D')[1], sqlfile) return # do we have minutes? latmmatch = latmpattern.search(pagetext) longmmatch = longmpattern.search(pagetext) if(latmmatch and longmmatch): try: latm = float(latmmatch.group(1)) longm = float(longmmatch.group(1)) except: #if that goes wrong, assume we have no minutes: # print "Error trying to convert string to float - latm: " + str(latmmatch.group() + ", lonm: " + longmmatch.group()) # TODO - remove latstring = str(latd) + " " + lath longstring = str(longd) + " " + lonh coords = LatLon.string2latlon(latstring, longstring, 'd% %H') # finally... addInsert(pagetitle, coords.to_string('D')[0], coords.to_string('D')[1], sqlfile) return else: latstring = str(latd) + " " + lath longstring = str(longd) + " " + lonh coords = LatLon.string2latlon(latstring, longstring, 'd% %H') # finally... addInsert(pagetitle, coords.to_string('D')[0], coords.to_string('D')[1], sqlfile) return # do we have seconds? lats = 0.0 longs = 0.0 latsmatch = latspattern.search(pagetext) longsmatch = longspattern.search(pagetext) if(latsmatch and longsmatch): lats = float(latsmatch.group(1)) longs = float(longsmatch.group(1)) #sqlfile.write(u"'" + str(int(latd)) + " " + str(latm) + " " + str(lats) + " " + lath + "', '" + str(int(longd)) + " " + str(longm) + " " + str(longs) + " " + lonh + "'\n") latstring = str(int(latd)) + " " + str(latm) + " " + str(lats) + " " + lath longstring = str(int(longd)) + " " + str(longm) + " " + str(longs) + " " + lonh # just to check: sqlfile.write(latstring +" "+ longstring+ "\n") coords = LatLon.string2latlon(latstring, longstring, 'd% %m% %S% %H') # finally... addInsert(pagetitle, coords.to_string('D')[0], coords.to_string('D')[1], sqlfile)