예제 #1
0
    def run(self):
        if self.state == Node.SUCCESS:
            return
        try:
            with open(self.filename) as f:
                pass
        except PermissionError:
            self.fail('Access denied.')
            return
        self.data = PolylineData()
        is_i2s = self.filename[-4:] == '.i2s'
        if is_i2s:
            with BlueKenue.Read(self.filename) as f:
                f.read_header()
                for poly in f.get_polygons():
                    self.data.add_line(poly)
            self.data.set_fields(['Value'])
        else:
            try:
                for polygon in Shapefile.get_polygons(self.filename):
                    self.data.add_line(polygon)
            except struct.error:
                self.fail('Inconsistent bytes.')
                return
            self.data.set_fields(Shapefile.get_all_fields(self.filename))

        if self.data.is_empty():
            self.fail('the file does not contain any polygon.')
            return

        self.success('The file contains {} polygon{}.'.format(
            len(self.data), 's' if len(self.data) > 1 else ''))
예제 #2
0
    def polygon_event(self):
        filename, _ = QFileDialog.getOpenFileName(self, 'Open a .shp file', '', 'Polygon file (*.shp)',
                                                  options=QFileDialog.Options() | QFileDialog.DontUseNativeDialog)
        if not filename:
            return
        if not test_open(filename):
            return

        polygons = []
        try:
            for polygon in Shapefile.get_polygons(filename):
                polygons.append(polygon)
        except struct.error:
            QMessageBox.critical(self, 'Error', 'Inconsistent bytes.', QMessageBox.Ok)
            return
        if not polygons:
            QMessageBox.critical(self, 'Error', 'The file does not contain any polygon.',
                                 QMessageBox.Ok)
            return
        items = ['%d - %s' % (index, name) for (index, name) in Shapefile.get_numeric_attribute_names(filename)]
        if not items:
            QMessageBox.critical(self, 'Error', 'The polygons do not have numeric attributes.', QMessageBox.Ok)
            return
        dlg = AttributeDialog(items)
        if dlg.exec_() != QDialog.Accepted:
            return
        attribute_index, attribute_name = dlg.attribute_box.currentText().split(' - ')
        attribute_index = int(attribute_index)

        short_name = os.path.join(os.path.basename(filename), os.path.split(filename)[1])
        row = self.polygon_table.rowCount()
        self.polygon_table.insertRow(row)
        self.polygon_table.setItem(row, 0, QTableWidgetItem('POLY%d' % (row+1)))
        self.polygon_table.setItem(row, 1, QTableWidgetItem(attribute_name))
        self.polygon_table.setItem(row, 2, QTableWidgetItem(short_name))
        self.parent.editor_tab.pool.add_polygonal_mask(polygons, attribute_index)