Module: QuadTile

Defined in:
lib/quad_tile.rb,
lib/quad_tile/version.rb,
ext/quad_tile/quad_tile.c

Constant Summary collapse

VERSION =
"1.0.1"

Class Method Summary collapse

Class Method Details

.iterate_tiles_for_area(bbox) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'ext/quad_tile/quad_tile.c', line 91

static VALUE iterate_tiles_for_area(VALUE self, VALUE bbox)
{
   unsigned int minx = lon2x(NUM2DBL(rb_iv_get(bbox, "@min_lon")));
   unsigned int maxx = lon2x(NUM2DBL(rb_iv_get(bbox, "@max_lon")));
   unsigned int miny = lat2y(NUM2DBL(rb_iv_get(bbox, "@min_lat")));
   unsigned int maxy = lat2y(NUM2DBL(rb_iv_get(bbox, "@max_lat")));
   tilelist_t   tl = tilelist_for_area(minx, miny, maxx, maxy);

   if (tl.tilec > 0)
   {
      unsigned int first;
      unsigned int last;
      unsigned int t;
      VALUE        a = rb_ary_new();

      qsort(tl.tilev, tl.tilec, sizeof(unsigned int), tile_compare);

      first = last = tl.tilev[0];

      for (t = 1; t < tl.tilec; t++)
      {
         unsigned int tile = tl.tilev[t];

         if (tile == last + 1)
         {
            last = tile;
         }
         else
         {
            rb_ary_store(a, 0, UINT2NUM(first));
            rb_ary_store(a, 1, UINT2NUM(last));
            rb_yield(a);

            first = last = tile;
         }
      }

      rb_ary_store(a, 0, UINT2NUM(first));
      rb_ary_store(a, 1, UINT2NUM(last));
      rb_yield(a);
   }

   free(tl.tilev);

   return Qnil;
}

.sql_for_area(bbox, prefix) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/quad_tile.rb', line 5

def self.sql_for_area(bbox, prefix)
  sql = []
  single = []

  iterate_tiles_for_area(bbox) do |first, last|
    if first == last
      single.push(first)
    else
      sql.push("#{prefix}tile BETWEEN #{first} AND #{last}")
    end
  end

  sql.push("#{prefix}tile IN (#{single.join(',')})") unless single.empty?

  "( " + sql.join(" OR ") + " )"
end

.tile_for_point(lat, lon) ⇒ Object



58
59
60
61
62
63
64
# File 'ext/quad_tile/quad_tile.c', line 58

static VALUE tile_for_point(VALUE self, VALUE lat, VALUE lon)
{
   unsigned int x = lon2x(NUM2DBL(lon));
   unsigned int y = lat2y(NUM2DBL(lat));

   return UINT2NUM(xy2tile(x, y));
}

.tile_for_xy(x, y) ⇒ Object



86
87
88
89
# File 'ext/quad_tile/quad_tile.c', line 86

static VALUE tile_for_xy(VALUE self, VALUE x, VALUE y)
{
   return UINT2NUM(xy2tile(NUM2UINT(x), NUM2UINT(y)));
}

.tiles_for_area(bbox) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'ext/quad_tile/quad_tile.c', line 66

static VALUE tiles_for_area(VALUE self, VALUE bbox)
{
   unsigned int minx = lon2x(NUM2DBL(rb_iv_get(bbox, "@min_lon")));
   unsigned int maxx = lon2x(NUM2DBL(rb_iv_get(bbox, "@max_lon")));
   unsigned int miny = lat2y(NUM2DBL(rb_iv_get(bbox, "@min_lat")));
   unsigned int maxy = lat2y(NUM2DBL(rb_iv_get(bbox, "@max_lat")));
   tilelist_t   tl = tilelist_for_area(minx, miny, maxx, maxy);
   VALUE        tiles = rb_ary_new();
   unsigned int t;

   for (t = 0; t < tl.tilec; t++)
   {
      rb_ary_push(tiles, UINT2NUM(tl.tilev[t]));
   }

   free(tl.tilev);

   return tiles;
}