def main(self, pytopo_args): """main execution routine for pytopo.""" self.exec_config_file() # Remember how many known sites we got from the config file; # the rest are read in from saved sites and may need to be re-saved. self.first_saved_site = len(self.KnownSites) # Now it's safe to read the saved sites. self.read_saved_sites() self.read_tracks() gc.enable() mapwin = MapWindow(self) try: self.parse_args(mapwin, pytopo_args) except ArgParseException: # Didn't match any known run mode: # start in selector mode to choose a location: if not mapwin.selection_window(): sys.exit(0) # For cProfile testing, run with a dummy collection (no data needed): # mapwin.collection = MapCollection("dummy", "/tmp") # print(cProfile.__file__) # cProfile.run('mapwin.show_window()', 'cprof.out') # http://docs.python.org/library/profile.html # To analyze cprof.out output, do this: # import pstats # p = pstats.Stats('fooprof') # p.sort_stats('time').print_stats(20) mapwin.show_window(self.init_width, self.init_height)
def main(self, pytopo_args): """main execution routine for pytopo.""" self.exec_config_file() # Remember how many known sites we got from the config file; # the rest are read in from saved sites and may need to be re-saved. self.first_saved_site = len(self.KnownSites) self.read_saved_sites() self.read_tracks() gc.enable() mapwin = MapWindow(self) self.parse_args(mapwin, pytopo_args) # For cProfile testing, run with a dummy collection (no data needed): # mapwin.collection = MapCollection("dummy", "/tmp") # print cProfile.__file__ # cProfile.run('mapwin.show_window()', 'cprof.out') # http://docs.python.org/library/profile.html # To analyze cprof.out output, do this: # import pstats # p = pstats.Stats('fooprof') # p.sort_stats('time').print_stats(20) mapwin.show_window(self.init_width, self.init_height)
def get_next_maplet(self, fullpathname, dX, dY): """Given a maplet's pathname, get the next or previous one. Does not currently work for jumps more than 1 in any direction. Returns pixbuf, newpath (either may be None). """ pathname, filename = os.path.split(fullpathname) if (self.Debug): print "Generic get_next_maplet", filename, dX, dY name, ext = os.path.splitext(filename) # traceback.print_stack() mapb = int(name[-self.numdigits:]) if self.usedash: mapa = int(name[-self.numdigits * 2 - 1: -self.numdigits - 1]) else: mapa = int(name[-self.numdigits * 2: -self.numdigits]) if self.latfirst: newa = MapUtils.ohstring(mapa + dX, self.numdigits) newb = MapUtils.ohstring(mapb + dY, self.numdigits) else: newa = MapUtils.ohstring(mapa + dY, self.numdigits) newb = MapUtils.ohstring(mapb + dX, self.numdigits) if self.usedash: newname = self.prefix + newa + "-" + newb else: newname = self.prefix + newa + newb newpath = os.path.join(self.location, newname + ext) if filename is None or not os.access(filename, os.R_OK): return None, newpath pixbuf = MapWindow.load_image_from_file(newpath) return pixbuf, newpath
def fetch_or_download_maplet(self, path): """Return a pixbuf if the file is on disk, else download""" # Does the tile already exist on local disk? # Even if it does, do we need to re-download it? # XXX This calls os.access then os.stat. Does that mean # we're statting the file twice? Might be a performance issue. try: on_disk = os.access(path, os.R_OK) needs_download = (not on_disk) or \ (self.reload_tiles and os.stat(path).st_mtime < self.reload_tiles) except OSError: on_disk = False needs_download = True if needs_download: if self.download_url: # path is a full path on the local filesystem, OS independent. # We need to turn it into a url (Unix path) with slashes. thedir = os.path.dirname(path) if not os.access(thedir, os.W_OK): os.makedirs(thedir) if self.Debug: print "Need to download", path if not on_disk: print "Wasn't on disk" else: print os.stat(path).st_mtime, "<", self.reload_tiles self.download_tiles.push(self.url_from_path(path), path) else: if self.Debug: print "Downloads not enabled; skipping", path # We've queued any needed downloads. # Now return the current pixbuf, if any. if not on_disk: return None try: pixbuf = MapWindow.load_image_from_file(path) except: pixbuf = None # return None # In case something went wrong, don't keep a bad file around: if not pixbuf or pixbuf.get_width() <= 0 or pixbuf.get_height() <= 0: print "Couldn't open pixbuf from", path os.rename(path, path + ".bad") pixbuf = None # XXX Sometimes despite that check, we mysteriously get # GtkWarning: gdk_drawable_real_draw_pixbuf: # assertion 'width >= 0 && height >= 0' failed # Not clear what we can do about that since we're already checking. return pixbuf
def get_maplet(self, longitude, latitude): """Get the maplet containing the specified coordinates. Returns pixbuf, x_offset, y_offset, filename where offsets are pixels from top left of the specified coords and pixbuf or (less often) filename may be None. """ filename = self.coords_to_filename(longitude - self.lon_correction, latitude - self.lat_correction) if (self.Debug): print("T1MC get_maplet(", MapUtils.dec_deg2deg_min_str(longitude), end=' ') print(",", MapUtils.dec_deg2deg_min_str(latitude), "):", filename) # Calculate offsets. # Maplets are self.series minutes wide and tall, # so any offset from that is an offset into the maplet: # the number of pixels in X and Y that have to be added # to get from the maplet's upper left corner to the # indicated coordinates. # But then we have to correct to get to WGS84 coordinates. # XXX the WGS84 part doesn't work right yet. # longitude increases rightward: x_off = int((longitude - MapUtils.truncate2frac(longitude, self.frac) - self.lon_correction) * self.xscale) if (self.Debug): print("truncated", MapUtils.dec_deg2deg_min_str(longitude), "to", end=' ') print(MapUtils.dec_deg2deg_min_str(MapUtils.truncate2frac(longitude, self.frac))) # Latitude decreases downward: y_off = int((MapUtils.truncate2frac(latitude, self.frac) + self.frac - latitude - self.lat_correction) * self.yscale) if (self.Debug): print("truncated", MapUtils.dec_deg2deg_min_str(latitude), "to", end=' ') print(MapUtils.dec_deg2deg_min_str(MapUtils.truncate2frac(latitude, self.frac))) print("y_off is", y_off) if not os.access(filename, os.R_OK): return None, x_off, y_off, filename pixbuf = MapWindow.load_image_from_file(filename) return pixbuf, x_off, y_off, filename
def get_maplet(self, longitude, latitude): """Get the maplet containing the specified coordinates. Returns pixbuf, x_offset, y_offset, filename where offsets are pixels from top left of the specified coords and pixbuf or (less often) filename may be None. """ filename = self.coords_to_filename(longitude - self.lon_correction, latitude - self.lat_correction) if (self.Debug): print "T1MC get_maplet(", MapUtils.dec_deg2deg_min_str(longitude), print ",", MapUtils.dec_deg2deg_min_str(latitude), "):", filename # Calculate offsets. # Maplets are self.series minutes wide and tall, # so any offset from that is an offset into the maplet: # the number of pixels in X and Y that have to be added # to get from the maplet's upper left corner to the # indicated coordinates. # But then we have to correct to get to WGS84 coordinates. # XXX the WGS84 part doesn't work right yet. # longitude increases rightward: x_off = int((longitude - MapUtils.truncate2frac(longitude, self.frac) - self.lon_correction) * self.xscale) if (self.Debug): print "truncated", MapUtils.dec_deg2deg_min_str(longitude), "to", print MapUtils.dec_deg2deg_min_str(MapUtils.truncate2frac(longitude, self.frac)) # Latitude decreases downward: y_off = int((MapUtils.truncate2frac(latitude, self.frac) + self.frac - latitude - self.lat_correction) * self.yscale) if (self.Debug): print "truncated", MapUtils.dec_deg2deg_min_str(latitude), "to", print MapUtils.dec_deg2deg_min_str(MapUtils.truncate2frac(latitude, self.frac)) print "y_off is", y_off if not os.access(filename, os.R_OK): return None, x_off, y_off, filename pixbuf = MapWindow.load_image_from_file(filename) return pixbuf, x_off, y_off, filename
def get_maplet(self, longitude, latitude): """Get the maplet containing the specified coordinates. Returns pixbuf, x_offset, y_offset, filename where offsets are pixels from top left of the specified coords and pixbuf or (less often) filename may be None. """ filename = self.coords_to_filename(longitude, latitude) if (self.Debug): print "Generic get_maplet", longitude, latitude, "->", filename if filename is None or not os.access(filename, os.R_OK): # print "Can't open", filename, "for", longitude, latitude return None, 0, 0, filename # print "Opened", filename, "for", longitude, latitude pixbuf = MapWindow.load_image_from_file(filename) # Offsets aren't implemented yet: x_off = 0 y_off = 0 return pixbuf, x_off, y_off, filename
def draw_single_tile(self, path, mapwin): """After a new tile is downloaded, fetch its pixbuf and draw it in the right place for our currently displayed map. """ # Calculate x, y, x_offset, y_offset from the tile name. # Make sure it's even still visible. # Tile name is /path/to/zoom/x/y.ext head, tiley = os.path.split(path) tiley = os.path.splitext(tiley)[0] head, tilex = os.path.split(head) head, tilezoom = os.path.split(head) try: tiley = int(tiley) tilex = int(tilex) except ValueError: print("Bad tile filename", path) return # Now we need to turn tilex and tiley into x, y, x_off, y_off # for the MapWindow's current position. lat_deg, lon_deg = self.num2deg(tilex, tiley) # Where on the map window should this lat and lon appear? min_lon = mapwin.center_lon - mapwin.win_width / self.xscale / 2 max_lat = mapwin.center_lat + mapwin.win_height / self.yscale / 2 mapx = int((lon_deg - min_lon) * self.xscale) mapy = int((max_lat - lat_deg) * self.yscale) if mapx < 0: x_off = -mapx mapx = 0 else: x_off = 0 if mapy < 0: y_off = -mapy mapy = 0 else: y_off = 0 try: pixbuf = MapWindow.load_image_from_file(path) self.draw_tile_at_position(pixbuf, mapwin, mapx, mapy, x_off, y_off) except glib.GError as e: print("Couldn't draw tile:", e, end=' ') if not self.Debug: print("... deleting") else: print("") print("") if os.path.exists(path) and not self.Debug: os.unlink(path) self.num_failed_downloads += 1 # Usually this means OSM gave us a text file containing # a string like "Tile Not Available" except Exception as e: print("Error drawing tile:", e) self.num_failed_downloads += 1 # Redraw any trackpoints, zoom controls, and anything else that # has to draw over the tiles, since they might have been overwritten. # XXX should schedule this after a delay, so we're not constantly # redrawing large sets of trackpoints each time a new tile comes in. mapwin.draw_overlays()
def get_next_maplet(self, fullpathname, dX, dY): """Given a maplet's pathname, get the next or previous one. Does not currently work for jumps more than 1 in any direction. Returns pixbuf, newpath (either may be None). """ if (self.Debug): print("get_next_maplet:", fullpathname, dX, dY) pathname, filename = os.path.split(fullpathname) collecdir, mapdir = os.path.split(pathname) maplat = int(mapdir[1:3]) maplon = int(mapdir[3:6]) name, ext = os.path.splitext(filename) xdir = int(mapdir[-1]) ydir = ord(mapdir[-2]) - ord('a') # convert from letter a-h if self.series == 7.5: serstr = self.ser7prefix grid = 10 else: serstr = self.ser15prefix grid = 5 x = int(name[-4:-2]) + dX y = int(name[-2:]) + dY if x < 1: x = grid xdir = xdir + 1 if xdir > 8: xdir = 1 if self.Debug: print(mapdir, name, ": wrapping mapdir coordinates -x", end=' ') print(maplon) maplon = str(int(maplon) + 1) if x > grid: x = 1 xdir = xdir - 1 if xdir < 1: xdir = 8 if self.Debug: print(mapdir, name, ": wrapping mapdir coordinates +x", end=' ') print(maplon) maplon = str(int(maplon) - 1) if y > grid: y = 1 ydir = ydir - 1 if ydir < 0: ydir = 7 if self.Debug: print(mapdir, name, ": wrapping mapdir coordinates +y", end=' ') print(maplat) maplat = str(int(maplat) - 1) if y < 1: y = grid ydir = ydir + 1 if ydir > 7: ydir = 0 if self.Debug: print(mapdir, name, ": wrapping mapdir coordinates -y", end=' ') print(maplat) maplat = str(int(maplat) + 1) # We're ready to piece the filename back together! newpath = os.path.join(collecdir, "q" + MapUtils.ohstring(maplat, 2) + MapUtils.ohstring(maplon, 3) + chr(ydir + ord('a')) + str(xdir), serstr + MapUtils.ohstring(x, 2) + MapUtils.ohstring(y, 2) + ext) if not os.access(newpath, os.R_OK): if self.Debug: print("get_next_maplet(", fullpathname, dX, dY, ")") print(" Can't open", newpath) return None, newpath pixbuf = MapWindow.load_image_from_file(newpath) return pixbuf, newpath
def get_next_maplet(self, fullpathname, dX, dY): """Given a maplet's pathname, get the next or previous one. Does not currently work for jumps more than 1 in any direction. Returns pixbuf, newpath (either may be None). """ if (self.Debug): print "get_next_maplet:", fullpathname, dX, dY pathname, filename = os.path.split(fullpathname) collecdir, mapdir = os.path.split(pathname) maplat = int(mapdir[1:3]) maplon = int(mapdir[3:6]) name, ext = os.path.splitext(filename) xdir = int(mapdir[-1]) ydir = ord(mapdir[-2]) - ord('a') # convert from letter a-h if self.series == 7.5: serstr = self.ser7prefix grid = 10 else: serstr = self.ser15prefix grid = 5 x = int(name[-4:-2]) + dX y = int(name[-2:]) + dY if x < 1: x = grid xdir = xdir + 1 if xdir > 8: xdir = 1 if self.Debug: print mapdir, name, ": wrapping mapdir coordinates -x", print maplon maplon = str(int(maplon) + 1) if x > grid: x = 1 xdir = xdir - 1 if xdir < 1: xdir = 8 if self.Debug: print mapdir, name, ": wrapping mapdir coordinates +x", print maplon maplon = str(int(maplon) - 1) if y > grid: y = 1 ydir = ydir - 1 if ydir < 0: ydir = 7 if self.Debug: print mapdir, name, ": wrapping mapdir coordinates +y", print maplat maplat = str(int(maplat) - 1) if y < 1: y = grid ydir = ydir + 1 if ydir > 7: ydir = 0 if self.Debug: print mapdir, name, ": wrapping mapdir coordinates -y", print maplat maplat = str(int(maplat) + 1) # We're ready to piece the filename back together! newpath = os.path.join(collecdir, "q" + MapUtils.ohstring(maplat, 2) + MapUtils.ohstring(maplon, 3) + chr(ydir + ord('a')) + str(xdir), serstr + MapUtils.ohstring(x, 2) + MapUtils.ohstring(y, 2) + ext) if not os.access(newpath, os.R_OK): if self.Debug: print "get_next_maplet(", fullpathname, dX, dY, ")" print " Can't open", newpath return None, newpath pixbuf = MapWindow.load_image_from_file(newpath) return pixbuf, newpath