Skip to content
Thomas Nipen edited this page Jun 18, 2020 · 20 revisions

The gridpp library supports two classes for describing geographical positions of datasets. The Grid class encapsulates a 2 dimensional gridded field of locations. It can be created from 2D vectors of latitude and longitude (degrees) as follows:

grid = gridpp.Grid(lats, lons)

The Points class encapsulates a 1 dimensional vector of locations. Objects can be created from 1D vectors of latitude and longitude (degrees) as follows:

points = gridpp.Points(lats, lons)

The library operates exclusively with latitude,longitude coordinates, instead of x,y coordinates on projected grids. This means grids do not need to be on a specific projection. The latitude/longitude coordinates are converted to 3D spherical coordinates and distances between points are calculated in 3D space. Thus distances do not follow the curvature of the earth but are straight-line distance through the earth.

Getting nearest neighbours

Both classes have efficient methods for retrieving the nearest point, the nearest N points, and all points within a specified radius. Grid and Points use the helper class KDTree, which uses the rtree implementation in boost.

index = grid.get_nearest_neighbour(lat, lon)
index = points.get_nearest_neighbour(lat, lon)

This returns an index into the lats,lons arrays that the objects were initialized with. For Grid, the index is a vector of length 2, one value for each dimension. For Points the index is scalar.

To get all locations within a radius [m], use:

indices = grid.get_neighbour(lat, lon, radius)
indices = points.get_neighbour(lat, lon, radius)

These functions return a vector of indices, one for each neighbour.

To get the num nearest locations, use:

index = grid.get_clossest_neighbour(lat, lon, num)
index = points.get_closest_neighbour(lat, lon, num)

Functions (such as bilinear) require both a 2D vector of values, and a Grid object to describe the coordinates of the 2D vector.

Clone this wiki locally