geodetic 0.6.0 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +59 -0
- data/README.md +23 -0
- data/docs/reference/geos-acceleration.md +176 -0
- data/docs/reference/map-rendering.md +102 -16
- data/examples/05_map_rendering/demo.rb +90 -105
- data/examples/12_geos_benchmark.rb +258 -0
- data/examples/13_geos_operations.rb +538 -0
- data/examples/14_geos_map_rendering.rb +323 -0
- data/examples/README.md +89 -0
- data/examples/geos_showcase.png +0 -0
- data/examples/sample_geometries.wkb.hex +6 -6
- data/lib/geodetic/areas/polygon.rb +66 -10
- data/lib/geodetic/geos.rb +547 -0
- data/lib/geodetic/map/base.rb +132 -0
- data/lib/geodetic/map/lib_gd_gis.rb +149 -0
- data/lib/geodetic/map.rb +31 -0
- data/lib/geodetic/path.rb +20 -8
- data/lib/geodetic/version.rb +1 -1
- data/lib/geodetic.rb +2 -0
- metadata +11 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 542167ec9e7b27122d31d5287f967481dc9d0d4b293eaaaa349ca4203e2eb302
|
|
4
|
+
data.tar.gz: f544332a931f775df18ce4e0d2ed78e4d2619bbb946df6d287c8bc3a798d6c9f
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: cca8fc5c8b38722ab95fecfeea576104c180352a9af854171858702d23a240368bef870b831c463601f06f6f04d76671aa8a5254edfc37880b3acef1aced476a
|
|
7
|
+
data.tar.gz: 29f289c88b14fcbe64091256d61afaa1752a4f405a5a3f88e55b966c7fbc50076c6db6a5e88bbe4f1e4840f3a424c88e37daf167acdca64b1872d81f8a2dcc01
|
data/CHANGELOG.md
CHANGED
|
@@ -8,6 +8,65 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
10
|
|
|
11
|
+
## [0.8.0] - 2026-03-11
|
|
12
|
+
|
|
13
|
+
### Added
|
|
14
|
+
|
|
15
|
+
- **Map adapter pattern** (`Geodetic::Map`) for rendering geodetic objects on maps with pluggable backends
|
|
16
|
+
- `Geodetic::Map::Base` — abstract adapter interface with `add(object, **style)` auto-dispatch by geometry type
|
|
17
|
+
- `Geodetic::Map::LibGdGis` — concrete adapter for raster PNG output via the `libgd-gis` gem
|
|
18
|
+
- `MapMethods` mixin — adds `add_to_map(map, **style)` to all coordinates, paths, segments, areas, and features
|
|
19
|
+
- Render block pattern: `map.render(path) { |gd_map| ... }` for custom GD drawing after adapter layers
|
|
20
|
+
- `gd_map` accessor for post-render access to the underlying `GD::GIS::Map`
|
|
21
|
+
- Accepts `BoundingBox` or `[west, south, east, north]` array for bbox
|
|
22
|
+
- Supports point, line, and polygon layers with style options (color, stroke, fill, width, label, icon, font, symbol, segments)
|
|
23
|
+
- GEOS map rendering example (`examples/14_geos_map_rendering.rb`) — visualizes 8 GEOS operation categories (boolean intersection/difference, point/path buffering, convex hull, simplification, nearest points, prepared geometry containment) on a single raster map with distinct colors and an embedded legend
|
|
24
|
+
- 28 new map adapter tests covering all geometry types, style handling, ENU/NED rejection, chaining, and LibGdGis bbox resolution
|
|
25
|
+
- Documentation: `docs/reference/map-rendering.md` (architecture, usage, style options, color format, prerequisites)
|
|
26
|
+
|
|
27
|
+
### Changed
|
|
28
|
+
|
|
29
|
+
- Refactored `examples/05_map_rendering/demo.rb` to use the new `Map::LibGdGis` adapter instead of raw `GD::GIS::Map` calls
|
|
30
|
+
- Updated `docs/reference/geos-acceleration.md` with reference to example 14
|
|
31
|
+
- Updated `examples/README.md` with example 14 description
|
|
32
|
+
- Updated `CLAUDE.md` with map adapter architecture, file layout, and expanded examples range
|
|
33
|
+
|
|
34
|
+
## [0.7.0] - 2026-03-10
|
|
35
|
+
|
|
36
|
+
### Added
|
|
37
|
+
|
|
38
|
+
- **`Geodetic::Geos` module** — optional GEOS C library integration via `fiddle` for accelerated spatial operations
|
|
39
|
+
- **Library binding**: auto-discovers `libgeos_c` on macOS and Linux; uses reentrant `_r` API for thread safety
|
|
40
|
+
- **Predicates**: `Geos.contains?(a, b)`, `Geos.intersects?(a, b)`, `Geos.is_valid?(geom)`, `Geos.is_valid_reason(geom)`
|
|
41
|
+
- **Boolean operations**: `Geos.intersection(a, b)`, `Geos.difference(a, b)`, `Geos.symmetric_difference(a, b)`, `Geos.union(geom)`
|
|
42
|
+
- **Geometry construction**: `Geos.buffer(geom, distance)`, `Geos.buffer_with_style(geom, distance, ...)`, `Geos.convex_hull(geom)`, `Geos.simplify(geom, tolerance)`, `Geos.make_valid(geom)`
|
|
43
|
+
- **Measurements**: `Geos.area(geom)`, `Geos.length(geom)`, `Geos.distance(a, b)`, `Geos.nearest_points(a, b)`
|
|
44
|
+
- **`PreparedGeometry`**: `Geos.prepare(polygon)` builds a spatial index for O(log n) batch `contains?`/`intersects?` queries
|
|
45
|
+
- **Graceful degradation**: `Geos.available?` returns false when `libgeos_c` is not installed; all operations fall back to pure Ruby
|
|
46
|
+
- **`GEODETIC_GEOS_DISABLE` env var**: forces pure Ruby for all operations even when GEOS is installed
|
|
47
|
+
- **`LIBGEOS_PATH` env var**: specify a custom `libgeos_c` library path
|
|
48
|
+
- All GEOS operations accept any Geodetic geometry type and return standard Geodetic objects (Polygon, Path, LLA, etc.)
|
|
49
|
+
- **GEOS-accelerated polygon validation** — `Polygon.new` delegates self-intersection validation to GEOS when available, using O(n log n) spatial indexing vs Ruby's O(n^2) pairwise test
|
|
50
|
+
- **GEOS-accelerated point-in-polygon** — `polygon.includes?(point)` uses GEOS for polygons with 15+ vertices (`Polygon::GEOS_INCLUDES_THRESHOLD`); below threshold, Ruby's winding-number algorithm is faster
|
|
51
|
+
- **GEOS-accelerated path intersection** — `path.intersects?(other_path)` uses GEOS when available (wins at all tested sizes vs Ruby's O(n*m) brute-force)
|
|
52
|
+
- **Improved Ruby polygon validation** — added `validate_distinct_vertices!` and `validate_noncollinear!` checks so pure Ruby matches GEOS accuracy for degenerate polygons
|
|
53
|
+
- GEOS benchmark example (`examples/12_geos_benchmark.rb`) — compares Ruby vs GEOS performance across polygon validation, point-in-polygon, path intersection, PreparedGeometry batch containment, single segment (Ruby wins), and GEOS-only operations
|
|
54
|
+
- GEOS operations example (`examples/13_geos_operations.rb`) — 11-section demo covering boolean overlay, buffering, convex hull, simplification, validity checking, geometry repair, planar measurements, nearest points, PreparedGeometry, operation chaining, and GeoJSON/WKT export of results
|
|
55
|
+
- 27 GEOS tests covering all operations, predicates, PreparedGeometry, and error handling
|
|
56
|
+
- 3 new polygon validation tests: collinear boundary, insufficient distinct vertices, all-same-point
|
|
57
|
+
- Documentation: `docs/reference/geos-acceleration.md` (installation, automatic dispatch, performance expectations, API reference)
|
|
58
|
+
|
|
59
|
+
### Changed
|
|
60
|
+
|
|
61
|
+
- Updated README with GEOS Acceleration feature, optional dependency section, and examples 12-13 in the examples table
|
|
62
|
+
- Updated `examples/README.md` with example 12 and 13 descriptions
|
|
63
|
+
- Updated `examples/sample_geometries.wkb.hex` to use valid (non-degenerate) triangles in polygon entries
|
|
64
|
+
|
|
65
|
+
### Fixed
|
|
66
|
+
|
|
67
|
+
- WKB test data used collinear triangle `(1,2)-(3,4)-(5,6)` which is now correctly rejected; updated to valid triangle `(1,2)-(3,4)-(5,2)`
|
|
68
|
+
- Polygon validation error message regex in tests updated to match both Ruby and GEOS formats
|
|
69
|
+
|
|
11
70
|
## [0.6.0] - 2026-03-10
|
|
12
71
|
|
|
13
72
|
### Added
|
data/README.md
CHANGED
|
@@ -27,6 +27,7 @@
|
|
|
27
27
|
- <strong>GeoJSON Export</strong> - Build FeatureCollections from any mix of objects and save to file<br>
|
|
28
28
|
- <strong>WKT Serialization</strong> - Well-Known Text export/import with SRID/EWKT and Z-dimension support<br>
|
|
29
29
|
- <strong>WKB Serialization</strong> - Well-Known Binary export/import with EWKB, SRID, hex encoding, and file I/O<br>
|
|
30
|
+
- <strong>GEOS Acceleration</strong> - Optional native acceleration for polygon validation, point-in-polygon, path intersection, and boolean operations<br>
|
|
30
31
|
- <strong>Validated Setters</strong> - Type coercion and range validation on all coordinate attributes<br>
|
|
31
32
|
- <strong>Serialization</strong> - to_s(precision), to_a, from_string, from_array, DMS format<br>
|
|
32
33
|
- <strong>Multiple Datums</strong> - WGS84, Clarke 1866, GRS 1980, Airy 1830, and more<br>
|
|
@@ -65,6 +66,26 @@ brew install h3
|
|
|
65
66
|
|
|
66
67
|
You can also set the `LIBH3_PATH` environment variable to point to a custom `libh3` location.
|
|
67
68
|
|
|
69
|
+
### Optional: GEOS Spatial Acceleration
|
|
70
|
+
|
|
71
|
+
The [GEOS](https://libgeos.org/) library accelerates polygon validation, point-in-polygon tests, path intersection, and adds boolean geometry operations (intersection, difference, convex hull, etc.). Without it, all operations use pure Ruby implementations. Geodetic automatically uses GEOS when available and falls back to Ruby when it is not.
|
|
72
|
+
|
|
73
|
+
```bash
|
|
74
|
+
# macOS
|
|
75
|
+
brew install geos
|
|
76
|
+
|
|
77
|
+
# Linux (Debian/Ubuntu)
|
|
78
|
+
sudo apt-get install libgeos-dev
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
Geodetic uses GEOS selectively — only where it provides a measurable speedup. Single segment intersections stay in Ruby (FFI overhead exceeds the computation cost). For polygons with fewer than 15 vertices, point-in-polygon tests also stay in Ruby.
|
|
82
|
+
|
|
83
|
+
Set `GEODETIC_GEOS_DISABLE=1` to force pure Ruby for all operations, even when GEOS is installed. See [GEOS Acceleration](docs/reference/geos-acceleration.md) for detailed performance analysis and [example 12](examples/12_geos_benchmark.rb) for a runnable benchmark.
|
|
84
|
+
|
|
85
|
+
```ruby
|
|
86
|
+
Geodetic::Geos.available? # => true when libgeos_c is found and not disabled
|
|
87
|
+
```
|
|
88
|
+
|
|
68
89
|
## Usage
|
|
69
90
|
|
|
70
91
|
### Basic Coordinate Creation
|
|
@@ -873,6 +894,8 @@ The [`examples/`](examples/) directory contains runnable demo scripts showing pr
|
|
|
873
894
|
| [`09_geojson_export.rb`](examples/09_geojson_export.rb) | GeoJSON export: `to_geojson` on all geometry types, `GeoJSON` class for building FeatureCollections with `<<`, delete/clear, Enumerable, and `save` to file |
|
|
874
895
|
| [`10_wkt_serialization.rb`](examples/10_wkt_serialization.rb) | WKT serialization: `to_wkt` on all geometry types, SRID/EWKT, Z-dimension handling, parsing, and roundtrip verification |
|
|
875
896
|
| [`11_wkb_serialization.rb`](examples/11_wkb_serialization.rb) | WKB serialization: `to_wkb`/`to_wkb_hex` on all geometry types, EWKB/SRID, Z-dimension, parsing, roundtrip, and binary/hex file I/O |
|
|
897
|
+
| [`12_geos_benchmark.rb`](examples/12_geos_benchmark.rb) | GEOS performance benchmark: polygon validation, point-in-polygon, path intersection, PreparedGeometry batch containment, and GEOS-only boolean operations |
|
|
898
|
+
| [`13_geos_operations.rb`](examples/13_geos_operations.rb) | GEOS-only operations: boolean overlay (intersection, difference, symmetric difference, union), buffering, convex hull, simplification, validity checking, geometry repair, planar measurements, nearest points, PreparedGeometry, and operation chaining |
|
|
876
899
|
|
|
877
900
|
Run any example with:
|
|
878
901
|
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
# GEOS Acceleration
|
|
2
|
+
|
|
3
|
+
Geodetic optionally integrates with the [GEOS](https://libgeos.org/) (Geometry Engine - Open Source) C library to accelerate spatial operations. When `libgeos_c` is available, Geodetic transparently delegates performance-critical geometry operations to GEOS while keeping pure Ruby as the fallback.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
# macOS
|
|
9
|
+
brew install geos
|
|
10
|
+
|
|
11
|
+
# Linux (Debian/Ubuntu)
|
|
12
|
+
sudo apt-get install libgeos-dev
|
|
13
|
+
|
|
14
|
+
# Linux (Fedora/RHEL)
|
|
15
|
+
sudo dnf install geos-devel
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
Verify installation:
|
|
19
|
+
|
|
20
|
+
```ruby
|
|
21
|
+
require "geodetic"
|
|
22
|
+
Geodetic::Geos.available? # => true
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## How It Works
|
|
26
|
+
|
|
27
|
+
Geodetic uses Ruby's `fiddle` stdlib (the same approach used for H3) to call the GEOS C API directly — no compiled extensions or external gems required. The integration uses the reentrant `_r` API for thread safety.
|
|
28
|
+
|
|
29
|
+
The bridge works through WKT serialization:
|
|
30
|
+
|
|
31
|
+
1. Geodetic objects are converted to WKT via `to_wkt`
|
|
32
|
+
2. GEOS parses the WKT into its internal geometry representation
|
|
33
|
+
3. GEOS performs the operation using optimized C code with spatial indexing
|
|
34
|
+
4. Results are converted back to WKT and parsed into Geodetic objects
|
|
35
|
+
|
|
36
|
+
This approach keeps the integration simple and maintainable while providing significant performance gains for complex geometries.
|
|
37
|
+
|
|
38
|
+
## Automatic Dispatch
|
|
39
|
+
|
|
40
|
+
Geodetic selectively uses GEOS only where it provides a measurable speedup. The dispatch logic is built into the existing classes:
|
|
41
|
+
|
|
42
|
+
### Polygon Validation
|
|
43
|
+
|
|
44
|
+
`Polygon.new(boundary: [...])` automatically validates the boundary for self-intersection. GEOS uses an O(n log n) spatial index compared to Ruby's O(n^2) pairwise segment test.
|
|
45
|
+
|
|
46
|
+
- GEOS is used at **all polygon sizes** when available
|
|
47
|
+
- Speedup grows with vertex count: ~2x at 50 vertices, ~5x at 100, ~50x+ at 500
|
|
48
|
+
|
|
49
|
+
### Point-in-Polygon
|
|
50
|
+
|
|
51
|
+
`polygon.includes?(point)` tests whether a point lies inside a polygon.
|
|
52
|
+
|
|
53
|
+
- GEOS is used for polygons with **15 or more vertices** (`Polygon::GEOS_INCLUDES_THRESHOLD`)
|
|
54
|
+
- Below 15 vertices, Ruby's winding-number algorithm is faster (FFI overhead dominates)
|
|
55
|
+
- Above the threshold, GEOS provides 2-10x speedup depending on polygon complexity
|
|
56
|
+
|
|
57
|
+
### Path Intersection
|
|
58
|
+
|
|
59
|
+
`path.intersects?(other_path)` tests whether two paths cross.
|
|
60
|
+
|
|
61
|
+
- GEOS is **always used** when available (wins at all tested sizes)
|
|
62
|
+
- Ruby uses O(n*m) brute-force segment pair testing
|
|
63
|
+
- GEOS uses spatial indexing for efficient intersection detection
|
|
64
|
+
- Speedup is most dramatic for non-intersecting paths with overlapping bounds (worst case for Ruby): 10x at 100 points, 100x+ at 1000 points
|
|
65
|
+
|
|
66
|
+
### Where Ruby Wins
|
|
67
|
+
|
|
68
|
+
Single segment intersection (`segment.intersects?(other_segment)`) stays in pure Ruby at all times. The FFI marshaling overhead for a single pair of line segments exceeds the computation cost. Geodetic does not use GEOS for this operation.
|
|
69
|
+
|
|
70
|
+
## PreparedGeometry
|
|
71
|
+
|
|
72
|
+
For batch operations (testing many points against the same polygon), `PreparedGeometry` builds a spatial index once and reuses it for O(log n) queries:
|
|
73
|
+
|
|
74
|
+
```ruby
|
|
75
|
+
polygon = Geodetic::Areas::Polygon.new(boundary: vertices)
|
|
76
|
+
prepared = Geodetic::Geos.prepare(polygon)
|
|
77
|
+
|
|
78
|
+
points.each do |pt|
|
|
79
|
+
prepared.contains?(pt) # O(log n) per query
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
prepared.release # free the GEOS geometry
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
This is significantly faster than calling `polygon.includes?` in a loop, which creates a new GEOS geometry for each call.
|
|
86
|
+
|
|
87
|
+
## GEOS-Only Operations
|
|
88
|
+
|
|
89
|
+
GEOS provides operations that have no pure Ruby equivalent in Geodetic:
|
|
90
|
+
|
|
91
|
+
```ruby
|
|
92
|
+
Geos = Geodetic::Geos
|
|
93
|
+
|
|
94
|
+
# Boolean operations — return Geodetic objects
|
|
95
|
+
Geos.intersection(poly_a, poly_b) # area of overlap
|
|
96
|
+
Geos.difference(poly_a, poly_b) # A minus B
|
|
97
|
+
Geos.symmetric_difference(poly_a, poly_b) # area in either but not both
|
|
98
|
+
|
|
99
|
+
# Geometry analysis
|
|
100
|
+
Geos.convex_hull(polygon) # smallest convex polygon
|
|
101
|
+
Geos.simplify(polygon, tolerance) # Douglas-Peucker simplification
|
|
102
|
+
Geos.make_valid(polygon) # repair invalid geometry
|
|
103
|
+
Geos.is_valid?(polygon) # OGC validity check
|
|
104
|
+
Geos.is_valid_reason(polygon) # human-readable validity reason
|
|
105
|
+
|
|
106
|
+
# Measurements
|
|
107
|
+
Geos.area(polygon) # area in coordinate units
|
|
108
|
+
Geos.length(path) # length in coordinate units
|
|
109
|
+
Geos.distance(geom_a, geom_b) # minimum distance
|
|
110
|
+
|
|
111
|
+
# Spatial relationships
|
|
112
|
+
Geos.nearest_points(geom_a, geom_b) # closest point pair
|
|
113
|
+
Geos.contains?(polygon, point) # containment test
|
|
114
|
+
Geos.intersects?(geom_a, geom_b) # intersection test
|
|
115
|
+
|
|
116
|
+
# Buffering
|
|
117
|
+
Geos.buffer(geometry, width) # buffer zone
|
|
118
|
+
Geos.buffer_with_style(geometry, width, quad_segs:, cap_style:, join_style:)
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
## Disabling GEOS
|
|
122
|
+
|
|
123
|
+
Set the `GEODETIC_GEOS_DISABLE` environment variable to force pure Ruby for all operations:
|
|
124
|
+
|
|
125
|
+
```bash
|
|
126
|
+
GEODETIC_GEOS_DISABLE=1 ruby -Ilib my_script.rb
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
```ruby
|
|
130
|
+
ENV['GEODETIC_GEOS_DISABLE'] = '1'
|
|
131
|
+
Geodetic::Geos.available? # => false (even when libgeos_c is installed)
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
This is useful for:
|
|
135
|
+
|
|
136
|
+
- Benchmarking Ruby vs GEOS performance (see example 12)
|
|
137
|
+
- Debugging to isolate whether an issue is in GEOS or Ruby code
|
|
138
|
+
- Running in environments where GEOS cannot be installed
|
|
139
|
+
|
|
140
|
+
## Performance Expectations
|
|
141
|
+
|
|
142
|
+
The following table shows representative speedups measured with [example 12](../../examples/12_geos_benchmark.rb). Actual results vary by hardware and polygon complexity.
|
|
143
|
+
|
|
144
|
+
| Operation | Size | Typical Speedup |
|
|
145
|
+
|-----------|------|-----------------|
|
|
146
|
+
| Polygon validation | 50 vertices | ~2x |
|
|
147
|
+
| Polygon validation | 100 vertices | ~5x |
|
|
148
|
+
| Polygon validation | 500 vertices | ~50x |
|
|
149
|
+
| Point-in-polygon | 30 vertices | ~2x |
|
|
150
|
+
| Point-in-polygon | 100 vertices | ~5x |
|
|
151
|
+
| Point-in-polygon | 500 vertices | ~10x |
|
|
152
|
+
| Path intersection | 100 points | ~10x |
|
|
153
|
+
| Path intersection | 500 points | ~50x |
|
|
154
|
+
| Path intersection | 1000 points | ~100x |
|
|
155
|
+
| Batch containment (prepared) | 1000 points × 100v | ~20x |
|
|
156
|
+
| Single segment | 2 points | Ruby is ~2x faster |
|
|
157
|
+
|
|
158
|
+
Run the benchmark yourself:
|
|
159
|
+
|
|
160
|
+
```bash
|
|
161
|
+
ruby -Ilib examples/12_geos_benchmark.rb
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
For a visual demonstration of GEOS operations rendered on a map:
|
|
165
|
+
|
|
166
|
+
```bash
|
|
167
|
+
ruby -Ilib examples/14_geos_map_rendering.rb
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
## Architecture Notes
|
|
171
|
+
|
|
172
|
+
- **Thread safety**: Uses the reentrant GEOS `_r` API with per-process context initialization
|
|
173
|
+
- **No compiled extensions**: Uses `fiddle` from Ruby's stdlib, same pattern as the H3 integration
|
|
174
|
+
- **Graceful degradation**: All operations work without GEOS; it is purely an optimization
|
|
175
|
+
- **WKT bridge**: Geometries are serialized to WKT for transfer between Ruby and GEOS, leveraging Geodetic's existing WKT infrastructure
|
|
176
|
+
- **Memory management**: GEOS geometries and readers/writers are properly freed via `GEOSGeom_destroy_r`, `GEOSWKTReader_destroy_r`, etc.
|
|
@@ -1,25 +1,106 @@
|
|
|
1
|
-
# Map Rendering
|
|
1
|
+
# Map Rendering
|
|
2
2
|
|
|
3
|
-
Geodetic coordinates
|
|
3
|
+
Geodetic provides a map adapter pattern (`Geodetic::Map`) for rendering coordinates, paths, areas, and features on maps. The adapter abstracts the rendering backend so the same Geodetic objects work with different map engines.
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## Architecture
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
```
|
|
8
|
+
Geodetic::Map::Base # Abstract adapter interface
|
|
9
|
+
├── Geodetic::Map::LibGdGis # Raster PNG output via libgd-gis
|
|
10
|
+
├── (future) Leaflet # Interactive HTML/JS maps
|
|
11
|
+
├── (future) GoogleMaps # Google Maps HTML or Static API
|
|
12
|
+
└── (future) KML # KML XML output
|
|
13
|
+
```
|
|
8
14
|
|
|
9
|
-
|
|
15
|
+
`MapMethods` is a mixin applied to all coordinates, areas, paths, segments, and features, providing `add_to_map(map, **style)`.
|
|
10
16
|
|
|
11
|
-
|
|
12
|
-
- **Polygon overlays** from `Geodetic::Areas::Polygon` boundaries
|
|
13
|
-
- **Bearing arrows** computed with `Feature#bearing_to` and drawn as lines with arrowheads
|
|
14
|
-
- **Distance labels** using `Feature#distance_to` for annotation
|
|
15
|
-
- **Icon compositing** with scaled PNG images positioned at projected coordinates
|
|
16
|
-
- **Light and dark basemaps** via `:carto_light` and `:carto_dark`
|
|
17
|
+
## Usage
|
|
17
18
|
|
|
18
|
-
|
|
19
|
+
### Adding objects to a map
|
|
20
|
+
|
|
21
|
+
Two equivalent APIs — use whichever reads better:
|
|
22
|
+
|
|
23
|
+
```ruby
|
|
24
|
+
# Map-centric
|
|
25
|
+
map = Geodetic::Map::LibGdGis.new(bbox: bbox, zoom: 12, basemap: :carto_dark)
|
|
26
|
+
map.add(polygon, fill: [0, 200, 120, 170], stroke: [0, 200, 120, 30], width: 2)
|
|
27
|
+
map.add(path, color: [255, 220, 50], width: 3)
|
|
28
|
+
map.add(coordinate, color: [255, 0, 0], label: "Marker")
|
|
29
|
+
|
|
30
|
+
# Object-centric (via MapMethods mixin)
|
|
31
|
+
polygon.add_to_map(map, fill: [0, 200, 120, 170])
|
|
32
|
+
path.add_to_map(map, color: [255, 220, 50], width: 3)
|
|
33
|
+
coordinate.add_to_map(map, color: [255, 0, 0], label: "Marker")
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
The `add` method auto-detects the object type and dispatches to the appropriate handler. Supported types:
|
|
37
|
+
|
|
38
|
+
| Object | Layer type | Notes |
|
|
39
|
+
|--------|-----------|-------|
|
|
40
|
+
| Any coordinate (18 systems) | `:point` | Converts to LLA automatically |
|
|
41
|
+
| `Path` | `:line` | Renders all waypoints as a line |
|
|
42
|
+
| `Segment` | `:line` | Two-point line |
|
|
43
|
+
| `Polygon` (and subclasses) | `:polygon` | Includes Triangle, Rectangle, Hexagon, etc. |
|
|
44
|
+
| `Circle` | `:polygon` | Approximated as N-gon (default 32 segments) |
|
|
45
|
+
| `BoundingBox` | `:polygon` | Four corners as a rectangle |
|
|
46
|
+
| `Feature` | delegates | Extracts geometry; merges label into style |
|
|
47
|
+
| `ENU` / `NED` | rejected | Raises `ArgumentError` (relative systems) |
|
|
48
|
+
|
|
49
|
+
### Rendering
|
|
50
|
+
|
|
51
|
+
```ruby
|
|
52
|
+
# Simple render to file
|
|
53
|
+
map.render("output.png")
|
|
54
|
+
|
|
55
|
+
# Render with custom drawing block (LibGdGis-specific)
|
|
56
|
+
map.render("output.png") do |gd_map|
|
|
57
|
+
img = gd_map.image
|
|
58
|
+
# Use GD::Image primitives for custom markers, labels, arrows, etc.
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# Render without saving (returns GD::GIS::Map)
|
|
62
|
+
gd_map = map.render
|
|
63
|
+
```
|
|
19
64
|
|
|
20
|
-
|
|
65
|
+
### Bounding box
|
|
21
66
|
|
|
22
|
-
|
|
67
|
+
The LibGdGis adapter accepts bounding boxes as either:
|
|
68
|
+
|
|
69
|
+
```ruby
|
|
70
|
+
# Geodetic BoundingBox object
|
|
71
|
+
bbox = Geodetic::Areas::BoundingBox.new(nw: nw_point, se: se_point)
|
|
72
|
+
map = Geodetic::Map::LibGdGis.new(bbox: bbox, zoom: 12)
|
|
73
|
+
|
|
74
|
+
# Raw [west, south, east, north] array
|
|
75
|
+
map = Geodetic::Map::LibGdGis.new(bbox: [-74.05, 40.65, -73.75, 40.82], zoom: 12)
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## LibGdGis Adapter
|
|
79
|
+
|
|
80
|
+
### Color format
|
|
81
|
+
|
|
82
|
+
Colors are `[r, g, b]` or `[r, g, b, alpha]` arrays. The alpha channel follows libgd convention:
|
|
83
|
+
|
|
84
|
+
- `0` = fully opaque
|
|
85
|
+
- `255` = fully transparent
|
|
86
|
+
|
|
87
|
+
### Style options
|
|
88
|
+
|
|
89
|
+
| Option | Point | Line | Polygon |
|
|
90
|
+
|--------|:-----:|:----:|:-------:|
|
|
91
|
+
| `color` | marker color | stroke color | — |
|
|
92
|
+
| `stroke` | — | stroke color | outline color |
|
|
93
|
+
| `fill` | — | — | fill color |
|
|
94
|
+
| `width` | — | line width (px) | outline width (px) |
|
|
95
|
+
| `label` | text label | — | — |
|
|
96
|
+
| `icon` | image path | — | — |
|
|
97
|
+
| `font` | font path | — | — |
|
|
98
|
+
| `size` | font size | — | — |
|
|
99
|
+
| `font_color` | label color | — | — |
|
|
100
|
+
| `symbol` | marker style | — | — |
|
|
101
|
+
| `segments` | — | — | circle approximation (default 32) |
|
|
102
|
+
|
|
103
|
+
### Prerequisites
|
|
23
104
|
|
|
24
105
|
```bash
|
|
25
106
|
gem install libgd-gis
|
|
@@ -27,6 +108,11 @@ brew install gd # macOS
|
|
|
27
108
|
# apt install libgd-dev # Linux
|
|
28
109
|
```
|
|
29
110
|
|
|
30
|
-
##
|
|
111
|
+
## Feature Class
|
|
112
|
+
|
|
113
|
+
`Geodetic::Feature` wraps a geometry (any coordinate or area) with a label and a metadata hash. It delegates `distance_to` and `bearing_to` to its geometry, using the centroid for area geometries. When added to a map, the label is automatically included in the style.
|
|
114
|
+
|
|
115
|
+
## Examples
|
|
31
116
|
|
|
32
|
-
|
|
117
|
+
- [`examples/05_map_rendering/demo.rb`](https://github.com/madbomber/geodetic/tree/main/examples/05_map_rendering) — NYC landmarks with icons, Central Park polygon, bearing arrows, light/dark themes
|
|
118
|
+
- [`examples/14_geos_map_rendering.rb`](https://github.com/madbomber/geodetic/tree/main/examples/14_geos_map_rendering.rb) — GEOS operations (intersection, difference, buffer, convex hull, simplification, nearest points, prepared geometry) all visualized on a single map with distinct colors
|