$ show twpayne/go-geom
This repository is a comprehensive Go library for geometric data structures and operations. It provides implementations of OGC-standard geometry types (Point, LineString, Polygon, MultiPoint, MultiLineString, MultiPolygon, GeometryCollection) with support for 2D, 3D, and measured dimensions. The library includes algorithms for computational geometry (area, centroid, convex hull, line intersection, etc.) and encoders/decoders for formats like WKT, WKB, GeoJSON, KML, and IGC. Architecture is modular: core `geom` package defines types and interfaces, `xy`/`xyz` packages handle planar/spatial algorithms, `encoding/*` packages manage format serialization, and utilities like `transform` and `sorting` offer reusable helpers. Command-line tools for IGC parsing and PostGIS integration demonstrate practical usage. The code emphasizes clean separation of concerns, interface-driven design, and thorough testing including property-based and fuzzing tests.
The LLM identified 4 files worth highlighting for reuse:
Defines the core geom.T interface that all geometry types implement, providing a uniform contract for methods like Layout(), Stride(), Bounds(), and FlatCoords(). This enables polymorphic algorithms across different geometry types and is the foundation for the library's extensibility. The interface is small, focused, and allows algorithms to work with any geometry without knowing concrete types.
Implements area and centroid calculation for polygons using the shoelace formula. The function is pure, takes a geom.T, and returns computed values without side effects. It handles all geometry types via the geom.T interface, demonstrating how to write reusable geometric algorithms that depend only on the core interface. The implementation is efficient and correctly handles edge cases like holes in polygons.
Provides WKB encoding and decoding with configurable options (e.g., endianness, SRID handling) using the functional options pattern. The encoder and decoder are stateless and depend only on the geom.T interface, making them highly reusable. The code cleanly separates concerns: options modify behavior, and the core logic handles byte order and geometry type dispatching. This pattern is easily adaptable to other binary formats.
Implements a tree set for efficient deduplication of coordinates using a red-black tree behind the scenes. The structure is generic over coordinate types and provides Insert, Contains, and Len methods. It's self-contained (only depends on geom.T and stdlib) and solves a common problem in geometry processing: removing duplicate vertices while preserving order. The implementation is readable and performant.