9 Proximity analysis

 

This chapter covers

  • Nearest neighbor searches
  • K-nearest neighbor (KNN) distance operators
  • Using KNN with geography and geometry
  • Geotagging
  • PostGIS clustering window functions

Once you’ve located places with a set of coordinates, questions such as the following arise: How far is my house from the nearest expressway? How many burger joints are within a mile drive? What’s the average distance that people have to commute to work? Which three hospitals closest to me offer emergency vasectomies? If I need to get at least this much exercise but don’t want to overdo it, how many kale shake joints are between 1K and 2K away from my home? How do I assign visits to my field reps to balance the work? We’ll file all these questions under the heading of proximity analysis, or loosely, the study of how far something is located from something else.

We’ll cover both the traditional methods of finding closest neighbors as well as newer methods using k-nearest neighbor (KNN) indexes and PostGIS clustering window functions. Speed is often a concern when performing proximity analyses, and we’ll offer techniques and advice on how to speed up slow queries.

You’ll learn what you should consider when choosing between a geography type and geometry. You’ll learn about trade-offs such as performance, features, and ease of use.

9.1 Nearest neighbor searches

9.1.1 Which places are within X distance?

9.1.2 Using ST_DWithin and ST_Distance for N closest results

9.1.3 Using ST_DWithin and DISTINCT ON to find closest locations

9.1.4 Intersects with tolerance

9.1.5 Items between distances

9.1.6 Finding the N closest places using KNN distance operators

9.2 Using KNN with geography types

9.2.1 Using window functions to number the closest N places

9.3 Geotagging

9.3.1 Tagging data to a specific region