class SedimentalLayerModel(QtCore.QAbstractListModel): def __init__(self, parent=None): super().__init__(parent) self.file = None def setLayer(self, layer): self.beginResetModel() self.file = PreCourlisFileLine(layer) self.endResetModel() def rowCount(self, parent=QtCore.QModelIndex()): if self.file is None: return 0 if self.file.layer() is None: return 0 return len(self.file.layers()) + 1 def data(self, index, role=QtCore.Qt.DisplayRole): if role == QtCore.Qt.DisplayRole: if index.row() == 0: if index.column() == 0: return "zfond" layers = self.file.layers() if index.row() - 1 < len(layers): layer = self.file.layers()[index.row() - 1] if index.column() == 0: return layer
def prepare_sections(self, parameters, context, feedback): sections = self.parameterAsSource(parameters, self.SECTIONS, context) # Prefix layer fields by "Z" for TatooineMesher file = PreCourlisFileLine(sections) alg_params = { "INPUT": parameters[self.SECTIONS], "FIELDS_MAPPING": [ { "name": "sec_id", "type": QVariant.Int, "expression": '"sec_id"' }, { "name": "p_id", "type": QVariant.Int, "expression": '"p_id"' }, { "name": "Zzfond", "type": QVariant.Double, "expression": '"zfond"' }, ] + [{ "name": "Z{}".format(layer), "type": QVariant.Double, "expression": '"{}"'.format(layer), } for layer in file.layers()], "OUTPUT": QgsProcessingUtils.generateTempFilename("tatooine_input.shp"), } outputs = processing.run( "qgis:refactorfields", alg_params, context=context, feedback=feedback, is_child_algorithm=True, ) return outputs["OUTPUT"]
def processAlgorithm(self, parameters, context, feedback): input_layer = self.parameterAsVectorLayer(parameters, self.INPUT, context) reach_name = (self.parameterAsString(parameters, self.REACH_NAME, context) or input_layer.name()) output_path = self.parameterAsString(parameters, self.OUTPUT, context) # Processing GUI does not correctly handle uppercase characters in file extensions before # QGIS 3.14 if Qgis.QGIS_VERSION_INT < 31400: output_path = output_path.replace(".georefc", ".georefC") output_path = output_path.replace(".geoc", ".geoC") precourlis_file = PreCourlisFileLine(input_layer) reach = precourlis_file.get_reach(reach_name) output_file = MascaretGeoFile(output_path, mode="write") output_file.has_ref = "ref" in os.path.splitext(output_path)[1][1:] output_file.add_reach(reach) output_file.nlayers = len(precourlis_file.layers()) output_file.save(output_path) return {self.OUTPUT: output_path}
def processAlgorithm(self, parameters, context, feedback): source = self.parameterAsSource(parameters, self.INPUT, context) fields = PreCourlisFilePoint.base_fields() file = PreCourlisFileLine(source) layers = file.layers() for layer in layers: fields.append(QgsField(layer, QVariant.Double)) (sink, dest_id) = self.parameterAsSink( parameters, self.OUTPUT, context, fields, QgsWkbTypes.PointZ, source.sourceCrs(), ) if sink is None: raise QgsProcessingException( self.invalidSinkError(parameters, self.OUTPUT)) total = 100.0 / source.featureCount() if source.featureCount() else 0 features = source.getFeatures() for current, line_feature in enumerate(features): # Take only the first parts (QgsMultiLineString => QgsLineString) line = next(line_feature.geometry().constParts()).clone() line_layers_values = [ line_feature.attribute(layer).split(",") for layer in layers ] for point, p_id, topo_bat, abs_lat, zfond, point_layers_values, in zip( line.points(), line_feature.attribute("p_id").split(","), line_feature.attribute("topo_bat").split(","), line_feature.attribute("abs_lat").split(","), line_feature.attribute("zfond").split(","), list(zip(*line_layers_values)) or [[]] * line.numPoints(), ): point_feature = QgsFeature() point_feature.setAttributes([ line_feature.attribute("sec_id"), line_feature.attribute("sec_name"), line_feature.attribute("abs_long"), line_feature.attribute("axis_x"), line_feature.attribute("axis_y"), line_feature.attribute("layers"), int(p_id), topo_bat, self.to_float(abs_lat), self.to_float(point.x()), self.to_float(point.y()), self.to_float(zfond), ] + [self.to_float(v) for v in point_layers_values]) point_feature.setGeometry( QgsGeometry(QgsPoint(point.x(), point.y(), 0.0))) sink.addFeature(point_feature, QgsFeatureSink.FastInsert) # Update the progress bar feedback.setProgress(int(current * total)) return {self.OUTPUT: dest_id}
def processAlgorithm(self, parameters, context, feedback): source = self.parameterAsSource(parameters, self.INPUT, context) axis = self.parameterAsSource(parameters, self.AXIS, context) self.first_section_abs_long = self.parameterAsDouble( parameters, self.FIRST_SECTION_ABS_LONG, context) if parameters.get(self.FIRST_AXIS_POINT_ABS_LONG, None) is None: self.first_axis_point_abs_long = None else: self.first_axis_point_abs_long = self.parameterAsDouble( parameters, self.FIRST_AXIS_POINT_ABS_LONG, context) group_field = self.parameterAsString(parameters, self.GROUP_FIELD, context) order_field = self.parameterAsString(parameters, self.ORDER_FIELD, context) self.axis = next(axis.getFeatures()) if axis else None request = QgsFeatureRequest() request.addOrderBy('"{}"'.format(group_field), True, True) file = PreCourlisFileLine(source) fields = PreCourlisFileLine.base_fields() for layer in file.layers(): fields.append(QgsField(layer, QVariant.String, len=100000)) (sink, dest_id) = self.parameterAsSink( parameters, self.OUTPUT, context, fields, QgsWkbTypes.LineString, source.sourceCrs(), ) if sink is None: raise QgsProcessingException( self.invalidSinkError(parameters, self.OUTPUT)) total = 100.0 / source.featureCount() if source.featureCount() else 0 features = source.getFeatures(request) group = None point_features = [] sec_id = 0 abs_long_offset = 0 for current, point_feature in enumerate(features): # Stop the algorithm if cancel button has been clicked if feedback.isCanceled(): break if current == 0: if self.first_axis_point_abs_long is not None: abs_long_offset = self.first_axis_point_abs_long else: abs_long_offset = (self.first_section_abs_long - point_feature.attribute("abs_long")) if group is not None and point_feature.attribute( group_field) != group: sec_id += 1 sink.addFeature( self.line_feature_from_points( sorted(point_features, key=lambda f: f.attribute(order_field)), sec_id, point_features[0].attribute("abs_long") + abs_long_offset, ), QgsFeatureSink.FastInsert, ) group = None point_features = [] group = point_feature.attribute(group_field) point_features.append(point_feature) # Update the progress bar feedback.setProgress(int(current * total)) if group is not None: sec_id += 1 sink.addFeature( self.line_feature_from_points( sorted(point_features, key=lambda f: f.attribute(order_field)), sec_id, point_features[0].attribute("abs_long") + abs_long_offset, ), QgsFeatureSink.FastInsert, ) group = None return {self.OUTPUT: dest_id}