Exemple #1
0
def cover_geometry(tilescheme, geom, zooms):
    """Covers the provided geometry with tiles.

    Args:
        tilescheme: The tile scheme to use.  This needs to implement
                    the public protocal of the schemes defined within
                    tiletanic.
        geom: The geometry we would like to cover.  This should be a
              shapely geometry.
        zooms: The zoom levels of the tiles to cover geom with.  If
               you provide an iterable of zoom levels, you'll get the
               biggest tiles available that cover the geometry at
               those levels.

    Yields:
        An iterator of Tile objects ((x, y, z) named tuples) that
        cover the input geometry.
    """
    # Only shapely geometries allowed.
    if not isinstance(geom, geometry.base.BaseGeometry):
        raise ValueError("Input 'geom' is not a known shapely geometry type")

    if geom.is_empty:
        return

    zooms = zooms if isinstance(zooms, Iterable) else [zooms]

    # Generate the covering.
    prep_geom = prepared.prep(geom)    
    if isinstance(geom, (geometry.Polygon, geometry.MultiPolygon)):        
        for tile in _cover_polygonal(tilescheme, Tile(0, 0, 0), prep_geom, geom, zooms):
            yield tile
    else:
        for tile in _cover_geometry(tilescheme, Tile(0, 0, 0), prep_geom, geom, zooms):
            yield tile
Exemple #2
0
    def quadkey_to_tile(self, qk):
        """Returns the Tile object represented by the input quadkey.

        Args:
            qk: A string representing the quadkey.

        Returns:
            The Tile object represented by the input quadkey.
        """
        if not qk_regex.match(qk):
            raise ValueError("Input quadkey is invalid.")

        x = 0
        y = 0
        for i, digit in enumerate(reversed(qk)):
            mask = 1 << i
            if digit == '1':
                x = x | mask
            elif digit == '2':
                y = y | mask
            elif digit == '3':
                x = x | mask
                y = y | mask

        return Tile(x, 2**len(qk) - y - 1, len(qk))
Exemple #3
0
    def children(self, *tile):
        """Returns the children of the (x, y, z) tile.

        Args:
            *tile: (x, y, z) tile coordinates or a Tile object we want
                   the children of.

        Yields: 
            An iterable of Tile objects representing the children of
            this tile.
        """
        if len(tile) == 1:  # Handle if a Tile object was inputted.
            tile = tile[0]
        x, y, z = tile

        return [
            Tile(2 * x, 2 * y, z + 1),
            Tile(2 * x + 1, 2 * y, z + 1),
            Tile(2 * x, 2 * y + 1, z + 1),
            Tile(2 * x + 1, 2 * y + 1, z + 1)
        ]
Exemple #4
0
    def parent(self, *tile):
        """Returns the parent of the (x, y, z) tile.

        Args:
            *tile: (x, y, z) tile coordinates or a Tile object we want
                   the parent of.

        Returns:
            A Tile object representing the parent of the input.
        """
        if len(tile) == 1:  # Handle if a Tile object was inputted.
            tile = tile[0]
        x, y, z = tile

        if x % 2 == 0 and y % 2 == 0:  # x and y even
            return Tile(x // 2, y // 2, z - 1)
        elif x % 2 == 0:  # x even, y odd
            return Tile(x // 2, (y - 1) // 2, z - 1)
        elif y % 2 == 0:  # x odd, y even
            return Tile((x - 1) // 2, y // 2, z - 1)
        else:  # x odd, y odd
            return Tile((x - 1) // 2, (y - 1) // 2, z - 1)
Exemple #5
0
    def children(self, *tile):
        """Returns the children of the (x, y, z) tile.

        For DGTiling, note that level 0 only returns two tiles because
        the level 0 tile is twice the size of the map!

        Args:
            *tile: (x, y, z) tile coordinates or a Tile object we want
                   the children of.

        Yields: 
            An iterable of Tile objects representing the children of
            this tile.
        """
        if len(tile) == 1:  # Handle if a Tile object was inputted.
            tile = tile[0]
        x, y, z = tile

        # DGTiling is weird at level zero, so deal with it.
        if z == 0:
            return [Tile(0, 0, 1), Tile(1, 0, 1)]
        return super(DGTiling, self).children(x, y, z)
Exemple #6
0
    def tile(self, xcoord, ycoord, zoom):
        """Returns the (x, y, z) tile at the given zoom level that
        contains the input coordinates.

        Args:
            xcoord: x direction geospatial coordinate within the tile
                    we want. 
            ycoord: y direction geospatial coordinate within the tile
                    we want. 
            zoom: zoom level of the tile we want.

        Returns:
            A Tile object that covers the given coordinates at the
            provided zoom level.
        """
        return Tile(x=self._x(xcoord, zoom), y=self._y(ycoord, zoom), z=zoom)
Exemple #7
0
def setup_tiles(players, game_mode, movement_rules, exploration_rules,
                mythical_city):
    '''Part of game setup for Cartolan, this places the intital tiles ready for play
    
    Arguments:
    List of Cartolan.Player for the Players involved in the game
    Cartolan.Game for the game that these tiles are being laid for
    String giving the movement rules variant that will apply for this game
    String giving the exploration rules variant that will apply for this game
    '''

    game = game_mode(players, movement_rules, exploration_rules)
    #     exec("CityTile" +game_mode+ "(game, True, True).place_tile(0,0)")
    game.CITY_TYPE(game, WindDirection(True, True),
                   TileEdges(True, True, True, True), True,
                   True).place_tile(0, 0)
    #     capital_tile = CityTileBeginner(game, True, True)
    #     capital_tile.place_tile(0,0)

    #place surrounding water tiles
    #     if len(players) == 2:
    if True:
        Tile(game, "water", WindDirection(True, True),
             TileEdges(True, True, True, True), False).place_tile(0, 1)  #north
        Tile(game, "water", WindDirection(True, True),
             TileEdges(True, True, True, True), False).place_tile(1, 0)  #east
        Tile(game, "water", WindDirection(True, True),
             TileEdges(True, True, True, True), False).place_tile(0,
                                                                  -1)  #south
        Tile(game, "water", WindDirection(True, True),
             TileEdges(True, True, True, True), False).place_tile(-1, 0)  #west
    elif mythical_city and len(players) == 3:
        Tile(game, "water", WindDirection(True, True),
             TileEdges(True, True, True, True), False).place_tile(0, 1)  #north
        Tile(game, "water", WindDirection(True, True),
             TileEdges(True, True, True, True), False).place_tile(1, 0)  #east
        Tile(game, "water", WindDirection(True, True),
             TileEdges(True, True, True, True), False).place_tile(0,
                                                                  -1)  #south
        Tile(game, "water", WindDirection(True, True),
             TileEdges(True, True, True, True), False).place_tile(-1, 0)  #west
    elif len(players) == 4 or (not mythical_city and len(players) == 3):
        Tile(game, "water", WindDirection(False, False),
             TileEdges(True, True, True, True), False).place_tile(0, 1)  #north
        Tile(game, "water", WindDirection(True, True),
             TileEdges(True, True, True, True), False).place_tile(1, 0)  #east
        Tile(game, "water", WindDirection(True, True),
             TileEdges(True, True, True, True), False).place_tile(0,
                                                                  -1)  #south
        Tile(game, "water", WindDirection(False, False),
             TileEdges(True, True, True, True), False).place_tile(-1, 0)  #west

    print("Placed the Capital tile, and surrounding water tiles")
    return game
Exemple #8
0
    def setup_tile_pile(self, tile_back):
        '''Part of game setup for Cartolan, this creates a tile of shuffled water-backed tiles ready for play
        
        Arguments:
        tile_back a string designating the type of tile and so its distribution of edge combinations
        '''
        total_distribution = []
        special_distributions = {}  # for wonder/disaster
        for tile_type in self.TILE_TYPE_COLS:
            special_distributions[tile_type] = []

        tile_filename = self.TILE_PREFIX + tile_back + self.TILE_EXT
        with open(tile_filename) as csvfile:
            readCSV = csv.reader(csvfile)
            for row in readCSV:
                total_distribution.append(int(row[0]))
                for tile_type in self.TILE_TYPE_COLS:
                    special_distributions[tile_type].append(
                        int(row[self.TILE_TYPE_COLS[tile_type]]))

        #construct the tile deck
        row_count = 0
        tiles = []
        for uc_water in [True, False]:
            for ua_water in [True, False]:
                for dc_water in [True, False]:
                    for da_water in [True, False]:
                        #for this combination of tile edges create tiles of each special type and plain ones
                        special_tile_num = 0
                        for tile_type in special_distributions:
                            for tile_num in range(
                                    0,
                                    int(special_distributions[tile_type]
                                        [row_count])):
                                wind_direction = WindDirection(north=True,
                                                               east=True)
                                tile_edges = TileEdges(uc_water, ua_water,
                                                       dc_water, da_water)
                                tiles.append(self.TILE_TYPES[tile_type](
                                    self, tile_back, wind_direction,
                                    tile_edges))
                                special_tile_num += 1
                        for tile_num in range(
                                0,
                                int(total_distribution[row_count]) -
                                special_tile_num):
                            wind_direction = WindDirection(north=True,
                                                           east=True)
                            tile_edges = TileEdges(uc_water, ua_water,
                                                   dc_water, da_water)
                            tiles.append(
                                Tile(self, tile_back, wind_direction,
                                     tile_edges, False))
                        row_count += 1

        #draw a suitable number of tiles from the deck for a pile

    #     num_tiles = len(players)*game.WATER_TILES_PER_PLAYER
        num_tiles = self.NUM_TILES[tile_back]
        tile_pile = self.tile_piles[tile_back]
        for tile in random.sample(tiles, num_tiles):
            tile_pile.add_tile(tile)

        tile_pile.shuffle_tiles()

        print("Built a " + tile_back + " tile pile with " +
              str(len(self.tile_piles[tile_back].tiles)) +
              " tiles, and shuffled it")