def validate_rasters(objeto, layers, rasters, proj4): combo_by_index = objeto.combo_name_by_layer_index for combo_name, idx in combo_by_index.iteritems(): if idx <= 0: return u'Selecione um raster para o combo box "{}".'.format( combo_name), None current = 0 size = len(combo_by_index) _, bar = QtHandler.progress_dialog(label='Validating rasters...') rasters_selected = [] for combo_name, idx in combo_by_index.iteritems(): raster_name = rasters[idx] layer = find_one_layer_by_name(layers, raster_name) config = objeto.params['raster'][combo_name] if not raster_ok(config, layer, proj4): return u'Raster "{}" selecionado no combo box "{}" e invalido!'.format( layer.name(), combo_name), None rasters_selected.append(layer) current += 1 progress = (float(current) / float(size)) * 100 bar.setValue(progress) return None, rasters_selected
def intersect(plugin_name, progress, line_layer, buffer_pc_layer): dlg, bar = QtHandler.progress_dialog(label='Creating Intersection...') tempfile = tmp_filename_without_extension() bar.setValue(20) line_layer_source = line_layer.source() buffer_pc_layer_source = buffer_pc_layer.source() bar.setValue(40) try: output = intersects(line_layer_source, buffer_pc_layer_source, True, tempfile) bar.setValue(80) except Exception as e: __logger.fatal(u'Fail to intersects buffer with line: {}'.format( str(e))) raise RuntimeError(str(e)) bar.setValue(100) bar.close() dlg.close() del dlg __logger.info(u'Intersection created successfully.') return output
def classify_risco(self, layer): _, bar = QtHandler.progress_dialog(label='Classifying Risk...') fields = [str(field.name()) for field in layer.pendingFields()] pr = layer.dataProvider() bar.setValue(20) if self.params['att_cls_criticidade'] not in fields: pr.addAttributes([ QgsField(self.params['att_cls_criticidade'], QVariant.String) ]) layer.updateFields() bar.setValue(50) layer.startEditing() for feature in layer.getFeatures(): criticidade = feature[self.params['att_criticidade']] classificacao = self.classify(criticidade) layer.changeAttributeValue( feature.id(), layer.fieldNameIndex(self.params['att_cls_criticidade']), classificacao) layer.commitChanges() bar.setValue(100) return None, layer
def create_line(plugin_name, progress, layer): dlg, bar = QtHandler.progress_dialog(label='Creating line...') params = __config[plugin_name] tempfile = tmp_filename_without_extension() layer_source = layer.source() bar.setValue(20) try: output = point_to_path(layer_source, params['att_grupo'], params['att_vao_id'], '', tempfile, None) bar.setValue(80) except Exception as e: __logger.fatal(u'Fail to convert point to path: {}'.format(str(e))) raise RuntimeError(str(e)) bar.setValue(100) bar.close() dlg.close() del dlg __logger.info(u'Line created successfully.') return output
def clip_vectors(vao_layer, line_layer, plugin_name): dlg, bar = QtHandler.progress_dialog(label='Clipping Vectors...') params = __config[plugin_name] success = True new_features = [] lines = {} bar.setValue(20) writer = VectorWriter('memory:', None, vao_layer.pendingFields().toList(), QGis.WKBLineString, vao_layer.crs(), None) vl = writer.layer pr = writer.writer bar.setValue(30) vl.startEditing() for feat_line in line_layer.getFeatures(): key = int(feat_line[params['att_line_id']]) lines[key] = feat_line bar.setValue(40) for feat_vao in vao_layer.getFeatures(): idx = feat_vao.fieldNameIndex(params['att_vao_id']) attr = int(feat_vao.attributes()[idx]) attributes = feat_vao.attributes() try: line = lines[attr] geom = feat_vao.geometry() geom_line = line.geometry() fet = QgsFeature() fet.setGeometry(geom.intersection(geom_line)) fet.setAttributes(attributes) new_features.append(fet) except Exception as e: __logger.error(str(e)) bar.setValue(80) pr.addFeatures(new_features) vl.updateFields() vl.commitChanges() if len(new_features) > 0: QgsMapLayerRegistry.instance().addMapLayer(vl) bar.setValue(100) bar.close() dlg.close() del dlg __logger.info(u'All vectors clipped successfully.') return success, vl
def buffering(plugin_name, progress, layer, destination='memory:', open_output_layer=True): dlg, bar = QtHandler.progress_dialog(label='Buffering...') name = layer.name() __logger.info(u'Buffering layer "{}"...'.format(name)) if not is_vector_layer(layer): __logger.warn( u'Layer "{}" is not QVectorLayer, got layer type {}'.format( name, layer.type())) return bar.setValue(10) writer = VectorWriter(destination, None, layer.pendingFields().toList(), QGis.WKBPolygon, layer.crs(), None) bar.setValue(20) params = __config[plugin_name] __logger.info( u'Running buffering algorithm for "{}"...'.format(destination)) __logger.info(params['dissolve']) buff.buffering(progress, writer, params['distance'], None, False, layer, params['dissolve'], params['segments']) bar.setValue(60) output = writer.layer output_layer_name = u'{}_buffer'.format(name) if output: output.setName(output_layer_name) else: output = QgsVectorLayer(destination, output_layer_name, 'ogr') bar.setValue(80) if output.isValid(): if open_output_layer: QgsMapLayerRegistry.instance().addMapLayers([output]) __logger.info(u'Buffer created with success.') else: __logger.fatal(u'Layer created is invalid.') bar.setValue(100) bar.close() dlg.close() del dlg return output
def clip_rasters(buffer_layer, raster_layers): success = True tempfile = None new_layers = [] buffer_source = buffer_layer.source() current = 0 last_progress = 0 size = len(raster_layers) total = 100 / size if size > 0 else 1 dlg, bar = QtHandler.progress_dialog(label='Clipping rasters...') __logger.info(u'Clip {} rasters with buffer "{}"'.format( str(size), buffer_layer.name())) for layer in raster_layers: name = layer.name() source = layer.source() datatype, datatype_name = raster_data_type(source) datatype = datatype - 1 __logger.info( u'Clipping raster "{}" with source "{}", datatype: "{}" and datatype_name: "{}"...' .format(name, source, str(datatype), str(datatype_name))) try: output = clip_raster_by_mask(source, buffer_source, tempfile, raster_type=datatype) new_layers.append(output) except Exception as e: success = False __logger.fatal(u'Fail to clip raster "{}": {} => {}'.format( name, e.message, str(traceback.format_exc()))) current += 1 percentage = int(current * total) __logger.info(u'Clipping status: {}%...'.format(str(percentage))) bar.setValue(percentage) last_progress = percentage if last_progress < 100: bar.setValue(100) bar.close() dlg.close() del dlg if success: __logger.info(u'All rasters clipped successfully.') return success, new_layers
def apply_model(self, vegetacao, clinografia, orientacao_vertente, proximidade_estradas, hipsometria, output_raster): """ Apply propagacao model. :param vegetacao: A QgsRasterLayer layer. :param clinografia: A QgsRasterLayer layer. :param orientacao_vertente: A QgsRasterLayer layer. :param proximidade_estradas: A QgsRasterLayer layer. :param hipsometria: A QgsRasterLayer layer. :param output_raster: A string with the layer output path. :return: QgsRasterLayer """ dlg, bar = QtHandler.progress_dialog(label='Applying model...') expression_template = self.params['expression'] default_band = self.params['expression_default_band'] expression_base_layer = self.params['expression_base_layer'] qgs_raster_layers = [vegetacao, clinografia, orientacao_vertente, proximidade_estradas, hipsometria] expression_alias = findall(r'\{(.*?)\}', expression_template) bar.setValue(10) idx = expression_alias.index(expression_base_layer) qgs_raster_base_layer = qgs_raster_layers[idx] tmp_output_raster = tmp_filename_without_extension() + '.tif' bar.setValue(20) code, msg = raster_calculator_from_config(qgs_raster_layers, expression_alias, expression_template, qgs_raster_base_layer, tmp_output_raster, default_band) bar.setValue(80) if code == 0: propagacao = PropagacaoModelReclass(tmp_output_raster, output_raster) re = Reclassify(propagacao) re.apply() layer = import_layer_qgis(output_raster) set_layer_style(layer, PLUGIN_PROPAGACAO, PLUGIN_PROPAGACAO) else: self.logger.fatal(msg) bar.setValue(100) bar.close() dlg.close() del dlg return code, msg
def reclassify(self, layers, rasters): suffix = '_clipped' sources = {} layers_by_name = {} combo_by_index = self.__qt.combo_name_by_layer_index current = 0 size = len(combo_by_index) total = 100 / size last_progress = 0 dlg, bar = QtHandler.progress_dialog(label='Reclassifying rasters...') for combo_name, idx in combo_by_index.iteritems(): raster_name = rasters[idx] raster_name = raster_name + suffix self.logger.info('Searching raster to reclassify, index {} ({})...'.format(idx, raster_name)) layer = find_one_layer_by_name(layers, raster_name) if not layer: self.logger.warn('Layer "{}" not found to reclassify...'.format(raster_name)) return input_raster = layer.source() output_raster = tmp_filename_without_extension() + '.tif' algorithm = self.reclass_algorithms[combo_name] reclass = algorithm(input_raster, output_raster) re = Reclassify(reclass) re.apply() sources[combo_name] = output_raster current += 1 percentage = int(current * total) self.logger.info('Reclassify status: {}%...'.format(str(percentage))) bar.setValue(percentage) last_progress = percentage if last_progress < 100: bar.setValue(100) bar.close() dlg.close() del dlg for combo_name, source in sources.iteritems(): output = import_layer_qgis(source, suffix='reclass', base_layer=combo_name, add_map_layer=False) layers_by_name[combo_name] = output return layers_by_name
def buffer_line(plugin_name, progress, layer, output): dlg, bar = QtHandler.progress_dialog(label='Buffering Line...') layer_source = layer.source() bar.setValue(20) params = __config[plugin_name] extent = layer.extent() xmin = extent.xMinimum() xmax = extent.xMaximum() ymin = extent.yMinimum() ymax = extent.yMaximum() vector = str(xmin) + ',' + str(xmax) + ',' + str(ymin) + ',' + str(ymax) bar.setValue(40) try: output_temp = buffer_by_column(layer_source, params['att_buffer_dist'], params['scale'], params['max_distance'], params['straight_corners'], params['caps_ends'], vector, params['tolerance'], params['min_area'], params['output_type'], output) bar.setValue(80) except Exception as e: __logger.fatal(u'Fail to create buffer: {}'.format(str(e))) raise RuntimeError(str(e)) output_layer = QgsVectorLayer(output, 'area_manutencao', 'ogr') QgsMapLayerRegistry.instance().addMapLayers([output_layer]) style = params['style'] set_layer_style(output_layer, plugin_name, style['faixa_manutencao']) bar.setValue(100) bar.close() dlg.close() del dlg __logger.info(u'Buffer created successfully.') return output_temp
def create_grid(input_gridfn, output_gridfn, xmin, xmax, ymin, ymax, grid_height, grid_width): """ Creates a grid using a buffer. This method uses pure GDAL python. :param input_gridfn: A string with base buffer source. :param output_gridfn: A string with output source. :param xmin: float :param xmax: float :param ymin: float :param ymax: float :param grid_height: Cell size :param grid_width: Cell size :return: void """ xmin = float(xmin) xmax = float(xmax) ymin = float(ymin) ymax = float(ymax) grid_width = float(grid_width) grid_height = float(grid_height) assert (not isnan(xmin)) and (not isinf(xmin)) assert (not isnan(xmax)) and (not isinf(xmax)) assert (not isnan(ymin)) and (not isinf(ymin)) assert (not isnan(ymax)) and (not isinf(ymax)) assert (not isnan(grid_width)) and (not isinf(grid_width)) assert (not isnan(grid_height)) and (not isinf(grid_height)) if not os.path.isfile(input_gridfn): raise RuntimeError( u'Base layer does not exist in filesystem, got "{}".'.format( input_gridfn)) # get rows rows = ceil((ymax - ymin) / grid_height) # get columns cols = ceil((xmax - xmin) / grid_width) label = u'Creating a grid with {} rows and {} cols ({} cells)...'.format( rows, cols, rows * cols) __logger.info(label) dlg, bar = QtHandler.progress_dialog(label=label) # start grid cell envelope ring_xleft_origin = xmin ring_xright_origin = xmin + grid_width ring_ytop_origin = ymax ring_ybottom_origin = ymax - grid_height # create output file if os.path.exists(output_gridfn): os.remove(output_gridfn) ogr_driver = ogr.GetDriverByName(__OGR_DRIVER_NAME) base_data_source = ogr.Open(input_gridfn, 0) if not base_data_source: raise RuntimeError( u'Fail to open base layer "{}".'.format(input_gridfn)) bbox_layer = base_data_source.GetLayer() bbox_feature = bbox_layer.GetNextFeature() bbox_geom = bbox_feature.GetGeometryRef() bbox_srs = bbox_layer.GetSpatialRef() out_data_source = ogr_driver.CreateDataSource(output_gridfn) out_layer = out_data_source.CreateLayer(output_gridfn, bbox_srs, ogr.wkbPolygon) feature_defn = out_layer.GetLayerDefn() current = 0 last_progress = 0 total = 100.0 / cols if cols > 0 else 1 # create grid cells countcols = 0 while countcols < cols: countcols += 1 # reset envelope for rows ring_ytop = ring_ytop_origin ring_ybottom = ring_ybottom_origin countrows = 0 while countrows < rows: countrows += 1 geom = __create_polygon(ring_xleft_origin, ring_xright_origin, ring_ybottom, ring_ytop) if geom.Intersects(bbox_geom): # add new geom to layer __add_geom_to_layer(feature_defn, out_layer, geom) # new envelope for next geom ring_ytop = ring_ytop - grid_height ring_ybottom = ring_ybottom - grid_height # new envelope for next geom ring_xleft_origin = ring_xleft_origin + grid_width ring_xright_origin = ring_xright_origin + grid_width current += 1 progress = int(current * total) if progress != last_progress and progress % 10 == 0: __logger.info('Grid status: {}%...'.format(str(progress))) bar.setValue(progress) last_progress = progress # Save and close DataSources del feature_defn del out_layer del out_data_source del bbox_feature del bbox_layer del base_data_source if last_progress < 100: bar.setValue(100) bar.close() dlg.close() del dlg
def apply_model(self, layer, weight): start_time = datetime.now() str_start_time = start_time.strftime(self.pt_br_format) self.logger.info( 'Running Ignicao model at {}...'.format(str_start_time)) field_name = self.params['equation_field'] writer = layer.dataProvider() idx = layer.fieldNameIndex(field_name) if idx != -1: writer.deleteAttributes([idx]) layer.updateFields() writer.addAttributes( [QgsField(field_name, QVariant.Double, '', 24, 15)]) layer.updateFields() last_progress = 0 total = 100.0 / layer.featureCount() if layer.featureCount() > 0 else 1 dlg, bar = QtHandler.progress_dialog(label='Applying model Ignicao...') constant = float(weight['constant']) weight_queima = float(weight['queima']) weight_energia = float(weight['energia']) weight_enterra = float(weight['enterra']) weight_altitude = float(weight['altitude']) weight_insolacao = float(weight['insolacao']) weight_tmax = float(weight['tmax']) weight_vven = float(weight['vven']) weight_lsat_gndvi = float(weight['lsat_gndvi']) weight_lsat_ndvi = float(weight['lsat_ndvi']) weight_ab_med = float(weight['ab_med']) weight_ab_dif = float(weight['ab_dif']) weight_dens_med = float(weight['dens_med']) weight_vol_med = float(weight['vol_med']) weight_uso_c01_p = float(weight['uso_c01_p']) weight_uso_c02_p = float(weight['uso_c02_p']) weight_uso_c03_p = float(weight['uso_c03_p']) weight_uso_c04_p = float(weight['uso_c04_p']) weight_uso_c05_p = float(weight['uso_c05_p']) weight_uso_c06_p = float(weight['uso_c06_p']) weight_uso_c07_p = float(weight['uso_c07_p']) weight_uso_c08_p = float(weight['uso_c08_p']) weight_uso_c09_p = float(weight['uso_c09_p']) weight_uso_c10_p = float(weight['uso_c10_p']) weight_uso_c11_p = float(weight['uso_c11_p']) weight_uso_c12_p = float(weight['uso_c12_p']) weight_uso_c13_p = float(weight['uso_c13_p']) weight_uso_c14_p = float(weight['uso_c14_p']) weight_uso_c15_p = float(weight['uso_c15_p']) weight_uso_c16_p = float(weight['uso_c16_p']) weight_uso_c17_p = float(weight['uso_c17_p']) weight_uso_c18_p = float(weight['uso_c18_p']) weight_uso_c19_p = float(weight['uso_c19_p']) weight_uso_c20_p = float(weight['uso_c20_p']) weight_uso_c21_p = float(weight['uso_c21_p']) weight_uso_c22_p = float(weight['uso_c22_p']) layer.startEditing() bar.setValue(1) for current, ft in enumerate(layer.getFeatures()): ft[field_name] = self.__get_equation_value( ft, constant, weight_queima, weight_energia, weight_enterra, weight_altitude, weight_insolacao, weight_tmax, weight_vven, weight_lsat_gndvi, weight_lsat_ndvi, weight_ab_med, weight_ab_dif, weight_dens_med, weight_vol_med, weight_uso_c01_p, weight_uso_c02_p, weight_uso_c03_p, weight_uso_c04_p, weight_uso_c05_p, weight_uso_c06_p, weight_uso_c07_p, weight_uso_c08_p, weight_uso_c09_p, weight_uso_c10_p, weight_uso_c11_p, weight_uso_c12_p, weight_uso_c13_p, weight_uso_c14_p, weight_uso_c15_p, weight_uso_c16_p, weight_uso_c17_p, weight_uso_c18_p, weight_uso_c19_p, weight_uso_c20_p, weight_uso_c21_p, weight_uso_c22_p) layer.updateFeature(ft) progress = int(current * total) if progress != last_progress and progress % 10 == 0: self.logger.debug('{}%'.format(str(progress))) bar.setValue(progress) last_progress = progress layer.commitChanges() if last_progress != 100: bar.setValue(100) bar.close() dlg.close() del dlg end_time = datetime.now() time_elapsed = end_time - start_time str_end_time = end_time.strftime(self.pt_br_format) self.logger.info( 'Summing up, done at {}! Time elapsed {}(hh:mm:ss.ms)'.format( str_end_time, time_elapsed))