def timeArgument(s): """Allow a relative datetime like -1w3d2h3s""" units = { 's': 1, 'm': 60,'h': 3600, 'd': 86400, 'w': 604800 } if s[0] in '+-': # ([+-][0-9]+[smhdw])+ ) i = 1 val = 0; while i < len(s): j = i; while s[j:j+1] in '0123456789': j += 1; if s[j:j+1] in units.keys(): val += int(s[i:j]) * units[s[j]] i = j + 1; isonow= datetime.isoformat(datetime.now()) + 'Z'; now = isodate.parse(isonow); if s[0] == '-': val = - val; print "That must be ", isodate.fullString(now + val); return now + val; else: return isodate.parse(s);
def timeArgument(s): """Allow a relative datetime like -1w3d2h3s""" units = {'s': 1, 'm': 60, 'h': 3600, 'd': 86400, 'w': 604800} if s[0] in '+-': # ([+-][0-9]+[smhdw])+ ) i = 1 val = 0 while i < len(s): j = i while s[j:j + 1] in '0123456789': j += 1 if s[j:j + 1] in units.keys(): val += int(s[i:j]) * units[s[j]] i = j + 1 isonow = datetime.isoformat(datetime.now()) + 'Z' now = isodate.parse(isonow) if s[0] == '-': val = -val print "That must be ", isodate.fullString(now + val) return now + val else: return isodate.parse(s)
def straightPath(self, lon, lat, date): x, y = self.deg_to_px(lon, lat) self.wr("L %f %f " % (x, y)) lastlon, lastlat, lastdate = self.last dx = (lon - lastlon) * self.x_m_per_degree dy = (lat - lastlat) * self.y_m_per_degree ds = sqrt(dx * dx + dy * dy) t1 = isodate.parse(lastdate) t2 = isodate.parse(date) dt = t2 - t1 if dt > 0: speed = ds / dt # m/s else: speed = 0.0 # well, infinity # progress('Path length: %sm, speed: %f m/s ' %(`ds`, speed)) if speed > 0.5 and speed < 2.0: self.walking += dt self.total_m += ds self.last = (lon, lat, date)
def straightPath(self, lon, lat, date): x, y = self.deg_to_px(lon, lat) self.wr("L %f %f " % (x,y)) lastlon, lastlat, lastdate = self.last dx = (lon-lastlon) * self.x_m_per_degree dy = (lat-lastlat) * self.y_m_per_degree ds = sqrt(dx*dx + dy*dy) t1 = isodate.parse(lastdate) t2 = isodate.parse(date) dt = t2-t1 if dt > 0: speed = ds/dt # m/s else: speed = 0.0 # well, infinity # progress('Path length: %sm, speed: %f m/s ' %(`ds`, speed)) if speed > 0.5 and speed < 2.0: self.walking += dt self.total_m += ds self.last = (lon, lat, date)
def __init__(self, lon, lat, ele, date): self.lon = lon self.lat = lat self.ele = ele # float pi = 3.14159265358979323846 # (say) degree = pi / 180 # r_earth = 6400000.0 # (say) meters phi = self.lat * degree # See http://en.wikipedia.org/wiki/Earth_radius a = 6.378137e6 # m b = 6.3567523e6 #m r_earth = sqrt(((a * a * cos(phi))**2 + ((a * a * sin(phi)))**2) / ((a * cos(phi))**2 + ((a * sin(phi)))**2)) # print "Local radius of earth = ", r_earth self.y_m_per_degree = r_earth * pi / 180 self.x_m_per_degree = self.y_m_per_degree * cos(self.lat * degree) self.dz = 0 self.ds = None self.s = 0 self.grade = None self.last = None self.next = None self.date = date if date is not None: self.t = isodate.parse(date) ### self.kph = None self.mps = None self.dt = None self.climb = 0.0
def __init__(self, lon, lat, ele, date): self.lon = lon self.lat = lat self.ele = ele # float pi = 3.14159265358979323846 # (say) degree = pi/180 # r_earth = 6400000.0 # (say) meters phi = self.lat * degree # See http://en.wikipedia.org/wiki/Earth_radius a = 6.378137e6 # m b = 6.3567523e6 #m r_earth = sqrt( ((a*a*cos(phi))**2 + ((a*a*sin(phi)))**2)/ ((a*cos(phi))**2 + ((a*sin(phi)))**2)) # print "Local radius of earth = ", r_earth self.y_m_per_degree = r_earth * pi /180 self.x_m_per_degree = self.y_m_per_degree * cos(self.lat*degree) self.dz = 0 self.ds = None self.s = 0 self.grade = None self.last = None self.next = None self.date = date; if date is not None: self.t = isodate.parse(date); ### self.kph = None self.mps = None self.dt = None self.climb = 0.0
maxlo = lo elif ty == "P": ph, photo = da if last == None: progress("%s: Photo %s before any trackpoints" % (dt, photo)) continue j = i + 1 while j < n: dt2, ty2, da2 = events[j] if ty2 == "T": break j = j + 1 else: progress("%s: Photo %s off the end of trackpoints" % (dt, photo)) continue t = isodate.parse(dt) dt1, ty1, (la1, lo1) = events[last] lat1, long1 = float(la1), float(lo1) t1 = isodate.parse(dt1) dt2, ty2, (la2, lo2) = events[j] lat2, long2 = float(la2), float(lo2) t2 = isodate.parse(dt2) delta = t2 - t1 progress("%s: Photo %s between trackpoints %s and %s" % (dt, da, dt1, dt2)) # print " Delta", delta, "seconds between", events[last], "and", events[j] a = (t - t1) / (t1 - t2) lat = lat1 + a * (lat2 - lat1) long = long1 + a * (long2 - long1) progress("%s: Before (%f, %f)" % (dt1, lat1, long1)) progress("%s: Guess (%f, %f)" % (dt, lat, long)) progress("%s: After (%f, %f)" % (dt2, lat2, long2))
def __init__(self, minla, maxla, minlo, maxlo, svgStream=None): def getSize(s, atr): i = s.find(atr + '="') + len(atr) + 2 val = "" while s[i] in '0123456789': val += s[i] i = i + 1 x = int(val) progress("Found attribute %s=%i" % (atr, x)) return x progress("Lat between %f and %f, Long %f and %f" % (minla, maxla, minlo, maxlo)) #pageLong_m = 10.5 * 25.4 / 1000 # Say for 8.5 x 11" US Paper YMMV #pageShort_m = 8.0 * 25.4 / 1000 #This is printable # Not to scale ?!! pageLong_m = 7 * 25.4 / 1000 # Say for 8.5 x 11" US Paper YMMV pageShort_m = 5 * 25.4 / 1000 #This is printable if svgStream == None: self.wr = sys.stdout.write else: self.wr = svgStream.write self.marks = [] # List of marks on the map to avoid overlap self.midla = (minla + maxla) / 2.0 self.midlo = (minlo + maxlo) / 2.0 self.total_m = 0 # Meters self.last = None # (lon, lat, date) self.t0 = isodate.parse("2035-01-01T00:00:00Z") # Initialize to far future @@@ why doesnt 2099 or 2999 work? self.t9 = isodate.parse("1999-01-01T00:00:00Z") # Initialise to far past self.speeds = [] # self.elevations = []; pi = 3.14159265358979323846 # (say) degree = pi / 180 # r_earth = 6400000.0 # (say) meters phi = self.midla * degree # See http://en.wikipedia.org/wiki/Earth_radius a = 6.378137e6 # m b = 6.3567523e6 #m r_earth = sqrt(((a * a * cos(phi))**2 + ((a * a * sin(phi)))**2) / ((a * cos(phi))**2 + ((a * sin(phi)))**2)) print "Local radius of earth = ", r_earth self.y_m_per_degree = r_earth * pi / 180 self.x_m_per_degree = self.y_m_per_degree * cos(self.midla * degree) progress('Metres per degree: (%f,%f)' % (self.x_m_per_degree, self.y_m_per_degree)) # OpsenStreetMap Map hila = maxla + (maxla - minla) * 0.1 # Make margins an extra 10% all round hilo = maxlo + (maxlo - minlo) * 0.1 lola = minla - (maxla - minla) * 0.1 lolo = minlo - (maxlo - minlo) * 0.1 subtended_x = (hilo - lolo) * self.x_m_per_degree subtended_y = (hila - lola) * self.y_m_per_degree progress("Area subtended %f (E-W) %f (N-S) meters" % (subtended_x, subtended_y)) vertical = subtended_y > subtended_x if vertical: if subtended_y / pageLong_m > subtended_x / pageShort_m: # constrained by height osmScale = subtended_y / pageLong_m hilo = self.midlo + 0.5 * (pageShort_m * osmScale / self.x_m_per_degree) lolo = self.midlo - 0.5 * (pageShort_m * osmScale / self.x_m_per_degree) else: # constrained by width osmScale = subtended_x / pageShort_m hila = self.midla + 0.5 * (pageLong_m * osmScale / self.y_m_per_degree) lola = self.midla - 0.5 * (pageLong_m * osmScale / self.y_m_per_degree) else: if subtended_x / pageLong_m > subtended_y / pageShort_m: # constrained by long width osmScale = subtended_x / pageLong_m hila = self.midla + 0.5 * (pageShort_m * osmScale / self.y_m_per_degree) lola = self.midla - 0.5 * (pageShort_m * osmScale / self.y_m_per_degree) else: # constrained by short height osmScale = subtended_y / pageShort_m hilo = self.midlo + 0.5 * (pageLong_m * osmScale / self.x_m_per_degree) lolo = self.midlo - 0.5 * (pageLong_m * osmScale / self.x_m_per_degree) progress("OSM scale: %f" % osmScale) zoom = 20 - log(osmScale / 500, 2) progress("float zoom: %f" % zoom) zoomLevel = int(zoom) progress("float zoom: %i" % zoomLevel) progress("Area subtended %f (E-W) %f (N-S) meters" % (subtended_x, subtended_y)) pizelsPerAtZ20ATEquator = 156412.0 # osmScale = 10000 # say #self.pixels_per_m = 122.94/25.4 * 1000 / osmScale # pixels per metre on the ground - dpi was 120 now 123 #self.pixels_per_m = 85 /25.4 * 1000 / osmScale # Calculating this doesn't sem to work -- lets look at the actual map #self.page_x = (hilo-lolo) * self.x_m_per_degree * self.pixels_per_m #self.page_y = (hila-lola) * self.y_m_per_degree * self.pixels_per_m layers = 'C' # Cyclemap # Like http://tile.openstreetmap.org/cgi-bin/export?bbox=-71.2118,42.42694,-71.19273,42.44086&scale=25000&format=svg # OSM_URI = ("http://tile.openstreetmap.org/cgi-bin/export?bbox=%f,%f,%f,%f&scale=%i&format=svg" % (lolo, lola, hilo, hila, osmScale)) # OSM_URI = ("http://render.openstreetmap.org/cgi-bin/export?bbox=%f,%f,%f,%f&scale=%i&format=svg" % (lolo, lola, hilo, hila, osmScale)) OSM_URI = ( "http://render.openstreetmap.org/cgi-bin/export?bbox=%f,%f,%f,%f&scale=%i&format=svg&layers=%s" % (lolo, lola, hilo, hila, osmScale, layers)) progress("Batch OSM map at: ", OSM_URI) interactiveMapUri = "https://openstreetmap.org/#map=%i/%f/%f&layers=C" % ( zoom, (lola + hila) / 2.0, (lolo + hilo) / 2.0) progress("Interactive OSM map at: ", interactiveMapUri) try: pass #saveAs(OSM_URI, "background-map.svg") if false: osmStream = urlopen(OSM_URI) osmData = osmStream.read( ) # Beware of server overloaded errors osmStream.close else: osmData = requests.get(OSM_URI, cookies={ "_osm_totp_token": "102462" }, headers={ 'User-Agent': 'Mozilla/5.0' }).text except IOError: progress("Unable to get OSM map") sys.exit(4) # @@ should extract the error code from somwhere i = osmData.rfind('</svg>') if i < 0: progress("Invalid SVG file from OSM:\n" + osmData[:1000]) sys.exit(5) self.wr(osmData[:i]) # Everything except for the last </svg> # Set up parametrs for point mapping: self.page_x = getSize(osmData, 'width') self.page_y = getSize(osmData, 'height') self.lolo = lolo self.lola = lola self.hilo = hilo self.hila = hila self.pixels_per_deg_lat = self.page_y / (hila - lola) self.pixels_per_deg_lon = self.page_x / (hilo - lolo) # self.pixels_per_deg_lat = self.pixels_per_m * r_earth * pi /180 # self.pixels_per_deg_lon = self.pixels_per_deg_lat * cos(self.midla*degree) # page_x = 800.0 # pixels # page_y = 600.0 # max_x_scale = page_x / subtended_x # max_y_scale = page_y / subtended_y # self.pixels_per_m = min(max_x_scale, max_y_scale) * 0.9 # make margins #self.page_x = int(subtended_x * self.pixels_per_m) #self.page_y = int(subtended_y * self.pixels_per_m) # TIGER map if 0: map_wid = subtended_x / 0.9 map_ht = subtended_y / 0.9 tigerURI = ( "http://tiger.census.gov/cgi-bin/mapper/map.gif?" + "&lat=%f&lon=%f&ht=%f&wid=%f&" + "&on=CITIES&on=majroads&on=miscell&on=places&on=railroad&on=shorelin&on=streets" + "&on=interstate&on=statehwy&on=states&on=ushwy&on=water" + "&tlevel=-&tvar=-&tmeth=i&mlat=&mlon=&msym=bigdot&mlabel=&murl=&conf=mapnew.con" + "&iht=%i&iwd=%i") % (self.midla, self.midlo, map_ht, map_wid, self.page_y, self.page_x) progress("Getting tiger map ", tigerURI) try: saveAs(tigerURI, "tiger.gif") except IOError: progress("Offline? No tigermap.") # tigerURI = ("http://tiger.census.gov/cgi-bin/mapper/map.gif?&lat=%f&lon=%f&ht=%f" # +"&wid=%f&&on=majroads&on=miscell&tlevel=-&tvar=-&tmeth=i&mlat=&mlon=&msym=bigdot&mlabel=&murl=" # +"&conf=mapnew.con&iht=%i&iwd=%i" ) % (self.midla, self.midlo, maxla-minla, maxlo-minlo, self.page_y, self.page_x) if 0: self.wr("""<?xml version="1.0" encoding="UTF-8" standalone="no"?> <!DOCTYPE svg PUBLIC '-//W3C//DTD SVG 1.0//EN' 'http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd'> <!-- Generated by @@@ --> <svg width="%ipx" height="%ipx" xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink'> <g> """ % (self.page_x, self.page_y)) #" progress('Map page size (%f,%f)' % (self.page_x, self.page_y))
if p.lon > maxlo: maxlo = p.lon elif ty == "P": ph, photo = p if last == None: progress("%s: Photo %s before any trackpoints" % (dt, photo)) continue j = i + 1 while j < n: dt2, ty2, da2 = events[j] if ty2 == "T": break j = j + 1 else: progress("%s: Photo %s off the end of trackpoints" % (dt, photo)) continue t = isodate.parse(dt) dt1, ty1, (la1, lo1, ele1) = events[last] lat1, long1 = float(la1), float(lo1) t1 = isodate.parse(dt1) dt2, ty2, (la2, lo2, ele2) = events[j] lat2, long2 = float(la2), float(lo2) t2 = isodate.parse(dt2) delta = t2 - t1 progress("%s: Photo %s between trackpoints %s and %s" % (dt, p, dt1, dt2)) # print " Delta", delta, "seconds between", events[last], "and", events[j] a = (t - t1) / (t1 - t2) lat = lat1 + a * (lat2 - lat1) long = long1 + a * (long2 - long1) progress("%s: Before (%f, %f)" % (dt1, lat1, long1)) progress("%s: Guess (%f, %f)" % (dt, lat, long))
def __init__(self, minla, maxla, minlo, maxlo, svgStream=None): def getSize(s, atr): i = s.find(atr+'="') + len(atr) + 2 val = "" while s[i] in '0123456789': val += s[i] i = i+1 x = int(val) progress("Found attribute %s=%i" %(atr,x)) return x progress("Lat between %f and %f, Long %f and %f" % (minla, maxla, minlo, maxlo)) #pageLong_m = 10.5 * 25.4 / 1000 # Say for 8.5 x 11" US Paper YMMV #pageShort_m = 8.0 * 25.4 / 1000 #This is printable # Not to scale ?!! pageLong_m = 7 * 25.4 / 1000 # Say for 8.5 x 11" US Paper YMMV pageShort_m = 5 * 25.4 / 1000 #This is printable if svgStream==None: self.wr = sys.stdout.write else: self.wr = svgStream.write self.marks = [] # List of marks on the map to avoid overlap self.midla = (minla + maxla)/2.0 self.midlo = (minlo + maxlo)/2.0 self.total_m = 0 # Meters self.last = None # (lon, lat, date) self.t0 = isodate.parse("2035-01-01T00:00:00Z"); # Initialize to far future @@@ why doesnt 2099 or 2999 work? self.t9 = isodate.parse("1999-01-01T00:00:00Z"); # Initialise to far past self.speeds = []; # self.elevations = []; pi = 3.14159265358979323846 # (say) degree = pi/180 # r_earth = 6400000.0 # (say) meters phi = self.midla * degree # See http://en.wikipedia.org/wiki/Earth_radius a = 6.378137e6 # m b = 6.3567523e6 #m r_earth = sqrt( ((a*a*cos(phi))**2 + ((a*a*sin(phi)))**2)/ ((a*cos(phi))**2 + ((a*sin(phi)))**2)) print "Local radius of earth = ", r_earth self.y_m_per_degree = r_earth * pi /180 self.x_m_per_degree = self.y_m_per_degree * cos(self.midla*degree) progress('Metres per degree: (%f,%f)' % (self.x_m_per_degree, self.y_m_per_degree)) # OpsenStreetMap Map hila = maxla + (maxla - minla) * 0.1 # Make margins an extra 10% all round hilo = maxlo + (maxlo - minlo) * 0.1 lola = minla - (maxla - minla) * 0.1 lolo = minlo - (maxlo - minlo) * 0.1 subtended_x = (hilo - lolo) * self.x_m_per_degree subtended_y = (hila - lola) * self.y_m_per_degree progress("Area subtended %f (E-W) %f (N-S) meters" %(subtended_x, subtended_y)) vertical = subtended_y > subtended_x if vertical: if subtended_y / pageLong_m > subtended_x/pageShort_m: # constrained by height osmScale = subtended_y / pageLong_m hilo = self.midlo + 0.5 * (pageShort_m * osmScale/self.x_m_per_degree) lolo = self.midlo - 0.5 * (pageShort_m * osmScale/self.x_m_per_degree) else: # constrained by width osmScale = subtended_x/pageShort_m hila = self.midla + 0.5 * (pageLong_m * osmScale/self.y_m_per_degree) lola = self.midla - 0.5 * (pageLong_m * osmScale/self.y_m_per_degree) else: if subtended_x / pageLong_m > subtended_y/pageShort_m: # constrained by long width osmScale = subtended_x / pageLong_m hila = self.midla + 0.5 * (pageShort_m * osmScale/self.y_m_per_degree) lola = self.midla - 0.5 * (pageShort_m * osmScale/self.y_m_per_degree) else: # constrained by short height osmScale = subtended_y/pageShort_m hilo = self.midlo + 0.5 * (pageLong_m * osmScale/self.x_m_per_degree) lolo = self.midlo - 0.5 * (pageLong_m * osmScale/self.x_m_per_degree) progress("Area subtended %f (E-W) %f (N-S) meters" %(subtended_x, subtended_y)) # osmScale = 10000 # say #self.pixels_per_m = 122.94/25.4 * 1000 / osmScale # pixels per metre on the ground - dpi was 120 now 123 #self.pixels_per_m = 85 /25.4 * 1000 / osmScale # Calculating this doesn't sem to work -- lets look at the actual map #self.page_x = (hilo-lolo) * self.x_m_per_degree * self.pixels_per_m #self.page_y = (hila-lola) * self.y_m_per_degree * self.pixels_per_m layers = 'C'; # Cyclemap # Like http://tile.openstreetmap.org/cgi-bin/export?bbox=-71.2118,42.42694,-71.19273,42.44086&scale=25000&format=svg # OSM_URI = ("http://tile.openstreetmap.org/cgi-bin/export?bbox=%f,%f,%f,%f&scale=%i&format=svg" % (lolo, lola, hilo, hila, osmScale)) # OSM_URI = ("http://render.openstreetmap.org/cgi-bin/export?bbox=%f,%f,%f,%f&scale=%i&format=svg" % (lolo, lola, hilo, hila, osmScale)) OSM_URI = ("http://render.openstreetmap.org/cgi-bin/export?bbox=%f,%f,%f,%f&scale=%i&format=svg&layers=%s" % (lolo, lola, hilo, hila, osmScale, layers)) progress("FYI OSM map at: ", OSM_URI) try: pass #saveAs(OSM_URI, "background-map.svg") osmStream = urlopen(OSM_URI) osmData = osmStream.read() # Beware of server overloaded errors osmStream.close except IOError: progress("Unable to get OSM map") sys.exit(4) # @@ should extract the error code from somwhere i = osmData.rfind('</svg>') if i <0: progress("Invalid SVG file from OSM:\n" + osmData[:1000]) sys.exit(5) self.wr(osmData[:i]) # Everything except for the last </svg> # Set up parametrs for point mapping: self.page_x = getSize(osmData, 'width') self.page_y = getSize(osmData, 'height') self.lolo = lolo self.lola = lola self.hilo = hilo self.hila = hila self.pixels_per_deg_lat = self.page_y / (hila-lola) self.pixels_per_deg_lon = self.page_x / (hilo-lolo) # self.pixels_per_deg_lat = self.pixels_per_m * r_earth * pi /180 # self.pixels_per_deg_lon = self.pixels_per_deg_lat * cos(self.midla*degree) # page_x = 800.0 # pixels # page_y = 600.0 # max_x_scale = page_x / subtended_x # max_y_scale = page_y / subtended_y # self.pixels_per_m = min(max_x_scale, max_y_scale) * 0.9 # make margins #self.page_x = int(subtended_x * self.pixels_per_m) #self.page_y = int(subtended_y * self.pixels_per_m) # TIGER map if 0: map_wid = subtended_x /0.9 map_ht = subtended_y /0.9 tigerURI = ("http://tiger.census.gov/cgi-bin/mapper/map.gif?" +"&lat=%f&lon=%f&ht=%f&wid=%f&" +"&on=CITIES&on=majroads&on=miscell&on=places&on=railroad&on=shorelin&on=streets" +"&on=interstate&on=statehwy&on=states&on=ushwy&on=water" +"&tlevel=-&tvar=-&tmeth=i&mlat=&mlon=&msym=bigdot&mlabel=&murl=&conf=mapnew.con" +"&iht=%i&iwd=%i") % (self.midla, self.midlo, map_ht, map_wid, self.page_y, self.page_x) progress("Getting tiger map ", tigerURI) try: saveAs(tigerURI, "tiger.gif") except IOError: progress("Offline? No tigermap.") # tigerURI = ("http://tiger.census.gov/cgi-bin/mapper/map.gif?&lat=%f&lon=%f&ht=%f" # +"&wid=%f&&on=majroads&on=miscell&tlevel=-&tvar=-&tmeth=i&mlat=&mlon=&msym=bigdot&mlabel=&murl=" # +"&conf=mapnew.con&iht=%i&iwd=%i" ) % (self.midla, self.midlo, maxla-minla, maxlo-minlo, self.page_y, self.page_x) if 0: self.wr("""<?xml version="1.0" encoding="UTF-8" standalone="no"?> <!DOCTYPE svg PUBLIC '-//W3C//DTD SVG 1.0//EN' 'http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd'> <!-- Generated by @@@ --> <svg width="%ipx" height="%ipx" xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink'> <g> """ % (self.page_x,self.page_y)) #" progress('Map page size (%f,%f)'% (self.page_x,self.page_y))