Skip to content

Interior Point

Interior Point computes a representative point guaranteed to lie inside a geometry. It is a faithful port of the JTS (Java Topology Suite) InteriorPoint algorithm, available as both a TypeScript library and a Rust crate.

Installation

TypeScript

bash
npm install interior-point

Rust

Add to your Cargo.toml:

toml
[dependencies]
interior-point = "0.1"
geo-types = "0.7"

or using cargo add:

sh
cargo add interior-point
cargo add geo-types

Usage

TypeScript

typescript
import { interiorPoint } from "interior-point";

const polygon = {
  type: "Polygon",
  coordinates: [
    [
      [0, 0],
      [6, 0],
      [6, 2],
      [2, 2],
      [2, 8],
      [0, 8],
      [0, 0],
    ],
  ],
};

const point = interiorPoint(polygon);
console.log(point);
// => [1, 5]

Rust

rust
use interior_point::interior_point;
use geo_types::{Polygon, LineString};

let poly = Polygon::new(
    LineString::from(vec![
        (0.0, 0.0),
        (6.0, 0.0),
        (6.0, 2.0),
        (2.0, 2.0),
        (2.0, 8.0),
        (0.0, 8.0),
        (0.0, 0.0),
    ]),
    vec![],
);
let result = interior_point(&poly.into());
// => Some(Coord { x: 1.0, y: 5.0 })