Geovisualization Developer's Guide

$pixel_within_tile

This filter operator returns true if the variable that it is applied on lies inside a tile given by x and y coordinates, or within tile_buffer pixels around it.

The example below shows a query that prepares data to be used for a heat map. It receives the tile's coordinates tx and ty as parameter, as well as the zoom level zoom and the buffer tile_buffer (a request setting those parameters automatically generated by the heat map control of the Geovisualization JavaScript API). It will return values only for rows whose lat and lon values lie within the borders of the provided tile, or tile_buffer around it.

In order to achieve this, the datasets lat and lon are first translated to screen pixel coordinates py and px respectively, using the lat_to_y and lon_to_x transformation rules. Then, a tuple of those values called pixels is created. Finally, in the filtering section, it is tested whether this pixel lies within the range of the desired tile.

One row per px,py pair is generated (as these are the facets), containing the coordinates themselves plus the metric pixel_value which contains the sum of all values of the column count in all rows mapped to the same pixel.

{
  "facets":["px","py"],
  "namespace":{
    "pixel_value":{"source":["count"],"apply":[{"type":"aggregate","fn":"sum"}]},
    "tile_buffer":{"source":[{"$input":"buffer"}]},
    "px":{"apply":[{"type":"transform","fn":"lon_to_x"}], "source":["lon","zoom"]},
    "py":{"apply":[{"type":"transform","fn":"lat_to_y"}], "source":["lat","zoom"]},
    "zoom":{"source":[{"$input":"zoom"}]},
    "pixels":{"source":["px","py"]},
    "tile_y":{"source":[{"$input":"ty"}]},
    "tile_x":{"source":[{"$input":"tx"}]}},
    "dataset":"a6e43f1d38644ca8824c271bb5ba1ee2",
    "metrics":["pixel_value"],
    "version":0.3,
    "filters":[
      {"pixels":{"$pixel_within_tile":["tile_x","tile_y","tile_buffer"]}}
    ],
    "order":[{"month":"asc"}]
}

For more information on using methods and operators in queries in a REST API request, see Queries.