Exercise 4 | Grounds Maintenance Project - Labelling Features |
Data | City Parks (MapInfo TAB) |
Overall Goal | Calculate the size and average size of each park in the city, to use in Grounds Maintenance estimates for grass cutting, hedge trimming, etc. |
Demonstrates | Content transformation with parallel transformers |
Start Workspace | C:\FMEData2019\Workspaces\DesktopBasic\DataTransformation-Ex4-Begin.fmw |
End Workspace | C:\FMEData2019\Workspaces\DesktopBasic\DataTransformation-Ex4-Complete.fmw C:\FMEData2019\Workspaces\DesktopBasic\DataTransformation-Ex4-Complete-Advanced.fmw |
Let's continue your work on the grounds maintenance project.
In this part of the project, we’ll create a label for each park and write it to a new output layer. This step is best done using a parallel stream of data.
1) Start FME Workbench
Start FME Workbench (if necessary) and open the workspace from Exercise 3. Alternatively you can open C:\FMEData2019\Workspaces\DesktopBasic\DataTransformation-Ex4-Begin.fmw
The previous exercise measured park areas with the AreaCalculator. Now we are asked to add this information as labels to the output dataset.
This can be accomplished by using the LabelPointReplacer transformer but first, let's add another writer feature type to contain the labels.
2) Create New Writer Feature Type
Because we want to write label features to a separate layer (table) in the output, we need to create another feature type object on the canvas. There is more about this in a later chapter, but for now, right-click the writer feature type and choose the option Duplicate. Doing so creates a new feature type (here, a layer, but for other formats could be a table or other grouping of data) in the output dataset.
Now clean up this feature type's schema. View the feature type's dialog and rename the new feature type to ParkLabels. In the User Attributes tab delete all of the existing user attributes.
TIP |
You can quickly remove all of the existing user attributes by switching the Attribute Definition to Automatic and then back to Manual. The Automatic will clear it because there are currently no attributes coming in. This only works for feature types that have not been connected to the rest of the workspace yet |
3) Place a StringConcatenator Transformer
Click onto a blank area of canvas. Type "StringConcatenator" to add a transformer of this type.
Connect it to the Complete port of the StatisticsCalculator by dragging a second connection from there to the new transformer.
Make a new connection from the StringConcatenator to the ParkLabels feature type.
4) Check the StringConcatenator Parameters
View the parameters for the StringConcatenator transformer. There are both basic and advanced dialogs, and the basic one looks like this:
Enter LabelText as the name for the new attribute to create.
In the String Parts section, set the following four parts:
String Type | String Value |
---|---|
Attribute Value | ParkName |
New Line | |
Attribute Value | ParkArea |
Constant | sq meters |
Be sure to include a space character in the constant before "sq meters".
TIP |
You may find it quicker to switch to the Advanced editor dialog and enter the content directly:
@Value(ParkName) @Value(ParkArea) sq meters |
5) Run the Workspace
Let's run the workspace to ensure that the StringConcatenator is doing what we expected. Ensure that Feature Caching is enabled, and then click on the StringContatenator to open the popup menu. Then click on Run to This, that way the StringContatenator will run, but we won't be rewriting the data out:
Then click the magnifying glass on the output port to view the cache in Visual Preview. If you double-click on any of the features in the LabelText column, a dialog will appear that will show the label strings formatted correctly:
6) Place a LabelPointReplacer Transformer
Click onto the connection between StringConcatenator:Output and the ParkLabels feature type. Type "LabelPointReplacer" to add a transformer of this type.
The new transformer will be added and automatically connected between those two objects.
7) Check LabelPointReplacer Parameters
Inspect the LabelPointReplacer parameters.
Firstly click the drop-down arrow to the right of the Label parameter:
Select Attribute Value > LabelText to select the label previously defined in the StringConcatenator.
Now click in the Label Height field and type 25 (that’s 25 working units, which in this case is meters).
The “Always Rotate Label” parameter can be left to its default value.
TIP |
Many parameter fields (like Label Height) can be set either as a constant value (by typing it in) or set to an attribute by clicking the drop down arrow and selecting Attribute Value.
And - as you'll see shortly - it's also possible to construct a parameter value directly inside the transformer settings |
8) Run the Translation
Add another bookmark if you wish, then run the translation, since the translation is cached up to the StringContatenator it will only take a moment to write out the data.
Click on the ParksMaintenanceData to open the popup menu, then click on View Written Data:
The data will open in Visual Preview, but we want to view both our outputs together, so we will have to move to FME Data Inspector. In Visual Preview, click on the Open in Data Inspector button or hit Ctrl + Alt + D. FME Data Inspector will open and display the data in the current view:
Now with FME Data Inspector open, click on the Add data button on the menu bar. This will add data to the current view so we can view the labels on top of the parks:
In the Select Dataset to Add dialog, set the Format to MapInfo TAB (MITAB) and then browse to the ParkLabels Dataset:
C:\FMEData2019\Output\Training\ParkLabels.tab
Click OK, and the labels should now be on the parks. Notice that the output is in two layers in two files. Use the FME Data Inspector to open both output files in the same view.
Map tiles by Stamen Design, under CC-BY-3.0. Data by OpenStreetMap, under CC-BY-SA.
Advanced Exercise |
Now you know how to create a new feature type in the output, how to test data, and how to use parallel streams, why not try this task: Identify which parks are smaller than average and which parks are larger than average, and write them out to different feature types. |
CONGRATULATIONS |
By completing this exercise you have learned how to:
|