|
37 | 37 | pluginPath = os.path.dirname(__file__)
|
38 | 38 |
|
39 | 39 |
|
| 40 | +class GtfsStopsAlgorithm(QgsProcessingAlgorithm): |
| 41 | + INPUT = "INPUT" |
| 42 | + OUTPUT = "OUTPUT" |
| 43 | + |
| 44 | + def __init__(self): |
| 45 | + super().__init__() |
| 46 | + |
| 47 | + def name(self): |
| 48 | + return "gtfs_stops" |
| 49 | + |
| 50 | + def displayName(self): |
| 51 | + return self.tr("Extract stops") |
| 52 | + |
| 53 | + def group(self): |
| 54 | + return self.tr("GTFS") |
| 55 | + |
| 56 | + def groupId(self): |
| 57 | + return "Gtfs" |
| 58 | + |
| 59 | + def tr(self, text): |
| 60 | + return QCoreApplication.translate("trajectools", text) |
| 61 | + |
| 62 | + def helpUrl(self): |
| 63 | + return "https://github.com/Bondify/gtfs_functions" |
| 64 | + |
| 65 | + def shortHelpString(self): |
| 66 | + return self.tr( |
| 67 | + "<p>Extracts stops from a GTFS ZIP file using " |
| 68 | + "gtfs_functions.Feed.stops</p>" |
| 69 | + ) |
| 70 | + |
| 71 | + def createInstance(self): |
| 72 | + return type(self)() |
| 73 | + |
| 74 | + def initAlgorithm(self, config=None): |
| 75 | + self.addParameter( |
| 76 | + QgsProcessingParameterFile( |
| 77 | + name=self.INPUT, |
| 78 | + description=self.tr("Input GTFS file"), |
| 79 | + ) |
| 80 | + ) |
| 81 | + self.addParameter( |
| 82 | + QgsProcessingParameterFeatureSink( |
| 83 | + name=self.OUTPUT, |
| 84 | + description=self.tr("GTFS stops"), |
| 85 | + type=QgsProcessing.TypeVectorPoint, |
| 86 | + ) |
| 87 | + ) |
| 88 | + |
| 89 | + def processAlgorithm(self, parameters, context, feedback): |
| 90 | + gtfs_file = self.parameterAsFile(parameters, self.INPUT, context) |
| 91 | + (self.sink_stops, self.dest_stops) = self.parameterAsSink( |
| 92 | + parameters, |
| 93 | + self.OUTPUT, |
| 94 | + context, |
| 95 | + self.get_fields(), |
| 96 | + QgsWkbTypes.Point, |
| 97 | + QgsCoordinateReferenceSystem("EPSG:4326"), |
| 98 | + ) |
| 99 | + |
| 100 | + feed = Feed(gtfs_file) |
| 101 | + stops = feed.stops |
| 102 | + for _, stop in stops.iterrows(): |
| 103 | + pt = QgsGeometry.fromWkt(stop.geometry.wkt) |
| 104 | + f = QgsFeature() |
| 105 | + f.setGeometry(pt) |
| 106 | + attrs = [ |
| 107 | + stop.stop_id, |
| 108 | + stop.stop_code, |
| 109 | + stop.stop_name |
| 110 | + ] |
| 111 | + f.setAttributes(attrs) |
| 112 | + self.sink_stops.addFeature(f, QgsFeatureSink.FastInsert) |
| 113 | + |
| 114 | + return {self.OUTPUT: self.dest_stops} |
| 115 | + |
| 116 | + def get_fields(self): |
| 117 | + fields = QgsFields() |
| 118 | + fields.append(QgsField("stop_id", QVariant.String)) |
| 119 | + fields.append(QgsField("stop_code", QVariant.String)) |
| 120 | + fields.append(QgsField("stop_name", QVariant.String)) |
| 121 | + return fields |
| 122 | + |
40 | 123 |
|
41 | 124 | class GtfsShapesAlgorithm(QgsProcessingAlgorithm):
|
42 | 125 | INPUT = "INPUT"
|
@@ -119,7 +202,6 @@ def get_fields(self):
|
119 | 202 | return fields
|
120 | 203 |
|
121 | 204 |
|
122 |
| - |
123 | 205 | class GtfsSegmentsAlgorithm(QgsProcessingAlgorithm):
|
124 | 206 | INPUT = "INPUT"
|
125 | 207 | SPEED_OPTION = "SPEED"
|
|
0 commit comments