Skip to content

Commit 48ec82a

Browse files
committed
Add GtfsStopsAlgorithm, fixes #43
1 parent 7df73dc commit 48ec82a

File tree

2 files changed

+89
-2
lines changed

2 files changed

+89
-2
lines changed

qgis_processing/gtfsAlgorithm.py

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,89 @@
3737
pluginPath = os.path.dirname(__file__)
3838

3939

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+
40123

41124
class GtfsShapesAlgorithm(QgsProcessingAlgorithm):
42125
INPUT = "INPUT"
@@ -119,7 +202,6 @@ def get_fields(self):
119202
return fields
120203

121204

122-
123205
class GtfsSegmentsAlgorithm(QgsProcessingAlgorithm):
124206
INPUT = "INPUT"
125207
SPEED_OPTION = "SPEED"

qgis_processing/trajectoolsProvider.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@
3939
pass
4040

4141
try: # gtfs_functions-based algs
42-
from .gtfsAlgorithm import GtfsShapesAlgorithm, GtfsSegmentsAlgorithm
42+
from .gtfsAlgorithm import (
43+
GtfsStopsAlgorithm,
44+
GtfsShapesAlgorithm,
45+
GtfsSegmentsAlgorithm
46+
)
4347
except ImportError:
4448
pass
4549

@@ -101,6 +105,7 @@ def getAlgs(self):
101105
except NameError:
102106
pass
103107
try: # gtfs_functions-based algs
108+
algs.append(GtfsStopsAlgorithm())
104109
algs.append(GtfsShapesAlgorithm())
105110
algs.append(GtfsSegmentsAlgorithm())
106111
except NameError:

0 commit comments

Comments
 (0)