# BorderPatrol: KML border checking in Ruby

DevFeed: [BorderPatrol: KML border checking in Ruby](<https://devfeed.tech/articles/borderpatrol-kml-border-checking-in-ruby-15552.md>)

Original publisher: [Read original article](<https://developer.squareup.com/blog/borderpatrol-kml-border-checking-in-ruby>)

Author: Square Engineering

Published: 2011-07-05T16:00:00Z

Content type: article

Language: en

Sources: [Square Corner](<https://devfeed.tech/sources/square-corner-blog-medium.md>)

Topics: [Ruby](<https://devfeed.tech/topics/ruby.md>), [Algorithms](<https://devfeed.tech/topics/algorithms.md>), [rubygems](<https://devfeed.tech/topics/rubygems.md>), [Benchmark](<https://devfeed.tech/topics/benchmark.md>)

Tags: [algorithm](<https://devfeed.tech/tags/algorithm.md>), [engineering](<https://devfeed.tech/tags/engineering.md>), [files](<https://devfeed.tech/tags/files.md>), [import](<https://devfeed.tech/tags/import.md>), [points](<https://devfeed.tech/tags/points.md>), [ray](<https://devfeed.tech/tags/ray.md>), [ray-casting](<https://devfeed.tech/tags/ray-casting.md>), [regions](<https://devfeed.tech/tags/regions.md>), [ruby](<https://devfeed.tech/tags/ruby.md>)

## AI overview

BorderPatrol is a Ruby gem that parses KML-defined regions and tests whether points fall inside them, including regions composed of multiple unlinked polygons. It uses point-in-polygon ray casting: an odd number of polygon-edge crossings indicates an interior point, and an even number indicates an exterior point. The gem first checks a polygon's bounding box and uses polygon equality checks and a hash function to reduce repeated work. The author reports more than 10,000 point checks per second on an early 2011 MacBook Pro with a relatively simple KML file; this is an anecdotal result, and the article includes a benchmarking script for testing other cases. The author says it was used in production for real-time assessments and offline evaluation.

## Source excerpt

Presenting a gem to import KML files and detect whether or not points are contained within defined regions.