gspatial_tools package

The examples mentioned in the documentation is also part of examples jupyter notebook present in the github repo, So feel free to download it and play with it.

generate_points_grid(bounds, grid_space=0.01, crs='4326', clip=False, shape=None)

Generates a grid of point data for given bounds. The data can be clipped by passing geometries for clipping supported by Geopandas

Parameters:
  • bounds (list) – Bounding box for the grid

  • grid_space (float) – Resolution of the grid. Grid space needs to be chosen according to crs. Defaults to 0.01.

  • crs (str) – Coordinate Reference System for the grid. Defaults to “4326”.

  • clip (bool) – Clips by shape parameter if True. Defaults to False.

  • shape (Polygon/GeoSeries/GeoDataframe/Anything supported by Geopandas Clip) – Shape to clip. Should be in same CRS as grid. Defaults to None.

Returns:

GeoDataFrame containing points

Return type:

GeoDataFrame

Examples

grid = generate_points_grid(argentina.total_bounds,grid_space=1)
grid = generate_points_grid(argentina.total_bounds,grid_space=1, clip=True, shape=argentina)
filter_points_to_radius(grid, points, radius, cap_style=3)

Filters grid to a certain radius taking the point passed as center.

Parameters:
  • grid (GeoDataFrame) – Grid of points

  • points (GeoSeries/GeoDataFrame) – Set of points need to filter the grid to a certain radius. Should be in same CRS as grid.

  • radius (float) – Radius to filter points. Should be in same CRS as grid and points

  • cap_style (int) – cap_style parameter to buffer function. Defaults to 3 which is a square.

Returns:

filtered GeoDataFrame

Return type:

GeoDataFrame

Examples

filtered = filter_points_to_radius(grid, point, 5)
bulk_clip_and_save_raster(data, gdf, names_col=None, path=None)

Utility function to clip and save rasters in bulk using a GeoDataFrame.

Parameters:
  • data (raster/ndarray) – Data read from rasterio

  • gdf (GeoDataFrame) – Shapes to clip the raster

  • names_col (str) – Column name to name to file saved. Defaults to None.

  • path (str) – Path to save clipped files. Defaults to None.

Examples bulk_clip_and_save_raster(data, world, names_col=”name”)

reproject_rasterio(data, dst_crs, output_file)

Utility function to reproject and save raster in rasterio

Parameters:
  • data (raster/ndarray) – Data read from rasterio

  • dst_crs (str) – Target crs supported by rasterio

  • output_file (str) – Output file name

Examples

japan = rasterio.open("Japan.tif")
reproject_rasterio(japan,"EPSG:4326","Japan_WGS84.tif")
stitch_rasters(file_list, out_path)

Merges a list of raster files into a single file. (All need to be in same CRS)

Parameters:
  • file_list (list) – List of filenames

  • out_path (str) – Output file path/name

Examples

stitch_rasters(file_list,"stitched.tif")
read_raster_as_gdf(filename, x='x', y='y', name='data', dropna=True)

Read raster file as GeoDataFrame

Parameters:
  • filename (str) – Name of raster file

  • x (str) – column/field name for x coordinate. Defaults to “x”.

  • y (str) – column/field name for y coordinate. Defaults to “y”.

  • name (str) – Name for. Defaults to “data”.

  • dropna (bool) – Drops null value from raster. Defaults to True.

Returns:

GeoDataFrame

Return type:

gdf

Raises:

e – Any exception raised during the process

Examples

gdf = read_raster_as_gdf("Japan.tif")
convert_xarray_to_gdf(data, x='x', y='y', dropna=True)

Converts xarray to gdf

Parameters:
  • data (xarray Dataset/DataArray) – xarray data to be converted

  • x (str) – column/field name for x coordinate. Defaults to “x”.

  • y (str) – column/field name for y coordinate. Defaults to “y”.

  • dropna (bool) – Drops null value from raster. Defaults to True.

Returns:

GeoDataFrame

Return type:

gdf

Raises:

e – Any exception raised during the process

Examples

gdf = convert_xarray_to_gdf(nc, x="lon", y="lat")
clip_each_geom_by_rect(gdf, xmin, ymin, xmax, ymax)

Clips each geometry in a Dataframe by rectangle and returns the result

Parameters:
  • gdf (GeoDataFrame) – Input GeoDataFrame

  • xmin (float) – Minimum x bounds

  • ymin (float) – Minimum y bounds

  • xmax (float) – Maximum x bounds

  • ymax (float) – Maximum y bounds

Returns:

GeoDataFrame

Return type:

GeoDataFrame

Examples

us = clip_each_geom_by_rect(us,-180,-90,-69,83)
sample_points_from_polygons(gdf, n: int, crs=None)

Samples n points inside set of polygons

Parameters:
  • gdf (GeoDataFrame/GeoSeries) – GeoDataFrame/GeoSeries containing polygons

  • n (int) – number of samples

  • crs (str) – CRS for the points, If nothing is passed, CRS of gdf is set. Defaults to None

  • n – int:

Returns:

A Geoseries containing sampled points

Return type:

points

Examples

points = sample_points_from_polygons(argentina,200)
sample_points_from_bbox(bounds, n: int, crs=None)

Samples n points inside a bounding box

Parameters:
  • bounds (list) – bounding box/total_bounds of a GeoDataFrame/GeoSeries/Polygon

  • n (int) – number of samples

  • n – int:

  • crs – (Default value = None)

Returns:

A Geoseries containing sampled points

Return type:

points

Examples

points = sample_points_from_bbox(world.total_bounds,20, crs="4326")
sample_data_from_raster(data, points, col_name='data', n_bands=1)

Samples data directly from a raster file

Parameters:
  • data (rasterio.io.DatasetReader) – Raster file to sample data from

  • points (GeoDataFrame/GeoSeries) – A GeoDataFrame/GeoSeries containing Point geometries.

  • col_name (str) – Name of the column containing sampled_data. Defaults to “data”.

  • n_bands – (Default value = 1)

Returns:

points

Return type:

GeoDataFrame

Raises:

e – Any exception due to crs issues

Examples

sample_points = sample_data_from_raster(argentina_raster, points, col_name="pop")
nearest_points(left_gdf, right_gdf, k=3, leaf_size=15, distance_unit='radians', return_indices=False)

Returns k nearest points to left GeoDataFrame

Parameters:
  • left_gdf – GeoDataFrame

  • right_gdf – GeoDataFrame

  • k – int (Default value = 3)

  • leaf_size – int (Default value = 15)

  • distance_unit – str (Default value = “radians”)

  • return_indices – bool (Default value = False)

Returns:

result: GeoDataFrame containing results

Examples

nearest_points(points,points, distance_unit="kilometers", return_indices=True)
move_and_scale_shape(gdf, identifier_col, identifier_value, scale_factor, x_distance, y_distance)

Move and scale a specific polygon in a GeoDataFrame

Parameters:
  • gdf (GeoDataFrame) – GeoDataFrame to be modified

  • identifier_col (str) – Column name to identify polygon/shape

  • identifier_value (str) – Value corresponding to the column name

  • scale_factor (float) – Scale factor to resize the polygon

  • x_distance (float) – Offset in x coordinate

  • y_distance (float) – Offset in x coordinate

Returns:

modified_gdf-> Returns the modfied GeoDataFrame

Return type:

GeoDataFrame

Examples

states = move_and_scale_shape(states,"NAME","Alaska",0.5,30,-40)
states = move_and_scale_shape(states,"NAME","Hawaii",1,70,0)