惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Jina AI
Jina AI
博客园 - Franky
Apple Machine Learning Research
Apple Machine Learning Research
酷 壳 – CoolShell
酷 壳 – CoolShell
阮一峰的网络日志
阮一峰的网络日志
量子位
雷峰网
雷峰网
宝玉的分享
宝玉的分享
V
Visual Studio Blog
博客园_首页
小众软件
小众软件
The Cloudflare Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
大猫的无限游戏
大猫的无限游戏
博客园 - 聂微东
S
SegmentFault 最新的问题
博客园 - 【当耐特】
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 叶小钗
月光博客
月光博客
博客园 - 三生石上(FineUI控件)
人人都是产品经理
人人都是产品经理
WordPress大学
WordPress大学

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Point-in-polygon: one PostGIS query for geofencing, zones...
Samuel Toh · 2026-06-21 · via DEV Community

Samuel Toh

You've got a coordinate. You've got a pile of regions on a map. You need to know: which region does this point fall in, or is it inside one at all?

That single question hides inside a lot of features:

  • Geofencing: is this device or user inside the zone?
  • Service / delivery areas: does this address fall in our coverage?
  • Spatial lookups: which territory, district, or catchment owns this point?

They all reduce to one primitive: point in polygon. Learn it once and a whole class of "is this place inside or affected by X?" questions collapses into a single query. An example usage in real world context I have done in project is "is this address in a flood zone?" the exact same way. More on that at the end.

Let me walk through it.

Given a GeoJSON dataset:

Note: Most open spatial data ships as GeoJSON, a plain-text format for shapes. A region (a delivery area, a territory, a geofence) is just a polygon:

{
  "type": "Feature",
  "properties": { "name": "Zone A", "level": "high" },
  "geometry": {
    "type": "Polygon",
    "coordinates": 
      [
        [
          [152.97, -27.47],
          [152.98, -27.47],
          [152.98, -27.48], 
          [152.97, -27.48],
          [152.97, -27.47]
        ]
     ]
  }
}

A geometry (the shape, as [lng, lat] points) plus properties. A full dataset is a FeatureCollection of thousands of these. GeoJSON is an open standard used worldwide, so once you can read one set of regions, you can read any of them.

Ingest the dataset into PostGIS

CREATE TABLE zones (
    id    serial PRIMARY KEY,
    name  text,
    geom  geometry(MultiPolygon, 4326)
);
-- load the GeoJSON with ogr2ogr (or shp2pgsql for shapefiles)

(4326 is WGS84, the standard lat/lng system GPS and GeoJSON use.)

The query:
Given a coordinate, you can use the ST_Intersects API to test if its inside any zone:

SELECT EXISTS (
  SELECT 1 
    FROM zones
   WHERE ST_Intersects(geom, ST_SetSRID(ST_MakePoint(:lng, :lat), 4326)
  )
) AS inside_a_zone;

Want to know which zone, not just yes/no? Return it:

SELECT name 
  FROM zones
 WHERE ST_Intersects(geom, ST_SetSRID(ST_MakePoint(:lng, :lat), 4326));

And that is the whole pattern: spatial data plus one intersection test.

A few use cases I have used in reallife applications:

  1. Did the device enter this zone?
  2. Which district is this in?
  3. Is this address flooded?
  4. Can we deliver to this drop off point?

Same handful of lines every time, the formula almost never change. Point-in-polygon is most of "is this location inside or affected by X?" in geospatial software. And because GeoJSON is a global standard, it isn't region-specific: your delivery map, a US boundary set, a European zone layer, all the same approach.

Experience:
I lean on this pattern heavily at my company Property X-Ray, where most of the questions are based around address lookup runs and point-in-polygon against hundreds of overlapping overlays like flood, residential zoning, crime rate and more. The hard part is never the query, it's wrangling the data. But the primitive above is the whole foundation. Once point-in-polygon clicks, a huge category of "is this place inside something?" problems becomes one query.