# dufour-peyton-intersection

> Reference Implementation of the Dufour-Peyton Intersection Algorithm. Calculates the Intersections of Arbitrary Polygons with a Geospatial Raster.

Latest version **0.2.2** (published 2025-03-27) · MIT license · 0 weekly downloads

## Install

```sh
npm install dufour-peyton-intersection
pnpm add dufour-peyton-intersection
yarn add dufour-peyton-intersection
bun add dufour-peyton-intersection
```

## Health

**Score 25/100 (F)** — status: maintenance-mode.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.2 |
| Published | 2025-03-27 |
| First published | 2022-02-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 77.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Author | Daniel J. Dufour & Stephen Peyton |
| Maintainers | danieljdufour |
| Keywords | geojson, georaster, geospatial, geotiff, gis, intersection, polygon, raster |

## Links

- npm: https://www.npmjs.com/package/dufour-peyton-intersection
- Repository: https://github.com/GeoTIFF/dufour-peyton-intersection
- Homepage: https://github.com/GeoTIFF/dufour-peyton-intersection#readme
- Issues: https://github.com/GeoTIFF/dufour-peyton-intersection/issues
- npm.io page: https://npm.io/package/dufour-peyton-intersection

## Dependencies (1)

- [get-depth](https://npm.io/package/get-depth.md) ^0.0.3

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 0.2.2 (latest) — 2025-03-27
- 0.2.0-0 (next) — 2023-06-20
- 0.2.1 — 2024-01-26
- 0.2.0 — 2023-07-25
- 0.1.3 — 2023-05-31
- 0.1.2 — 2022-04-19
- 0.1.1 — 2022-04-17
- 0.1.0 — 2022-04-17
- 0.0.2 — 2022-04-16
- 0.0.1 — 2022-02-19

## README

# dufour-peyton-intersection
Reference Implementation of the Dufour-Peyton Intersection Algorithm.  Calculates the Intersections of Arbitrary Polygons with a Geospatial Raster.  Originally developed for [geoblaze](geoblaze.io).

## features
- supports very large rasters because speed determined by number of geometry vertices
- avoids double counting pixels in overlapping polygons
- supports multi-polygons
- supports overlapping holes

## install
```bash
npm install dufour-peyton-intersection
```

## usage
```js
import dufour_peyton_intersection from "dufour-peyton-intersection";

const result = dufour_peyton_intersection.calculate({
  // bounding box of raster in format [xmin, ymin, xmax, ymax]
  raster_bbox: [ 69.15892987765864, 1.4638624159537426, 90.43900703997244, 11.81870408668788],

  // height of the raster in pixels
  raster_height: 472,

  // width of the raster in pixels
  raster_width: 970,

  // height of each pixel in the spatial reference system
  // in the example below, height is in degrees
  pixel_height: 0.02193822387867402,

  // width of each pixel in the spatial reference system
  // in the example below, width is in degrees
  pixel_width: 0.02193822387867402,

  // a GeoJSON
  // currently, this algorithm only support the following geometry types: Polygon and MultiPolygon
  geometry: geojson,

  // callback function run on each horizontal strip of consecutive intersecting pixels
  per_row_segment: ({ row, columns }) => {
    console.log("row index is (starting from zero):", row);
    const [start, end] = columns;
    console.log(`columns range is inclusive, starting at column ${start} and ending at column ${end}`);
  },

  // callback function run on each raster pixel that intersects the geometry
  per_pixel: ({ row, column }) => {
    console.log("we found a raster pixel that intersects the geometry at");
    console.log("row (from top to bottom): " + row);
    console.log("column (from left to right): " + column)
  }
});
```
calculate returns the following object:
```js
{
  rows: [
    <91 empty items>, // empty rows mean that the geometry does not intersect these raster rows 
    [ [ 500, 504 ] ], // 5 pixels (500 to 504) in row 92 (zero-index) intersect the geometry
    [ [ 491, 505 ] ],
    [ [ 490, 499 ], [ 501, 505 ] ], // two parts of the geometry intersect this row and are separated by 1 pixel at index 500
    [ [ 487, 506 ] ],
    ... 380 more items
  ]
}
```

## links
- https://medium.com/@DanielJDufour/calculating-intersection-of-polygon-with-a-raster-89c2624d78a2

## used by
- [geoblaze](https://geoblaze.io/): blazing fast raster statistics engine
- [geomask](https://github.com/danieljdufour/geomask): low-level geospatial masking functions

---
_Source: https://npm.io/package/dufour-peyton-intersection · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
