geo.filterRows() function

The geo.filterRows() function is experimental and subject to change at any time. By using this function, you accept the risks of experimental functions.

The geo.filterRows() function filters data by a specified geographic region with the option of strict filtering. This function is a combination of geo.gridFilter() and geo.strictFilter().

*Function type: Transformation*

  1. import "experimental/geo"
  2. geo.filterRows(
  3. region: {lat: 40.69335938, lon: -73.30078125, radius: 20.0},
  4. minSize: 24,
  5. maxSize: -1,
  6. level: -1,
  7. s2cellIDLevel: -1,
  8. correlationKey: ["_time"],
  9. strict: true
  10. )

s2_cell_id must be part of the group key

To filter geo-temporal data with geo.filterRows(), s2_cell_id must be part of the group key. To add s2_cell_id to the group key, use experimental.group:

  1. import "experimental"
  2. // ...
  3. |> experimental.group(columns: ["s2_cell_id"], mode: "extend")

Strict and non-strict filtering

In most cases, the specified geographic region does not perfectly align with S2 grid cells.

  • Non-strict filtering returns points that may be outside of the specified region but inside S2 grid cells partially covered by the region.
  • Strict filtering returns only points inside the specified region.

S2 grid cell
Filter region
Returned point

Strict filtering

geo.filterRows - 图1

Non-strict filtering

geo.filterRows - 图2

Parameters

region

The region containing the desired data points. Specify record properties for the shape. See Region definitions.

*Data type: Record*

minSize

Minimum number of cells that cover the specified region. Default is 24.

*Data type: Integer*

maxSize

Maximum number of cells that cover the specified region. Default is -1.

*Data type: Integer*

level

S2 cell level of grid cells. Default is -1.

*Data type: Integer*

level is mutually exclusive with minSize and maxSize and must be less than or equal to s2cellIDLevel.

s2cellIDLevel

S2 Cell level used in s2_cell_id tag. Default is -1.

*Data type: Integer*

When set to -1, geo.filterRows() attempts to automatically detect the S2 Cell ID level.

correlationKey

List of columns used to uniquely identify a row for output. Default is ["_time"].

*Data type: Array of strings*

strict

Enable strict geographic data filtering which filters points by longitude (lon) and latitude (lat). For S2 grid cells that are partially covered by the defined region, only points with coordinates in the defined region are returned. Default is true. See Strict and non-strict filtering above.

*Data type: Boolean*

Examples

Strictly filter data in a box-shaped region
  1. import "experimental/geo"
  2. from(bucket: "example-bucket")
  3. |> range(start: -1h)
  4. |> filter(fn: (r) => r._measurement == "example-measurement")
  5. |> geo.filterRows(
  6. region: {
  7. minLat: 40.51757813,
  8. maxLat: 40.86914063,
  9. minLon: -73.65234375,
  10. maxLon: -72.94921875
  11. }
  12. )
Approximately filter data in a circular region

The following example returns points with coordinates located in S2 grid cells partially covered by the defined region even though some points my be located outside of the region.

  1. import "experimental/geo"
  2. from(bucket: "example-bucket")
  3. |> range(start: -1h)
  4. |> filter(fn: (r) => r._measurement == "example-measurement")
  5. |> geo.filterRows(
  6. region: {
  7. lat: 40.69335938,
  8. lon: -73.30078125,
  9. radius: 20.0
  10. }
  11. strict: false
  12. )
Filter data in a polygonal region
  1. import "experimental/geo"
  2. from(bucket: "example-bucket")
  3. |> range(start: -1h)
  4. |> filter(fn: (r) => r._measurement == "example-measurement")
  5. |> geo.filterRows(
  6. region: {
  7. points: [
  8. {lat: 40.671659, lon: -73.936631},
  9. {lat: 40.706543, lon: -73.749177},
  10. {lat: 40.791333, lon: -73.880327}
  11. ]
  12. }
  13. )

Function definition

  1. filterRows = (
  2. tables=<-,
  3. region,
  4. minSize=24,
  5. maxSize=-1,
  6. level=-1,
  7. s2cellIDLevel=-1,
  8. strict=true
  9. ) => {
  10. _columns =
  11. |> columns(column: "_value")
  12. |> tableFind(fn: (key) => true )
  13. |> getColumn(column: "_value")
  14. _rows =
  15. if contains(value: "lat", set: _columns) then
  16. tables
  17. |> gridFilter(
  18. region: region,
  19. minSize: minSize,
  20. maxSize: maxSize,
  21. level: level,
  22. s2cellIDLevel: s2cellIDLevel)
  23. else
  24. tables
  25. |> gridFilter(
  26. region: region,
  27. minSize: minSize,
  28. maxSize: maxSize,
  29. level: level,
  30. s2cellIDLevel: s2cellIDLevel)
  31. |> toRows()
  32. _result =
  33. if strict then
  34. _rows
  35. |> strictFilter(region)
  36. else
  37. _rows
  38. return _result
  39. }

Related articles

functions geo