Example: Simplify Lines with a Transform Expression

In this example we use an expression written in the Expression template in the Transform pane to simplify lines in a drawing so that longer lines are defined with fewer coordinates.  We will also use the Edit Query button to show how different queries are created automatically depending on if we want to update a field or to add a new component.

 

 

We being with a map with one layer, a drawing showing highways in Mexico as lines.  We have used the Layers pane to specify a calm beige color for the background.  We zoom in to one of the highways so we can see the many changes in direction of the line.

 

 

We will use an expression to simplify the line, so that it has fewer coordinates defining it and thus fewer changes in direction. One reason we might want to do that could be to create a publication that shows highways in simpler form for greater legibility.

 

With the focus on the map window, in the Transform pane we choose the Mexico Roads layer and the Geom field.  We then double-click the Expression template to launch it.

 

 

In the Expression template, we press the Edit Expression button to launch the expression builder dialog.

 

 

In the blank, upper, pane we enter the following expression:

 

GeomSmooth([Geom],

   GeomLength([Geom], 0)/100)

 

White space is not significant in expressions so we may format what we enter with indents and use of new lines to increase legibility.   The expression takes 1% of the length of the line, as calculated by GeomLength([Geom], 0)/100, and uses that as an argument for the GeomSmooth function that smooths the line.   The result is that we smooth lines to 1% of their lengths.  As a result longer lines will be smoothed more to lose more detail.

 

Press OK to return to the Transform pane.

 

 

Back in the Transform pane, in the Result destination box we choose New Field to save the result into a new geometry field in the same table, and we enter Geom_smoothed as the name of the new geometry field.   The template automatically creates a drawing to visualize the new field.   We enter the name Simplified Roads for the name of the New drawing.    

 

We can use whatever names we want for the new field and the new drawing, but it is wise to use names that indicate what the new field and drawing are.

 

To see a preview of what the expression will do, press the Preview button.

 

 

The preview is drawn in blue preview colors above all other layers in the map.   A blue preview caption bar appears at the top of the map window, giving the name of the template being previewed.  We can right-click the caption bar for a context menu of commands to control the preview display.  

 

 

If we would like to see just the preview, without the Mexico Roads layer, we can double-click the tab for the Mexico Roads layer to turn that layer off.

 

Press Transform to apply the transform.

 

A new geometry field called Geom_smooth appears in the table for the Mexico Roads drawing, and a new drawing called Simplified Roads appears in the Project pane.

 

 

We drag and drop the new Simplified Roads drawing into the map, using the Style pane to color it a light blue with a touch of dark blue outline/halo.  Turning off the Mexico Roads layer, we can see the line in the new drawing has been simplified.

 

 

Turning the Mexico Roads layer back on, and using Style to increase the thickness of the Mexico Roads lines, we can see how the Simplified Roads line is defined using fewer coordinates (also known as vertices).  

 

We can increase the effect by dividing by 50 in the expression instead of by 100.  

 

In the Transform pane, we press the Edit Expression button again to open the expression in the expression builder dialog.

 

We alter the expression to read:

 

GeomSmooth([Geom],

   GeomLength([Geom], 0)/50)

 

That will smooth lines to 2% of their lengths.   Press OK to get back to the Transform pane.

 

 

In the Transform pane, from the pull down menu for the Result destination box we choose the Geom_smoothed field that we added to the table in the previous step.   That will write the results of the transform operation into the Geom_smoothed field, which will cause the Simplified Roads drawing to automatically update to use the new geometry.

 

Press Preview to see a preview.  We are not required to do a preview before applying a transform, but previews are fun and can help avoid errors before we make them.

 

 

We can see from the preview that the line that will be created is even more simplified.

 

Press Transform.

 

 

The Simplified Roads layer immediately updates to show the new geometry.  We can see that the line has been simplified even more.

 

 

Turning on the Mexico Roads layer, we can see that even fewer vertices are now used to draw the simplified lines.

 

 

 

 

Comparing the original line with the effects of 1% simplification (dividing by 100 in the expression) and 2% simplification (dividing by 50 in the expression) we can more easily see the reduction in detail caused by the GeomSmooth function.

 

 

We can see what query would be generated to implement this expression and to update the table with the results by pressing the Edit Query button.  

 

 

Doing so opens a Command Window that is populated with an automatically generated SQL query that is the equivalent of the expression we wrote in the Transform pane.  We can now modify the query or save it for later use by choosing Edit - Save as Query.    

 

The query text is:

 

-- $manifold$

--

-- Auto-generated

--

-- Expression

--   Layer: Mexico Roads

--   Field: Geom

--   Expression: GeomSmooth([Geom],GeomLength([Geom],0)/50)

--   Result: Geom_smoothed

--   Resources: all CPU cores, all GPU cores

--   Transform selection only: FALSE

--

 

UPDATE (

  SELECT [mfd_id], [Geom_smoothed],

    (GeomSmooth([Geom],GeomLength([Geom],0)/50)) AS [Geom_smoothed New]

  FROM [Mexico Roads Table] THREADS SystemCpuCount()

) SET [Geom_smoothed] = [Geom_smoothed New];

 

 

The first part of the query is a series of comments lines that give the parameters entered into the Transform pane.  The THREADS statement is a housekeeping statement saying to use all CPU cores in parallel.   Without the housekeeping statements the query is a simple UPDATE query because it operates on the original data in the original table:  

 

UPDATE (

  SELECT [mfd_id], [Geom_smoothed],

    (GeomSmooth([Geom],GeomLength([Geom],0)/50)) AS [Geom_smoothed New]

  FROM [Mexico Roads Table]

) SET [Geom_smoothed] = [Geom_smoothed New];

 

The Transform pane also gives us the option to save changes into a new component instead of updating a field in the original table:

 

 

In the pull down menu for the Result destination, we choose New Table.

 

 

When we press the Edit Query button the query generated by the template will be checked for errors.  If it tries to create a new drawing using a name that already in use in the project, that will generate an error, so for the name of the New drawing we use a new name, Simple.

 

Writing results to a new drawing and new table that the query generates will, of course, also change the query that will be generated when we press the Edit Query button.

 

Press Edit Query.

 

A Command Window launches again with the new query:

 

 

This is a longer query, in three parts.   The query text:

 

-- $manifold$

--

-- Auto-generated

--

-- Expression

--   Layer: Mexico Roads

--   Field: Geom

--   Expression: GeomSmooth([Geom],GeomLength([Geom],0)/50)

--   Result: (new table)

--   Result type: geom

--   New drawing: Simple

--   New table: Simple Table

--   Resources: all CPU cores, all GPU cores

--   Transform selection only: FALSE

--

 

-- prepare begin

 

CREATE TABLE [Simple Table] (

  [mfd_id] INT64,

  [Geom] GEOM,

  INDEX [mfd_id_x] BTREE ([mfd_id]),

  INDEX [Geom_x] RTREE ([Geom]),

  PROPERTY 'FieldCoordSystem.Geom' ComponentFieldCoordSystem([Mexico Roads Table], 'Geom')

);

CREATE DRAWING [Simple] (

  PROPERTY 'Table' '[Simple Table]',

  PROPERTY 'FieldGeom' 'Geom'

);

 

-- prepare end

 

DELETE FROM [Simple Table];

INSERT INTO [Simple Table] (

  [Geom]

) SELECT

  (GeomSmooth([Geom],GeomLength([Geom],0)/50))

FROM [Mexico Roads Table] THREADS SystemCpuCount();

 

This query is considerably longer than the previous one because instead of being a simple UPDATE query it must create a table and a drawing with all necessary infrastructure, such as the coordinate system to use for the geom, correctly specified, and then insert into the new table the results of the expression.

 

The query is in three parts:  First, a series of comment lines that report the parameters used in the Transform pane for the query.   Next, the section betwee -- prepare begin and -- prepare end comments create a new table and drawing.  Finally, an INSERT INTO statement populates the new table.

Notes

Is that smooth or simplify? - Manifold terminology is inconsistent when using the terms smooth and simplify.  The Reshape : simplify transform operator uses the term simplify since that better describes the visual effect of reducing the number of coordinates (vertices) that define a line.  However, the function which does the work, GeomSmooth, uses the smooth word for the exact same thing.   

 

The function came first, and was named using smooth based on the idea that a straight line is "smoother" than a line with many kinks in it, so reducing the number of coordinates, and thus eliminating many kinks, is in some sense a "smoothing" operation.  The Transform operator uses the simplify word because a line with many coordinates, such as the border of a country, can appear to be more smoothly curved than a simplified line that has fewer coordinates and thus appears kinked.  The kinked line can be said to be "simpler," but visually it does not appear to be "smoother."  

 

In a perfect world, the function would be renamed to be GeomSimplify, but since it is already in use the legacy name remains.

 

See Also

Transform Pane

 

Command Window

 

Queries

 

Example: Two Drawings from the Same Table - Take a table with a geom field that is visualized by a drawing.  Add a second geom field to the table and create an rtree index on that field so it can be visualized by a drawing.   Copy the first drawing, paste it and adjust the pasted copy so it uses the second geom field. Demonstrate how to use the Transform pane to show "live" modifications in the second drawing compared to the first drawing.

 

Example: Copy one Column into Another Column with Transform - How to use the  Transform pane to copy the contents of one column in a table into another column, but only for selected records.  Uses the Products table from the Nwind example data set.  

 

Example: Transform Field Values using an Expression in the Transform Pane - How the Expression template in the Transform pane may be used to change the values of fields.  

 

Example: Edit a Drawing with Transform Templates - In this example we open a drawing and edit objects in the drawing using Transform pane templates.  Includes examples of saving results to a new component and also the Edit Query button.

 

Example: Use a Transform Expression to Create Buffers in a Drawing - Use the Expression template in the Transform pane to create three different sizes of buffers for different lines in a drawing and then automatically create a query which does the same thing.  Includes examples of saving results to a new component and also the Edit Query button.

 

Example: Clip Areas with a Transform Expression - Use the Expression template in the Transform pane to clip areas in a drawing to fit within horizontal bounds.   Includes examples of saving results to a new component and also the Edit Query button.

 

Example: Transform Templates, Expressions and Queries - We learn to use a function by clicking on a template in the Transform pane , seeing what it does in a preview, looking at the query Manifold creates and then trying out the function in the Expression tab.