9 static tilelist_t tilelist_for_area(unsigned int minx, unsigned int miny, unsigned int maxx, unsigned int maxy)
14 unsigned int maxtilec;
18 tl.tilev = malloc(maxtilec * sizeof(unsigned int));
21 for (x = minx; x <= maxx; x++)
23 for (y = miny; y <= maxy; y++)
25 if (tl.tilec == maxtilec)
27 maxtilec = maxtilec * 2;
29 tl.tilev = realloc(tl.tilev, maxtilec * sizeof(unsigned int));
32 tl.tilev[tl.tilec++] = xy2tile(x, y);
39 static int tile_compare(const void *ap, const void *bp)
41 unsigned int a = *(unsigned int *)ap;
42 unsigned int b = *(unsigned int *)bp;
58 static VALUE tile_for_point(VALUE self, VALUE lat, VALUE lon)
60 unsigned int x = lon2x(NUM2DBL(lon));
61 unsigned int y = lat2y(NUM2DBL(lat));
63 return UINT2NUM(xy2tile(x, y));
66 static VALUE tiles_for_area(VALUE self, VALUE bbox)
68 unsigned int minx = lon2x(NUM2DBL(rb_iv_get(bbox, "@min_lon")));
69 unsigned int maxx = lon2x(NUM2DBL(rb_iv_get(bbox, "@max_lon")));
70 unsigned int miny = lat2y(NUM2DBL(rb_iv_get(bbox, "@min_lat")));
71 unsigned int maxy = lat2y(NUM2DBL(rb_iv_get(bbox, "@max_lat")));
72 tilelist_t tl = tilelist_for_area(minx, miny, maxx, maxy);
73 VALUE tiles = rb_ary_new();
76 for (t = 0; t < tl.tilec; t++)
78 rb_ary_push(tiles, UINT2NUM(tl.tilev[tl.tilec]));
86 static VALUE tile_for_xy(VALUE self, VALUE x, VALUE y)
88 return UINT2NUM(xy2tile(NUM2UINT(x), NUM2UINT(y)));
91 static VALUE iterate_tiles_for_area(VALUE self, VALUE bbox)
93 unsigned int minx = lon2x(NUM2DBL(rb_iv_get(bbox, "@min_lon")));
94 unsigned int maxx = lon2x(NUM2DBL(rb_iv_get(bbox, "@max_lon")));
95 unsigned int miny = lat2y(NUM2DBL(rb_iv_get(bbox, "@min_lat")));
96 unsigned int maxy = lat2y(NUM2DBL(rb_iv_get(bbox, "@max_lat")));
97 tilelist_t tl = tilelist_for_area(minx, miny, maxx, maxy);
104 VALUE a = rb_ary_new();
106 qsort(tl.tilev, tl.tilec, sizeof(unsigned int), tile_compare);
108 first = last = tl.tilev[0];
110 for (t = 1; t < tl.tilec; t++)
112 unsigned int tile = tl.tilev[t];
114 if (tile == last + 1)
120 rb_ary_store(a, 0, UINT2NUM(first));
121 rb_ary_store(a, 1, UINT2NUM(last));
128 rb_ary_store(a, 0, UINT2NUM(first));
129 rb_ary_store(a, 1, UINT2NUM(last));
138 void Init_quad_tile_so(void)
140 VALUE m = rb_define_module("QuadTile");
142 rb_define_module_function(m, "tile_for_point", tile_for_point, 2);
143 rb_define_module_function(m, "tiles_for_area", tiles_for_area, 1);
144 rb_define_module_function(m, "tile_for_xy", tile_for_xy, 2);
145 rb_define_module_function(m, "iterate_tiles_for_area", iterate_tiles_for_area, 1);