Need to determine if a location falls within a defined boundary? This guide covers everything about point-in-polygon testing via API, including the underlying algorithms, geofencing applications, and implementation examples.
What is Point in Polygon?
Point in polygon (PIP) is a computational geometry problem that determines whether a given point lies inside, outside, or on the boundary of a polygon. It's fundamental to many geospatial applications.
The most common use is geofencing - triggering actions when a device enters or exits a defined geographic area.
The Algorithm
The most common algorithm is the ray casting method:
Ray Casting Algorithm
Draw an imaginary ray from the point to infinity and count how many times it crosses the polygon boundary. An odd count means inside, even means outside.
Edge Cases
- On boundary: Points exactly on the edge may be considered inside or outside
- Complex polygons: Works with concave polygons and polygons with holes
- Geographic coordinates: Requires handling of spherical geometry for large polygons
Using the Point in Polygon API
TinyFn provides a simple endpoint to test point containment:
POST https://api.tinyfn.io/v1/geo/point-in-polygon
Headers: X-API-Key: your-api-key
Content-Type: application/json
{
"point": {"lat": 40.7580, "lng": -73.9855},
"polygon": [
{"lat": 40.7484, "lng": -73.9967},
{"lat": 40.7484, "lng": -73.9717},
{"lat": 40.7680, "lng": -73.9717},
{"lat": 40.7680, "lng": -73.9967}
]
}
{
"inside": true,
"on_boundary": false,
"distance_to_boundary": 0.234
}
Parameters
| Parameter | Type | Description |
|---|---|---|
point |
object | Coordinate to test with lat/lng properties (required) |
polygon |
array | Array of coordinates defining the polygon (required) |
include_boundary |
boolean | Consider points on boundary as inside (default: true) |
holes |
array | Array of hole polygons (optional) |
Code Examples
JavaScript / Node.js
const response = await fetch(
'https://api.tinyfn.io/v1/geo/point-in-polygon',
{
method: 'POST',
headers: {
'X-API-Key': 'your-api-key',
'Content-Type': 'application/json'
},
body: JSON.stringify({
point: { lat: 40.7580, lng: -73.9855 },
polygon: [
{ lat: 40.7484, lng: -73.9967 },
{ lat: 40.7484, lng: -73.9717 },
{ lat: 40.7680, lng: -73.9717 },
{ lat: 40.7680, lng: -73.9967 }
]
})
}
);
const { inside } = await response.json();
console.log(inside ? 'Point is inside!' : 'Point is outside');
Python
import requests
response = requests.post(
'https://api.tinyfn.io/v1/geo/point-in-polygon',
headers={'X-API-Key': 'your-api-key'},
json={
'point': {'lat': 40.7580, 'lng': -73.9855},
'polygon': [
{'lat': 40.7484, 'lng': -73.9967},
{'lat': 40.7484, 'lng': -73.9717},
{'lat': 40.7680, 'lng': -73.9717},
{'lat': 40.7680, 'lng': -73.9967}
]
}
)
result = response.json()
print('Inside!' if result['inside'] else 'Outside')
cURL
curl -X POST "https://api.tinyfn.io/v1/geo/point-in-polygon" \
-H "X-API-Key: your-api-key" \
-H "Content-Type: application/json" \
-d '{
"point": {"lat": 40.7580, "lng": -73.9855},
"polygon": [
{"lat": 40.7484, "lng": -73.9967},
{"lat": 40.7484, "lng": -73.9717},
{"lat": 40.7680, "lng": -73.9717},
{"lat": 40.7680, "lng": -73.9967}
]
}'
Common Use Cases
- Geofencing: Trigger notifications when users enter/exit defined areas
- Delivery Zones: Check if an address falls within a service area
- Territory Assignment: Assign leads or customers to sales territories
- Location Verification: Verify that check-ins occur in designated areas
- Map Interactions: Detect clicks/taps on polygon features
Best Practices
- Close polygons: Ensure the first and last points are the same
- Use bounding box pre-filter: Check bbox containment first for performance
- Handle holes correctly: For polygons with holes (e.g., donuts), provide hole arrays
- Consider winding order: Some systems require specific vertex ordering
Use via MCP
Your AI agent can call this tool directly via Model Context Protocol — no HTTP code needed. Add TinyFn to Claude Desktop, Cursor, or any MCP client:
{
"mcpServers": {
"tinyfn-geo": {
"url": "https://api.tinyfn.io/mcp/geo/",
"headers": {
"X-API-Key": "your-api-key"
}
}
}
}
See all geolocation tools available via MCP in our Geolocation MCP Tools for AI Agents guide.
Try the Point in Polygon API
Get your free API key and start testing polygon containment in seconds.
Get Free API Key