Class: TG::Geometry::Index

Inherits:
Object
  • Object
show all
Defined in:
lib/tg/geometry.rb,
ext/tg_geometry/tg_geometry_ext.c

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build(*args) ⇒ Object



4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
# File 'ext/tg_geometry/tg_geometry_ext.c', line 4589

static VALUE rb_tg_geometry_index_build(int argc, VALUE *argv, VALUE klass) {
    VALUE entries_value;
    VALUE kwargs;
    VALUE via_value;
    VALUE strategy_value;
    VALUE predicate_value;
    VALUE geometry_index_value;
    enum tg_geometry_index_via via;
    enum tg_geometry_index_strategy strategy;
    enum tg_geometry_index_predicate predicate;
    enum tg_index geometry_index;
    long len;
    tg_index_t *idx;
    VALUE wrapper;
    tg_index_build_args_t args;
    int state = 0;

    rb_scan_args(argc, argv, "1:", &entries_value, &kwargs);
    {
        ID allowed[] = {id_via, id_strategy, id_predicate, id_geometry_index};
        validate_keywords(kwargs, allowed, sizeof(allowed) / sizeof(allowed[0]));
    }

    if (!RB_TYPE_P(entries_value, T_ARRAY)) {
        rb_raise(rb_eTypeError, "entries must be Array");
    }

    via_value = required_kwargs_value(kwargs, id_via, "via:");
    strategy_value = required_kwargs_value(kwargs, id_strategy, "strategy:");
    predicate_value = kwargs_value(kwargs, id_predicate, ID2SYM(id_covers));
    geometry_index_value = kwargs_value(kwargs, id_geometry_index, ID2SYM(id_ystripes));

    via = parse_index_via_symbol(via_value);
    strategy = parse_index_strategy_symbol(strategy_value);
    predicate = parse_index_predicate_symbol(predicate_value);
    geometry_index = parse_index_symbol(geometry_index_value);

    len = RARRAY_LEN(entries_value);
    wrapper = TypedData_Make_Struct(klass, tg_index_t, &tg_index_type, idx);
    idx->len = len;
    idx->capacity = len;
    idx->initialized = 0;
    idx->strategy = strategy;
    idx->predicate = predicate;
    idx->rtree = NULL;
    idx->frozen = false;
    idx->has_bbox = false;

    if (len > 0) {
        if ((size_t)len > SIZE_MAX / sizeof(tg_index_entry_t)) {
            rb_raise(rb_eNoMemError, "entries allocation size overflow");
        }

#ifdef TG_DEBUG_TEST
        if (tg_debug_fail_next_entries_alloc) {
            tg_debug_fail_next_entries_alloc = false;
            rb_raise(rb_eNoMemError, "entries allocation failed");
        }
#endif

        idx->entries = calloc((size_t)len, sizeof(tg_index_entry_t));
        if (!idx->entries) {
            rb_raise(rb_eNoMemError, "entries allocation failed");
        }

        idx->entries_bytes = (size_t)len * sizeof(tg_index_entry_t);
        rb_gc_adjust_memory_usage((ssize_t)idx->entries_bytes);
    }

    args.idx = idx;
    args.entries = entries_value;
    args.via = via;
    args.geometry_index = geometry_index;

    rb_protect(index_build_body, (VALUE)&args, &state);

    if (state) {
        index_dispose(idx);
        RB_GC_GUARD(entries_value);
        RB_GC_GUARD(wrapper);
        rb_jump_tag(state);
    }

    if (idx->initialized != idx->len) {
        index_dispose(idx);
        rb_raise(eTGGeometryError, "internal index build initialization mismatch");
    }

    idx->frozen = true;
    rb_obj_freeze(wrapper);

    RB_GC_GUARD(entries_value);
    RB_GC_GUARD(wrapper);
    return wrapper;
}

Instance Method Details

#_entries_bytes_for_testObject



4868
4869
4870
4871
# File 'ext/tg_geometry/tg_geometry_ext.c', line 4868

static VALUE rb_tg_geometry_index_entries_bytes_for_test(VALUE self) {
    tg_index_t *idx = get_index_wrapper(self);
    return ULL2NUM((unsigned long long)idx->entries_bytes);
}

#_force_dispose_for_test!Object



4883
4884
4885
4886
4887
# File 'ext/tg_geometry/tg_geometry_ext.c', line 4883

static VALUE rb_tg_geometry_index_force_dispose_for_test(VALUE self) {
    tg_index_t *idx = get_index_wrapper(self);
    index_dispose(idx);
    return Qnil;
}

#_initialized_entries_for_testObject



4878
4879
4880
4881
# File 'ext/tg_geometry/tg_geometry_ext.c', line 4878

static VALUE rb_tg_geometry_index_initialized_entries_for_test(VALUE self) {
    tg_index_t *idx = get_index_wrapper(self);
    return LONG2NUM(idx->initialized);
}

#_owned_geom_bytes_for_testObject



4873
4874
4875
4876
# File 'ext/tg_geometry/tg_geometry_ext.c', line 4873

static VALUE rb_tg_geometry_index_owned_geom_bytes_for_test(VALUE self) {
    tg_index_t *idx = get_index_wrapper(self);
    return ULL2NUM((unsigned long long)idx->owned_geom_bytes_total);
}

#_rtree_bytes_for_testObject



4863
4864
4865
4866
# File 'ext/tg_geometry/tg_geometry_ext.c', line 4863

static VALUE rb_tg_geometry_index_rtree_bytes_for_test(VALUE self) {
    tg_index_t *idx = get_index_wrapper(self);
    return ULL2NUM((unsigned long long)idx->rtree_bytes);
}

#bboxObject



4716
4717
4718
4719
4720
4721
4722
4723
# File 'ext/tg_geometry/tg_geometry_ext.c', line 4716

static VALUE rb_tg_geometry_index_bbox(VALUE self) {
    tg_index_t *idx = get_index_wrapper(self);

    if (!idx->has_bbox)
        return Qnil;

    return rect_from_tg_rect(idx->bbox);
}

#containing_geom_ids(geom) ⇒ Object



4118
4119
4120
# File 'ext/tg_geometry/tg_geometry_ext.c', line 4118

static VALUE rb_tg_geometry_index_containing_geom_ids(VALUE self, VALUE geom) {
    return index_geom_query_ids(self, geom, TG_GEOMETRY_GEOM_QUERY_CONTAINS);
}

#covering_geom_ids(geom) ⇒ Object



4114
4115
4116
# File 'ext/tg_geometry/tg_geometry_ext.c', line 4114

static VALUE rb_tg_geometry_index_covering_geom_ids(VALUE self, VALUE geom) {
    return index_geom_query_ids(self, geom, TG_GEOMETRY_GEOM_QUERY_COVERS);
}

#covering_ids(lon_value, lat_value) ⇒ Object



4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
# File 'ext/tg_geometry/tg_geometry_ext.c', line 4745

static VALUE rb_tg_geometry_index_covering_ids(VALUE self, VALUE lon_value, VALUE lat_value) {
    tg_index_t *idx = get_index_wrapper(self);
    double lon;
    double lat;
    unsigned char *marks;
    VALUE result;

    parse_public_point_args(lon_value, lat_value, &lon, &lat);
    marks = index_covering_marks(idx, lon, lat);
    result = build_ids_from_marks_protected(idx, marks);

    RB_GC_GUARD(self);
    return result;
}

#covering_ids_batch_packed(input) ⇒ Object



4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
# File 'ext/tg_geometry/tg_geometry_ext.c', line 4782

static VALUE rb_tg_geometry_index_covering_ids_batch_packed(VALUE self, VALUE input) {
    tg_index_t *idx = get_index_wrapper(self);
    const char *data;
    long byte_len;
    long count;
    VALUE result;

    if (!RB_TYPE_P(input, T_STRING)) {
        rb_raise(rb_eTypeError, "packed input must be String");
    }

    byte_len = RSTRING_LEN(input);
    if (byte_len % (long)(2 * sizeof(double)) != 0) {
        rb_raise(eTGGeometryArgumentError, "packed input length must be multiple of 16 bytes");
    }

    count = byte_len / (long)(2 * sizeof(double));
    result = rb_ary_new_capa(count);
    data = RSTRING_PTR(input);

    for (long i = 0; i < count; i++) {
        double lon;
        double lat;
        VALUE id;

        memcpy(&lon, data + (i * (long)(2 * sizeof(double))), sizeof(double));
        memcpy(&lat, data + (i * (long)(2 * sizeof(double))) + (long)sizeof(double),
               sizeof(double));

        check_finite_double(lon, "lon");
        check_finite_double(lat, "lat");

        id = index_find_covering_value(idx, lon, lat);
        rb_ary_push(result, id);
    }

    RB_GC_GUARD(input);
    RB_GC_GUARD(self);
    RB_GC_GUARD(result);
    return result;
}

#find_covering(lon_value, lat_value) ⇒ Object



4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
# File 'ext/tg_geometry/tg_geometry_ext.c', line 4732

static VALUE rb_tg_geometry_index_find_covering(VALUE self, VALUE lon_value, VALUE lat_value) {
    tg_index_t *idx = get_index_wrapper(self);
    double lon;
    double lat;
    VALUE result;

    parse_public_point_args(lon_value, lat_value, &lon, &lat);
    result = index_find_covering_value(idx, lon, lat);

    RB_GC_GUARD(self);
    return result;
}

#intersecting_geom_ids(geom) ⇒ Object



4110
4111
4112
# File 'ext/tg_geometry/tg_geometry_ext.c', line 4110

static VALUE rb_tg_geometry_index_intersecting_geom_ids(VALUE self, VALUE geom) {
    return index_geom_query_ids(self, geom, TG_GEOMETRY_GEOM_QUERY_INTERSECTS);
}

#intersecting_rect(min_x_value, min_y_value, max_x_value, max_y_value) ⇒ Object



4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
# File 'ext/tg_geometry/tg_geometry_ext.c', line 4760

static VALUE rb_tg_geometry_index_intersecting_rect(VALUE self, VALUE min_x_value,
                                                    VALUE min_y_value, VALUE max_x_value,
                                                    VALUE max_y_value) {
    tg_index_t *idx = get_index_wrapper(self);
    double min_x = NUM2DBL(min_x_value);
    double min_y = NUM2DBL(min_y_value);
    double max_x = NUM2DBL(max_x_value);
    double max_y = NUM2DBL(max_y_value);
    struct tg_rect query_rect;
    unsigned char *marks;
    VALUE result;

    validate_rect_coordinates(min_x, min_y, max_x, max_y);
    query_rect = tg_rect_from_xyxy(min_x, min_y, max_x, max_y);

    marks = index_intersecting_rect_marks(idx, query_rect);
    result = build_ids_from_marks_protected(idx, marks);

    RB_GC_GUARD(self);
    return result;
}

#predicateObject



4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
# File 'ext/tg_geometry/tg_geometry_ext.c', line 4703

static VALUE rb_tg_geometry_index_predicate(VALUE self) {
    tg_index_t *idx = get_index_wrapper(self);

    switch (idx->predicate) {
    case TG_GEOMETRY_INDEX_PREDICATE_COVERS:
        return ID2SYM(id_covers);
    case TG_GEOMETRY_INDEX_PREDICATE_CONTAINS:
        return ID2SYM(id_contains);
    }

    return Qnil;
}

#sizeObject



4685
4686
4687
4688
# File 'ext/tg_geometry/tg_geometry_ext.c', line 4685

static VALUE rb_tg_geometry_index_size(VALUE self) {
    tg_index_t *idx = get_index_wrapper(self);
    return LONG2NUM(idx->len);
}

#strategyObject



4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
# File 'ext/tg_geometry/tg_geometry_ext.c', line 4690

static VALUE rb_tg_geometry_index_strategy(VALUE self) {
    tg_index_t *idx = get_index_wrapper(self);

    switch (idx->strategy) {
    case TG_GEOMETRY_INDEX_STRATEGY_FLAT:
        return ID2SYM(id_flat);
    case TG_GEOMETRY_INDEX_STRATEGY_RTREE:
        return ID2SYM(id_rtree);
    }

    return Qnil;
}

#within_distance_ids_lnglat_meters(*args) ⇒ Object



4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
# File 'ext/tg_geometry/tg_geometry_ext.c', line 4387

static VALUE rb_tg_geometry_index_within_distance_ids_lnglat_meters(int argc, VALUE *argv,
                                                                    VALUE self) {
    tg_index_t *idx = get_index_wrapper(self);
    VALUE lng_value, lat_value, radius_value;
    tg_distance_metric_t metric;
    struct tg_rect query_rect;
    double lng;
    double lat;
    double radius;

    tg_distance_parse_no_keywords(argc, argv, &lng_value, &lat_value, &radius_value);
    lng = tg_distance_lng_value(lng_value);
    lat = tg_distance_lat_value(lat_value);
    radius = tg_distance_radius_value(radius_value, "radius_m");
    tg_distance_metric_lnglat(&metric, lng, lat);
    query_rect = tg_distance_lnglat_prefilter_rect(lng, lat, radius);

    RB_GC_GUARD(self);
    return tg_index_within_distance_common(idx, query_rect, &metric, lng, lat, radius, false, true);
}

#within_distance_ids_xy(*args) ⇒ Object



4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
# File 'ext/tg_geometry/tg_geometry_ext.c', line 4347

static VALUE rb_tg_geometry_index_within_distance_ids_xy(int argc, VALUE *argv, VALUE self) {
    tg_index_t *idx = get_index_wrapper(self);
    VALUE x_value, y_value, radius_value;
    tg_distance_metric_t metric;
    struct tg_rect query_rect;
    double x;
    double y;
    double radius;

    tg_distance_parse_no_keywords(argc, argv, &x_value, &y_value, &radius_value);
    x = tg_coerce_finite_double(x_value, "x");
    y = tg_coerce_finite_double(y_value, "y");
    radius = tg_distance_radius_value(radius_value, "radius");
    tg_distance_metric_xy(&metric, x, y);
    query_rect = tg_rect_from_xyxy(x - radius, y - radius, x + radius, y + radius);

    RB_GC_GUARD(self);
    return tg_index_within_distance_common(idx, query_rect, &metric, x, y, radius, false, true);
}

#within_distance_lnglat_meters(*args) ⇒ Object



4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
# File 'ext/tg_geometry/tg_geometry_ext.c', line 4367

static VALUE rb_tg_geometry_index_within_distance_lnglat_meters(int argc, VALUE *argv, VALUE self) {
    tg_index_t *idx = get_index_wrapper(self);
    VALUE lng_value, lat_value, radius_value;
    tg_distance_metric_t metric;
    struct tg_rect query_rect;
    double lng;
    double lat;
    double radius;
    bool sort = tg_distance_parse_sort_kw(argc, argv, &lng_value, &lat_value, &radius_value);

    lng = tg_distance_lng_value(lng_value);
    lat = tg_distance_lat_value(lat_value);
    radius = tg_distance_radius_value(radius_value, "radius_m");
    tg_distance_metric_lnglat(&metric, lng, lat);
    query_rect = tg_distance_lnglat_prefilter_rect(lng, lat, radius);

    RB_GC_GUARD(self);
    return tg_index_within_distance_common(idx, query_rect, &metric, lng, lat, radius, sort, false);
}

#within_distance_xy(*args) ⇒ Object



4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
# File 'ext/tg_geometry/tg_geometry_ext.c', line 4327

static VALUE rb_tg_geometry_index_within_distance_xy(int argc, VALUE *argv, VALUE self) {
    tg_index_t *idx = get_index_wrapper(self);
    VALUE x_value, y_value, radius_value;
    tg_distance_metric_t metric;
    struct tg_rect query_rect;
    double x;
    double y;
    double radius;
    bool sort = tg_distance_parse_sort_kw(argc, argv, &x_value, &y_value, &radius_value);

    x = tg_coerce_finite_double(x_value, "x");
    y = tg_coerce_finite_double(y_value, "y");
    radius = tg_distance_radius_value(radius_value, "radius");
    tg_distance_metric_xy(&metric, x, y);
    query_rect = tg_rect_from_xyxy(x - radius, y - radius, x + radius, y + radius);

    RB_GC_GUARD(self);
    return tg_index_within_distance_common(idx, query_rect, &metric, x, y, radius, sort, false);
}