• Stars
    star
    118
  • Rank 290,447 (Top 6 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 5 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A point in polygon library based on the paper "Optimal Reliable Point-in-Polygon Test and Differential Coding Boolean Operations on Polygons" by Hao

A small library for detecting in a point lies inside a polygon

Features

  • Works on polygons with holes
  • Works with degenerate/self-intersecting polyons
  • Returns 0 if on the edge
  • Not effected by floating point errors

Usage

Install via npm install point-in-polygon-hao

import inside from 'point-in-polygon-hao'

const polygon = [
  [
    [1, 1],
    [1, 2],
    [2, 2],
    [2, 1],
    [1, 1]
  ]
];

inside([ 1.5, 1.5 ], polygon)
// => true

inside([ 4.9, 1.2 ], polygon)
// => false

inside([1, 2], polygon)
// => 0 to indicate on edge

Note: The input polygon format aligns with the GeoJson specification for polygons. This means that the first and last coordinate in a polygon must be repeated, if not this library will throw an error.

const polygonWithHole = [
  [
    [0, 0], [1, 0], [1, 1], [0, 1], [0, 0]
  ],
  [
    [0.1, 0.1], [0.1, 0.9], [0.9, 0.9], [0.9, 0.1], [0.1, 0.1]
  ]
]

The library does not support multi-polygons.

Comparisons

Some rough comparisons to similar libraries. While point-in-polygon is slightly faster in most cases it does not support polygons with holes or degenerate polygons.

// For a point in a much larger geometry (700+ vertices)
point-in-poly-hao x 474,180 ops/sec ±0.55% (93 runs sampled)
point-in-polygon x 489,649 ops/sec ±0.75% (91 runs sampled)
robust-point-in-polygon x 376,268 ops/sec ±0.79% (89 runs sampled)
// For a point in bounding box check
point-in-poly-hao x 29,365,704 ops/sec ±1.30% (90 runs sampled)
point-in-polygon x 42,339,450 ops/sec ±0.78% (95 runs sampled)
robust-point-in-polygon x 20,675,569 ops/sec ±0.65% (95 runs sampled)

Algorithm

This library is based on the paper Optimal Reliable Point-in-Polygon Test and Differential Coding Boolean Operations on Polygons

Other notes

  • Works irrespective of winding order of polygon
  • Does not appear to be effected by floating point errors compared to point-in-polygon or robust-point-in-polygon

More Repositories

1

vue-dropzone

A Vue.js component for Dropzone.js - a drag’n’drop file uploads utility with image previews
Vue
2,009
star
2

leaflet-easyPrint

A leaflet plugin which adds an icon to print the map - Demo @ http://rowanwins.github.io/leaflet-easyPrint/
JavaScript
244
star
3

sweepline-intersections

A small module using a sweepline algorithm to detect intersections (& self-intersections) in polygons or polylines.
JavaScript
64
star
4

Leaflet.SvgShapeMarkers

Additional SVG shape marker types for leaflet.js
JavaScript
50
star
5

visibility-graph

Visibility graph implementation to support shortest path calculations such as dijkstra or a-star
JavaScript
48
star
6

polygon-splitter

A small (<10kb minified) javascript library for splitting polygons by a polyline.
JavaScript
45
star
7

mapbox-gl-arcgis-featureserver

A library for retrieving features from an ArcGIS FeatureServer or MapServer. This library makes tiled requests rather than simply requesting every feature.
JavaScript
41
star
8

geojson-editor

A simple online editor for geojson
Vue
38
star
9

shamos-hoey

A module to check if a polygon self-intersects
JavaScript
28
star
10

maplibregl-mapbox-request-transformer

Transform mapbox:// urls in MapboxGL Styles for use in MapLibreGL.
JavaScript
23
star
11

turf-builder

A simple UI for generating custom browserify builds of Turf.js
HTML
20
star
12

geotiff-server

WIP
JavaScript
19
star
13

arcgis-pbf-parser

A library for converting an arcgis-pbf into a geojson FeatureCollection.
JavaScript
19
star
14

mapbox-gl-flatgeobuf

A little library for mapbox-gl that provides a tiled approach to consuming flatgeobuf files.
JavaScript
18
star
15

kwc-lineclip

Line clipping based on the algorithm by Kodituwakka, Wijeweera & Chamikara
JavaScript
11
star
16

mbtiles-extractor

Take tiles from an mbtiles file locally (eg vector tiles) and extracts them to ZXY structure in an S3 bucket or locally.
JavaScript
10
star
17

geojson-buffer

WIP progress for buffering (aka offset) geojson
JavaScript
10
star
18

smallest-enclosing-circle

An implementation of Welzl's algorithm for calculating the smallest enclosing circle of a set of points
JavaScript
10
star
19

leaflet-simplestyle

Extends L.geoJSON to support the simplestyle spec with 1kb
JavaScript
8
star
20

vue-file-picker

A fairly minimal file picker & drop area which aims to stay out of the way of your application.
Vue
6
star
21

esri-leaflet-stream

A plugin for Esri Leaflet that enables consuming Stream Services published by ArcGIS for Server.
JavaScript
6
star
22

bentley-ottmann-intersections

A module to check if a polygon self-intersects using the bentley-ottmann algorithm
JavaScript
6
star
23

supercluster-server

A node.js server to publish super-cluster services from databases or shapefiles
JavaScript
5
star
24

sweepline-simplify

A module for creating simple polygons out of self-intersecting polygons
JavaScript
4
star
25

vector-to-grid

Convert geojson to a grid/raster
JavaScript
4
star
26

po-box

This simple tool for checking for the presence of, and extracting PO Box details from an address string.
JavaScript
4
star
27

date-fns-startOf

Equivalent of the startOf method in moment.js
JavaScript
3
star
28

geotiff-pbf

Store geotiffs as protocol buffers
JavaScript
3
star
29

tippecanoe-ui

A UI wrapper for tippecanoe using electron
JavaScript
3
star
30

geometry-js-links

A bunch of links related to computational geometry and javascript
3
star
31

stac-helper

JavaScript
3
star
32

polyline-splitter

A small (<10kb minified) javascript library for splitting geojson polylines by other polylines.
TypeScript
2
star
33

shp-to-geojson

WIP
JavaScript
2
star
34

convex-hull-wp

A convex hull implementation based on the paper by Wijeweera & Pinidiyaarachchi
JavaScript
2
star
35

generate-point-in-polygon

A small module to generate a point in a polygon in O(n) time.
JavaScript
2
star
36

boolean-jsts

JavaScript
2
star
37

triangulation-wk

A basic triangulation implementation based on work by Wijeweera & Kodituwakku
JavaScript
1
star
38

vue3-vite-antd-pinia-ts-starter

Less
1
star
39

leaflet-pinnacle-marker

A plugin for leafletjs to create a pinnacle style marker.
JavaScript
1
star
40

proj4-on-demand

proj4-on-demand is a JavaScript library to transform point coordinates from one coordinate system to another, including datum transformations. This library is based on proj but aims to be more light-weight while incoporating extra functionality.
JavaScript
1
star