Exemple #1
0
def main1():
    input = sys.argv[1]
    output = sys.argv[2]
    pyramid = Pyramid(levels=range(0, 22))
    features = list()
    with open(input, 'rb') as fp:
        reader = csv.reader(fp)
        for row in reader:
            z, x, y = tuple(map(int, row))
            tile_index = pyramid.create_tile_index(z, x, y, range_check=False)
            envelope = tile_index.envelope
            coordinates = [[
                envelope.leftbottom.make_tuple(),
                envelope.lefttop.make_tuple(),
                envelope.righttop.make_tuple(),
                envelope.rightbottom.make_tuple(),
#                envelope.leftbottom.make_tuple(),
            ]]

            polygon = geojson.Polygon(coordinates=coordinates)
            feature = geojson.Feature(geometry=polygon, properties=dict(z=z, x=x, y=y))
            features.append(feature)

    collection = geojson.FeatureCollection(features=features)

    with open(output, 'wb') as fp:
        geojson.dump(collection, fp,
#                     indent=2
                     )
class TestFileClusterStorage(TileStorageTestMixin, unittest.TestCase):

    def setUp(self):
        self.pyramid = Pyramid(levels=range(21), format=Format.DATA)
        self.metadata = Metadata.make_metadata(tag='TestClusterStorage',
                                               version='1.0.0.0.0.0')
        self.output_dir = os.path.join('output', 'TestClusterStorage')

        if os.path.exists(self.output_dir):
            shutil.rmtree(self.output_dir, ignore_errors=True)

        self.storage = factory('cluster',
                               self.pyramid,
                               self.metadata,
                               stride=2,
                               servers=['localhost:11211'],
                               timeout=0,
                               root=self.output_dir,
                               compress=False,)

    def tearDown(self):
#        self.storage.flush_all()
        self.storage.close()

    def testGetPutMulti(self):
        tiles = [self.pyramid.create_tile(4, 0, 0, b'tile1'),
                 self.pyramid.create_tile(4, 1, 0, b'tile2'),
                 self.pyramid.create_tile(4, 0, 1, b'tile3'),
                 self.pyramid.create_tile(4, 1, 1, b'tile2'), ]

        self.storage.put_multi(tiles)

        tileindex1 = self.pyramid.create_tile_index(4, 0, 1)
        tileindex2 = self.pyramid.create_tile_index(4, 1, 1)

        tile1 = self.storage.get(tileindex1)
        self.assertEqual(tile1.data, b'tile3')

        tile2 = self.storage.get(tileindex2)
        self.assertEqual(tile2.data, b'tile2')
class TestMBTilesTileStorageBackgroundWriter(unittest.TestCase):

    def setUp(self):
        self.pyramid = Pyramid(levels=range(21), format=Format.DATA)

        self.metadata = Metadata.make_metadata(tag='TestMBTilesTileStorageBackgroundWriter',)
        filename = './output/TestMBTilesTileStorageBackgroundWriter.db'
        if os.path.exists(filename):
            os.unlink(filename)
        self.storage = factory('mbtilesbw',
                               self.pyramid,
                               self.metadata,
                               database=filename)

    def tearDown(self):
#        self.storage.flush_all()
        self.storage.close()

    def testGetPut(self):
        tile1 = self.pyramid.create_tile(3, 4, 5, b'tile1')
        self.storage.put(tile1)
        self.storage.sync()
        tileindex1 = self.pyramid.create_tile_index(3, 4, 5)
        tileindex2 = self.pyramid.create_tile_index(3, 4, 6)
        self.assertTrue(self.storage.get(tileindex1) is not None)
        self.assertTrue(self.storage.get(tileindex2) is None)

        tile3 = self.storage.get(tileindex1)

        self.assertEqual(tile1.index, tile3.index)
        self.assertEqual(tile1.data, tile3.data)

        self.assertTrue(self.storage.has(tileindex1))
        self.storage.delete(tileindex1)
        self.storage.sync()
        self.assertFalse(self.storage.has(tileindex1))

    def testGetPutMulti(self):
        tile1 = self.pyramid.create_tile(4, 5, 6, b'tile1')
        tile2 = self.pyramid.create_tile(4, 5, 7, b'tile2')
        tile3 = self.pyramid.create_tile(4, 5, 8, b'tile3')

        self.storage.put_multi([tile1, tile2, tile3])
        self.storage.sync()

        tileindex1 = self.pyramid.create_tile_index(4, 5, 6)
        tileindex2 = self.pyramid.create_tile_index(4, 5, 7)
        tileindex3 = self.pyramid.create_tile_index(4, 5, 9)
        tileindex4 = self.pyramid.create_tile_index(4, 5, 10)

        tiles1 = self.storage.get_multi([tileindex1, tileindex2])

        self.assertSetEqual(set(tiles1.keys()),
                            set([tileindex1, tileindex2]))
        self.assertEqual(tiles1[tileindex1].data, tile1.data)
        self.assertDictEqual(self.storage.get_multi([tileindex3,
                                                     tileindex4])
                             , {})

        tiles2 = self.storage.get_multi([tileindex1, tileindex2,
                                         tileindex3])

        self.assertSetEqual(set(tiles2.keys()),
                            set([tileindex1, tileindex2]))

        self.assertTrue(self.storage.has_all([tileindex1, tileindex2]))
        self.assertFalse(self.storage.has_all([tileindex1, tileindex4]))
        self.assertTrue(self.storage.has_any([tileindex1, tileindex4]))
        self.storage.delete_multi([tileindex1, tileindex3, tileindex4])
        self.storage.sync()
        self.assertTrue(self.storage.has_any([tileindex1, tileindex2]))
        self.assertFalse(self.storage.has_any([tileindex1, tileindex3]))