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_open_polylines(): self.data.add_line(poly) self.data.set_fields(['Value']) else: try: for poly in Shapefile.get_open_polylines(self.filename): self.data.add_line(poly) 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 2D open polyline.') return self.success('The file contains {} open line{}.'.format( len(self.data), 's' if len(self.data) > 1 else ''))
def read(self): self.fields = shp.get_all_fields(self.from_file) for index, name in shp.get_numeric_attribute_names(self.from_file): self.numeric_fields.append((index, name)) if self.shape_type in (3, 5): self.read_line() elif self.shape_type in (13, 15): self.read_linez() else: self.read_linem()
def to_shp(self, new_shapes, to_file, shape_type, attribute_name): open_lines, closed_lines = [], [] for line in new_shapes: if line.is_closed(): closed_lines.append(line) else: open_lines.append(line) if shape_type % 5 == 0: shp.write_bk_lines(to_file, shape_type, closed_lines, attribute_name) else: shp.write_bk_lines(to_file, shape_type, open_lines, attribute_name)
def read(self): self.fields = shp.get_all_fields(self.from_file) self.numeric_fields = shp.get_numeric_attribute_names(self.from_file) if self.shape_type == 8: self.read_point() elif self.shape_type == 18: try: self.read_pointz() except RuntimeError: raise RuntimeError else: self.read_pointm() if not self.shapes: raise ValueError
def read(self): self.fields = shp.get_all_fields(self.from_file) for index, name in shp.get_numeric_attribute_names(self.from_file): self.numeric_fields.append((index, name)) if self.shape_type == 1: self.read_point() elif self.shape_type == 11: try: self.read_pointz() except RuntimeError: raise RuntimeError else: self.read_pointm() if not self.shapes: raise ValueError
def _to_multi_init(self, shape_type): """! @brief Prepare Writer for MultiPoints* Shapefile @param shape_type <int>: shape type identifier """ w = shp.MyWriter( shapeType=shape_type ) # brute-force fix for MultiPointZ- and MultiPointM-writing bug for field_name, field_type, field_length, decimal_length in self.fields: w.field(field_name, field_type, str(field_length), decimal_length) return w
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 = PointData() try: for point, attribute in Shapefile.get_points(self.filename): self.data.add_point(point) self.data.add_attribute(attribute) 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 point.') return self.success('The file contains {} point{}.'.format( len(self.data), 's' if len(self.data) > 1 else ''))
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)
def to_multi(self, new_shapes, to_file): w = shp.MyWriter( shapeType=self.shape_type ) # brute-force fix for MultiPointZ- and MultiPointM-writing bug for field_name, field_type, field_length, decimal_length in self.fields: w.field(field_name, field_type, str(field_length), decimal_length) for points, points_m, attributes in zip(new_shapes, self.m, self.attributes): coords = np.array(points) new_m = [0 if m is None else m for m in points_m] m_flat = np.array(new_m).reshape((coords.shape[0], 1)) coords = np.hstack((coords, m_flat)) w.poly(parts=[list(map(tuple, coords))], shapeType=self.shape_type) w.record(*attributes) w.save(to_file)
def _handleOpenShapefile(self, filename): try: with open(filename, 'r') as f: pass except PermissionError: QMessageBox.critical(None, 'Error', 'Permission denied.', QMessageBox.Ok) self.parent.reset() return False try: shape_type = shp.get_shape_type(filename) except shapefile.ShapefileException: QMessageBox.critical( None, 'Error', 'Failed to open shp file (is the .dbf file present?).', QMessageBox.Ok) self.parent.reset() return False if shape_type == 1: self.from_type = 'shp Point' elif shape_type == 11: self.from_type = 'shp PointZ' elif shape_type == 21: self.from_type = 'shp PointM' elif shape_type == 3: self.from_type = 'shp Polyline' elif shape_type == 13: self.from_type = 'shp PolylineZ' elif shape_type == 23: self.from_type = 'shp PolylineM' elif shape_type == 5: self.from_type = 'shp Polygon' elif shape_type == 15: self.from_type = 'shp PolygonZ' elif shape_type == 25: self.from_type = 'shp PolygonM' elif shape_type == 8: self.from_type = 'shp MultiPoint' elif shape_type == 18: self.from_type = 'shp MultiPointZ' elif shape_type == 28: self.from_type = 'shp MultiPointM' elif shape_type == 0: QMessageBox.critical( None, 'Error', 'The shape type Null is currently not supported!', QMessageBox.Ok) self.parent.reset() return False else: QMessageBox.critical( None, 'Error', 'The shape type MultiPatch is currently not supported!', QMessageBox.Ok) self.parent.reset() return False if shape_type in (1, 11, 21): self.converter = convert.ShpPointConverter(filename, shape_type) elif shape_type in (3, 5, 13, 15, 23, 25): self.converter = convert.ShpLineConverter(filename, shape_type) else: self.converter = convert.ShpMultiPointConverter( filename, shape_type) return True
def to_shp(self, new_shapes, to_file, z_name): shp.write_bk_points(to_file, z_name, new_shapes)