Kusto Query Language (KQL) contains many built-in functions to work with Geographic data. In this article, I will describe some of those that I found most useful.
For the examples in this article, we will use a table created with the following ADX commands:
This function calculates the distance in meters between 2 points, given the latitude and longitude of each. The syntax is:
geo_distance_2points(p1_longitude, p1_latitude, p2_longitude, p2_latitude)
- p1_longitude, p1_latitude are the latitude and longitude of the first point
- p2_longitude, p2_latitude are the latitude and longitude of the second point
The path between the points is assumed to be a straight line, so it may or may not be the path taken to move from one point to the other. For example, you may be tracking a car's movements and that car is likely be restricted to driving on roads and not driving through buildings.
Here is a sample call:
The query above yields the results shown in Fig. 1
This function returns true, if a given point is inside a given polygon; otherwise, it returns false.
The syntax is:
geo_point_in_polygon(longitude, latitude, polygon)
- longitude is the longitude of the point to test
- latitude is the longitude of the point to test
- polygon is the polygon in question
There are 2 ways to create a polygon.
One is to assign an array of longitude/latitude pairs to an object of type polygon. The other is to use the pack function and pass in an object containing an array of longitude/latitude pairs.
Below are examples of each type:
The polygons above each define a rectangle in New York City, as shown in Fig. 2
Here is a sample call to geo_point_in_polygon, using either of the polygon objects created above:
The query above will return only those rows that are inside the polygon. The results are shown in Fig. 3.
The S2 system divides the Earth into rectangles of approximately equal size. The rectangles take into account the curvature of the Earth's globe, eliminating gaps and distortions caused by models that attempt to flatten the Earth's surface. The size of each rectangle is dependent upon the level (00 through 30). Lower level values divide the earth into larger rectangles, so it takes fewer rectangles to cover the globe. For a given S2 level, if two points share the same S2 value, they exist in the same rectangle.
You can read more about S2 at s2geometry.io
The syntax of geo_point_to_s2cell() is:
geo_point_to_s2cell(longitude, latitude, level)
- longitude and latitude represent a point on the globe
- level is the level that defines the S2 rectangle in which to place this point
The following example places calculates the S2 Level for every data point of Vehicle 2:
The query above yields the results shown in Fig. 4
This function returns an array of s2 cell values that cover a polygon. The syntax is:
- polygon is a polygon, as described above
- S2_level is the level (0-30) defining the size of the S2 rectangles, as described above.
For example, the following code will return an array of 15-level S2 cells that completely covers a polygon named myPolygon. Every S2 cell in the area will overlap myPolygon.
You can then use this array to further refine searches, as in the example below:
This code returns the same results as in Fig. 3 above; however, it can be much faster if our table contains many points all over the world, far from the polygon. By first finding the overlapping S2 cells, we speed our query by only considering points near the polygon. This will run even faster if we pre-populate the S2 cell value.
In this article, I described the Geo features of KQL that we found most useful. You can view more KQL Geo functions here