Skip to content
/ tg Public
forked from tidwall/tg

Geometry library for C - Fast point-in-polygon

License

Notifications You must be signed in to change notification settings

jcdyer/tg

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TG

TG is a geometry library for C that is small, fast, and easy to use. I designed it for programs that need real-time geospatial, such as geofencing, monitoring, and streaming analysis.

Features

  • Implements OGC Simple Features including Point, LineString, Polygon, MultiPoint, MultiLineString, MultiPolygon, GeometryCollection.
  • Optimized polygon indexing that introduces two new structures.
  • Reads and writes WKT, WKB, and GeoJSON.
  • Provides a purely functional API that is reentrant and thread-safe.
  • Spatial predicates including "intersects", "covers", "touches", "equals", etc.
  • Test suite with 100% coverage using sanitizers and Valgrind.
  • Self-contained library that is encapsulated in the single tg.c source file.
  • Pretty darn good performance. 🚀 [benchmarks]

Goals

The main goal of TG is to provide the fastest, most memory efficent geometry library for the purpose of monitoring spatial relationships, specifically operations like point-in-polygon and geometry intersect.

It's a non-goal for TG to be a full GIS library. Consider GEOS if you need GIS algorithms like generating a convex hull or voronoi diagram.

Performance

TG uses entirely new indexing structures that speed up geometry predicates. It can index more than 10GB per second of point data on modern hardware, while using less than 7% of additional memory, and can perform over 10 million point-in-polygon operations per second, even when using large polygons with over 10K points.

The following benchmark provides an example of the point-in-polygon performance of TG when using a large polygon. In this case of Brazil, which has 39K points.

Brazil              ops/sec    ns/op  points  hits       built      bytes
tg/none              96,944    10315   39914  3257    46.73 µs    638,720
tg/natural       10,143,419       99   39914  3257    53.17 µs    681,360
tg/ystripes      15,174,761       66   39914  3257   884.06 µs  1,059,548
geos/none            29,708    33661   39914  3257   135.18 µs    958,104
geos/prepared     7,885,512      127   39914  3257  2059.94 µs  3,055,496
  • "built": Column showing how much time the polygon and index took to construct.
  • "bytes": Column showing the final in-memory size of the polygon and index.
  • "none": No indexing was used.
  • "natural": Using TG Natural indexing
  • "ystripes": Using TG YStripes indexing
  • "prepared": Using a GEOS PreparedGeometry

See all benchmarks for more information.

Using

Just drop the "tg.c" and "tg.h" files into your project. Uses standard C11 so most modern C compilers should work.

$ cc -c tg.c

Programmer notes

Check out the complete API for detailed information.

Pure functions

TG library functions are thread-safe, reentrant, and (mostly) without side effects. The exception being with the use of malloc by some functions like geometry constructors. In those cases, it's the programmer's responsibiilty to check the return value before continuing.

struct tg_geom *geom = tg_geom_new_point(-112, 33);
if (!geom) {
    // System is out of memory.
}

Fast cloning