gmtspatial

Geospatial operations on points, lines and polygons

Synopsis

gmt spatial [ table ] [ -A[amin_dist][unit]] [ -C ] [ -D[+aamax][+c|Ccmax][+ddmax][+ffile][+p][+sfactor] ] [ -E+p|n ] [ -F[l] ] [ -I[e|i] ] [ -Ldist/noise/offset ] [ -Npfile[+a][+i][+p[start]][+r][+z] ] [ -Q[unit][+cmin[/max]][+h][+l][+p][+s[a|d]] ] [ -Rregion ] [ -Sbwidth|h|s ] [ -T[clippolygon] ] [ -V[level] ] [ -Wdist[unit][+f|l] ] [ -aflags ] [ -bbinary ] [ -dnodata[+ccol] ] [ -eregexp ] [ -fflags ] [ -ggaps ] [ -hheaders ] [ -iflags ] [ -jflags ] [ -oflags ] [ -qflags ] [ -sflags ] [ -:[i|o] ] [ --PAR=value ]

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

Description

spatial reads one or more data files (which may be multisegment files) that contains closed polygons and operates of these polygons in the specified way. Operations include area calculation, handedness reversals, and polygon intersections.

Required Arguments

table

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

-A[amin_dist][unit]

Perform spatial nearest neighbor (NN) analysis: Determine the nearest neighbor of each point and report the NN distances and the point IDs involved in each pair (IDs are the input record numbers starting at 0). Use -Aa to decimate a data set so that no NN distance is lower than the threshold min_dist. In this case we write out the (possibly averaged) coordinates and the updated NN distances and point IDs. A negative point number means the original point was replaced by a weighted average (the absolute ID value gives the ID of the first original point ID to be included in the average.). Note: The input data are assumed to contain (lon, lat) or (x, y), optionally followed by a z and a weight [1] column. We compute a weighted average of the location and z (if weight is present).

-C

Clips polygons to the map region, including map boundary to the polygon as needed. The result is a closed polygon (see -T for truncation instead). Requires -R.

-D[+c|Ccmax][+ddmax][+ffile][+p][+sfact]

Check for duplicates among the input lines (or polygons). We consider both the cases of exact (same number and coordinates) and approximate matches (average distance between nearest points of two features is less than a threshold). We also consider that some features may have been reversed. By default, we compute the mean line separation.

  • +c - Set threshold of a pair’s closeness (defined as the average distance between the features divided by their average length) [0.01].

  • +C - Use +Ccmin to instead compute the median line separation and therefore a robust closeness value.

  • +d - Features are considered approximate matches if their minimum distance is less than dmax [0] (see Units) and their closeness (+c) is less than cmax.

  • +f - Check if the input features already exist among the features in file.

  • +p - Limit the comparison to points that project perpendicularly to points on the other line (and not its extension) [Default considers all distances between points on one line and another.

For each duplicate found, the output record begins with the single letter Y (exact match) or ~ (approximate match). If the two matching segments differ in length by more than a factor of 2 then we consider the duplicate to be either a subset (-) or a superset (+) and are flagged accordingly. Finally, we also note if two lines are the result of splitting a continuous line across the Dateline (|).

-E+p|n

Reset the handedness of all polygons to match the given +p (counter-clockwise; positive) or +n (clockwise; negative).

-F[l]

Force input data to become polygons on output, i.e., close them explicitly if not already closed. Optionally, append l to force line geometry.

-I[e|i]

Determine the intersection locations between all pairs of polygons. Append i to only compute internal (i.e., self-intersecting polygons) crossovers or e to only compute external (i.e., between pairs of polygons) crossovers [Default is both]. Output records will list the coordinates of the crossing, the relative times along the two segments (i.e., floating point record numbers at the crossing), and the names of the two segments (as trailing text).

-Ldist/noise/offset

Remove tile Lines. These are superfluous lines that were digitized with a polygon but that all fall along the rectangular -R border and should be removed. Append dist (in m) [0], coordinate noise [1e-10], and max offset from gridlines [1e-10].

-Npfile[+a][+i][+p[start]][+r][+z]

Lines and polygons: Determine if one (or all) points of each feature in the input data are inside any of the polygons given in the pfile. If inside, then report which polygon it is. The polygon ID is taken from the aspatial value assigned to Z or the segment header (first -Z, then -L are scanned). By default the input segments that are found to be inside a polygon are written to standard output with the polygon ID encoded in the segment header as -ZID. Modifiers can be used to adjust the process:

  • +a - All the points of a feature must be inside the polygon.

  • +i - Point clouds, determine the polygon ID for every individual input point and add it as the last output column.

  • +p - Instead of segment headers, assign a running ID number that is initialized to begin from start [0].

  • +r - Just report which polygon contains a feature.

  • +z - Add the IDs as an extra data column on output.

Segments that fail to be inside a polygon are not written out. If more than one polygon contains the same segment we skip the second (and further) scenarios.

-Q[unit][+cmin[/max]][+h][+l][+p][+s[a|d]]

Measure the area of all polygons or length of all line segments. For polygons we also compute the centroid location while for lines we compute the mid-point (half-length) position. For geographical data, optionally append a distance unit to select the unit used (see Units) [k]. Note that the area will depend on the current setting of PROJ_ELLIPSOID; this should be a recent ellipsoid to get accurate results. The centroid is computed using the mean of the 3-D Cartesian vectors making up the polygon vertices, while the area is obtained via a sum of areas for spherical triangles. Normally, all input segments will be be reflected on output. By default, we consider open polygons as lines and closed polygons as polygons. Use modifiers to change the above behavior:

  • +c - Restrict processing to those features whose length (or area for polygons) fall inside the specified range set by min and max. If max is not set it defaults to infinity.

  • +h - Append the area to each polygonʻs segment header [Default simply writes the area to standard output].

  • +l - Consider all input features as lines, even if closed.

  • +p - Close open polygons and thus consider all input as polygons.

  • +s - Sort the segments based on their lengths or area. Append a for ascending [Default] and d for descending order.

-Rwest/east/south/north[/zmin/zmax][+r][+uunit]

Specify the region of interest. Clips polygons to the map region, including map boundary to the polygon as needed. The result is a closed polygon.

The region may be specified in one of several ways:

  1. -Rwest/east/south/north. This is the standard way to specify geographic regions when using map projections where meridians and parallels are rectilinear. The coordinates may be specified in decimal degrees or in [±]dd:mm[:ss.xxx][W|E|S|N] format.

  2. -Rwest/south/east/north+r. This form is useful for map projections that are oblique, making meridians and parallels poor choices for map boundaries. Here, we instead specify the lower left corner and upper right corner geographic coordinates, followed by the modifier +r. This form guarantees a rectangular map even though lines of equal longitude and latitude are not straight lines.

  3. -Rg or -Rd. These forms can be used to quickly specify the global domain (0/360 for -Rg and -180/+180 for -Rd in longitude, with -90/+90 in latitude).

  4. -Rcode1,code2,…[+e|r|Rincs]. This indirectly supplies the region by consulting the DCW (Digital Chart of the World) database and derives the bounding regions for one or more countries given by the codes. Simply append one or more comma-separated countries using either the two-character ISO 3166-1 alpha-2 convention (e.g., NO) or the full country name (e.g., Norway). To select a state within a country (if available), append .state (e.g, US.TX), or the full state name (e.g., Texas). To specify a whole continent, spell out the full continent name (e.g., -RAfrica). Finally, append any DCW collection abbreviations or full names for the extent of the collection or named region. All names are case-insensitive. The following modifiers can be appended:

    • +r to adjust the region boundaries to be multiples of the steps indicated by inc, xinc/yinc, or winc/einc/sinc/ninc [default is no adjustment]. For example, -RFR+r1 will select the national bounding box of France rounded to nearest integer degree, where inc can be positive to expand the region or negative to shrink the region.

    • +R to adjust the region by adding the amounts specified by inc, xinc/yinc, or winc/einc/sinc/ninc [default is no extension], where inc can be positive to expand the region or negative to shrink the region.

    • +e to adjust the region boundaries to be multiples of the steps indicated by inc, xinc/yinc, or winc/einc/sinc/ninc, while ensuring that the bounding box is adjusted by at least 0.25 times the increment [default is no adjustment], where inc can be positive to expand the region or negative to shrink the region.

  5. -Rxmin/xmax/ymin/ymax[+uunit] specifies a region in projected units (e.g., UTM meters) where xmin/xmax/ymin/ymax are Cartesian projected coordinates compatible with the chosen projection (-J) and unit is an allowable distance unit [e]; we inversely project to determine the actual rectangular geographic region. For projected regions centered on (0,0) you may use the short-hand -Rhalfwidth[/halfheight]+uunit, where halfheight defaults to halfwidth if not given. This short-hand requires the +u modifier.

  6. -Rjustifylon0/lat0/nx/ny, where justify is a 2-character combination of L|C|R (for left, center, or right) and T|M|B (for top, middle, or bottom) (e.g., BL for lower left). The two character code justify indicates which point on a rectangular region region the lon0/lat0 coordinates refer to and the grid dimensions nx and ny are used with grid spacings given via -I to create the corresponding region. This method can be used when creating grids. For example, -RCM25/25/50/50 specifies a 50x50 grid centered on 25,25.

  7. -Rgridfile. This will copy the domain settings found for the grid in specified file. Note that depending on the nature of the calling module, this mechanism will also set grid spacing and possibly the grid registration (see Grid registration: The -r option).

  8. -Ra[uto] or -Re[xact]. Under modern mode, and for plotting modules only, you can automatically determine the region from the data used. You can either get the exact area using -Re [Default if no -R is given] or a slightly larger area sensibly rounded outwards to the next multiple of increments that depend on the data range using -Ra.

-Sbwidth|h|s

Spatial processing of polygons. Choose from several directives:

  • b - Append width which computes a buffer polygon around lines.

  • h - Identifies perimeter and hole polygons (and flags/reverses them).

  • s - Split polygons that straddle the Dateline.

Note: -Sb is a purely Cartesian operation so width must be in data units. That is, for geographical coordinates width must be provided in degrees or, preferably, project data into an equal-area projection with mapproject, compute the buffer and then convert back to geographical.

-T[clippolygon]

Truncate polygons and lines against the specified polygon given, possibly resulting in open polygons. If no argument is given to -T we create a clipping polygon from -R which then is required. Note that when the -R clipping is in effect we will also look for polygons of length 4 or 5 that exactly match the -R clipping polygon.

-V[level]

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

-Wdist[unit][+f|l]

Extend all segments with a new first and last point such that these points are dist away from their neighbor point in the direction implied by the two points at each end of the segment. For geographic data you may append a unit (see Units). To give separate distances for the two ends, give distf[unit]/distl[unit] instead. Optionally, append either +f or +l to only extend the first or last point this way [both]. The mode of geographical calculations depends on -j.

-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).

-je|f|g (more …)

Determine how spherical distances or coordinate transformations are calculated.

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

Select output columns and transformations (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.

--PAR=value

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

Units

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).

Inside/outside Status

To determine if a point is inside, outside, or exactly on the boundary of a polygon we need to balance the complexity (and execution time) of the algorithm with the type of data and shape of the polygons. For any Cartesian data we use a non-zero winding algorithm, which is quite fast. For geographic data we will also use this algorithm as long as (1) the polygons do not include a geographic pole, and (2) the longitude extent of the polygons is less than 360. If this is the situation we also carefully adjust the test point longitude for any 360 degree offsets, if appropriate. Otherwise, we employ a full spherical ray-shooting method to determine a points status.

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.

Examples

To determine the centroid of the remote GSHHH high-resolution polygon for Australia, as well as the land area in km squared, try:

gmt spatial @GSHHS_h_Australia.txt -fg -Qk

To turn all lines in the multisegment file lines.txt into closed polygons, run:

gmt spatial lines.txt -F > polygons.txt

To append the polygon ID of every individual point in cloud.txt that is inside the polygons in the file poly.txt and write that ID as the last column per output row, run:

gmt spatial cloud.txt -Npoly.txt+i  > cloud_IDs.txt

To compute the area of all geographic polygons in the multisegment file polygons.txt, run:

gmt spatial polygons.txt -Q > areas.txt

Same data, but now orient all polygons to go counter-clockwise and write their areas to the segment headers, run:

gmt spatial polygons.txt -Q+h -E+p > areas.txt

To determine the areas of all the polygon segments in the file janmayen_land_full.txt, add this information to the segment headers, sort the segments from largest to smallest in area but only keep polygons with area larger than 1000 sq. meters, run:

gmt spatial -Qe+h+p+c1000+sd -V janmayen_land_full.txt > largest_pols.txt

To determine the intersections between the polygons A.txt and B.txt, run:

gmt spatial A.txt B.txt -Ie > crossovers.txt

To truncate polygons A.txt against polygon B.txt, resulting in an open line segment, run:

gmt spatial A.txt -TB.txt > line.txt

If you want to plot a polygon with holes (donut polygon) from a multiple segment file which contains both perimeters and holes, it could be necessary first to reorganize the file so it can plotted with plot. To do this, run:

gmt spatial file.txt -Sh > organized_file.txt

Notes

OGR/GMT files are considered complete datasets and thus you cannot specify more than one at a given time. This causes problems if you want to examine the intersections of two OGR/GMT files. The solution is to convert them to regular datasets via gmtconvert and then run gmt spatial on the converted files.

See Also

gmt, gmtconvert, gmtselect, gmtsimplify, mapproject