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.0"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

._debug_fail_next_entries_alloc!Object



3977
3978
3979
3980
3981
# File 'ext/tg_geometry/tg_geometry_ext.c', line 3977

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



4001
4002
4003
4004
4005
# File 'ext/tg_geometry/tg_geometry_ext.c', line 4001

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



3983
3984
3985
3986
3987
# File 'ext/tg_geometry/tg_geometry_ext.c', line 3983

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



3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
# File 'ext/tg_geometry/tg_geometry_ext.c', line 3989

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



3969
3970
3971
3972
3973
3974
3975
# File 'ext/tg_geometry/tg_geometry_ext.c', line 3969

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 53

.empty_geometrycollectionObject



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

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

.empty_linestringObject



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

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

.empty_multilinestringObject



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

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

.empty_multipointObject



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

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

.empty_multipolygonObject



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

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

.empty_pointObject



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

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

.empty_polygonObject



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

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

.line_string(*args) ⇒ Object



1472
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
# File 'ext/tg_geometry/tg_geometry_ext.c', line 1472

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



1611
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
# File 'ext/tg_geometry/tg_geometry_ext.c', line 1611

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



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

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



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

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



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

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



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

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



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

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



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

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



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

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



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

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



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

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



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

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



1514
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
# File 'ext/tg_geometry/tg_geometry_ext.c', line 1514

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

#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 53

#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 53

#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 53

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

Returns planar Euclidean.

Returns:



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

#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