Example #1
0
 def __init__(self, subdivide, tilegrid=None, queue=None, seeds=()):
     self.subdivide = subdivide
     self.tilegrid = tilegrid
     if self.tilegrid is None:
         self.tilegrid = QuadTileGrid()
     self.queue = queue
     if self.queue is None:
         self.queue = deque()
     for seed in seeds:
         self.queue.append(seed)
Example #2
0
 def __init__(self, subdivide, tilegrid=None, queue=None, seeds=()):
     super(RenderingTheWorldTileStore, self).__init__()
     self.subdivide = subdivide
     self.tilegrid = tilegrid
     if self.tilegrid is None:
         self.tilegrid = QuadTileGrid()
     self.queue = queue
     if self.queue is None:
         self.queue = deque()
     for seed in seeds:
         self.queue.append(seed)
Example #3
0
class TestQuadTileGridFlipY(unittest.TestCase):

    def setUp(self):
        self.qtsn = QuadTileGrid()
        self.qtsf = QuadTileGrid(flip_y=True)

    def test_flip_y(self):
        self.assertEqual(self.qtsn.extent(TileCoord(2, 0, 0)), self.qtsf.extent(TileCoord(2, 0, 3)))
        self.assertEqual(self.qtsn.extent(TileCoord(2, 1, 1)), self.qtsf.extent(TileCoord(2, 1, 2)))
        self.assertEqual(self.qtsn.extent(TileCoord(2, 2, 2)), self.qtsf.extent(TileCoord(2, 2, 1)))
        self.assertEqual(self.qtsn.extent(TileCoord(2, 3, 3)), self.qtsf.extent(TileCoord(2, 3, 0)))
Example #4
0
class TestQuadTileGridFlipY(unittest.TestCase):

    def setUp(self):
        self.qtsn = QuadTileGrid()
        self.qtsf = QuadTileGrid(flip_y=True)

    def test_flip_y(self):
        assert self.qtsn.extent(TileCoord(2, 0, 0)) == self.qtsf.extent(TileCoord(2, 0, 3))
        assert self.qtsn.extent(TileCoord(2, 1, 1)) == self.qtsf.extent(TileCoord(2, 1, 2))
        assert self.qtsn.extent(TileCoord(2, 2, 2)) == self.qtsf.extent(TileCoord(2, 2, 1))
        assert self.qtsn.extent(TileCoord(2, 3, 3)) == self.qtsf.extent(TileCoord(2, 3, 0))
Example #5
0
class RenderingTheWorldTileStore(TileStore):
    """http://mapbox.com/blog/rendering-the-world/"""
    def __init__(self, subdivide, tilegrid=None, queue=None, seeds=()):
        super(RenderingTheWorldTileStore, self).__init__()
        self.subdivide = subdivide
        self.tilegrid = tilegrid
        if self.tilegrid is None:
            self.tilegrid = QuadTileGrid()
        self.queue = queue
        if self.queue is None:
            self.queue = deque()
        for seed in seeds:
            self.queue.append(seed)

    def list(self):
        try:
            while True:
                yield self.queue.popleft()
        except IndexError:
            pass

    def put_one(self, tile):
        if self.subdivide(tile):
            for tilecoord in self.tilegrid.children(tile.tilecoord):
                self.queue.append(Tile(tilecoord))
        return tile
Example #6
0
class RenderingTheWorldTileStore(TileStore):
    """
    http://mapbox.com/blog/rendering-the-world/
    """
    def __init__(
            self,
            subdivide: Callable[[Tile], Iterable[Tile]],
            tilegrid: Optional[TileGrid] = None,
            queue: Optional[Deque[Tile]] = None,
            seeds: Tuple[Tile, ...] = (),
    ):
        super().__init__()
        self.subdivide = subdivide
        self.tilegrid = tilegrid
        if self.tilegrid is None:
            self.tilegrid = QuadTileGrid()
        if queue is None:
            queue = deque()
        self.queue = queue
        for seed in seeds:
            self.queue.append(seed)

    def list(self) -> Iterator[Tile]:
        try:
            while True:
                yield self.queue.popleft()
        except IndexError:
            pass

    def put_one(self, tile: Tile) -> Tile:
        if self.subdivide(tile):
            for tilecoord in self.tilegrid.children(
                    tile.tilecoord):  # type: ignore
                self.queue.append(Tile(tilecoord))
        return tile
Example #7
0
class RenderingTheWorldTileStore(TileStore):
    """http://mapbox.com/blog/rendering-the-world/"""

    def __init__(self, subdivide, tilegrid=None, queue=None, seeds=()):
        self.subdivide = subdivide
        self.tilegrid = tilegrid
        if self.tilegrid is None:
            self.tilegrid = QuadTileGrid()
        self.queue = queue
        if self.queue is None:
            self.queue = deque()
        for seed in seeds:
            self.queue.append(seed)

    def list(self):
        try:
            while True:
                yield self.queue.popleft()
        except IndexError:
            pass

    def put_one(self, tile):
        if self.subdivide(tile):
            for tilecoord in self.tilegrid.children(tile.tilecoord):
                self.queue.append(Tile(tilecoord))
        return tile
Example #8
0
 def __init__(
         self,
         subdivide: Callable[[Tile], Iterable[Tile]],
         tilegrid: Optional[TileGrid] = None,
         queue: Optional[Deque[Tile]] = None,
         seeds: Tuple[Tile, ...] = (),
 ):
     super().__init__()
     self.subdivide = subdivide
     self.tilegrid = tilegrid
     if self.tilegrid is None:
         self.tilegrid = QuadTileGrid()
     if queue is None:
         queue = deque()
     self.queue = queue
     for seed in seeds:
         self.queue.append(seed)
Example #9
0
 def __init__(self, subdivide, tilegrid=None, queue=None, seeds=()):
     self.subdivide = subdivide
     self.tilegrid = tilegrid
     if self.tilegrid is None:
         self.tilegrid = QuadTileGrid()
     self.queue = queue
     if self.queue is None:
         self.queue = deque()
     for seed in seeds:
         self.queue.append(seed)
Example #10
0
 def __init__(self, subdivide, tilegrid=None, queue=None, seeds=()):
     super(RenderingTheWorldTileStore, self).__init__()
     self.subdivide = subdivide
     self.tilegrid = tilegrid
     if self.tilegrid is None:
         self.tilegrid = QuadTileGrid()
     self.queue = queue
     if self.queue is None:
         self.queue = deque()
     for seed in seeds:
         self.queue.append(seed)
Example #11
0
class TestFreeQuadTileGridEquivalence(unittest.TestCase):

    def setUp(self):
        self.ftg = FreeTileGrid([8, 4, 2, 1], tile_size=0.125)
        self.qtg = QuadTileGrid(max_zoom=3)

    def test_children(self):
        tc = TileCoord(2, 2, 3)
        self.assertEqual(sorted(self.ftg.children(tc)), sorted(self.qtg.children(tc)))

    def test_children_root(self):
        tc = TileCoord(0, 0, 0)
        self.assertEqual(sorted(self.ftg.children(tc)), sorted(self.qtg.children(tc)))

    def test_extent(self):
        for z in xrange(0, 4):
            for x in xrange(0, 1 << z):
                for y in xrange(0, 1 << z):
                    tilecoord = TileCoord(z, x, y)
                    self.assertEqual(self.ftg.extent(tilecoord), self.qtg.extent(tilecoord))

    def test_parent(self):
        tc = TileCoord(3, 3, 5)
        self.assertEqual(self.ftg.parent(tc), self.qtg.parent(tc))

    def test_roots(self):
        self.assertEqual(list(self.ftg.roots()), list(self.qtg.roots()))

    def test_zs(self):
        self.assertEqual(list(self.ftg.zs()), list(self.qtg.zs()))
Example #12
0
class TestFreeQuadTileGridEquivalence(unittest.TestCase):

    def setUp(self):
        self.ftg = FreeTileGrid([8, 4, 2, 1], tile_size=0.125)
        self.qtg = QuadTileGrid(max_zoom=3)

    def test_children(self):
        tc = TileCoord(2, 2, 3)
        assert sorted(self.ftg.children(tc)) == sorted(self.qtg.children(tc))

    def test_children_root(self):
        tc = TileCoord(0, 0, 0)
        assert sorted(self.ftg.children(tc)) == sorted(self.qtg.children(tc))

    def test_extent(self):
        for z in xrange(0, 4):
            for x in xrange(0, 1 << z):
                for y in xrange(0, 1 << z):
                    tilecoord = TileCoord(z, x, y)
                    assert self.ftg.extent(tilecoord) == self.qtg.extent(tilecoord)

    def test_parent(self):
        tc = TileCoord(3, 3, 5)
        assert self.ftg.parent(tc) == self.qtg.parent(tc)

    def test_roots(self):
        assert list(self.ftg.roots()) == list(self.qtg.roots())

    def test_zs(self):
        assert list(self.ftg.zs()) == list(self.qtg.zs())
Example #13
0
 def setUp(self):
     self.qtg = QuadTileGrid(max_extent=(0.0, 1.0, 2.0, 3.0))
Example #14
0
class TestQuadTileGrid(unittest.TestCase):

    def setUp(self):
        self.qtg = QuadTileGrid(max_extent=(0.0, 1.0, 2.0, 3.0))

    def test_children(self):
        self.assertEqual(
            sorted(self.qtg.children(TileCoord(1, 2, 3))),
            [TileCoord(2, 4, 6), TileCoord(2, 4, 7), TileCoord(2, 5, 6), TileCoord(2, 5, 7)])

    def test_children_root(self):
        self.assertEqual(
            sorted(self.qtg.children(TileCoord(0, 0, 0))),
            [TileCoord(1, 0, 0), TileCoord(1, 0, 1), TileCoord(1, 1, 0), TileCoord(1, 1, 1)])

    def test_extent_z0(self):
        self.assertEqual(self.qtg.extent(TileCoord(0, 0, 0)), (0.0, 1.0, 2.0, 3.0))

    def test_extent_z1(self):
        self.assertEqual(self.qtg.extent(TileCoord(1, 0, 0)), (0.0, 2.0, 1.0, 3.0))
        self.assertEqual(self.qtg.extent(TileCoord(1, 0, 1)), (0.0, 1.0, 1.0, 2.0))
        self.assertEqual(self.qtg.extent(TileCoord(1, 1, 0)), (1.0, 2.0, 2.0, 3.0))
        self.assertEqual(self.qtg.extent(TileCoord(1, 1, 1)), (1.0, 1.0, 2.0, 2.0))

    def test_extent_z2(self):
        self.assertEqual(self.qtg.extent(TileCoord(2, 0, 0)), (0.0, 2.5, 0.5, 3.0))
        self.assertEqual(self.qtg.extent(TileCoord(2, 1, 1)), (0.5, 2.0, 1.0, 2.5))
        self.assertEqual(self.qtg.extent(TileCoord(2, 2, 2)), (1.0, 1.5, 1.5, 2.0))
        self.assertEqual(self.qtg.extent(TileCoord(2, 3, 3)), (1.5, 1.0, 2.0, 1.5))

    def test_parent(self):
        self.assertEqual(self.qtg.parent(TileCoord(5, 11, 21)), TileCoord(4, 5, 10))

    def test_parent_root(self):
        self.assertEqual(self.qtg.parent(TileCoord(0, 0, 0)), None)

    def test_roots(self):
        self.assertEqual(list(self.qtg.roots()), [TileCoord(0, 0, 0)])

    def test_tilecoord(self):
        for z in xrange(0, 4):
            for x in xrange(0, 1 << z):
                for y in xrange(0, 1 << z):
                    tilecoord = TileCoord(z, x, y)
                    minx, miny, _, _ = self.qtg.extent(tilecoord)
                    self.assertEqual(self.qtg.tilecoord(z, minx, miny), tilecoord)

    def test_zs(self):
        self.assertEqual(list(islice(self.qtg.zs(), 50)), [e for e in range(50)])
Example #15
0
 def setUp(self):
     self.ftg = FreeTileGrid([8, 4, 2, 1], tile_size=0.125)
     self.qtg = QuadTileGrid(max_zoom=3)
Example #16
0
def main():
    parser = ArgumentParser(
        description=
        "Used to import the osm2pgsql expire-tiles file to Postgres",
        prog=sys.argv[0])
    parser.add_argument(
        "--buffer",
        type=float,
        default=0.0,
        help="Extent buffer to the tiles [m], default is 0",
    )
    parser.add_argument(
        "--simplify",
        type=float,
        default=0.0,
        help="Simplify the result geometry [m], default is 0",
    )
    parser.add_argument(
        "--create",
        default=False,
        action="store_true",
        help="create the table if not exists",
    )
    parser.add_argument(
        "--delete",
        default=False,
        action="store_true",
        help="empty the table",
    )
    parser.add_argument(
        "file",
        metavar="FILE",
        help="The osm2pgsql expire-tiles file",
    )
    parser.add_argument(
        "connection",
        metavar="CONNECTION",
        help=("The PostgreSQL connection string e.g. "
              '"user=www-data password=www-data dbname=sig host=localhost"'),
    )
    parser.add_argument(
        "table",
        metavar="TABLE",
        help="The PostgreSQL table to fill",
    )
    parser.add_argument(
        "--schema",
        default="public",
        help=
        "The PostgreSQL schema to use (should already exists), default is public",
    )
    parser.add_argument(
        "column",
        metavar="COLUMN",
        default="geom",
        nargs="?",
        help='The PostgreSQL column, default is "geom"',
    )
    parser.add_argument(
        "--srid",
        type=int,
        default=3857,
        nargs="?",
        help="The stored geometry SRID, no conversion by default (3857)",
    )
    options = parser.parse_args()

    connection = psycopg2.connect(options.connection)
    cursor = connection.cursor()

    if options.create:
        cursor.execute(
            "SELECT count(*) FROM pg_tables WHERE schemaname='{}' AND tablename='{}'"
            .format(options.schema, options.table))
        if cursor.fetchone()[0] == 0:
            cursor.execute(
                'CREATE TABLE IF NOT EXISTS "{}"."{}" (id serial)'.format(
                    options.schema, options.table))
            cursor.execute(
                "SELECT AddGeometryColumn('{}', '{}', '{}', {}, 'MULTIPOLYGON', 2)"
                .format(options.schema, options.table, options.column,
                        options.srid))

    if options.delete:
        cursor.execute('DELETE FROM "{}"'.format((options.table)))

    geoms = []
    grid = QuadTileGrid(max_extent=(-20037508.34, -20037508.34, 20037508.34,
                                    20037508.34), )
    with open(options.file, "r") as f:
        for coord in f:
            extent = grid.extent(parse_tilecoord(coord), options.buffer)
            geoms.append(
                Polygon((
                    (extent[0], extent[1]),
                    (extent[0], extent[3]),
                    (extent[2], extent[3]),
                    (extent[2], extent[1]),
                )))
    if len(geoms) == 0:
        print("No coords found")
        connection.commit()
        cursor.close()
        connection.close()
        sys.exit(0)
    geom = cascaded_union(geoms)
    if geom.geom_type == "Polygon":
        geom = MultiPolygon((geom, ))

    if options.simplify > 0:
        geom.simplify(options.simplify)

    sql_geom = "ST_GeomFromText('{}', 3857)".format(geom.wkt)
    if options.srid <= 0:
        sql_geom = "ST_GeomFromText('{}')".format(geom.wkt)  # pragma: no cover
    elif options.srid != 3857:
        sql_geom = "ST_Transform({}, {})".format(sql_geom, options.srid)

    cursor.execute('INSERT INTO "{}" ("{}") VALUES ({})'.format(
        options.table, options.column, sql_geom))
    connection.commit()
    cursor.close()
    connection.close()
    print("Import successful")
Example #17
0
def main():
    parser = ArgumentParser(
        description=
        'Used to import the osm2pgsql expire-tiles file to Postgres',
        prog='./buildout/bin/import_expire_tiles',
    )
    parser.add_argument(
        '--buffer',
        type=float,
        default=0.0,
        help='Extent buffer to the tiles [m], default is 0',
    )
    parser.add_argument(
        '--simplify',
        type=float,
        default=0.0,
        help='Simplify the result geometry [m], default is 0',
    )
    parser.add_argument(
        '--create',
        default=False,
        action="store_true",
        help='create the table if not exists',
    )
    parser.add_argument(
        '--delete',
        default=False,
        action="store_true",
        help='empty the table',
    )
    parser.add_argument(
        'file',
        metavar='FILE',
        help='The osm2pgsql expire-tiles file',
    )
    parser.add_argument(
        'connection',
        metavar='CONNECTION',
        help=
        'The PostgreSQL connection string e.g. "user=www-data password=www-data dbname=sig host=localhost"',
    )
    parser.add_argument(
        'table',
        metavar='TABLE',
        help='The PostgreSQL table to fill',
    )
    parser.add_argument(
        '--schema',
        default='public',
        help=
        'The PostgreSQL schema to use (should already exists), default is public',
    )
    parser.add_argument(
        'column',
        metavar='COLUMN',
        default='geom',
        nargs='?',
        help='The PostgreSQL column, default is "geom"',
    )
    parser.add_argument(
        '--srid',
        type=int,
        default=3857,
        nargs='?',
        help='The stored geometry SRID, no conversion by default (3857)',
    )
    options = parser.parse_args()

    connection = psycopg2.connect(options.connection)
    cursor = connection.cursor()

    if options.create:
        cursor.execute(
            "SELECT count(*) FROM pg_tables WHERE schemaname='%s' AND tablename='%s'"
            % (
                options.schema,
                options.table,
            ))
        if cursor.fetchone()[0] == 0:
            cursor.execute('CREATE TABLE IF NOT EXISTS "%s"."%s" (id serial)' %
                           (
                               options.schema,
                               options.table,
                           ))
            cursor.execute(
                "SELECT AddGeometryColumn('%s', '%s', '%s', %s, 'MULTIPOLYGON', 2)"
                % (
                    options.schema,
                    options.table,
                    options.column,
                    options.srid,
                ))

    if options.delete:
        cursor.execute('DELETE FROM "%s"' % (options.table))

    geoms = []
    f = file(options.file, 'r')
    grid = QuadTileGrid(max_extent=(-20037508.34, -20037508.34, 20037508.34,
                                    20037508.34), )
    for coord in f:
        extent = grid.extent(parse_tilecoord(coord), options.buffer)
        geoms.append(
            Polygon(((extent[0], extent[1]), (extent[0], extent[3]),
                     (extent[2], extent[3]), (extent[2], extent[1]))))
    f.close()
    if len(geoms) == 0:
        print "No coords found"
        connection.commit()
        cursor.close()
        connection.close()
        exit(0)
    geom = cascaded_union(geoms)
    if geom.geom_type == 'Polygon':
        geom = MultiPolygon((geom, ))

    if options.simplify > 0:
        geom.simplify(options.simplify)

    sql_geom = "ST_GeomFromText('%s', 3857)" % geom.wkt
    if options.srid <= 0:
        sql_geom = "ST_GeomFromText('%s')" % geom.wkt  # pragma: no cover
    elif options.srid != 3857:
        sql_geom = 'ST_Transform(%s, %i)' % (sql_geom, options.srid)

    cursor.execute('INSERT INTO "%s" ("%s") VALUES (%s)' %
                   (options.table, options.column, sql_geom))
    connection.commit()
    cursor.close()
    connection.close()
    print 'Import successful'
Example #18
0
class TestQuadTileGrid(unittest.TestCase):

    def setUp(self):
        self.qtg = QuadTileGrid(max_extent=(0.0, 1.0, 2.0, 3.0))

    def test_children(self):
        assert sorted(self.qtg.children(TileCoord(1, 2, 3))) == \
            [TileCoord(2, 4, 6), TileCoord(2, 4, 7), TileCoord(2, 5, 6), TileCoord(2, 5, 7)]

    def test_children_root(self):
        assert sorted(self.qtg.children(TileCoord(0, 0, 0))) == \
            [TileCoord(1, 0, 0), TileCoord(1, 0, 1), TileCoord(1, 1, 0), TileCoord(1, 1, 1)]

    def test_extent_z0(self):
        assert self.qtg.extent(TileCoord(0, 0, 0)) == (0.0, 1.0, 2.0, 3.0)

    def test_extent_z1(self):
        assert self.qtg.extent(TileCoord(1, 0, 0)) == (0.0, 2.0, 1.0, 3.0)
        assert self.qtg.extent(TileCoord(1, 0, 1)) == (0.0, 1.0, 1.0, 2.0)
        assert self.qtg.extent(TileCoord(1, 1, 0)) == (1.0, 2.0, 2.0, 3.0)
        assert self.qtg.extent(TileCoord(1, 1, 1)) == (1.0, 1.0, 2.0, 2.0)

    def test_extent_z2(self):
        assert self.qtg.extent(TileCoord(2, 0, 0)) == (0.0, 2.5, 0.5, 3.0)
        assert self.qtg.extent(TileCoord(2, 1, 1)) == (0.5, 2.0, 1.0, 2.5)
        assert self.qtg.extent(TileCoord(2, 2, 2)) == (1.0, 1.5, 1.5, 2.0)
        assert self.qtg.extent(TileCoord(2, 3, 3)) == (1.5, 1.0, 2.0, 1.5)

    def test_parent(self):
        assert self.qtg.parent(TileCoord(5, 11, 21)) == TileCoord(4, 5, 10)

    def test_parent_root(self):
        assert self.qtg.parent(TileCoord(0, 0, 0)) is None

    def test_roots(self):
        assert list(self.qtg.roots()) == [TileCoord(0, 0, 0)]

    def test_tilecoord(self):
        for z in xrange(0, 4):
            for x in xrange(0, 1 << z):
                for y in xrange(0, 1 << z):
                    tilecoord = TileCoord(z, x, y)
                    minx, miny, _, _ = self.qtg.extent(tilecoord)
                    assert self.qtg.tilecoord(z, minx, miny) == tilecoord

    def test_zs(self):
        assert list(islice(self.qtg.zs(), 50)) == [e for e in range(50)]
Example #19
0
from tilecloud.grid.quad import QuadTileGrid

GoogleTileGrid = QuadTileGrid(max_extent=(-20037508.34, -20037508.34,
                                          20037508.34, 20037508.34),
                              tile_size=256)
Example #20
0
def main() -> None:
    """Import the osm2pgsql expire-tiles file to Postgres."""
    try:
        parser = ArgumentParser(
            description=
            "Used to import the osm2pgsql expire-tiles file to Postgres",
            prog=sys.argv[0])
        parser.add_argument(
            "--buffer",
            type=float,
            default=0.0,
            help="Extent buffer to the tiles [m], default is 0",
        )
        parser.add_argument(
            "--simplify",
            type=float,
            default=0.0,
            help="Simplify the result geometry [m], default is 0",
        )
        parser.add_argument(
            "--create",
            default=False,
            action="store_true",
            help="create the table if not exists",
        )
        parser.add_argument(
            "--delete",
            default=False,
            action="store_true",
            help="empty the table",
        )
        parser.add_argument(
            "file",
            metavar="FILE",
            help="The osm2pgsql expire-tiles file",
        )
        parser.add_argument(
            "connection",
            metavar="CONNECTION",
            help=(
                "The PostgreSQL connection string e.g. "
                '"user=www-data password=www-data dbname=sig host=localhost"'),
        )
        parser.add_argument(
            "table",
            metavar="TABLE",
            help="The PostgreSQL table to fill",
        )
        parser.add_argument(
            "--schema",
            default="public",
            help=
            "The PostgreSQL schema to use (should already exists), default is public",
        )
        parser.add_argument(
            "column",
            metavar="COLUMN",
            default="geom",
            nargs="?",
            help='The PostgreSQL column, default is "geom"',
        )
        parser.add_argument(
            "--srid",
            type=int,
            default=3857,
            nargs="?",
            help="The stored geometry SRID, no conversion by default (3857)",
        )
        options = parser.parse_args()

        connection = psycopg2.connect(options.connection)
        cursor = connection.cursor()

        if options.create:
            cursor.execute(
                "SELECT count(*) FROM pg_tables WHERE schemaname=%(schema)s AND tablename=%(table)s",
                {
                    "schema": options.schema,
                    "table": options.table
                },
            )
            if cursor.fetchone()[0] == 0:
                cursor.execute(
                    f'CREATE TABLE IF NOT EXISTS "{options.schema}"."{options.table}" (id serial)'
                )
                cursor.execute(
                    "SELECT AddGeometryColumn(%(schema)s, %(table)s, %(column)s, %(srid)s, 'MULTIPOLYGON', 2)",
                    {
                        "schema": options.schema,
                        "table": options.table,
                        "column": options.column,
                        "srid": options.srid,
                    },
                )

        if options.delete:
            cursor.execute(
                psycopg2.sql.SQL("DELETE FROM {}").format(
                    psycopg2.sql.Identifier(options.table)))

        geoms = []
        grid = QuadTileGrid(max_extent=(-20037508.34, -20037508.34,
                                        20037508.34, 20037508.34), )
        with open(options.file, encoding="utf-8") as f:
            for coord in f:
                extent = grid.extent(parse_tilecoord(coord), options.buffer)
                geoms.append(
                    Polygon((
                        (extent[0], extent[1]),
                        (extent[0], extent[3]),
                        (extent[2], extent[3]),
                        (extent[2], extent[1]),
                    )))
        if len(geoms) == 0:
            print("No coords found")
            connection.commit()
            cursor.close()
            connection.close()
            sys.exit(0)
        geom = unary_union(geoms)
        if geom.geom_type == "Polygon":
            geom = MultiPolygon((geom, ))

        if options.simplify > 0:
            geom.simplify(options.simplify)

        sql_geom = f"ST_GeomFromText('{geom.wkt}', 3857)"
        if options.srid <= 0:
            sql_geom = f"ST_GeomFromText('{geom.wkt}')"
        elif options.srid != 3857:
            sql_geom = f"ST_Transform({sql_geom}, {options.srid})"

        cursor.execute(
            f'INSERT INTO "{options.table}" ("{options.column}") VALUES ({sql_geom})'
        )
        connection.commit()
        cursor.close()
        connection.close()
        print("Import successful")
    except SystemExit:
        raise
    except:  # pylint: disable=bare-except
        logger.exception("Exit with exception")
        sys.exit(1)
Example #21
0
 def setUp(self):
     self.qtsn = QuadTileGrid()
     self.qtsf = QuadTileGrid(flip_y=True)
Example #22
0
class TestQuadTileGrid(unittest.TestCase):

    def setUp(self):
        self.qtg = QuadTileGrid(max_extent=(0.0, 1.0, 2.0, 3.0))

    def test_children(self):
        self.assertEqual(sorted(self.qtg.children(TileCoord(1, 2, 3))), [TileCoord(2, 4, 6), TileCoord(2, 4, 7), TileCoord(2, 5, 6), TileCoord(2, 5, 7)])

    def test_children_root(self):
        self.assertEqual(sorted(self.qtg.children(TileCoord(0, 0, 0))), [TileCoord(1, 0, 0), TileCoord(1, 0, 1), TileCoord(1, 1, 0), TileCoord(1, 1, 1)])

    def test_extent_z0(self):
        self.assertEqual(self.qtg.extent(TileCoord(0, 0, 0)), (0.0, 1.0, 2.0, 3.0))

    def test_extent_z1(self):
        self.assertEqual(self.qtg.extent(TileCoord(1, 0, 0)), (0.0, 2.0, 1.0, 3.0))
        self.assertEqual(self.qtg.extent(TileCoord(1, 0, 1)), (0.0, 1.0, 1.0, 2.0))
        self.assertEqual(self.qtg.extent(TileCoord(1, 1, 0)), (1.0, 2.0, 2.0, 3.0))
        self.assertEqual(self.qtg.extent(TileCoord(1, 1, 1)), (1.0, 1.0, 2.0, 2.0))

    def test_extent_z2(self):
        self.assertEqual(self.qtg.extent(TileCoord(2, 0, 0)), (0.0, 2.5, 0.5, 3.0))
        self.assertEqual(self.qtg.extent(TileCoord(2, 1, 1)), (0.5, 2.0, 1.0, 2.5))
        self.assertEqual(self.qtg.extent(TileCoord(2, 2, 2)), (1.0, 1.5, 1.5, 2.0))
        self.assertEqual(self.qtg.extent(TileCoord(2, 3, 3)), (1.5, 1.0, 2.0, 1.5))

    def test_parent(self):
        self.assertEqual(self.qtg.parent(TileCoord(5, 11, 21)), TileCoord(4, 5, 10))

    def test_parent_root(self):
        self.assertEqual(self.qtg.parent(TileCoord(0, 0, 0)), None)

    def test_roots(self):
        self.assertEqual(list(self.qtg.roots()), [TileCoord(0, 0, 0)])

    def test_tilecoord(self):
        for z in xrange(0, 4):
            for x in xrange(0, 1 << z):
                for y in xrange(0, 1 << z):
                    tilecoord = TileCoord(z, x, y)
                    minx, miny, maxx, maxy = self.qtg.extent(tilecoord)
                    self.assertEqual(self.qtg.tilecoord(z, minx, miny), tilecoord)

    def test_zs(self):
        self.assertEqual(list(islice(self.qtg.zs(), 50)), range(50))
Example #23
0
 def setUp(self):
     self.qtg = QuadTileGrid(max_extent=(0.0, 1.0, 2.0, 3.0))
Example #24
0
def main():
    parser = ArgumentParser(
        description='Used to import the osm2pgsql expire-tiles file to Postgres',
        prog='./buildout/bin/import_expire_tiles',
    )
    parser.add_argument(
        '--buffer',
        type=float,
        default=0.0,
        help='Extent buffer to the tiles [m], default is 0',
    )
    parser.add_argument(
        '--simplify',
        type=float,
        default=0.0,
        help='Simplify the result geometry [m], default is 0',
    )
    parser.add_argument(
        '--create',
        default=False,
        action="store_true",
        help='create the table if not exists',
    )
    parser.add_argument(
        '--delete',
        default=False,
        action="store_true",
        help='empty the table',
    )
    parser.add_argument(
        'file',
        metavar='FILE',
        help='The osm2pgsql expire-tiles file',
    )
    parser.add_argument(
        'connection',
        metavar='CONNECTION',
        help='The PostgreSQL connection string e.g. "user=www-data password=www-data dbname=sig host=localhost"',
    )
    parser.add_argument(
        'table',
        metavar='TABLE',
        help='The PostgreSQL table to fill',
    )
    parser.add_argument(
        '--schema',
        default='public',
        help='The PostgreSQL schema to use (should already exists), default is public',
    )
    parser.add_argument(
        'column',
        metavar='COLUMN',
        default='geom',
        nargs='?',
        help='The PostgreSQL column, default is "geom"',
    )
    parser.add_argument(
        '--srid',
        type=int,
        default=3857,
        nargs='?',
        help='The stored geometry SRID, no conversion by default (3857)',
    )
    options = parser.parse_args()

    connection = psycopg2.connect(options.connection)
    cursor = connection.cursor()

    if options.create:
        cursor.execute(
            "SELECT count(*) FROM pg_tables WHERE schemaname='%s' AND tablename='%s'" % (
                options.schema, options.table,
            )
        )
        if cursor.fetchone()[0] == 0:
            cursor.execute('CREATE TABLE IF NOT EXISTS "%s"."%s" (id serial)' % (
                options.schema, options.table,
            ))
            cursor.execute("SELECT AddGeometryColumn('%s', '%s', '%s', %s, 'MULTIPOLYGON', 2)" % (
                options.schema, options.table, options.column, options.srid,
            ))

    if options.delete:
        cursor.execute('DELETE FROM "%s"' % (options.table))

    geoms = []
    f = file(options.file, 'r')
    grid = QuadTileGrid(
        max_extent=(-20037508.34, -20037508.34, 20037508.34, 20037508.34),
    )
    for coord in f:
        extent = grid.extent(parse_tilecoord(coord), options.buffer)
        geoms.append(Polygon((
            (extent[0], extent[1]),
            (extent[0], extent[3]),
            (extent[2], extent[3]),
            (extent[2], extent[1])
        )))
    f.close()
    if len(geoms) == 0:
        print "No coords found"
        connection.commit()
        cursor.close()
        connection.close()
        exit(0)
    geom = cascaded_union(geoms)
    if geom.geom_type == 'Polygon':
        geom = MultiPolygon((geom,))

    if options.simplify > 0:
        geom.simplify(options.simplify)

    sql_geom = "ST_GeomFromText('%s', 3857)" % geom.wkt
    if options.srid <= 0:
        sql_geom = "ST_GeomFromText('%s')" % geom.wkt  # pragma: no cover
    elif options.srid != 3857:
        sql_geom = 'ST_Transform(%s, %i)' % (sql_geom, options.srid)

    cursor.execute('INSERT INTO "%s" ("%s") VALUES (%s)' % (
        options.table, options.column, sql_geom
    ))
    connection.commit()
    cursor.close()
    connection.close()
    print 'Import successful'
Example #25
0
class TestQuadTileGrid(unittest.TestCase):
    def setUp(self) -> None:
        self.qtg = QuadTileGrid(max_extent=(0.0, 1.0, 2.0, 3.0))

    def test_children(self) -> None:
        assert sorted(self.qtg.children(TileCoord(1, 2, 3))) == [
            TileCoord(2, 4, 6),
            TileCoord(2, 4, 7),
            TileCoord(2, 5, 6),
            TileCoord(2, 5, 7),
        ]

    def test_children_root(self) -> None:
        assert sorted(self.qtg.children(TileCoord(0, 0, 0))) == [
            TileCoord(1, 0, 0),
            TileCoord(1, 0, 1),
            TileCoord(1, 1, 0),
            TileCoord(1, 1, 1),
        ]

    def test_extent_z0(self) -> None:
        assert self.qtg.extent(TileCoord(0, 0, 0)) == (0.0, 1.0, 2.0, 3.0)

    def test_extent_z1(self) -> None:
        assert self.qtg.extent(TileCoord(1, 0, 0)) == (0.0, 2.0, 1.0, 3.0)
        assert self.qtg.extent(TileCoord(1, 0, 1)) == (0.0, 1.0, 1.0, 2.0)
        assert self.qtg.extent(TileCoord(1, 1, 0)) == (1.0, 2.0, 2.0, 3.0)
        assert self.qtg.extent(TileCoord(1, 1, 1)) == (1.0, 1.0, 2.0, 2.0)

    def test_extent_z2(self) -> None:
        assert self.qtg.extent(TileCoord(2, 0, 0)) == (0.0, 2.5, 0.5, 3.0)
        assert self.qtg.extent(TileCoord(2, 1, 1)) == (0.5, 2.0, 1.0, 2.5)
        assert self.qtg.extent(TileCoord(2, 2, 2)) == (1.0, 1.5, 1.5, 2.0)
        assert self.qtg.extent(TileCoord(2, 3, 3)) == (1.5, 1.0, 2.0, 1.5)

    def test_parent(self) -> None:
        assert self.qtg.parent(TileCoord(5, 11, 21)) == TileCoord(4, 5, 10)

    def test_parent_root(self) -> None:
        assert self.qtg.parent(TileCoord(0, 0, 0)) is None

    def test_roots(self) -> None:
        assert list(self.qtg.roots()) == [TileCoord(0, 0, 0)]

    def test_tilecoord(self) -> None:
        for z in range(0, 4):
            for x in range(0, 1 << z):
                for y in range(0, 1 << z):
                    tilecoord = TileCoord(z, x, y)
                    minx, miny, _, _ = self.qtg.extent(tilecoord)
                    assert self.qtg.tilecoord(z, minx, miny) == tilecoord

    def test_zs(self) -> None:
        assert list(islice(self.qtg.zs(), 50)) == [e for e in range(50)]
Example #26
0
 def setUp(self):
     self.qtsn = QuadTileGrid()
     self.qtsf = QuadTileGrid(flip_y=True)
Example #27
0
 def setUp(self):
     self.ftg = FreeTileGrid([8, 4, 2, 1], tile_size=0.125)
     self.qtg = QuadTileGrid(max_zoom=3)