Connect individual lines whose end points match within tolerance


gmt connect [ table ] [ -C[closed] ] [ -D[template] ] [ -L[linkfile] ] [ -Q[template] ] [ -T[cutoff[+ssdist]] ] [ -V[level] ] [ -aflags ] [ -bbinary ] [ -dnodata[+ccol] ] [ -eregexp ] [ -fflags ] [ -ggaps ] [ -hheaders ] [ -iflags ] [ -oflags ] [ -qflags ] [ -sflags ] [ -:[i|o] ] [ --PAR=value ]

Note: No space is allowed between the option flag and the associated arguments.


connect reads standard input or one or more data files, which may be multisegment files, and examines the coordinates of the end points of all line segments. If a pair of end points are identical or closer to each other than the specified separation cutoff then the two line segments are joined into a single segment. The process repeats until all the remaining endpoints no longer pass the tolerance test; the resulting segments are then written out to standard output or specified output file. If it is not clear what the separation tolerance should be then use -L to get a list of all separation distances and analyze them to determine a suitable cutoff.

Required Arguments


One or more ASCII (or binary, see -bi[ncols][type]) data table file(s) holding a number of data columns. If no tables are given then we read from standard input.

Optional Arguments


Write all the already-closed polygons to file closed [gmtconnect_closed.txt] and all open segments to standard output. No connection will take place. Use -Tcutoff to set a minimum separation [0], and then any existing polygon whose first and last point are separated by less that cutoff will be considered to be closed. Note that if cutoff is > 0 then we explicitly close all polygons on output.


For multiple segment data, dump each segment to a separate output file [Default writes a single multiple segment file]. Append a format template for the individual file names; this template must contain a C format specifier that can format an integer argument (the segment number); this is usually %d but could be %08d for leading zeros, etc. Optionally, it may also contain the format %c before the integer; this will then be replaced by C (closed) or O (open) to indicate segment type. [Default is gmtconnect_segment_%d.txt]. Note that segment headers will be written in either case. For composite segments, a generic segment header will be written and the segment headers of individual pieces will be written out as comments to make it possible to identify where the connected pieces came from.


Writes link information to the specified file [gmtconnect_link.txt]. For each segment we write the original segment id, and for the beginning and end point of the segment we report the id of the nearest segment, whether it is the beginning (B) or end (E) point that is closest, and the distance between those points in units determined by -T.


Used with -D to write a list file with the names of the individual output files. Optionally, append a filename template for the individual file names; this template may contain a C format specifier that can format an character (C or O for closed or open, respectively). [Default is gmtconnect_list.txt].


Specifies the separation tolerance in the data coordinate units [0]; for geographic data append distance unit (see Units). If two lines have end-points that are closer than cutoff they will be joined. Optionally, append +ssdist which adds the extra requirement that a link will only be made if the second closest connection exceeds the sdist distance. The latter distance must be given in the same units as cutoff. However, if -T is no arguments then we close every line segment or polygon regardless of the gap distance between the first and last point in the segment.


Select verbosity level [w]. (See full description) (See cookbook information).

-a[[col=]name[,]] (more …)

Set aspatial column associations col=name.

-birecord[+b|l] (more …)

Select native binary format for primary table input. [Default is 2 input columns].

-borecord[+b|l] (more …)

Select native binary format for table output. [Default is same as input].

-d[i|o][+ccol]nodata (more …)

Replace input columns that equal nodata with NaN and do the reverse on output.

-e[~]“pattern” | -e[~]/regexp/[i] (more …)

Only accept data records that match the given pattern.

-f[i|o]colinfo (more …)

Specify data types of input and/or output columns.

-gx|y|z|d|X|Y|Dgap[u][+a][+ccol][+n|p] (more …)

Determine data gaps and line breaks.

-h[i|o][n][+c][+d][+msegheader][+rremark][+ttitle] (more …)

Skip or produce header record(s).

-icols[+l][+ddivisor][+sscale|d|k][+ooffset][,][,t[word]] (more …)

Select input columns and transformations (0 is first column, t is trailing text, append word to read one word only).

-ocols[,…][,t[word]] (more …)

Select output columns (0 is first column; t is trailing text, append word to write one word only).

-q[i|o][~]rows|limits[+ccol][+a|t|s] (more …)

Select input or output rows or data limit(s) [all].

-s[cols][+a][+r] (more …)

Set handling of NaN records for output.

-:[i|o] (more …)

Swap 1st and 2nd column on input and/or output.

-^ or just -

Print a short message about the syntax of the command, then exit (Note: on Windows just use -).

-+ or just +

Print an extensive usage (help) message, including the explanation of any module-specific option (but not the GMT common options), then exit.

-? or no arguments

Print a complete usage (help) message, including the explanation of all options, then exit.


Temporarily override a GMT default setting; repeatable. See gmt.conf for parameters.


For map distance unit, append unit d for arc degree, m for arc minute, and s for arc second, or e for meter [Default unless stated otherwise], f for foot, k for km, M for statute mile, n for nautical mile, and u for US survey foot. By default we compute such distances using a spherical approximation with great circles (-jg) using the authalic radius (see PROJ_MEAN_RADIUS). You can use -jf to perform “Flat Earth” calculations (quicker but less accurate) or -je to perform exact geodesic calculations (slower but more accurate; see PROJ_GEODESIC for method used).

ASCII Format Precision

The ASCII output formats of numerical data are controlled by parameters in your gmt.conf file. Longitude and latitude are formatted according to FORMAT_GEO_OUT, absolute time is under the control of FORMAT_DATE_OUT and FORMAT_CLOCK_OUT, whereas general floating point values are formatted according to FORMAT_FLOAT_OUT. Be aware that the format in effect can lead to loss of precision in ASCII output, which can lead to various problems downstream. If you find the output is not written with enough precision, consider switching to binary output (-bo if available) or specify more decimals using the FORMAT_FLOAT_OUT setting.


Note: Below are some examples of valid syntax for this module. The examples that use remote files (file names starting with @) can be cut and pasted into your terminal for testing. Other commands requiring input files are just dummy examples of the types of uses that are common but cannot be run verbatim as written.

To combine the digitized segment lines segment_*.txt (whose Cartesian coordinates are in cm) into as few complete lines as possible, assuming the end points’ digitization error could be up to 0.1 mm, run:

gmt connect segment_*.txt -T0.1 > new_segments.txt

To combine the digitized segments in the multisegment file my_lines.txt (whose coordinates are in lon,lat) into as few complete lines as possible, assuming the end points digitization error could be up to 150 m, and write the complete segments to individual files called Map_segment_0001.txt, Map_segment_0002.txt, etc., run:

gmt connect my_lines.txt -T150e -DMap_segment_%04d.txt


The line connection does not work if a line only has a single point. However, connect will correctly add the point to the nearest segment. Running connect again on the new set of lines will eventually connect all lines that satisfy the criteria given in -T.

See Also

gmt, gmt.conf, gmtsimplify, gmtspatial, mapproject