예제 #1
0
    def testWritePage(self):
        random.seed(0)
        db = Database(MapDirectory(self.testdatadir), "db00", 'a')
        f = db.getFileByIndex(4)
        npages = f.npages
        pagesize = f.fstruct.ft_pgsize

        writtendata = "".join(
            [chr(random.randint(0, 255)) for i in xrange(pagesize)])
        #        import pdb
        #        pdb.set_trace()
        ## Override incomplete page
        f.writePage(writtendata, 1)

        ## Update pz.next so that the incomplete_page buffer won't overwrite the written page
        f.pz.next = f.fstruct.ft_slots * f.page + 1
        assert (f.page == 2)
        assert (f.relslotnum == 0)

        db.close()

        ## Read back
        db = Database(MapDirectory(self.testdatadir), "db00", 'r')
        f = db.getFileByIndex(4)
        self.assertEqual(f.readPage(1), writtendata)
예제 #2
0
    def testOpenAppend(self):
        db = Database(MapDirectory(self.testdatadir), "db00", 'a')
        f = db.getFileByIndex(4)
        nslotsbefore = f.pz.next - 1

        self.assertEqual(nslotsbefore, 3)

        slotdatabefore = [f.readSlot(i + 1) for i in range(nslotsbefore)]

        db.close()

        ## Read back
        db = Database(MapDirectory(self.testdatadir), "db00", 'r')
        f = db.getFileByIndex(4)
        nslots = f.pz.next - 1

        self.assertEqual(nslots, nslotsbefore)

        slotdata = [f.readSlot(i + 1) for i in range(nslots)]

        self.assertEqual(len(slotdata), len(slotdatabefore))
        if slotdata != slotdatabefore:
            print "Equal elements", [
                a == b for a, b in zip(slotdatabefore, slotdata)
            ]
        self.assertEqual(slotdata, slotdatabefore)
예제 #3
0
    def testWriteSlot(self):
        random.seed(0)
        db = Database(MapDirectory(self.testdatadir), "db00", 'a')
        f = db.getFileByIndex(4)
        nslots = f.pz.next - 1

        slotdatabefore = [f.readSlot(i + 1) for i in range(nslots)]

        slotsize = f.fstruct.ft_slsize
        writtendata = "".join(
            [chr(random.randint(0, 255)) for i in xrange(slotsize)])
        slot = 2
        f.writeSlot(writtendata, slot)

        db.close()

        ## Read back
        db = Database(MapDirectory(self.testdatadir), "db00", 'r')
        f = db.getFileByIndex(4)
        slotdata = [f.readSlot(i + 1) for i in range(nslots)]

        slotdataexpected = slotdatabefore
        slotdataexpected[slot - 1] = writtendata

        self.assertEqual(slotdataexpected, slotdata)
예제 #4
0
    def testWriteNewSlot(self):
        random.seed(0)
        db = Database(MapDirectory(self.testdatadir), "db00", 'a')
        f = db.getFileByIndex(4)
        nslots = f.pz.next - 1

        slotdatabefore = [f.readSlot(i + 1) for i in range(nslots)]

        slotsize = f.fstruct.ft_slsize
        writtendata = "".join(
            [chr(random.randint(0, 255)) for i in xrange(slotsize)])

        f.writeSlot(writtendata)

        db.close()

        ## Read back
        db = Database(MapDirectory(self.testdatadir), "db00", 'r')
        f = db.getFileByIndex(4)
        nslots = f.pz.next - 1
        slotdata = [f.readSlot(i + 1) for i in range(nslots)]

        slotdataexpected = slotdatabefore + [writtendata]

        self.assertEqual(len(slotdataexpected), len(slotdata))
        if slotdataexpected != slotdata:
            print "Equal elements", [
                a == b for a, b in zip(slotdataexpected, slotdata)
            ]
        self.assertEqual(slotdataexpected, slotdata)
예제 #5
0
    def testAddData(self):
        db = Database(MapDirectory(self.testdatadir), "db00", 'a')
        table = db.getTableByName("R_GR0")

        beforedata = [curs.getRow().asList() for curs in table.getCursor(0)]

        ## Add an empty row
        refrow = Row(table)
        refrow.setColumn(0, 1)
        table.writeRow(refrow)

        refdata = refrow.asList()

        db.close()

        db = Database(MapDirectory(self.testdatadir), "db00")
        table = db.getTableByName("R_GR0")

        aux = db.getTableByName("AUX_GR0")
        am = AuxTableManager(aux)

        afterdata = [curs.getRow().asList() for curs in table.getCursor(0)]

        for curs in table.getCursor(0):
            row = curs.getRow()

            index = row.asDict()['NAME_REF'] & 0xffffff
            offset = row.asDict()['NAME_REF'] >> 24

        self.assertEqual(beforedata + [refdata], afterdata)
예제 #6
0
    def testAddRows(self):
        random.seed(0)
        db = Database(MapDirectory(self.testdatadir), "db00", 'a')
        table = db.getTableByName("R_GR0")

        rows = [curs.getRow() for curs in table.getCursor(0)]

        data = [
            "".join([
                chr(random.randint(0, 255))
                for i in xrange(table.rstruct.rt_len)
            ]) for j in xrange(5)
        ]

        newrows = [Row(table, data=d) for d in data]

        for row in newrows:
            table.writeRow(row)

        self.assertEqual(table.getRowCount(), 29 + 5)

        db.close()

        ## Read back
        db = Database(MapDirectory(self.testdatadir), "db00", 'r')
        table = db.getTableByName("R_GR0")

        rowsafter = [curs.getRow() for curs in table.getCursor(0)]

        self.assertEqual(table.getRowCount(), 29 + 5)

        self.assertEqual(rows + newrows, rowsafter)
예제 #7
0
    def testCreateDatabase(self):
        mapdir = MapDirectory()
        db = Database(mapdir, "db00", "w")
        db.close()

        self.assertTrue("db00.dbd" in mapdir.listdir())

        #        os.system('prdbd '+os.path.join(mapdir.dir, 'db00'))

        db = Database(mapdir, "db00", "r")

        db.close()
예제 #8
0
    def testCreateDatabase(self):
        mapdir = MapDirectory()
        db = Database(mapdir, "db00", 'w')
        db.close()

        self.assertTrue('db00.dbd' in mapdir.listdir())

        #        os.system('prdbd '+os.path.join(mapdir.dir, 'db00'))

        db = Database(mapdir, "db00", 'r')

        db.close()
예제 #9
0
    def testOpenWrite(self):
        db = Database(MapDirectory(self.testdatadir), "db00", 'r')
        f = db.getFileByIndex(4)
        f.close()
        f.open('w')
        nslotsbefore = f.pz.next - 1

        self.assertEqual(nslotsbefore, 0)

        db.close()

        ## Read back
        db = Database(MapDirectory(self.testdatadir), "db00", 'r')
        f = db.getFileByIndex(4)
        nslots = f.pz.next - 1

        self.assertEqual(nslots, 0)
예제 #10
0
    def testSimple(self):
        map = Map(MapDirectory(self.testdatadir), maptype=MapTypeImage)
        map.open('r')

        poigroup = map.getPOIGroup()
        poigroup.open('r')

        f = poigroup.getFeatureByIndex(0)
        aux = f.getAuxAsDict(poigroup)
        print f, aux
예제 #11
0
    def testAuxIndex(self):
        db = Database(MapDirectory(self.testdatadir), "db00")

        table = db.getTableByName("R_GR0")
        aux = db.getTableByName("AUX_GR0")

        rows = [x.getRow() for x in aux.getCursor(0)]
        index = rows[0].asDict()['NAME_BUF']

        n = len(index) / 4
        index = struct.unpack('%dI' % n, index)
예제 #12
0
    def testSimple(self):
        map = Map(MapDirectory(self.testdatadir), maptype=MapTypeImage)
        map.open('a')

        map.addPOIGroupAndLayer()
        poigroup = map.getPOIGroup()

        cat = POICategory("Aerials")
        cat.addField("POI Name")
        subcat = cat.addSubCategory(POISubCategory("NOSUB1000"))

        expected = [cat]

        poigroup.addCategory(cat)

        #        cat.addField

        actual = poigroup.getCategories()

        if actual != expected:
            print 'actual:', actual
            print 'expected:', expected

        self.assertEqual(actual, expected)

        # Close map and try it to open it again
        map.close()

        map = Map()

        map = createMap(self.testdatadir)
        map.open()

        poigroup = map.getPOIGroup()

        poigroup.open('r')

        catman = poigroup.getCategoryManager()

        print catman.getCategories()

        self.assertTrue(
            "Aerials" in [cat.getName() for cat in catman.getCategories()])

        actual = poigroup.getCategories()

        if actual != expected:
            print 'actual:', actual
            print 'expected:', expected

        self.assertEqual(actual, expected,
                         "categories were not read back correctly")
예제 #13
0
    def testCategories(self):
        map = Map(MapDirectory(self.testdatadir), maptype=MapTypeImage)
        map.open('a')

        poigroup = map.getPOIGroup()

        poigroup.open('a')

        catman = poigroup.getCategoryManager()

        print catman.getCategories()

        poigroup.close()
예제 #14
0
    def testOpenForAppend(self):
        random.seed(0)
        db = Database(MapDirectory(self.testdatadir), "db00", 'a')
        table = db.getTableByName("R_GR0")

        databefore = [curs.getRow() for curs in table.getCursor(0)]
        rowcountbefore = table.getRowCount()

        self.assertEqual(rowcountbefore, 29)

        db.close()

        ## Read back
        db = Database(MapDirectory(self.testdatadir), "db00", 'r')
        table = db.getTableByName("R_GR0")

        dataafter = [curs.getRow() for curs in table.getCursor(0)]
        rowcountafter = table.getRowCount()

        self.assertEqual(rowcountbefore, rowcountafter)
        self.assertEqual(len(databefore), len(dataafter))
        self.assertEqual(databefore, dataafter)
예제 #15
0
    def testSimple(self):
        db = Database(MapDirectory(self.testdatadir), "db00")

        table = db.getTableByName("R_GR0")

        for curs in table.getCursor(0):
            row = curs.getRow()

            aux = db.getTableByName("AUX_GR0")

            am = AuxTableManager(aux)

            index = row.asDict()['NAME_REF'] & 0xffffff
            offset = row.asDict()['NAME_REF'] >> 24
예제 #16
0
    def testAddCategory(self):
        map = Map(MapDirectory(self.testdatadir), maptype=MapTypeImage)
        map.open('a')
        poigroup = map.getPOIGroup()
        poilayer = map.getPOILayers()[0]
        #        poilayer.open('a')
        poigroup.open('a')
        map.close()
        map.writeImage("test.imi")

        map = Map(MapDirectory(self.testdatadir), maptype=MapTypeImage)
        map.open('a')

        poigroup = map.getPOIGroup()

        poigroup.open('a')

        cat = POICategory("Aerials")
        cat.addField("POI Name")
        subcat = cat.addSubCategory(POISubCategory("NOSUB1000"))

        expected = deepcopy(poigroup.getCategories()) + [cat]

        poigroup.addCategory(cat)

        #        cat.addField

        actual = poigroup.getCategories()

        if actual != expected:
            print 'actual:', actual
            print 'expected:', expected

        self.assertEqual(actual, expected)

        map.close()

        map = createMap(self.testdatadir)
        map.open()

        poigroup = map.getPOIGroup()

        poigroup.open('r')

        catman = poigroup.getCategoryManager()

        print catman.getCategories()

        self.assertTrue(
            "Aerials" in [cat.getName() for cat in catman.getCategories()])

        actual = poigroup.getCategories()

        if actual != expected:
            print 'actual:', actual
            print 'expected:', expected


#        expected.sort(lambda x,y: cmp(x.name.upper(), y.name.upper()))

        self.assertEqual(actual, expected)
예제 #17
0
 def testCreateDatabase(self):
     mapdir = MapDirectory()
     db = Database(mapdir, "db00", 'w')
예제 #18
0
    def testAddPOI(self):
        map = Map(MapDirectory(self.testdatadir), maptype=MapTypeImage)
        map.open('a')

        poigroup = map.getPOIGroup()
        poilayer = map.getPOILayers()[0]
        poigroup.open('a')

        print "nlevels", poilayer.nlevels

        catman = poigroup.getCategoryManager()
        cat = catman.getCategory(1)
        subcat = cat.getSubCategory(1)

        # Save number of pois in category 1
        cat1count = cat.getPOICount()
        self.assertEqual(cat1count, 5)

        wkt = "POINT (16.185 58.5912)"
        poi = CellElementPOI(poilayer, categoryid=1, subcategoryid=1, wkt=wkt)
        poi.discretizeGeometry(poilayer.getCell(1))
        feature = FeaturePOI(poilayer.addCellElement(poi),
                             ['Apmacken', '', '', '', '', ''], 1, 1)

        expected = Set([f for f in poigroup.getFeatures()]) | Set([feature])

        poigroup.addFeature(feature)

        actual = Set([f for f in poigroup.getFeatures()])

        self.assertEqual(actual, expected,
                         "feature not present in poi group in memory")

        map.close()

        # Verify that the category statistics are updated
        self.assertEqual(cat.getPOICount(), cat1count + 1)

        map = Map()
        map.open(os.path.join(self.testdatadir, "00map.ini"))

        poigroup = map.getPOIGroup()

        poigroup.open()

        actual = Set([f for f in poigroup.getFeatures()])

        self.assertEqual(len(actual), len(expected))

        if actual != expected:
            print "actual:", actual
            print "expected:", expected

#        expected.sort(lambda x,y: cmp(x.name.upper(), y.name.upper()))

        self.assertEqual(actual, expected,
                         "feature was not added correctly after read back")

        self.assertTrue(
            areSorted([(f.getCategoryId(), f.getSubCategoryId())
                       for f in poigroup.getFeatures()]),
            "Feature are not sorted")

        # Verify that the category statistics are updated
        cat = poigroup.getCategory(1)
        print poigroup.getCategories()
        subcat = cat.getSubCategory(1)
        self.assertEqual(cat.getPOICount(), cat1count + 1)

        f = poigroup.getFeatureByIndex(0)
        aux = f.getAuxAsDict(poigroup)
        print f, aux
예제 #19
0
 def testListFiles(self):
     db = Database(MapDirectory(self.testdatadir), "db00", 'a')
예제 #20
0
        searchgroup.addFeature(feature)

        ogrfeature = ogrlayer.GetNextFeature()

        i += 1
        if i > 2000:
            break


filename = 'shape/romania/roads.shp'

ds = ogr.Open(filename)

layer = ds.GetLayerByIndex(0)

map = Map(MapDirectory(), maptype=MapTypeImage)
map.open("w")

extent = layer.GetExtent()
map.bbox = ((extent[0], extent[2]), (extent[1], extent[3]))

## Add streets layer
streets = Layer(map,
                name="00_Streets",
                filename="00str",
                layertype=LayerTypePolyline)
streets.open(mode='w')
streetstyle = DetailMapLayerStyle()
streetstyle.style = 'US_STREET_LINE'
map.addLayer(streets, layerstyle=streetstyle)
map.getGroupByName('00_Roads').addLayer(streets)