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