Convert Raster Images to CAD Using Potrace

Liz Sanderson
Liz Sanderson
  • Updated

FME Version

  • FME 2020.2

Introduction

There are different methods of converting rasters to CAD formats using FME. The method covered in this article will use the custom PotraceCaller transformer. Potrace™  is a tool that is used for tracing bitmaps and creating vectors. In this example, we will take a JPEG screenshot of a neighbourhood and vectorize the buildings to output an AutoCAD dwg file.

 

Requirements

 

Step-by-step Instructions

1. Add JPEG Reader

Add a JPEG (Joint Photographic Experts Group) Reader to a blank canvas. Browse to the osm.jpg dataset available for download from the File(s) section at the top-right corner of this article. 

 

2. Inspect Data to Determine Color Range

Inspect the data with the FME Data Inspector (or Visual Preview). We have a screenshot of a map section in Burnaby, BC taken from Open Street Map. We would like to create polygons from the building shapes so we will need to determine the color range. Click on any section of any of the grey building shapes, including the darker border. Looking at the Feature Information pane, take note of Band 0 (RED8) values while clicking over the building polygons, this will be the values for the RasterExpressionEvaluator.

Click on any of the buildings, and take note of the Band 0 (RED8) values in the Feature Information Pane in the Data Inspector
 

3. RasterExpressionEvaluator Band Expression

Back in FME Desktop, add a RasterExpressionEvaluator to the canvas and connect it to the JPEG feature type. In the parameters, set the Interpretation to Red8, for the Expression, click the [...] to open the Arithmetic Editor - ‘Raster Expression’. While inspecting the data we determined that the Value of Red is between 200 and 235 depending on where you put your mouse. But the Red Value of the area surrounding the building is 240 and up. We need to create an expression that states if Bands A[0-2] is greater than Red Value 200, but Less than 235, determine that it is a building, which looks like:

if(A[0]>200&&A[0]<235&&A[1]>200&&A[1]<235&&A[2]>200&&A[2]<235,0,255)

RasterExpressionEvaluator expression for color range of buildings
 

4. Set NoData for the Remainder of Pixels

To set NoData for the remainder of the pixels in the image, use the RasterBandNodataSetter transformer. Set the Nodata Value to 255.

 

5. Using the PotraceCaller

Download Potrace™ and unzip the download file. Then add the PotraceCaller custom transformer. In the parameters, set the Potrace.exe Location to the location where you downloaded Potrace, and select the Potrace.exe file. Set the Remove Speckles Up To, to 500, this will remove the anti-aliasing that occurs in the JPEG to create a truer vector line. 
 

6. Simplify the Tracing Output

Inspecting the outlines created using the PotraceCaller, the lines are in the correct area but are very pixelated. To smooth out this pixelization, add a Generalizer transformer. In the parameters, the Algorithm is set to Douglas (Generalize) by default; this algorithm removes vertices that cause a deviation of less than the Generalization Tolerance, but the location of the remaining vertices stay the same. Set the Generalization Tolerance (>=0) to 10. You can play around with this, the lower the number the more of the shape is preserved, the higher the number the less of the shape is preserved. Since most of the buildings are square, a generalization tolerance of 10 will work, only a couple buildings in the upper right corner will lose their shape.

Comparison of different Generalization Tolerances using the Douglas Algorithm in the Generalizer transformer.
 

7. Stylize the Building Outlines

Add a DWGStyler transformer, set the color to Red. Then enable Areas, and set the Area Entity Type to MPolygon With Fill Pattern. For the Fill Pattern Name, select the wide brick pattern, AR-B816.

 

8. Write to DWG

Add an AutoDesk AutoCAD DWG/DXF Writer to the canvas. Name the Dataset OSM.dwg, then in the parameters, change the Layer Name to Buildings. Run the translation, view the output OSM.dwg file in AutoCAD to see the area fill pattern.

Styled OSM.dwg output in AutoDesk AutoCAD

 

Data Attribution

The data used in this article is from an open data license provided by © OpenStreetMap contributors

Was this article helpful?

Comments

0 comments

Please sign in to leave a comment.