Code Monkey home page Code Monkey logo

leaflet.pointinpolygon's Introduction

Leaflet.PointInPolygon


Leaflet.PointInPolygon is a Leaflet plugin/extension that provides point-in-polygon functions based on Dan Sunday's C++ winding number implementation. Functions contains(), getWindingNumber(), and isLeft() extend Leaflet classes L.Polygon, L.Polyline, and L.LatLng. The winding number method has an advantage to the crossing number method since "the winding number accurately determines if a point is inside a nonsimple closed polygon" (See Inclusion of a Point in Polygon by Dan Sunday).

Unlike the C++ implementation, points on the lines and vertices are considered as being included in the polygon. As such, the getWindingNumber() function will work for a polyline. The inclusiveness of points on the the polygon lines (and polylines) has a natural interpretation for locations on maps. A future version of Leaflet.PointInPolygon will support the ability to be inclusive or exclusive.

Requirements

  • Leaflet: This has been tested on a variant of Leaflet version 0.7.7, version Leaflet 1.0.3 (from GitHub), and Leaflet.Geodesic dated April 26, 2013.

Demos

Demo on JSFiddle: Leaflet.PointInPolygon Demo. And if you want to fork a copy of the fiddle, click here. Click on the map or the markers and a pop-up will show indicating if the point you clicked on, or the marker, is in the polygon.

![Leaflet.PointInPolygon Demo screen shot] (https://raw.githubusercontent.com/hayeswise/Leaflet.PointInPolygon/master/DemoScreenShot.PNG) (Clickable image)

Leaflet.PointInPolygon is plugin was Originally developed for the Ingress Intel Total Conversion(IITC). If you have a Ingress account, install the Ingress Intel Total Conversion desktop and the install the Portals-in-Polygons plugin.

Portals-in-polygons UI screen shot

Instructions for Including the Plugin

Web Page Inclusion

Use the script tag and pull from the CDN.

RawGit Production CDN:

<script src="https://cdn.rawgit.com/hayeswise/Leaflet.PointInPolygon/v1.0.0/wise-leaflet-pip.js"></script>

RawGit Development CDN

<script src="https://rawgit.com/hayeswise/Leaflet.PointInPolygon/master/wise-leaflet-pip.js"></script>

Greasemonkey or Tampermonkey

If you are using Greasemonkey or Tampermonkey scripts, as done with IITC plugins, use one of the following require metadata block keys and values:

API

See API.

Example

var polygon = L.polygon([
  [51.51, -0.08],
  [51.503, -0.06],
  [51.51, -0.047]
]).addTo(mymap);
var m1 = L.marker([51.515, -0.07]); // Outside and north of polygon
var m2 = L.marker([51.506, -0.06]); // In polygon, not on border
var m2 = L.marker([51.505, -0.074]); // Inside polygon boundary box but outside of polygon. 
var m2 = L.marker([51.51, -0.067]); // On polygon border.

console.log(polygon.contains(m1.getLatLng()));
// ==> false
console.log(polygon.contains(m2.getLatLng()));
// ==> true
console.log(polygon.contains(m3.getLatLng()));
// ==> false
console.log(polygon.contains(m4.getLatLng()));
// ==> true

For a more comprehensive example using the above code, see Leaflet.PointInPolygon Demo. Click on the Javascript tab to see the code.

CDN

CDN services provided by RawGit. This is useful for referencing the code distribution in JSFiddle. Just note that the CDN updates lag behind the GitHub updates. Production updates are behind Development updates. The Production CDN has no limits or throttling (but since this is a free service, there's no uptime or support guarantees).

Acknowledgements

Thanks to:

leaflet.pointinpolygon's People

Contributors

hayeswise avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.