def update_tile(tile_key_name, children_updated_list, gt_key_name=None): """Updates a single tile, returns Tile model corresponding to tile_str. Datastore is not updated.""" if not gt_key_name: gt_key_name = GeoTree._GEO_TREE_KEY t = Tile.get_by_key_name(tile_key_name) if not t: x,y,z = tile_str_to_tuple(tile_key_name) t = Tile(x=x, y=y, z=z, gt_key_name=gt_key_name, key_name=tile_key_name) points = sorted_points_list_limited([]) for child_tile_key_name in children_updated_list: ct = Tile.get_by_key_name(child_tile_key_name) if ct: # in order insert points from child tile to parent # stop when does not insert any more # TODO: refactor to a single function # from native points if ct.points_native: for p in json.loads(ct.points_native): if not points.insert(p): break # from points if ct.points: for p in json.loads(ct.points): if not points.insert(p): break t.points = (json.dumps(points)) return t
def update_tiles(cls, count=config.COUNT_UPDATE, gt_key_name=None): """Update several tiles and puts them into datastore.""" if not gt_key_name: gt_key_name = cls._GEO_TREE_KEY gt = cls.get_by_key_name(gt_key_name) if gt: # check if the first tile has larger z than min_z, if not - nothing to update logging.debug(gt_key_name) logging.debug(gt.tiles_updated) x,y,z = tile_str_to_tuple(gt.tiles_updated[0]) min_z = get_min_z(gt_key_name) if not (z > min_z): r = '\n\nInfo: updated 0 tiles, nothing to update' return r needs_update = True count_updated = 0 while count_updated < count and needs_update: # get a group of tiles with a common parent (starting with the first) first_tile = gt.tiles_updated[0] p = tile_parent(first_tile,return_str=True) c = tile_children(p,return_str=True) c_in_updated = [first_tile] for t in gt.tiles_updated[1:]: if t in c: c_in_updated.append(t) # update parent, use all children for update t = cls.update_tile(p, c, gt_key_name=gt_key_name) t.put() # remove the group of updated children tiles from tiles_updated tiles_updated = sorted_tiles_set(gt.tiles_updated) tiles_updated.remove(c_in_updated) # insert updated tile tiles_updated.insert(p) gt.tiles_updated = list(tiles_updated) gt.put() # check if the first tile has smaller z than min_z, if not needs_update = False x,y,z = tile_str_to_tuple(tiles_updated[0]) min_z = get_min_z(gt_key_name) if z == min_z: needs_update = False # increase count_updated count_updated += 1 else: return '\n\nError: no GeoTree exists'
def fetch_around_tile(tile_center_str, gt_key_name=None): """returns JSON text of all important points in tiles around center tile""" if not gt_key_name: gt_key_name = GeoTree._GEO_TREE_KEY tiles = [] # a square 3x3 tiles around center tile dx = 1 dy = 1 xc,yc,z = tile_str_to_tuple(tile_center_str) xend = 2**z-1 ymin = max(0,yc-dy) ymax = min(xend,yc+dy) xmin = xc - dx if xmin < 0: xmin = xend + 1 + xmin xmax = xc + dx if xmax > xend: xmax = xmax - xend - 1 for y in range(ymin,ymax+1): if xmin > xmax: # if go beyond 360 degrees tiles.extend(Tile.all().filter('gt_key_name', gt_key_name).filter('z',z).filter('y',y).filter('x >=',xmin).filter('x <=',xend).order('-x').fetch(xend-xmin+1)) tiles.extend(Tile.all().filter('gt_key_name', gt_key_name).filter('z',z).filter('y',y).filter('x >=', 0).filter('x <=',xmax).order('-x').fetch(xmax-0+1)) else: tiles.extend(Tile.all().filter('gt_key_name', gt_key_name).filter('z',z).filter('y',y).filter('x >=',xmin).filter('x <=',xmax).order('-x').fetch(xmax-xmin+1)) if not tiles: return '[]' # not to convert from JSON and than back, the JSON string is formatted directly # [1:-1] below to get rid of '[' and ']' - seems like dirty hack - think about refactoring tiles_points_json = "[" for t in tiles: # check that lists are not empty: not None and longer than '[]' if t.points_native and len(t.points_native)>2: tiles_points_json += t.points_native[1:-1]+',' if t.points and len(t.points)>2: tiles_points_json += t.points[1:-1]+',' # [:-1] to get rid of last ',' return tiles_points_json[:-1]+"]"
def test_tile_str_to_tuple(self): s = '2,3,4,a' t = (2,3,4) self.assertEqual(t,geomath.tile_str_to_tuple(s))
def test_tile_str_to_tuple(self): s = '2,3,4,a' t = (2, 3, 4) self.assertEqual(t, geomath.tile_str_to_tuple(s))