span8
span4
span8
span4
In theprevious example,we learned how to create folders using a feature type fanout.In this example,we will be focusing on creating folders through the use of KML IDs and KML parent attributes.This approach is best for when you want direct control over folder naming and parent-child relationships for as many levels deep as you want.In other words,this approach is for creating a custom folder hierarchy.
Not sure what folders are?No problem!KML folders are what defines the nested tree structure in the Google Earth Places pane.Check out the articleCreating KML Folders by Feature Type Fanoutfor a full definition.
Similar to the feature type fanout approach,these methods of creating folders are intended for use in KML rather than the KMZ (which is the zipped KML file).
We can create KML folders through KML IDs in two parts: by creating the folder structure,then by creating folder elements and associating all our features with their appropriate folders using kml_parent attributes.
For this example,we will be using a CSV file of Vancouver's rapid transit stations.Rather than having a big list of stations in the KML Places legend in Google Earth,we want to organize it into a folder hierarchy.We can do this by separating stations by train lines,and then separating the downtown stops for the Canada Line,like the structure below:
The KML ID approach to folder setup using three levels of folders.This is the file structure that we will use in our example.The parent level is Train Route.It has three children: Connection,Expo,and CanadaLine.The Canada Line has its own child folder,Downtown.
To create our empty folder structure,we will first use aCreatortransformer to create a Null Geometry Object.Delete the Creation Instance Attribute ‘_creation_instance'.You can leave all other parameters to default.
We will be using oneAttributeCreatortransformer to create each folder.So in other words,if I want five folders,I will be using five AttributeCreators.Each AttributeCreator will connect to the Creator.Referring to the image of the file structure above,we are creating our Parent folder first.Fill in the transformer parameters with the attributes and values below:
New Attribute | Attribute Value |
kml_id | TrainRoute |
kml_name | Train Route |
kml_document | transportation |
This is our parent folder,and we are naming it Train Route (the kml_name).Notice that the kml_id does not have any spaces.We are doing this to reduce the chance of error later when we need to associate our data to this folder.We will put data into these folders by associating it with the kml_id of the folder that we want.We need to be aware ofspacesandcase-sensitivitybecause our data won't go into the right folder if we have the wrong ID!
The last thing to point out here is the kml_document.Normally the kml_document would be set to the fme_basename (the source data file name) by default when reading from a dataset.However,当使用一个创造者,we need to set the kml_document.It's really important here that we set all of our subsequent AttributeCreators to the same kml_document name if we want to write it out into the same KML file.The name of the kml_document must also be the same name of the file that you are writing out.Folders and data tagged with a different document name than the file name will not be written out into that file.
The kml_document is named ‘transportation' in the AttributeCreator parameters.
We will be connecting a Writer later and we will be naming the KML file,‘transportation'.
为高级用户请注意:This means that by controlling what you name your kml_document,you can write out different KML files using the same workspace.For example,if I want an offshoot KML file called potatoes,I would name the kml_document ‘potatoes' in the AttributeCreator,and I would get an empty folder called potatoes.kml in addition to what I'm writing out in my workspace.Just be sure that your data is also being written to the desired folder and kml_document name.
We are now creating our next level of folders using another three AttributeCreators.We want to create the folders: Connection,Expo,and Canada Line.Alright,you got me!For those of you who are familiar with Vancouver,there is no line called Connection.It's just a category I've separated for Waterfront station which connects the two train lines in this dataset.
Let's connect a new AttributeCreator to the Creator.Fill in the first transformer like the one below for the folder,Connection:
New Attribute | Attribute Value |
kml_parent | TrainRoute |
kml_id | Connection |
kml_name | Connection |
kml_document | transportation |
Notice that we have a new attribute here called kml_parent.This method of assigning parent folders is what creates the hierarchy.Duplicate this AttributeCreator twice (select the transformer and hit Ctrl-D) and make the following changes for each:
AttributeCreator Duplicate 1:
AttributeCreator Duplicate 2:
The kml_parent will remain the same for these folders because all of these folders are on the same level.The kml_document will also stay the same so that we are writing this whole folder hierarchy to the same KML file.Connect our new AttributeCreators to the Creator.
Duplicate one last AttributeCreator and attach it to the Creator.This is our subfolder to the folder Canada Line.Make the following changes to the transformer parameters:
The attribute kml_document will not change.
The parameters for the folder,‘Downtown'.This folder is a child of the folder CanadaLine.
Add a Writer for the format Google KML.Set your dataset path,and set the ‘Feature Type Definition' toAutomatic.Press OK,and in the next dialog,name the ‘Feature Type Name',Folder.This feature type must be named Folder for Google Earth to recognize your folder structure.Click OK.Run your workspace now and check the output in Google Earth to see that you have created the desired folder structure.
The output folder structure displayed in the Google Earth Places legend.
The completed Part A workspace that creates the empty folder structure.
Now that we have the folder structure,we can add data to these folders.
In the same workspace as Part A,read in the CSV file rapid_train_stations.csv.Before pressing OK,set the Coordinate System to UTM84-10N.Next,go into the parameters and set the Attribute Definition toManual.Set x to Type x_coordinate and y to y_coordinate.
Next,we will use aTestFilterto filter out our data by train line and then our last folder,Downtown stations that belong on the Canada Line.We want to filter our data in the same way that our folders are set up so that we can associate data with the correct folder.Fill in the parameters like the table below:
Test Condition | Output Port | |
If | Line = Connection | Connection |
Else | Line = Expo | Expo |
Else If | STATION CONTAINS Vancouer OR | |
STATION CONTAINS YALETOWN | Downtown | |
Else |
|
CanadaLine |
Except for Waterfront,which we have categorized as a Connection,there are only two other stations on the Canada Line that we consider a part of downtown: Yaletown-Roundhouse and Vancouver City Centre stations.Using that information,we are filtering out Downtown stations on the condition that the station name contains parts of the stations we are looking for (keywords: Vancouver and Yaletown).
7.Set the Parent Folder for the Stations of Each Train Line
Add a new AttributeCreator to the CONNECTION output port of the TestFilter.In this step,we are setting the KML parent folder to the data and giving the station an ID name that will be displayed in Google KML by default.
We will set the kml_parent to the corresponding folder (Connection port has a kml_parent: Connection,Expo has a kml_parent: Expo,Downtown has a kml_parent: Downtown,Canada Line has a kml_parent: CanadaLine).We are using the kml_id of the folder as the kml_parent.Remember that we did not use any spaces in the kml_id's.Below are the parameters for the AttributeCreator connected to the CONNECTION output port of the TestFilter.
New Attribute | Attribute Value |
kml_parent | Connection |
kml_id | STATION |
For the kml_id here,we are setting it to the Attribute ‘STATION' in all four AttributeCreators.This attribute contains the train station name.Do the same for the AttributeCreators connected to Expo and CanadaLine,using their respective kml_parent IDs.
AttributeCreators connected to the TestFilter with annotations showing their respective parameters.
At this point in our workspace,all of our folders now have data.In this step,we are styling our stations by setting icons and icon colors for visualization.Add fourKMLStylersto the workspace and attach one to each AttributeCreator from Step 7.
Feel free to have some fun setting the Icon and the Icon Color.I have chosen to use the Icon Name:gme/gx_railand the colors magenta for Connection,yellow for Expo,turquoise for CanadaLine,and orange for the two Downtown stations.
If we ran our workspace now,we would find that all of our stations are written in capital letters.To make this look nicer,we will connect all of our KMLStylers to a singleKMLPropertySettertransformer.In the transformer parameters,open the text editor for ‘Name' in the section Navigation Tree.Double clickFullTitleCasein the String Functions andSTATIONin the FME Feature Attributes.Our string should like it does below:
@FullTitleCase(@Value(STATION))
This string function will convert our STATION name to title case.
Finally,to write out our KML,right click and insert a Writer Feature Type and attach it to the output port of the KMLPropertySetter.Name the feature type,Placemarks.Run the workspace.
The KML result displayed in Google Earth.Train stations are color coded by folder and display the name of the station represented.
The resulting folders displayed in the Places panel in Google Earth.
Congratulations!You have successfully learned how to create custom folders using kml_id's and kml_parent attributes.The key here is to create your folder structure first using a Creator,and then putting your data into those folders.Compared to the feature fanout method,this method gives you direct control to create as many subfolders as you need.Here are some tips and tricks to keep in mind when using the KML ID approach:
Note:For a more automated and Pythonic way of using the KML ID approach to create folders,please see Example 4 of thePythonCaller Transformer article.
The data used here originates from open data made available by theCity of Vancouver,British Columbia.It contains information licensed under the Open Government License - Vancouver.
© 2019 亚搏在线Safe Software Inc |Legal