This tutorial will show you how to do the following in QGIS:
Import Shapes.txt into QGIS and convert it into a line Shapefile
Join the stop-level frequency tables created in the previous tutorial to the Shapes layer and visualize frequencies in multiple ways
Merge multiple route variants into single route line features.
This tutorial is intended for QGIS users at the level of "advanced beginner" or higher - basic familiarity with fundamental GIS concepts and the program itself will make this an easier overall experience. Visit their website to learn more about QGIS and how to set it up on your machine.
All of these steps can also be carried out in ArcGIS, but the tools/processes themselves may have different names, and the steps may be done in a different order.
Before you import shapes.txt into QGIS or ArcMap, you’ll need to create the CSV described at the bottom of this page, which is what connects route_id to shape_id, since the route_id field does not appear in shapes.txt.
If you want to map frequency data at the route level, make sure you carry out all the steps outlined in the previous tutorial on setting up GTFS data in Excel.
If you're solely interested in getting shapes.txt into QGIS, you can move on to the next step.
In the top bar of the QGIS user interface, select Add Layer → Add Delimited Text Layer.
Navigate to where shapes.txt is saved, and select it. Make sure that under "Geometry Definition", the "Point Coordinates" radio button is selected, and that the correct latitude and longitude fields from the .txt file are marked as the X and Y fields in the menu.
Then, click Add to add the layer to the map. Compare it to the basemap to make sure it loaded properly.
When the shapes layer has been added into QGIS, the shapes themselves will appear as a series of points that reflect the route alignments.
Once shapes has been successfully imported into QGIS, we’ll need to export the layer as a Shapefile - certain attributes can’t be edited in .txt files. This can be done easily - right-click on the shapes layer, then Export → Save Features As.
In the menu that pops up, select “ESRI Shapefile” from the Format drop-down menu. Then, click the [...] box on the right side of the File Name field to navigate to the location where you want to save the shapefile, and give the file a new name.
Make sure that the box next to “Add saved file to map” at the bottom of the window is checked. Then, click OK.
Once the new shapes Shapefile has been successfully added to the map, you can remove the original shapes.txt layer.
Next, we'll need to convert this layer from a point shapefile to a line shapefile. Open the Processing toolbox, and search for Points to Path. Open the Points to Path tool and select the Run as Batch Process button (circled in red) in the lower-left corner of the pop-up window.
Here are the fields in the Batch Processing window (pictured below) and what to do with them, from left to right:
Input layer - click the [...] button and select the shapes layer you wish to convert to lines
Create Closed Paths - leave as default
Order Expression - select shape_pt_s. This is the truncated name for the shape_pt_sequence field in shapes that determines the order of the discrete points in the file that'll be converted to lines
Sort text containing numbers naturally - leave as default
Path group expression - select shape_id. This will group together the points by shape_id.
Paths - click the [...] button and navigate to where you want to save the new line file. Save it as a shapefile.
Directory for text output - leave as default
IMPORTANT: After you save the new line file, a pop-up called Autofill settings will appear. Select Fill with numbers from the drop-down menu, and click OK. The fill with numbers setting will preserve the shape_id field, which you will need for identification and analysis purposes.
Once all fields are filled out, click Run in the lower right corner of the Batch Processing window.
Once this process is complete, navigate to where you saved the new line shapefile on your machine, then import it into QGIS. Each shape should show up as a polyline, rather than a sequence of points, and the shape_id field should be preserved.
This section will discuss how to carry out the following tasks in QGIS:
Joining the Shapes2Routes CSV created in the prior tutorial (instructions at the bottom of the page here) to the shapes layer
Merging multiple shapes (i.e. route variations) into single combined routes
In the top bar of the QGIS user interface, select Add Layer → Add Delimited Text Layer.
Navigate to where the CSV is saved and select it. Make sure that under "Geometry Definition", the "No geometry (attribute-only table)" radio button is selected, as this CSV should not have any spatial information.
Then, click Add to add the layer to the map. It should show up in the left-hand Layers column as a table.
Right-click on the layer and select Open Attribute Table to view the table and make sure it imported correctly.
Right-click on the shapes shapefile layer, and select Properties. In the left-hand side of the pop-up menu, navigate to Joins (which has the blue arrow/triangle icon) and select the Green + button to add a vector join.
In the subsequent window that pops up, select the CSV that you plan to join to the stops layer under the Join Layer drop-down menu. The Join Field and Target Field should both be the shape_id - this is what ties together the shapes layer with the route information contained in the CSV table.
At the bottom of the window, check the Custom field name prefix box and leave it blank - this will prevent QGIS from auto-filling it with the name of the CSV table, which will delete the existing field names.
Select OK to close out of this pop-up menu, then click Apply on the Joins menu - this will carry out the vector join.
Right-click on the shapes layer and select Open Attribute Table to double-check that the table join succeeded. In the table on the right, you can see that route_ids and route_short_names have been appended to the shapes layer.
Once the join is complete, re-export the newly-joined layer as a new shapefile. This will solidify the join and permanently append the joined data to the shapes layer, and allow you to perform additional queries and analysis on the layer that aren’t possible otherwise.
For more information on vector joins/table joins, see the QGIS documentation.
Right-click on the shapes layer and select Open Attribute Table. Sort the file by route_id (Column 4). Select the first group of shapes that share the same route_id value (in this case, 28426). The selected routes should be highlighted on the map in yellow.
In the toolbar on the top of the window, select the yellow pencil icon to toggle on Edit Mode for the layer (see below). The shapes you selected in the previous step should now appear in red, like the image on the right.
Go to Edit → Edit Features → Merge Selected Features. This will bring up a menu that will show the attributes of the selected rows.
In that menu, you can use the drop-down menus in the ID row at the top of the window to select which feature you which to preserve for each column in the final merged line feature. In this case, the only fields we really need to preserve are Columns 4 (route_id) and 5 (route_short_name). In the lowest row in the window (merge), you can see the data that will be preserved in the final merged line feature.
Once you've adjusted the feature values to your preferences, click OK and the selected shapes will be merged into a single route.
You will need to repeat this process for every unique route that has multiple shapes; this may take a little while for agencies that have large numbers of routes and variants.
Once this step is complete, rename the layer to routes. This layer will be referred to as routes in all subsequent steps of this tutorial.
To join the CSV containing route-level frequencies to the newly-created routes layer with the merged shapes, follow the same process as outlined in the "Join the Stop-Level Frequencies CSV to the Stops Layer" tutorial, but use route_id as the Join field.
Once the routes layer and the CSV containing trip frequencies have been joined, right-click on the route layer, and select Properties → Layer Properties (the icon that looks like a paintbrush).
From the drop-down menu at the top, select Graduated.
From the Value drop-down, select the name of the field you wish to visualize (in this case, Weekday AM Peak trips).
From the Method drop-down, you can choose either Size or Color. The former will scale the line thickness based on values, while the latter will change the color ramp.
There are many different ways to visualize frequency data, whether by size, color, or both! Experiment with different methods and see what kinds of schemes you can come up with - below are a few examples:
Los Angeles Department of Transportation - Daily weekday trips per route by color and line weight
Los Angeles Department of Transportation - Daily weekday trips per route line color