|
| 1 | +import processing |
| 2 | +from qgis.core import QgsFeature, QgsProject |
| 3 | + |
| 4 | +from ..algorithms.utilities import GeocodingAlgorithmBase |
| 5 | +from .base import TestCaseBase |
| 6 | + |
| 7 | + |
| 8 | +class AlgorithmsBasicTest(TestCaseBase): |
| 9 | + """Testing algorithms with basic parameters (mostly default)""" |
| 10 | + |
| 11 | + def test_processing_time_map_simple(self): |
| 12 | + input_lyr = self._make_layer(["POINT(-3.1 55.9)"]) |
| 13 | + results = processing.runAndLoadResults( |
| 14 | + "ttp_v4:time_map_simple", |
| 15 | + { |
| 16 | + "INPUT_SEARCHES": input_lyr, |
| 17 | + "INPUT_TIME": self._today_at_noon().isoformat(), |
| 18 | + "OUTPUT": "memory:", |
| 19 | + # TODO: these inputs should work without values |
| 20 | + "INPUT_SEARCH_TYPE": 0, |
| 21 | + "INPUT_TRNSPT_TYPE": 0, |
| 22 | + "OUTPUT_RESULT_TYPE": 0, |
| 23 | + }, |
| 24 | + ) |
| 25 | + output_layer = QgsProject.instance().mapLayer(results["OUTPUT"]) |
| 26 | + self.assertEqual(output_layer.featureCount(), 1) |
| 27 | + |
| 28 | + def test_processing_time_filter_simple(self): |
| 29 | + input_lyr_a = self._make_layer(["POINT(0.0 51.5)"]) |
| 30 | + input_lyr_b = self._make_layer( |
| 31 | + ["POINT(0.1 51.5)", "POINT(-0.1 51.5)", "POINT(0.0 51.6)"] |
| 32 | + ) |
| 33 | + results = processing.runAndLoadResults( |
| 34 | + "ttp_v4:time_filter_simple", |
| 35 | + { |
| 36 | + "INPUT_SEARCHES": input_lyr_a, |
| 37 | + "INPUT_LOCATIONS": input_lyr_b, |
| 38 | + "INPUT_TIME": self._today_at_noon().isoformat(), |
| 39 | + "INPUT_TRAVEL_TIME": 15, |
| 40 | + "OUTPUT": "memory:", |
| 41 | + # TODO: these inputs should work without values |
| 42 | + "INPUT_SEARCH_TYPE": 0, |
| 43 | + "INPUT_TRNSPT_TYPE": 0, |
| 44 | + }, |
| 45 | + ) |
| 46 | + output_layer = QgsProject.instance().mapLayer(results["OUTPUT"]) |
| 47 | + self.assertEqual(output_layer.featureCount(), 3) |
| 48 | + |
| 49 | + def test_processing_routes_simple(self): |
| 50 | + input_lyr_a = self._make_layer(["POINT(0.0 51.5)"]) |
| 51 | + input_lyr_b = self._make_layer( |
| 52 | + ["POINT(0.1 51.5)", "POINT(-0.1 51.5)", "POINT(0.0 51.6)"] |
| 53 | + ) |
| 54 | + results = processing.runAndLoadResults( |
| 55 | + "ttp_v4:routes_simple", |
| 56 | + { |
| 57 | + "INPUT_SEARCHES": input_lyr_a, |
| 58 | + "INPUT_LOCATIONS": input_lyr_b, |
| 59 | + "INPUT_TIME": self._today_at_noon().isoformat(), |
| 60 | + "OUTPUT": "memory:", |
| 61 | + # TODO: these inputs should work without values |
| 62 | + "INPUT_SEARCH_TYPE": 0, |
| 63 | + "INPUT_TRNSPT_TYPE": 0, |
| 64 | + "OUTPUT_RESULT_TYPE": 0, |
| 65 | + }, |
| 66 | + ) |
| 67 | + output_layer = QgsProject.instance().mapLayer(results["OUTPUT"]) |
| 68 | + self.assertEqual(output_layer.featureCount(), 3) |
| 69 | + |
| 70 | + def test_processing_time_map(self): |
| 71 | + input_lyr_a = self._make_layer(["POINT(0.0 51.5)"]) |
| 72 | + results = processing.runAndLoadResults( |
| 73 | + "ttp_v4:time_map", |
| 74 | + { |
| 75 | + "INPUT_DEPARTURE_SEARCHES": input_lyr_a, |
| 76 | + "INPUT_DEPARTURE_TIME": self._today_at_noon().isoformat(), |
| 77 | + "INPUT_DEPARTURE_TRAVEL_TIME": "900", |
| 78 | + "INPUT_THROTTLING_STRATEGY": 0, |
| 79 | + "OUTPUT": "memory:", |
| 80 | + }, |
| 81 | + ) |
| 82 | + output_layer = QgsProject.instance().mapLayer(results["OUTPUT"]) |
| 83 | + self.assertEqual(output_layer.featureCount(), 1) |
| 84 | + |
| 85 | + def test_processing_time_filter(self): |
| 86 | + input_lyr_a = self._make_layer(["POINT(0.0 51.5)"]) |
| 87 | + input_lyr_b = self._make_layer( |
| 88 | + ["POINT(0.1 51.5)", "POINT(0.2 51.5)", "POINT(0.3 51.5)"] |
| 89 | + ) |
| 90 | + results = processing.runAndLoadResults( |
| 91 | + "ttp_v4:time_filter", |
| 92 | + { |
| 93 | + "INPUT_DEPARTURE_SEARCHES": input_lyr_a, |
| 94 | + "INPUT_DEPARTURE_TIME": self._today_at_noon().isoformat(), |
| 95 | + "INPUT_DEPARTURE_TRAVEL_TIME": "900", |
| 96 | + "INPUT_LOCATIONS": input_lyr_b, |
| 97 | + "INPUT_THROTTLING_STRATEGY": 0, |
| 98 | + "OUTPUT": "memory:", |
| 99 | + }, |
| 100 | + ) |
| 101 | + output_layer = QgsProject.instance().mapLayer(results["OUTPUT"]) |
| 102 | + self.assertEqual(output_layer.featureCount(), 3) |
| 103 | + |
| 104 | + def test_processing_routes(self): |
| 105 | + input_lyr_a = self._make_layer(["POINT(0.0 51.5)"]) |
| 106 | + input_lyr_b = self._make_layer( |
| 107 | + ["POINT(0.1 51.5)", "POINT(-0.1 51.5)", "POINT(0.0 51.6)"] |
| 108 | + ) |
| 109 | + results = processing.runAndLoadResults( |
| 110 | + "ttp_v4:routes", |
| 111 | + { |
| 112 | + "INPUT_DEPARTURE_SEARCHES": input_lyr_a, |
| 113 | + "INPUT_DEPARTURE_TIME": self._today_at_noon().isoformat(), |
| 114 | + "INPUT_DEPARTURE_TRAVEL_TIME": "900", |
| 115 | + "INPUT_LOCATIONS": input_lyr_b, |
| 116 | + "INPUT_THROTTLING_STRATEGY": 0, |
| 117 | + "OUTPUT": "memory:", |
| 118 | + }, |
| 119 | + ) |
| 120 | + output_layer = QgsProject.instance().mapLayer(results["OUTPUT"]) |
| 121 | + self.assertEqual(output_layer.featureCount(), 3) |
| 122 | + |
| 123 | + def test_processing_geocoding(self): |
| 124 | + input_lyr = self._make_layer( |
| 125 | + [], |
| 126 | + layer_type="NoGeometry?crs=EPSG:4326&field=place:string(255,0)", |
| 127 | + ) |
| 128 | + for place in ["London", "Birmingham Palace", "Newcastle"]: |
| 129 | + feat = QgsFeature() |
| 130 | + feat.setFields(input_lyr.fields()) |
| 131 | + feat.setAttribute("place", place) |
| 132 | + input_lyr.dataProvider().addFeature(feat) |
| 133 | + |
| 134 | + results = processing.runAndLoadResults( |
| 135 | + "ttp_v4:geocoding", |
| 136 | + { |
| 137 | + "INPUT_DATA": input_lyr, |
| 138 | + "INPUT_THROTTLING_STRATEGY": 0, |
| 139 | + "INPUT_QUERY_FIELD": '"place"', |
| 140 | + "OUTPUT": "memory:", |
| 141 | + "OUTPUT_RESULT_TYPE": GeocodingAlgorithmBase.RESULT_TYPE.index( |
| 142 | + "BEST_MATCH" |
| 143 | + ), |
| 144 | + }, |
| 145 | + ) |
| 146 | + output_layer = QgsProject.instance().mapLayer(results["OUTPUT"]) |
| 147 | + self.assertEqual(output_layer.featureCount(), 3) |
| 148 | + |
| 149 | + def test_processing_reverse_geocoding(self): |
| 150 | + input_lyr = self._make_layer( |
| 151 | + ["POINT(0.1 51.5)", "POINT(-0.1 51.5)", "POINT(0.0 51.6)"] |
| 152 | + ) |
| 153 | + results = processing.runAndLoadResults( |
| 154 | + "ttp_v4:reverse_geocoding", |
| 155 | + { |
| 156 | + "INPUT_DATA": input_lyr, |
| 157 | + "INPUT_THROTTLING_STRATEGY": 0, |
| 158 | + "OUTPUT": "memory:", |
| 159 | + "OUTPUT_RESULT_TYPE": GeocodingAlgorithmBase.RESULT_TYPE.index( |
| 160 | + "BEST_MATCH" |
| 161 | + ), |
| 162 | + }, |
| 163 | + ) |
| 164 | + output_layer = QgsProject.instance().mapLayer(results["OUTPUT"]) |
| 165 | + self.assertEqual(output_layer.featureCount(), 3) |
0 commit comments