orthogonalize
Description
Squares up a Polygon or MultiPolygon, nudging its vertices so that corners close to right angles, 45° diagonals, or flat lines land on them. This is a common cleanup step for building footprints traced from imagery.
Based on Lokhat and Touya, "Enhancing building footprints with squaring operations", Journal of Spatial Information Science 13 (2016), pages 33 to 60. DOI https://doi.org/10.5311/JOSIS.2016.13.276 PDF https://hal.science/hal-02147792/document
Each kind of corner has its own tolerance. Right angles matter most, so theirs is the widest, with a 15° default drawn from cartographic practice. Flat corners get the tightest, so that a curved wall drawn as a run of short segments keeps its curve. Diagonals at 45° and 135° are less common, so theirs sits in between.
Each ring is squared independently, so holes and adjacent polygons that share a wall can drift apart. An edge far shorter than the rest is merged away. A ring stays unchanged if squaring would fold it, cross its edges, collapse a wall, push it past a pole, or fail to settle. A polygon stays unchanged if squaring would make two of its rings newly cross, or move one into or out of another. A MultiPolygon stays unchanged if squaring would do the same to its parts. Each Polygon or MultiPolygon in a GeometryCollection is squared on its own, so members can end up overlapping. Running it again on its output can move a ring further, since the first run can bring new corners into tolerance.
Longitudes are read as plain numbers, so a ring crossing the antimeridian should run past ±180° or be cut into a MultiPolygon. Each part is squared on its own, so its cut edge can drift off ±180°.
Squaring operates in the planar coordinates of an equirectangular projection of a spherical Earth, with a separate projection centered on each ring. For rings up to 1 km across at latitudes up to 89°, the projection adds at most about 0.3° of error to each squared corner's angle. A ring that surrounds a pole is not squared and is returned as it is.
Parameters
| Name | Type | Description |
|---|---|---|
| geojson | FeatureCollection | Feature<Polygon | MultiPolygon | GeometryCollection<Polygon | MultiPolygon>> | Polygon | MultiPolygon | GeometryCollection<Polygon | MultiPolygon> | polygons to square up. |
| options? | Object | Optional parameters (default {}) |
| options.toleranceDegrees? | number | corners farther than this from 90° aren't pulled toward it, between 0° and 45° (default 15) |
| options.flatToleranceDegrees? | number | corners farther than this from 180° aren't pulled toward it, between 0° and 90° (default 5) |
| options.diagonalToleranceDegrees? | number | corners farther than this from 45° or 135° aren't pulled toward them, between 0° and 22.5° (default 8) |
Returns
FeatureCollection | Feature<Polygon | MultiPolygon | GeometryCollection<Polygon | MultiPolygon>> | Polygon | MultiPolygon | GeometryCollection<Polygon | MultiPolygon> a squared copy of the input
Examples
const polygon = turf.polygon([
[
[0, 0],
[0.001, 0.00005],
[0.00095, 0.001],
[-0.00005, 0.00095],
[0, 0],
],
]);
const squared = turf.orthogonalize(polygon);
Installation
$ npm install @turf/orthogonalize
import { orthogonalize } from "@turf/orthogonalize";
const result = orthogonalize(...);
$ npm install @turf/turf
import * as turf from "@turf/turf";
const result = turf.orthogonalize(...);