Exercise 3d | Best Practice Review |
---|---|
Scenario | FME Workspace Author |
Data | Roads (Autodesk AutoCAD DWG, Esri Shape, and/or PostGIS) |
Overall Goal | Find the shortest route from the FME User Conference to an Italian Cafe |
Demonstrates | Data Translation, data transformation, and Best Practice |
Start Workspace | None |
End Workspace | C:\FMEData2015\Workspaces\DesktopBasic\Exercise3d-Complete.fmwt |
In this exercise you are an attendee at an FME World Tour event. After a full day of learning about FME at the Vancouver Convention Centre you and some colleagues want to go and watch a soccer game on TV somewhere.
The Safe Software staff tell you the best experience would be at an Italian cafe on bustling Commercial Drive, but they neglect to tell you how to get there.
So, your task is to use the data available to you to calculate the best route from the convention centre to Commercial Drive, and to write out that data to GPX format so you can use it in your in-car navigation/GPS.
1) Create Database Connection
As we are going to use a PostGIS database, we should first create a connection to it. That way we can use the connection instead of having to enter connection parameters.
In a web browser visit http://fme.ly/database - this will show the parameters for a PostGIS database running on Amazon RDS.
In Workbench, select Tools > FME Options from the menubar.
Click on the icon for the Database Connections category, then click the [+] button to create a new connection. In the "Add Database Connection" dialog, first select PostgreSQL as the database type. Then enter the connection parameters obtained through the web browser.
Give the connection a name (such as PostGIS Training Database) and click Save. Then click OK to close the FME Options dialog.
2) Inspect Data
Start the FME Data Inspector to inspect the dataset we will be using. Select File > Open Dataset and, when prompted, enter the following:
Reader Format | PostGIS |
Reader Dataset | PostGIS Training Database |
Parameters | Under Table List choose Roads and One-Way Streets |
Click OK to close the dialogs and open the data. Change the color of the one-way streets so you can see that they are a different set of features. It's important to know which roads are oneway if we want to calculate a route that is actually legal!
If you have a background map applied, try to locate both the Vancouver Convention Centre and Commercial Drive.
NB: If you have any problems using the PostGIS database - for example connectivity problems with a firewall - then the following AutoCAD dataset can be substituted with very few changes required:
Reader Format | Autodesk AutoCAD DWG/DXF |
Reader Dataset | C:\FMEData2015\Data\Transportation\OneWayStreets.dwg |
3) Start Workbench
Start Workbench and use the option to Generate a workspace.
Reader Format | PostGIS |
Reader Dataset | PostGIS Training Database |
Parameters | Under Table List choose Roads and One-Way Streets |
Writer Format | GPS eXchange Format (GPX) |
Writer Dataset | C:\FMEData2015\Output\Training\DrivingRoute.gpx |
The workspace will look like this:
For the sake of Best Practice, you may want to put a bookmark around these features, and maybe annotate which are the one-way streets.
4) Add ShortestPathFinder
Now we need to start calculating a route. The obvious first step is to add a ShortestPathFinder transformer, which is how we can calculate our route.
So, add a ShortestPathFinder transformer. Connect Roads to the Network port.
The other input port on the ShortestPathFinder is for the From-To path (the start and end points of our journey). To add a feature to input here we can manually create it with the Creator transformer.
Add a Creator transformer and connect it to the From-To port.
Open the Creator parameters dialog.
Firstly enter UTM83-10 as the coordinate system of the data we are about to create. For the Geometry Object parameter, click the [...] browse button to the right to open a geometry-creation dialog. Select Line as the geometry type and enter the following coordinates:
X 491500 Y 5459550 X 494500 Y 5457440
The first coordinate is that of the convention centre and the second is the closest point in our network to Commercial Drive.
Click the OK button to close the dialog.
6) Check ShortestPathFinder Parameters
The coordinates of the feature we've added might not sit exactly on the road network. To get around this issue there are parameters we can use in the ShortestPathFinder.
So, open the ShortestPathFinder parameters dialog. Under Snap Options set From-To Snapping to Yes and enter 150 as the Snapping Tolerance:
Also notice that there are parameters for network costs - we'll be making use of those later.
7) Run Workspace
Add some Inspector transformers after the ShortestPathFinder and run the workspace to prove it is working up to this point.
If all has gone well, the output will look like this, with a route defined:
Of course, if all has not gone correctly, you must make use of Inspector and Logger transformers, plus Feature Debugging, to try and locate the error!
8) Add ChangeDetector
The result looks fine, but there are some things we are yet uncertain about: what if the route takes us the wrong way down a one-way street; and what if it uses slower, residential routes?
The first issue we can fix - most quickly - by ignoring one-way streets (i.e. avoiding them).
This we can implement in FME by matching up one-way streets so we can remove them from the network.
Add a ChangeDetector transformer to the workspace. Connect the Roads feature types to the Original port and the OneWayStreets feature type to the Revised port: i.e the Roads feature type includes one-way streets, and we can match them with the One-Way Streets feature type to filter them out:
Open up the ChangeDetector parameters. Note that we are matching geometry (correct) so don't change any parameters, just click OK to close the dialog.
By using the ChangeDetector road features that match a one-way street will emerge from the Unchanged port, so we should leave this unconnected. Similarly, Added features will be oneway streets that haven't been matched, and we can leave these out too.
So connect the Deleted port to the ShortestPathFinder Network port. The "Deleted" features are those that the transformer thinks have been deleted because they exist as roads, but not in the one-way data.
9) Re-Run Workspace
Now re-attach any Inspector transformers (if necessary) and re-run the workspace. Check the new route. If it has changed from before then you know this is because it is now avoiding oneway streets. This might not be the shortest route, but it's one we can confidently follow without heading directly into incoming traffic!
10) Add AttributeValueMapper
To weight our route in favour of arterial roads (not residential) we need to give each type of route a cost.
The best solution is an AttributeValueMapper transformer; so place one of these into the workspace.
After the ChangeDetector will be the best location, as then some features will have already been filtered out and this transformer will have less work to do.
11) Edit AttributeValueMapper
Now we can set up the AttributeValueMapper transformer. Open its parameters dialog:
- Select 'type' as the Source Attribute
- Enter Cost as the Destination Attribute
- Enter 2 as the Default Value
Now, underneath those parameters, we'll map some data.
Enter "Arterial" into the first Source Value field and a value of 1 in the matching Destination Value.
Enter "Residential" into the second Source Value field and a value of 3 in the matching Destination Value.
Basically, if the route is arterial (a main road) it will get a cost of 1; residential routes will get a cost of 3 and all other types will get a cost of 2 (because that's the default value). Click OK to close the dialog.
12) Apply Costs
Now we'll apply that newly calculated cost in the ShortestPathFinder.
Open the ShortestPathFinder parameters dialog. Set the Cost Type parameter to be "By Two Attributes".
Select "Cost" as the attribute for both the Forward and Reverse cost parameters (i.e. it will cost the same whichever direction the road feature runs and whichever direction we travel along it).
13) Run Workspace
The workspace will now look like this:
Save and then run the workspace. You'll find that your new route directs you from the convention centre to Commercial Drive avoiding one-way streets and taking the slowness of residential routes into account.
14) Connect Schema
Oh! Don't forget to remove the Inspector transformers and connect the Path port to the Route output feature type:
Now run the workspace, upload the data to your GPS device, and you are ready to go!
Advanced
Not really advanced, but you did use Best Practice throughout, right? I mean, you have bookmarks and annotations where needed, and no overlapping connections? If not, well you might want to fix that!
Congratulations! You have now:
completed the exercise for this chapter.