Class: TG::Geometry::Index

Inherits:
Object
  • Object
show all
Defined in:
ext/tg_geometry/tg_geometry_ext.c

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build(*args) ⇒ Object



2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
# File 'ext/tg_geometry/tg_geometry_ext.c', line 2590

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);

    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



2865
2866
2867
2868
# File 'ext/tg_geometry/tg_geometry_ext.c', line 2865

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



2880
2881
2882
2883
2884
# File 'ext/tg_geometry/tg_geometry_ext.c', line 2880

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



2875
2876
2877
2878
# File 'ext/tg_geometry/tg_geometry_ext.c', line 2875

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



2870
2871
2872
2873
# File 'ext/tg_geometry/tg_geometry_ext.c', line 2870

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



2860
2861
2862
2863
# File 'ext/tg_geometry/tg_geometry_ext.c', line 2860

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



2713
2714
2715
2716
2717
2718
2719
2720
# File 'ext/tg_geometry/tg_geometry_ext.c', line 2713

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);
}

#covering_ids(lon_value, lat_value) ⇒ Object



2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
# File 'ext/tg_geometry/tg_geometry_ext.c', line 2742

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



2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
# File 'ext/tg_geometry/tg_geometry_ext.c', line 2779

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



2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
# File 'ext/tg_geometry/tg_geometry_ext.c', line 2729

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_rect(min_x_value, min_y_value, max_x_value, max_y_value) ⇒ Object



2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
# File 'ext/tg_geometry/tg_geometry_ext.c', line 2757

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



2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
# File 'ext/tg_geometry/tg_geometry_ext.c', line 2700

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



2682
2683
2684
2685
# File 'ext/tg_geometry/tg_geometry_ext.c', line 2682

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

#strategyObject



2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
# File 'ext/tg_geometry/tg_geometry_ext.c', line 2687

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;
}