Module: TG::Geometry

Defined in:
lib/tg/geometry.rb,
lib/tg/geometry/version.rb,
lib/tg/geometry/registry.rb,
lib/tg/geometry/active_record_type.rb,
lib/tg/geometry/active_record_source.rb,
ext/tg_geometry/tg_geometry_ext.c

Overview

Fast immutable planar geometry engine with proper indexes and clean EWKB/PostGIS boundaries. Not a GIS. Not an rgeo replacement.

All distances and coordinates are in input units. SRID is metadata only; no reprojection is performed.

Defined Under Namespace

Modules: ActiveRecordSource, FeatureSource Classes: ActiveRecordType, ArgumentError, Error, FrozenIndexError, Geom, Index, Line, NearestSegment, ParseError, Polygon, Rect, Registry, Ring, Segment

Constant Summary collapse

VERSION =
"0.3.2"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

._debug_fail_next_entries_alloc!Object



4833
4834
4835
4836
4837
# File 'ext/tg_geometry/tg_geometry_ext.c', line 4833

static VALUE rb_tg_geometry_debug_fail_next_entries_alloc(VALUE self) {
    (void)self;
    tg_debug_fail_next_entries_alloc = true;
    return Qnil;
}

._debug_fail_next_match_buffer_alloc!Object



4857
4858
4859
4860
4861
# File 'ext/tg_geometry/tg_geometry_ext.c', line 4857

static VALUE rb_tg_geometry_debug_fail_next_match_buffer_alloc(VALUE self) {
    (void)self;
    tg_debug_fail_next_match_buffer_alloc = true;
    return Qnil;
}

._debug_fail_next_rtree_alloc!Object



4839
4840
4841
4842
4843
# File 'ext/tg_geometry/tg_geometry_ext.c', line 4839

static VALUE rb_tg_geometry_debug_fail_next_rtree_alloc(VALUE self) {
    (void)self;
    tg_debug_fail_rtree_alloc_countdown = 0;
    return Qnil;
}

._debug_fail_rtree_alloc_after!(count_value) ⇒ Object



4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
# File 'ext/tg_geometry/tg_geometry_ext.c', line 4845

static VALUE rb_tg_geometry_debug_fail_rtree_alloc_after(VALUE self, VALUE count_value) {
    long count;

    (void)self;
    count = NUM2LONG(count_value);
    if (count < 0) {
        rb_raise(eTGGeometryArgumentError, "count must be >= 0");
    }
    tg_debug_fail_rtree_alloc_countdown = count;
    return Qnil;
}

._debug_reset_test_hooks!Object



4825
4826
4827
4828
4829
4830
4831
# File 'ext/tg_geometry/tg_geometry_ext.c', line 4825

static VALUE rb_tg_geometry_debug_reset_test_hooks(VALUE self) {
    (void)self;
    tg_debug_fail_next_entries_alloc = false;
    tg_debug_fail_rtree_alloc_countdown = -1;
    tg_debug_fail_next_match_buffer_alloc = false;
    return Qnil;
}

.build(entries, via:, strategy:, predicate: :covers, geometry_index: :ystripes) ⇒ Object

predicate: affects only legacy point query methods: find_covering, covering_ids(x, y), covering_ids_batch_packed. The *_geom_ids methods use their own predicates derived from the method name.



# File 'lib/tg/geometry.rb', line 77

.empty_geometrycollectionObject



1273
1274
1275
1276
# File 'ext/tg_geometry/tg_geometry_ext.c', line 1273

static VALUE rb_tg_geometry_empty_geometrycollection(VALUE self) {
    (void)self;
    return wrap_constructed_geom(tg_geom_new_geometrycollection_empty());
}

.empty_linestringObject



1248
1249
1250
1251
# File 'ext/tg_geometry/tg_geometry_ext.c', line 1248

static VALUE rb_tg_geometry_empty_linestring(VALUE self) {
    (void)self;
    return wrap_constructed_geom(tg_geom_new_linestring_empty());
}

.empty_multilinestringObject



1263
1264
1265
1266
# File 'ext/tg_geometry/tg_geometry_ext.c', line 1263

static VALUE rb_tg_geometry_empty_multilinestring(VALUE self) {
    (void)self;
    return wrap_constructed_geom(tg_geom_new_multilinestring_empty());
}

.empty_multipointObject



1258
1259
1260
1261
# File 'ext/tg_geometry/tg_geometry_ext.c', line 1258

static VALUE rb_tg_geometry_empty_multipoint(VALUE self) {
    (void)self;
    return wrap_constructed_geom(tg_geom_new_multipoint_empty());
}

.empty_multipolygonObject



1268
1269
1270
1271
# File 'ext/tg_geometry/tg_geometry_ext.c', line 1268

static VALUE rb_tg_geometry_empty_multipolygon(VALUE self) {
    (void)self;
    return wrap_constructed_geom(tg_geom_new_multipolygon_empty());
}

.empty_pointObject



1243
1244
1245
1246
# File 'ext/tg_geometry/tg_geometry_ext.c', line 1243

static VALUE rb_tg_geometry_empty_point(VALUE self) {
    (void)self;
    return wrap_constructed_geom(tg_geom_new_point_empty());
}

.empty_polygonObject



1253
1254
1255
1256
# File 'ext/tg_geometry/tg_geometry_ext.c', line 1253

static VALUE rb_tg_geometry_empty_polygon(VALUE self) {
    (void)self;
    return wrap_constructed_geom(tg_geom_new_polygon_empty());
}

.line_string(*args) ⇒ Object



1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
# File 'ext/tg_geometry/tg_geometry_ext.c', line 1473

static VALUE rb_tg_geometry_line_string(int argc, VALUE *argv, VALUE self) {
    VALUE points_value;
    VALUE kwargs;
    VALUE index_value;
    VALUE srid_value;
    enum tg_index index;
    bool has_srid;
    int srid;
    struct tg_point *points;
    long len;
    struct tg_line *line;
    struct tg_geom *geom;

    (void)self;
    rb_scan_args(argc, argv, "1:", &points_value, &kwargs);
    {
        ID allowed[] = {id_index, id_srid};
        validate_keywords(kwargs, allowed, sizeof(allowed) / sizeof(allowed[0]));
    }

    index_value = kwargs_value(kwargs, id_index, ID2SYM(id_natural));
    srid_value = kwargs_value(kwargs, id_srid, Qnil);
    index = parse_index_symbol(index_value);
    has_srid = parse_srid_option(srid_value, &srid);

    points = parse_points_array(points_value, &len, "line_string");
    if (len < 2) {
        ruby_xfree(points);
        rb_raise(eTGGeometryArgumentError, "line_string requires at least 2 points, got %ld", len);
    }

    line = tg_line_new_ix(points, (int)len, index);
    ruby_xfree(points);
    if (!line) {
        rb_raise(rb_eNoMemError, "TG line allocation failed");
    }

    geom = tg_geom_new_linestring(line);
    tg_line_free(line);
    return wrap_constructed_geom_with_srid(geom, has_srid, srid);
}

.multi_polygon(*args) ⇒ Object



1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
# File 'ext/tg_geometry/tg_geometry_ext.c', line 1612

static VALUE rb_tg_geometry_multi_polygon(int argc, VALUE *argv, VALUE self) {
    VALUE polygons_value;
    VALUE kwargs;
    VALUE index_value;
    VALUE srid_value;
    enum tg_index index;
    bool has_srid;
    int srid;
    long npolys;
    tg_build_multipolygon_args_t args;
    int state = 0;

    (void)self;
    rb_scan_args(argc, argv, "1:", &polygons_value, &kwargs);
    {
        ID allowed[] = {id_index, id_srid};
        validate_keywords(kwargs, allowed, sizeof(allowed) / sizeof(allowed[0]));
    }

    if (!RB_TYPE_P(polygons_value, T_ARRAY)) {
        rb_raise(rb_eTypeError, "polygons must be an Array");
    }

    index_value = kwargs_value(kwargs, id_index, ID2SYM(id_ystripes));
    srid_value = kwargs_value(kwargs, id_srid, Qnil);
    index = parse_index_symbol(index_value);
    has_srid = parse_srid_option(srid_value, &srid);

    npolys = RARRAY_LEN(polygons_value);
    if (npolys > INT_MAX) {
        rb_raise(eTGGeometryArgumentError, "multi_polygon has too many polygons");
    }

    if (npolys == 0) {
        return wrap_constructed_geom_with_srid(tg_geom_new_multipolygon_empty(), has_srid, srid);
    }

    args.polygons_value = polygons_value;
    args.index = index;
    args.npolys = npolys;
    args.polys = NULL;
    args.built = 0;
    args.geom = NULL;

    rb_protect(build_multipolygon_body, (VALUE)&args, &state);
    build_multipolygon_cleanup(&args);

    if (state) {
        rb_jump_tag(state);
    }

    RB_GC_GUARD(polygons_value);
    return wrap_constructed_geom_with_srid(args.geom, has_srid, srid);
}

.parse(*args) ⇒ Object



1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
# File 'ext/tg_geometry/tg_geometry_ext.c', line 1021

static VALUE rb_tg_geometry_parse(int argc, VALUE *argv, VALUE self) {
    VALUE input;
    VALUE kwargs;
    VALUE format_value;
    VALUE index_value;
    enum tg_geometry_parse_format format;
    enum tg_index index;

    (void)self;
    rb_scan_args(argc, argv, "1:", &input, &kwargs);
    {
        ID allowed[] = {id_format, id_index};
        validate_keywords(kwargs, allowed, sizeof(allowed) / sizeof(allowed[0]));
    }

    format_value = kwargs_value(kwargs, id_format, ID2SYM(id_auto));
    index_value = kwargs_value(kwargs, id_index, ID2SYM(id_ystripes));
    format = parse_format_symbol(format_value);
    index = parse_index_symbol(index_value);

    return parse_string_with_format(input, format, index);
}

.parse_geobin(*args) ⇒ Object



1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
# File 'ext/tg_geometry/tg_geometry_ext.c', line 1120

static VALUE rb_tg_geometry_parse_geobin(int argc, VALUE *argv, VALUE self) {
    VALUE input;
    VALUE kwargs;
    VALUE index_value;
    enum tg_index index;

    (void)self;
    rb_scan_args(argc, argv, "1:", &input, &kwargs);
    {
        ID allowed[] = {id_index};
        validate_keywords(kwargs, allowed, sizeof(allowed) / sizeof(allowed[0]));
    }

    index_value = kwargs_value(kwargs, id_index, ID2SYM(id_ystripes));
    index = parse_index_symbol(index_value);

    return parse_string_with_format(input, TG_GEOMETRY_FORMAT_GEOBIN, index);
}

.parse_geojson(*args) ⇒ Object



1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
# File 'ext/tg_geometry/tg_geometry_ext.c', line 1044

static VALUE rb_tg_geometry_parse_geojson(int argc, VALUE *argv, VALUE self) {
    VALUE input;
    VALUE kwargs;
    VALUE index_value;
    enum tg_index index;

    (void)self;
    rb_scan_args(argc, argv, "1:", &input, &kwargs);
    {
        ID allowed[] = {id_index};
        validate_keywords(kwargs, allowed, sizeof(allowed) / sizeof(allowed[0]));
    }

    index_value = kwargs_value(kwargs, id_index, ID2SYM(id_ystripes));
    index = parse_index_symbol(index_value);

    return parse_string_with_format(input, TG_GEOMETRY_FORMAT_GEOJSON, index);
}

.parse_hex(*args) ⇒ Object



1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
# File 'ext/tg_geometry/tg_geometry_ext.c', line 1101

static VALUE rb_tg_geometry_parse_hex(int argc, VALUE *argv, VALUE self) {
    VALUE input;
    VALUE kwargs;
    VALUE index_value;
    enum tg_index index;

    (void)self;
    rb_scan_args(argc, argv, "1:", &input, &kwargs);
    {
        ID allowed[] = {id_index};
        validate_keywords(kwargs, allowed, sizeof(allowed) / sizeof(allowed[0]));
    }

    index_value = kwargs_value(kwargs, id_index, ID2SYM(id_ystripes));
    index = parse_index_symbol(index_value);

    return parse_string_with_format(input, TG_GEOMETRY_FORMAT_HEX, index);
}

.parse_wkb(*args) ⇒ Object



1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
# File 'ext/tg_geometry/tg_geometry_ext.c', line 1082

static VALUE rb_tg_geometry_parse_wkb(int argc, VALUE *argv, VALUE self) {
    VALUE input;
    VALUE kwargs;
    VALUE index_value;
    enum tg_index index;

    (void)self;
    rb_scan_args(argc, argv, "1:", &input, &kwargs);
    {
        ID allowed[] = {id_index};
        validate_keywords(kwargs, allowed, sizeof(allowed) / sizeof(allowed[0]));
    }

    index_value = kwargs_value(kwargs, id_index, ID2SYM(id_ystripes));
    index = parse_index_symbol(index_value);

    return parse_string_with_format(input, TG_GEOMETRY_FORMAT_WKB, index);
}

.parse_wkt(*args) ⇒ Object



1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
# File 'ext/tg_geometry/tg_geometry_ext.c', line 1063

static VALUE rb_tg_geometry_parse_wkt(int argc, VALUE *argv, VALUE self) {
    VALUE input;
    VALUE kwargs;
    VALUE index_value;
    enum tg_index index;

    (void)self;
    rb_scan_args(argc, argv, "1:", &input, &kwargs);
    {
        ID allowed[] = {id_index};
        validate_keywords(kwargs, allowed, sizeof(allowed) / sizeof(allowed[0]));
    }

    index_value = kwargs_value(kwargs, id_index, ID2SYM(id_ystripes));
    index = parse_index_symbol(index_value);

    return parse_string_with_format(input, TG_GEOMETRY_FORMAT_WKT, index);
}

.point(x_value, y_value) ⇒ Object



1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
# File 'ext/tg_geometry/tg_geometry_ext.c', line 1178

static VALUE rb_tg_geometry_point(VALUE self, VALUE x_value, VALUE y_value) {
    double x = NUM2DBL(x_value);
    double y = NUM2DBL(y_value);
    struct tg_point point;

    (void)self;
    check_finite_double(x, "x");
    check_finite_double(y, "y");

    point.x = x;
    point.y = y;
    return wrap_constructed_geom(tg_geom_new_point(point));
}

.point_m(x_value, y_value, m_value) ⇒ Object



1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
# File 'ext/tg_geometry/tg_geometry_ext.c', line 1208

static VALUE rb_tg_geometry_point_m(VALUE self, VALUE x_value, VALUE y_value, VALUE m_value) {
    double x = NUM2DBL(x_value);
    double y = NUM2DBL(y_value);
    double m = NUM2DBL(m_value);
    struct tg_point point;

    (void)self;
    check_finite_double(x, "x");
    check_finite_double(y, "y");
    check_finite_double(m, "m");

    point.x = x;
    point.y = y;
    return wrap_constructed_geom(tg_geom_new_point_m(point, m));
}

.point_z(x_value, y_value, z_value) ⇒ Object



1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
# File 'ext/tg_geometry/tg_geometry_ext.c', line 1192

static VALUE rb_tg_geometry_point_z(VALUE self, VALUE x_value, VALUE y_value, VALUE z_value) {
    double x = NUM2DBL(x_value);
    double y = NUM2DBL(y_value);
    double z = NUM2DBL(z_value);
    struct tg_point point;

    (void)self;
    check_finite_double(x, "x");
    check_finite_double(y, "y");
    check_finite_double(z, "z");

    point.x = x;
    point.y = y;
    return wrap_constructed_geom(tg_geom_new_point_z(point, z));
}

.point_zm(x_value, y_value, z_value, m_value) ⇒ Object



1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
# File 'ext/tg_geometry/tg_geometry_ext.c', line 1224

static VALUE rb_tg_geometry_point_zm(VALUE self, VALUE x_value, VALUE y_value, VALUE z_value,
                                     VALUE m_value) {
    double x = NUM2DBL(x_value);
    double y = NUM2DBL(y_value);
    double z = NUM2DBL(z_value);
    double m = NUM2DBL(m_value);
    struct tg_point point;

    (void)self;
    check_finite_double(x, "x");
    check_finite_double(y, "y");
    check_finite_double(z, "z");
    check_finite_double(m, "m");

    point.x = x;
    point.y = y;
    return wrap_constructed_geom(tg_geom_new_point_zm(point, z, m));
}

.polygon(*args) ⇒ Object



1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
# File 'ext/tg_geometry/tg_geometry_ext.c', line 1515

static VALUE rb_tg_geometry_polygon(int argc, VALUE *argv, VALUE self) {
    VALUE exterior_value;
    VALUE kwargs;
    VALUE holes_value;
    VALUE index_value;
    VALUE srid_value;
    enum tg_index index;
    bool has_srid;
    int srid;
    struct tg_poly *poly;
    struct tg_geom *geom;

    (void)self;
    rb_scan_args(argc, argv, "1:", &exterior_value, &kwargs);
    {
        ID allowed[] = {id_holes, id_index, id_srid};
        validate_keywords(kwargs, allowed, sizeof(allowed) / sizeof(allowed[0]));
    }

    holes_value = kwargs_value(kwargs, id_holes, Qnil);
    index_value = kwargs_value(kwargs, id_index, ID2SYM(id_ystripes));
    srid_value = kwargs_value(kwargs, id_srid, Qnil);
    index = parse_index_symbol(index_value);
    has_srid = parse_srid_option(srid_value, &srid);

    poly = build_poly_from_ruby(exterior_value, holes_value, index);
    geom = tg_geom_new_polygon(poly);
    tg_poly_free(poly);

    return wrap_constructed_geom_with_srid(geom, has_srid, srid);
}

Instance Method Details

#boundary_distance_to_lnglat_meters(lng, lat) ⇒ Float

Approximate meters to nearest boundary/segment/point.

Returns:

  • (Float)


# File 'lib/tg/geometry.rb', line 43

#boundary_distance_to_xy(x, y) ⇒ Float

Planar boundary distance in input coordinate units.

Returns:

  • (Float)


# File 'lib/tg/geometry.rb', line 43

#containing_geom_ids(geom) ⇒ Array<Object>

Stored geometries for which tg_geom_contains(stored, query) is true. Direction: stored contains query. Boundary points are not contained.

Returns:

  • (Array<Object>)

    ids in insertion order



# File 'lib/tg/geometry.rb', line 77

#covering_geom_ids(geom) ⇒ Array<Object>

Stored geometries for which tg_geom_covers(stored, query) is true. Direction: stored covers query. Boundary points are covered.

Returns:

  • (Array<Object>)

    ids in insertion order



# File 'lib/tg/geometry.rb', line 77

#distance_to_lnglat_meters(lng, lat) ⇒ Float

Approximate meters in a query-local equirectangular frame. Not geodesy.

Returns:

  • (Float)


# File 'lib/tg/geometry.rb', line 43

#distance_to_xy(x, y) ⇒ Float

Planar distance in input coordinate units.

Returns:

  • (Float)


# File 'lib/tg/geometry.rb', line 43

#intersecting_geom_ids(geom) ⇒ Array<Object>

Stored geometries for which tg_geom_intersects(stored, query) is true.

Returns:

  • (Array<Object>)

    ids in insertion order



# File 'lib/tg/geometry.rb', line 77

#nearest_point_lnglat(lng, lat) ⇒ Array(Float, Float)

Raw planar nearest boundary/geometry point. Longitude is not wrapped.

Returns:

  • (Array(Float, Float))


# File 'lib/tg/geometry.rb', line 43

#nearest_point_xy(x, y) ⇒ Array(Float, Float)

Planar nearest boundary/geometry point in input coordinate units.

Returns:

  • (Array(Float, Float))


# File 'lib/tg/geometry.rb', line 43

#nearest_segment(x, y) ⇒ TG::Geometry::NearestSegment?

Returns planar Euclidean.

Returns:



# File 'lib/tg/geometry.rb', line 114

#sridInteger?

Returns SRID metadata; not used for reprojection.

Returns:

  • (Integer, nil)

    SRID metadata; not used for reprojection



# File 'lib/tg/geometry.rb', line 43

#to_ewkb(srid: nil) ⇒ String

Writes EWKB with the SRID flag set. Uses explicit srid: when provided, otherwise Geom#srid. Raises if no SRID is available. to_wkb remains plain.

Returns:

  • (String)

    frozen ASCII-8BIT EWKB string



# File 'lib/tg/geometry.rb', line 43

#within_distance_ids_lnglat_meters(lng, lat, radius_m) ⇒ Array<Object>

Same membership as within_distance_lnglat_meters, ids only.

Returns:

  • (Array<Object>)


# File 'lib/tg/geometry.rb', line 77

#within_distance_ids_xy(x, y, radius) ⇒ Array<Object>

Same membership as within_distance_xy, ids only.

Returns:

  • (Array<Object>)


# File 'lib/tg/geometry.rb', line 77

#within_distance_lnglat_meters(lng, lat, radius_m, sort: false) ⇒ Array<Array(Object, Float)>

Rtree bbox prefilter plus exact approximate-meter distance filter.

Returns:

  • (Array<Array(Object, Float)>)


# File 'lib/tg/geometry.rb', line 77

#within_distance_xy(x, y, radius, sort: false) ⇒ Array<Array(Object, Float)>

Rtree bbox prefilter plus exact planar distance filter.

Returns:

  • (Array<Array(Object, Float)>)


# File 'lib/tg/geometry.rb', line 77