The other day I received some ASCII comma separated value formatted text file (an example is shown below) that stores the vertices of polygons in the first 2 columns and a unique polygon identifier in the 3rd. Each polygon is separated from another with a blank line. I needed to convert the text strings into polygon geometries in a shape file for display in a GIS. There are a myriad of ways to do it but I chose the new SAFE FME 2009 to give the new version a workout. Here's how I created the polygons from the CSV file:
- Run the FME Workbench and choose to use the workspace wizard.
The Create Translation Workspace Wizard appears.
- Choose Comma Separated Value (CSV). Click Next. Type in the path and filename of the CSV file e.g. C:\temp\polygons.csv.
- Click Settings.
The Input Settings for Comma Separated Value (CSV) appears.
Note: change the settings as necessary for your CSV file.
- When prompted for the destination format, select ESRI Shape.
- Click Next twice. Click Finish.
The Workbench shows the source and destination datasets.
- Use the Workbench's Transformers pane to add in the StringSearcher transformer. Join this to the source dataset. Open up the StringSearcher Parameters.
Note: we want to filter out the blank lines demarcating the polygons in the text file. One way of doing it is to specify a regular expression to match empty lines. For instance, the expression ^ means the beginning of the line and $ means the ending of the line. Together ^$ means matching lines with nothing from start to the end.
- In the Attribute field, select col0. In the Regular Expression field, type in ^$. Click OK.
- Add in the 2DPointAdder transformer and connect it to the StringSearcher transformer's NOT_MATCHED field. Open up the 2DPointAdder Parameters.
Note: the 2dPointAdder transformer will form a point geometry from the input X and Y strings. The input strings must be from non-blank lines and that is why we connect it to the UN_MATCHED output from the "blank line" StringSearcher transformer.
- In the X Value field, select col0. In the Y Value field, choose col1. Click OK.
- Add in the PointConnector transformer and connect it to the 2DPointAdder transformer. Open up the PointConnector Parameters.
Note: the PointConnector transfomer will make polygon geometries from the input point geometries. One way to differentiate from one polygon to another polygon is through the Connection Break Attributes - in this example, the 3rd column stores the polygon identifier and any change in this value indicates a change in the polygon.
- In the Connection Break Attributes, click the ... button.
The Select Connection Break Attributes Items dialog box appears.
- Toggle on col2. Click OK. Click OK again.
- Connect the PointConnector transformer's POLYGON output to the destination dataset.
- Open up the destination dataset's Feature Type Properties.
- In the Allowed Geometries field, select shape_polygon. Click OK.
The workbench process flow may look like this at this point.
- Run the translation and display the resultant shape file.
As you can see from the screen shot above, FME has successfully created the polygons from the input CSV file using the transformers outlined above.