asish.com
Software engineering

How a map knows whether your pin is inside a boundary

Asish Punnose ·

A map pin gives you a coordinate. Ray casting tells you whether that coordinate falls inside the boundary associated with a selected address.

Two polygons showing a ray from an inside point crossing one edge and a ray from an outside point crossing two edges.

You search for a street address, zoom in and drop a pin near the back fence. The map can show exactly where you clicked. A separate question remains. Does that pin fall within the property boundary associated with the address you selected?

Software answers that question with a point-in-polygon test. One way to perform the test is ray casting. It draws an imaginary line from the pin and uses the number of boundary crossings to work out where the pin started.

The address and the pin give you different things

Searching for an address typically gives you a reference location. The pin you drop gives you another coordinate. To check whether the pin is inside the selected property, you also need the polygon that represents its mapped boundary.

Imagine an address associated with a single property polygon. A user drops a pin beside the driveway. The software retrieves the boundary associated with that address and compares the pin’s coordinates with the polygon. The same street address may be associated with multiple parcels or units, so the system needs a clear rule for selecting the relevant boundary before running the test.

Why one crossing means the pin started inside

Imagine drawing a line horizontally to the right from the pin. Extend it until the tip is past the entire polygon. We know that tip is outside. Now follow the line backwards from the tip towards the pin.

  • Zero crossings. You stay outside all the way to the pin.
  • One crossing. You cross into the polygon and reach the pin while still inside.
  • Two crossings. You cross into the polygon, cross out again and reach the pin outside.

Every genuine crossing switches sides. That gives us the rule odd means inside, even means outside. In the illustration, both arrows end outside. The result describes the white point where each arrow starts.

What the code calculates

Use coordinates in the same coordinate reference system. In a planar representation, let the pin be (px, py). For every polygon edge from (ax, ay) to (bx, by), the code checks whether the edge intersects the horizontal level of the pin. If it does, the code calculates the x coordinate of the intersection and counts it only if it is to the right of the pin.

inside = false

for each edge A → B in each boundary ring:
    if pin lies on segment A → B:
        return "on boundary"

    if (ay > py) differs from (by > py):
        x = ax + (py - ay) * (bx - ax) / (by - ay)
        if x > px:
            inside = not inside

return "inside" if inside else "outside"

The test includes the edge that closes each ring. The strict comparison with py counts a shared vertex consistently. At a corner where the ray only touches the polygon, the two adjoining edges contribute either zero or two crossings, leaving the result unchanged. Horizontal edges are skipped by the crossing calculation and still participate in the boundary check.

What happens at the fence line?

If the pin lies on a segment or corner, return on boundary as its own result. This keeps the geometry separate from the decision about whether a location on the line is acceptable. For example, a workflow could ask the user to move a boundary pin or flag it for review.

A pin close to the line can also be affected by the accuracy of the input coordinates and the mapped boundary. If the workflow needs a tolerance measured in metres, calculate that distance in an appropriate coordinate system and define the tolerance explicitly.

Irregular shapes and holes

An L-shaped boundary works with the same rule. A pin in the cut-out may still be within the rectangle that encloses the property, but the ray crossing count correctly places it outside the polygon.

For a polygon with a hole, include both the outer ring and the hole’s ring. From a point inside the hole, the ray can cross the hole’s edge and then the outer edge, giving an even count. If the selected boundary is a multipolygon, test its component polygons and combine their results.

Where this fits in a map workflow

A browser can run the test as soon as the user moves the pin. If the classification controls whether a record can be submitted, the server can repeat the check using the selected boundary. The basic algorithm examines each edge once. A bounding box can quickly rule out points that are clearly too far away, and a spatial index can narrow the search when the user has yet to select a property.

The address identifies the boundary to check. Ray casting answers the geometric question of where the pin falls relative to that boundary.