A friend recently asked me to help him generate polyline shapefiles with *Z* and *M* values that he could deliver to a customer. The problem was, the software he was using supported the import of *Z* and *M* values, but did not support the export of those files. The other problem was, he has zillions of data tables that he needed to export!

Fortunately, PostGIS allows us to create Polyline ZM data rather easily. The next part was to figure out how to get it exported to a shapefile. So, here goes:

Assume you have a table with 4 columns: lineID, Z, M, and geometry. The geometry field is a point feature that represent vertices on a line. The lineID separates all those points by which line they are part of, and the Z and M values are, well, Z and M.

Make the Polyline ZM

The SQL command to create a 4D line is:

SELECTlineid,ST_MakeLine(ST_MakePoint(ST_X(geometry),ST_Y(geometry), z, m) )ASgFROMzptsGROUP BYlineid;

In this case, I am making a line (ST_MakeLine) as a series of X (ST_X), Y (ST_Y), Z, and M values. Grouping the results of the query by the lineid allows me to create a line for each set of points. Continue reading