Skip to content

Commit

Permalink
Add function rsgislib.vectorutils.createvectors.create_random_pts_in_…
Browse files Browse the repository at this point in the history
…bbox
  • Loading branch information
petebunting committed Jun 26, 2024
1 parent 32a2ba8 commit 8af0eee
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
1 change: 1 addition & 0 deletions doc/python/source/rsgislib_vectorutils.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Create Vectors
.. autofunction:: rsgislib.vectorutils.createvectors.create_bboxs_for_pts
.. autofunction:: rsgislib.vectorutils.create_lines_of_points
.. autofunction:: rsgislib.vectorutils.createvectors.create_random_pts_in_radius
.. autofunction:: rsgislib.vectorutils.createvectors.create_random_pts_in_bbox
.. autofunction:: rsgislib.vectorutils.create_copy_vector_lyr
.. autofunction:: rsgislib.vectorutils.createvectors.create_vec_for_image
.. autofunction:: rsgislib.vectorutils.createvectors.create_hex_grid_bbox
Expand Down
52 changes: 51 additions & 1 deletion python/rsgislib/vectorutils/createvectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -1408,7 +1408,7 @@ def create_random_pts_in_radius(
epsg_code: int,
out_vec_file: str,
out_vec_lyr: str,
out_format: str = "GeoJSON",
out_format: str = "GPKG",
rnd_seed: int = None,
n_pts_multi_bbox: float = 3,
):
Expand Down Expand Up @@ -1674,3 +1674,53 @@ def create_img_transects(
lines=out_lines_lst,
overwrite=True,
)


def create_random_pts_in_bbox(
bbox: List[float],
n_pts: int,
epsg_code: int,
out_vec_file: str,
out_vec_lyr: str,
out_format: str = "GPKG",
rnd_seed: int = None,
):
"""
A function which generates a set of random points within a boundary
box.
:param bbox: The bounding box the points ([xMin, xMax, yMin, yMax])
:param n_pts: the number of points to be generated.
:param epsg_code: the EPSG code for the projection of the points.
:param out_vec_file: the output file path and name.
:param out_vec_lyr: the output layer name.
:param out_format: the output file format (Default: GeoJSON)
:param rnd_seed: the seed for the random generator.
"""
import numpy
import geopandas

# Set the random seed.
numpy.random.seed(seed=rnd_seed)

# Create more than needed points within the bbox.
x_coords = numpy.random.uniform(bbox[0], bbox[1], n_pts)
y_coords = numpy.random.uniform(bbox[2], bbox[3], n_pts)

# Create geopandas dataframe with the points.
data_gdf = geopandas.GeoDataFrame(
geometry=geopandas.points_from_xy(x=x_coords, y=y_coords), crs=epsg_code
)

# Export the points
if out_format == "GPKG":
if out_vec_lyr is None:
import rsgislib.tools.filetools

out_vec_lyr = rsgislib.tools.filetools.get_file_basename(
out_vec_file, check_valid=True
)
data_gdf.to_file(out_vec_file, layer=out_vec_lyr, driver=out_format)
else:
data_gdf.to_file(out_vec_file, driver=out_format)

0 comments on commit 8af0eee

Please sign in to comment.