def testBestPathFinder(self):
        """Test full process to obtain layers (points and Lines layers)
        """
        import tempfile
        outputFolder = tempfile.mkdtemp("frd")

        polylineThreshold = 5

        parameters = {
            "min_slope_pct": 0,
            "max_slope_pct": 10,
            "semi_size": 2,
            "penalty_factor_xy": 40,
            "penalty_factor_z": 40
        }

        finder = oq.BestPathFinder(self.input_dtm, self.exclusion_areas_layer)
        finder.set_parameters(parameters)
        finder.set_output_folder(outputFolder)
        raw_layer = finder.create_raw_output_layer()

        simplified_layer = finder.create_simplified_output_layer(
            polylineThreshold)

        for point_index in af.waypoints_list(self.input_v_layer):
            finder.add_segment_to(point_index)

        self.assertEqual(raw_layer.dataProvider().featureCount(), 179)
        self.assertTrue(simplified_layer.isValid())
    def testExclusionUtility(self):
        """Test if exclusion utility is working
        """
        import tempfile
        from qgis.core import QGis
        outputFolder = tempfile.mkdtemp("frd")

        parameters = {
            "min_slope_pct": 0,
            "max_slope_pct": 10,
            "semi_size": 2,
            "penalty_factor_xy": 40,
            "penalty_factor_z": 40
        }

        finder = oq.BestPathFinder(self.input_dtm, self.exclusion_areas_layer)
        finder.set_parameters(parameters)
        finder.set_output_folder(outputFolder)
        raw_layer = finder.create_raw_output_layer()
        for point_index in af.waypoints_list(self.input_v_layer):
            finder.add_segment_to(point_index)
        raw_layer = finder.create_raw_output_layer()
        self.assertTrue(raw_layer)

        provider = raw_layer.dataProvider().getFeatures()
        puntos_geom = []
        for elem in provider:
            geom2 = elem.geometry()
            if geom2.wkbType() == QGis.WKBPoint:
                puntos_geom.append(geom2)

        exc_provider = self.exclusion_areas_layer.dataProvider().getFeatures()
        exc_geoms = []
        for elem in exc_provider:
            geom1 = elem.geometry()
            exc_geoms.append(geom1)

        contenido = []
        for elem_exc in self.exclusion_areas_layer.dataProvider().getFeatures(
        ):
            for elem in raw_layer.dataProvider().getFeatures():
                geom2 = elem.geometry()
                geom1 = elem_exc.geometry()
                if geom1.contains(geom2):
                    contenido.append(geom2)
        self.assertEqual(contenido, [])
Exemplo n.º 3
0
    def initRouteOptimization(self, interactive_mode):
        """Conect the process options and calls the optimizer module
        """
        if not self.outputFolderLineEdit.text():
            self.chooseOutputFolder()

        if not self.checkParameters(interactive_mode=interactive_mode):
            return False

        dtmLayer = QgsMapLayerRegistry.instance().mapLayersByName(
                self.dtmLayerComboBox.currentText())[0]

        if self.exclusionAreasCheckBox.isChecked():
            try:
                exclusionAreasLayer = \
                    QgsMapLayerRegistry.instance().mapLayersByName(
                        self.exclusionAreasComboBox.currentText())[0]
            except (ValueError, IndexError):
                exclusionAreasLayer = None
        else:
            exclusionAreasLayer = None
            
        try:
            ok, message = inputs_checker.check_layers(
                        dtmLayer,
                        None,
                        exclusionAreasLayer)
            if not ok:
                    raise ValueError(message)
            if interactive_mode == True:
                if not self.iface.mapCanvas().extent().intersects(
                        dtmLayer.extent()):
                    self.setCanvasExtent(dtmLayer)
                
            self.finder = optimizer_qgis.BestPathFinder(
                    dtmLayer, exclusionAreasLayer)
            self.finder.set_parameters(self.main_parameters())

        except ValueError as e:
            self.showMessageBox(unicode(e), u"Error del optimizador")
            self.showMessageBar(
                    u"Forest Road Designer: ha fallado la optimización",
                    u'Error', QgsMessageBar.WARNING)
            return False
        return True
    def test_checkAttrsValues(self):
        """Test for layers attributes
        """
        import tempfile
        outputFolder = tempfile.mkdtemp("frd")
        polylineThreshold = 0

        parameters = {
            "min_slope_pct": 0.5,
            "max_slope_pct": 10,
            "semi_size": 2,
            "penalty_factor_xy": 40,
            "penalty_factor_z": 40
        }

        #        finder = oq.BestPathFinder(self.input_dtm, self.exclusion_areas_layer)
        finder = oq.BestPathFinder(self.input_dtm)
        finder.set_parameters(parameters)
        finder.set_output_folder(outputFolder)
        _ = finder.create_raw_output_layer()
        simplified_layer = finder.create_simplified_output_layer(
            polylineThreshold)

        height_prof, slope_profile, _ = viewers.height_profile(
            finder.dtm["array"], finder.optimizer.waypoints_index(),
            finder.parameters["max_slope_pct"] / 100.0,
            finder.converter.pixel_width)

        self.assertEqual(
            len(slope_profile) - 1,
            simplified_layer.dataProvider().featureCount())

        feats = simplified_layer.dataProvider().getFeatures()
        for row_id, vals in enumerate(zip(feats, slope_profile[1:])):
            f, s = vals

            for att_id, a in enumerate(f.attributes()):
                with self.assertRaises(AttributeError):
                    a.isNull()
            self.assertAlmostEqual(s * 100.0, f.attribute("slope_p"))
            self.assertTrue(
                abs(f.attribute("slope_p")) <= parameters["max_slope_pct"])
            self.assertTrue(
                abs(f.attribute("slope_p")) >= parameters["min_slope_pct"])
    def test_empty_waypoints(self):
        """Test for empty layer
        """
        import tempfile
        outputFolder = tempfile.mkdtemp("frd")

        parameters = {
            "min_slope_pct": 0,
            "max_slope_pct": 10,
            "semi_size": 2,
            "penalty_factor_xy": 40,
            "penalty_factor_z": 40
        }

        finder = oq.BestPathFinder(self.input_dtm, self.exclusion_areas_layer)
        finder.set_parameters(parameters)
        finder.set_output_folder(outputFolder)
        finder.create_raw_output_layer()
        simp_layer = finder.create_simplified_output_layer(5)
        self.assertEqual(simp_layer.featureCount(), 0)