Beispiel #1
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)
Beispiel #2
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)
Beispiel #3
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)
Beispiel #4
0
    def close(self):
        if self.isopen:
            if self.mode in ['w', 'a']:
                self.maintable.setMode('w')
                self.addtable.setMode('w')
                self.auxtable.setMode('w')

                aux = AuxTableManager(self.auxtable)

                # Write features to database
                addrow = 0
                for f in self.xfeatures:
                    textslot = aux.appendText(f.name,
                                              self.maintable.getRowCount() + 1)

                    # Update cell elements
                    for ref, e in zip(f.cellelementrefs,
                                      f.getCellElements(self.map)):
                        e.textslot = textslot
                        layer = self.map.getLayerByIndex(f.layerindex)
                        layer.updateCellElement(ref, e)

                    row = Row(self.maintable)
                    row.setColumnUInt(
                        self.maintable.getColumnIndex("NAME_REF"), textslot)

                    row.setColumnUInt(
                        self.maintable.getColumnIndex("OBJ_TYPE"),
                        f.getObjtypeIndex(self))

                    cellelementrefs = f.getCellElementRefs()
                    if len(cellelementrefs) == 1:
                        row.setColumnUInt(
                            self.maintable.getColumnIndex("CELL_NUM"),
                            cellelementrefs[0][0])
                        row.setColumnUInt(
                            self.maintable.getColumnIndex("N_IN_C"),
                            cellelementrefs[0][1] + 1)
                    else:
                        row.setColumnUInt(
                            self.maintable.getColumnIndex("CELL_NUM"),
                            0x80000000 | (addrow + 1))
                        row.setColumnUInt(
                            self.maintable.getColumnIndex("N_IN_C"),
                            len(cellelementrefs))
                        for cref in cellelementrefs:
                            rowrc = Row(self.addtable)
                            rowrc.setColumnUInt(
                                self.addtable.getColumnIndex("CELL_NUM"),
                                cref[0])
                            rowrc.setColumnUInt(
                                self.addtable.getColumnIndex("N_IN_C"),
                                cref[1] + 1)
                            self.addtable.writeRow(rowrc)
                            addrow += 1
                    self.maintable.writeRow(row)

            self.isopen = False
Beispiel #5
0
    def close(self):
        if self.isopen:
            if self.mode in ['w','a']:
                self.maintable.setMode('w')
                self.addtable.setMode('w')
                self.auxtable.setMode('w')

                aux = AuxTableManager(self.auxtable)

                # Write features to database
                addrow = 0
                for f in self.xfeatures:
                    textslot = aux.appendText(f.name, self.maintable.getRowCount()+1)

                    # Update cell elements
                    for ref, e in zip(f.cellelementrefs, f.getCellElements(self.map)):
                        e.textslot = textslot
                        layer = self.map.getLayerByIndex(f.layerindex)
                        layer.updateCellElement(ref, e)

                    row = Row(self.maintable)
                    row.setColumnUInt(self.maintable.getColumnIndex("NAME_REF"), textslot)

                    row.setColumnUInt(self.maintable.getColumnIndex("OBJ_TYPE"), f.getObjtypeIndex(self))

                    cellelementrefs = f.getCellElementRefs()
                    if len(cellelementrefs) == 1:
                        row.setColumnUInt(self.maintable.getColumnIndex("CELL_NUM"), cellelementrefs[0][0])
                        row.setColumnUInt(self.maintable.getColumnIndex("N_IN_C"), cellelementrefs[0][1]+1)
                    else:
                        row.setColumnUInt(self.maintable.getColumnIndex("CELL_NUM"), 0x80000000|(addrow+1) )
                        row.setColumnUInt(self.maintable.getColumnIndex("N_IN_C"), len(cellelementrefs))
                        for cref in cellelementrefs:
                            rowrc = Row(self.addtable)
                            rowrc.setColumnUInt(self.addtable.getColumnIndex("CELL_NUM"), cref[0])
                            rowrc.setColumnUInt(self.addtable.getColumnIndex("N_IN_C"), cref[1]+1)
                            self.addtable.writeRow(rowrc)
                            addrow+=1
                    self.maintable.writeRow(row)

            self.isopen = False            
Beispiel #6
0
    def addToTable(self, id, db, cattable):
        row = Row(cattable)
        row.setColumnUInt(cattable.getColumnIndex('ID'), id)
        row.setColumnString(cattable.getColumnIndex('NAME'), self.name)
        row.setColumnUInt(cattable.getColumnIndex('POICOUNT'),self.poicount)
        row.setColumnUInt(cattable.getColumnIndex('COMM_SLOT'), 0)
        row.setColumnUIntVector(cattable.getColumnIndex('FIRSTCHSLOT'), self.first_char_slots)
        if 'SLOTFIRST' in cattable.getColumnNames():
            row.setColumnUInt(cattable.getColumnIndex('SLOTFIRST'), self.firstslot)
            row.setColumnUInt(cattable.getColumnIndex('SLOTLAST'), self.lastslot)

        catcursor = cattable.writeRow(row)

        # Add subcategories
        all_subcatg = db.getSetByName('ALL_SUBCATG_')
        subcatmember = all_subcatg.getMemberByIndex(0)
        subcattable = subcatmember.table
        for subcatindex,subcat in enumerate(self.subcategories):
            subcatid = subcatindex+1
            subcatcursor = subcat.addToTable(subcatid, db, subcattable)
            catcursor.addSetItem(subcatmember, subcatcursor)

        # Add fields
        fieldinfo_set = db.getSetByName(self.fieldinfo_setname)
        fieldinfomember = fieldinfo_set.getMemberByIndex(0)
        fieldtable = fieldinfomember.table
        for name,type in zip(self.fieldnames, self.fieldtypes):
            fieldrow = Row(fieldtable)
            fieldrow.setColumnString(fieldtable.getColumnIndex('NAME'), name)
            fieldrow.setColumnUInt(fieldtable.getColumnIndex('AUX_TYPE'), type)
            fieldcursor = fieldtable.writeRow(fieldrow)
            catcursor.addSetItem(fieldinfomember, fieldcursor)
                    
        return catcursor
Beispiel #7
0
    def close(self):
        if self.mode in ['w','a']:
            self.maintable.clear()
            self.auxtable.clear()

            aux = AuxTableManager(self.auxtable, endchar=chr(7), searchindex=False)

            # sort categories, note that this will create new category ids
            # and the features have to be updated
            if self.sortcategories:
                cattrans, subcattrans = self.catman.sortCategories()

                # translate categories
                for f in self.features:
                    oldcatid = f.getCategoryId()
                    oldsubcatid = f.getSubCategoryId()

                    f.setCategoryId(cattrans[oldcatid])
                    f.setSubCategoryId(subcattrans[(oldcatid, oldsubcatid)])

            # Sort features on ids
            self.features.sort(lambda x,y: cmp((x.getCategoryId(), x.getSubCategoryId(), x.name.upper()), (y.getCategoryId(), y.getSubCategoryId(), y.name.upper())))


            # Clear category statistics
            for cat in self.catman.getCategories():
                cat.clearStatistics()
                for subcat in cat.getSubCategories():
                    subcat.clearStatistics()

            # Write features to database
            slot = 1
            for f in self.features:
                # Update category statistics
                cat=self.catman.getCategory(f.getCategoryId())
                subcat=cat.getSubCategory(f.getSubCategoryId())
                cat.updateFromPOI(slot,f.getAux()[0])
                subcat.updateFromPOI(slot,f.getAux()[0])
                
                textslot = aux.appendText('\t'.join(f.getAux())+'\t')

                # Update cell elements
                for ref, e in zip(f.cellelementrefs, f.getCellElements(self.map)):
                    e.textslot = textslot
                    e.setCategoryId(f.getCategoryId())
                    e.setSubCategoryId(f.getSubCategoryId())
                    layer = self.map.getPOILayers()[0]
                    layer.updateCellElement(ref, e)
                
                row = Row(self.maintable)
                row.setColumnUInt(self.maintable.getColumnIndex("TEXT_SLOT"), textslot)

                row.setColumnUInt(self.maintable.getColumnIndex("CATG_ID"), f.getCategoryId())
                row.setColumnUInt(self.maintable.getColumnIndex("SUBCAT_ID"), f.getSubCategoryId())
                
                cellelementrefs = f.getCellElementRefs()
                row.setColumnUInt(self.maintable.getColumnIndex("CELL_NUMBER"), cellelementrefs[0][0])
                row.setColumnUInt(self.maintable.getColumnIndex("NUMBER_IN_CELL"), cellelementrefs[0][1]+1)
                self.maintable.writeRow(row)

                slot+=1

        self.catman.close()
Beispiel #8
0
    def addToTable(self, id, db, cattable):
        row = Row(cattable)
        row.setColumnUInt(cattable.getColumnIndex('ID'), id)
        row.setColumnString(cattable.getColumnIndex('NAME'), self.name)
        row.setColumnUInt(cattable.getColumnIndex('POICOUNT'), self.poicount)
        row.setColumnUInt(cattable.getColumnIndex('COMM_SLOT'), 0)
        row.setColumnUIntVector(cattable.getColumnIndex('FIRSTCHSLOT'),
                                self.first_char_slots)
        if 'SLOTFIRST' in cattable.getColumnNames():
            row.setColumnUInt(cattable.getColumnIndex('SLOTFIRST'),
                              self.firstslot)
            row.setColumnUInt(cattable.getColumnIndex('SLOTLAST'),
                              self.lastslot)

        catcursor = cattable.writeRow(row)

        # Add subcategories
        all_subcatg = db.getSetByName('ALL_SUBCATG_')
        subcatmember = all_subcatg.getMemberByIndex(0)
        subcattable = subcatmember.table
        for subcatindex, subcat in enumerate(self.subcategories):
            subcatid = subcatindex + 1
            subcatcursor = subcat.addToTable(subcatid, db, subcattable)
            catcursor.addSetItem(subcatmember, subcatcursor)

        # Add fields
        fieldinfo_set = db.getSetByName(self.fieldinfo_setname)
        fieldinfomember = fieldinfo_set.getMemberByIndex(0)
        fieldtable = fieldinfomember.table
        for name, type in zip(self.fieldnames, self.fieldtypes):
            fieldrow = Row(fieldtable)
            fieldrow.setColumnString(fieldtable.getColumnIndex('NAME'), name)
            fieldrow.setColumnUInt(fieldtable.getColumnIndex('AUX_TYPE'), type)
            fieldcursor = fieldtable.writeRow(fieldrow)
            catcursor.addSetItem(fieldinfomember, fieldcursor)

        return catcursor
Beispiel #9
0
    def close(self):
        if self.mode in ['w', 'a']:
            self.maintable.clear()
            self.auxtable.clear()

            aux = AuxTableManager(self.auxtable,
                                  endchar=chr(7),
                                  searchindex=False)

            # sort categories, note that this will create new category ids
            # and the features have to be updated
            if self.sortcategories:
                cattrans, subcattrans = self.catman.sortCategories()

                # translate categories
                for f in self.features:
                    oldcatid = f.getCategoryId()
                    oldsubcatid = f.getSubCategoryId()

                    f.setCategoryId(cattrans[oldcatid])
                    f.setSubCategoryId(subcattrans[(oldcatid, oldsubcatid)])

            # Sort features on ids
            self.features.sort(lambda x, y: cmp(
                (x.getCategoryId(), x.getSubCategoryId(), x.name.upper()),
                (y.getCategoryId(), y.getSubCategoryId(), y.name.upper())))

            # Clear category statistics
            for cat in self.catman.getCategories():
                cat.clearStatistics()
                for subcat in cat.getSubCategories():
                    subcat.clearStatistics()

            # Write features to database
            slot = 1
            for f in self.features:
                # Update category statistics
                cat = self.catman.getCategory(f.getCategoryId())
                subcat = cat.getSubCategory(f.getSubCategoryId())
                cat.updateFromPOI(slot, f.getAux()[0])
                subcat.updateFromPOI(slot, f.getAux()[0])

                textslot = aux.appendText('\t'.join(f.getAux()) + '\t')

                # Update cell elements
                for ref, e in zip(f.cellelementrefs,
                                  f.getCellElements(self.map)):
                    e.textslot = textslot
                    e.setCategoryId(f.getCategoryId())
                    e.setSubCategoryId(f.getSubCategoryId())
                    layer = self.map.getPOILayers()[0]
                    layer.updateCellElement(ref, e)

                row = Row(self.maintable)
                row.setColumnUInt(self.maintable.getColumnIndex("TEXT_SLOT"),
                                  textslot)

                row.setColumnUInt(self.maintable.getColumnIndex("CATG_ID"),
                                  f.getCategoryId())
                row.setColumnUInt(self.maintable.getColumnIndex("SUBCAT_ID"),
                                  f.getSubCategoryId())

                cellelementrefs = f.getCellElementRefs()
                row.setColumnUInt(self.maintable.getColumnIndex("CELL_NUMBER"),
                                  cellelementrefs[0][0])
                row.setColumnUInt(
                    self.maintable.getColumnIndex("NUMBER_IN_CELL"),
                    cellelementrefs[0][1] + 1)
                self.maintable.writeRow(row)

                slot += 1

        self.catman.close()