Skip to main content

Structurally Guided Sampling

Project description

sgsPy - Structurally Guided Sampling

sgsPy is a toolbox package of stratification and sampling methods for use on ALS metrics, designed to be fast and efficient on very large raster images.

Installation

sgsPy can be downloaded on either Windows or Linux from the Python Package Index (PyPI) with the following command:

pip install sgspy

At the moment, the import import sgspy may fail if you are using a package manager other than PyPi (pip).

Overview

sgsPy is based on the widely-used sgsR package, intended originally to help develop representative ground sample networks using auxiliary information like ALS data. sgsPy takes much of the existing functionality of the R package and improves upon its performance, allowing Python users to integrate structurally guided sampling approaches to their workflows.

While the original intention for the package was to be used with ALS metrics, any form of raster data can be used in lieu of ALS metrics.

If you encounter any bugs, unclear documentation, or have any feature ideas then please contribute by posting an issue. https://github.com/jbmeyer2001/sgsPy/issues

The currently implemented functionality includes:

Quickstart guide

files

Some helpful files to get started with can be found in the https://github.com/jbmeyer2001/sgsPy/tree/main/tests/files folder. Specifically mraster.tif, inventory_polygons.shp along with its associated .dbf, .prj, and .shx files, access.shp along with its associated .dbf, .prj, and .shx files, existing.shp along with its associated .dbf, .prj, and .shx files.

raster and vector data

The two classes used by the stratification, sampling, and calculating methods are the sgspy.SpatialRaster [SpatialRaster documentation] and sgspy.SpatialVector [SpatialVector documentation]. The beginning of any process using the sgspy package will involve creating an instance of one or more of these classes. This can be done by providing a file path. It can also be done by converting an object from a popular geospatial package.

import sgspy

#creating SpatialRaster and SpatialVector using file paths
rast = sgspy.SpatialRaster("mraster.tif")
vect = sgspy.SpatialVector("access.shp")

#plotting
rast.plot(band=0)
rast.plot(band="pzabove2")

#accessing underlying data as a numpy array
zq90_arr = rast.band("zq90")
first_band_arr = rast.band(0)

#to/from GDAL raster dataset
ds = gdal.Open("mraster.tif")
rast = sgspy.SpatialRaster.from_gdal(ds)
new_ds = rast.to_gdal()

#to/from rasterio dataset
ds = rasterio.open("mraster.tif")
rast = sgspy.SpatialRaster.from_rasterio(ds)
new_ds = rast.to_rasterio()

#to/from geopandas
gdf = gpd.read_file("inventory_polygons.shp")
vect = sgspy.SpatialVector.from_geopandas(gdf)
new_gdf = gpd.to_geopandas(vect)

example

More extensive examples on how to use each method is available in the user documentation. The following shows how one might use three of the different methods available in sgspy to create a new sampling network.

import sgspy

# create SpatialRaster object from desired tif file
rast = sgspy.SpatialRaster("mraster.tif")

# use principal component analysis for dimensionality reduction (to two components or raster layers)
pca = sgspy.calculate.pca(rast, num_comp=2)

# stratifiy the two components into 5 equally sized quantiles, and map the two layers into a single mapped output
srast = sgspy.stratify.quantiles(pca, quantiles={"comp_1":5, "comp2":5}, map=True)

# use stratified random sampling to sample each (of the 25 total) mapped strata proportional to it's number of pixels in the raster band
# additionaly, plot the output and write it to samples.shp
samples = sgspy.sample.strat(srast, band="map_strat", num_samples=200, num_strata=25, allocation="prop", plot=True, filename="samples.shp")

Funding

The development of the sgsPy package was funded by the Ontario Future Forests (KTTD 2B-2024) grant.

Developer information

This section includes installation steps for both Windows and Linux, in addition to notes on running the code. See the Developer Documentation for information on the underlying C++ implementations.

Linux installation:

  1. Ensure you have Python with pip installed, and git.
  2. clone the repository with the following command:
git clone https://github.com/jbmeyer2001/sgsPy.git
  1. create and activate a python virtual environment (highly recommended)
python -m venv .venv
source ./.venv/bin/activate
  1. install dependency requirements. If you do not already have these, the dependency installation will fail.
  • build-essential (if you don't already have a C++ compiler)
  • pkg-config
  • auto-conf
  • libtool
  • bison
  • flex
  • patchelf
sudo apt install build-essential
sudo apt install pkg-config
sudo apt install auto-conf
sudo apt install libtool
sudo apt install bison
sudo apt install flex
sudo apt install patchelf
  1. Run the following command to install and build project C++ dependencies.
bash scripts/install.sh
  1. Run the following command to build the project
bash scripts/build.sh
  1. If you intend to run the tests (using pytest), both pytest and geopandas are required and can be installed as follows:
pip install pytest
pip install geopandas

Windows installation:

NOTE: The commands given are for a Windows Powershell command prompt.

  1. ensure you have Python with pip, git, and a C++ compiler.

  2. If you do not already have a C++ compiler, one can installed by installing the Microsoft Visual Studio IDE with C++ build tools.

  3. clone the repository with the following command.

git clone https://github.com/jbmeyer2001/sgsPy.git
  1. create and activate a Python virtual environment (highly recommended).
python -m venv .venv
./.venv/Scripts/activate
  1. Run the following command to install and build project C++ dependencies. remaining dependencies
./scripts/install
  1. Run the following command to build the project
./scripts/build
  1. If you intend to run the tests (using pytest), both pytest and geopandas are required and can be installed as follow:
pip install pytest
pip install geopandas

How to run sgsPy:

Tests may be ran by running the following command from within the folder containing this file. Both pytest and geopandas must be installed to run the tests.

pytest

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

sgspy-1.0.6-cp314-cp314-win_amd64.whl (26.5 MB view details)

Uploaded CPython 3.14Windows x86-64

sgspy-1.0.6-cp314-cp314-manylinux_2_28_x86_64.whl (35.7 MB view details)

Uploaded CPython 3.14manylinux: glibc 2.28+ x86-64

sgspy-1.0.6-cp313-cp313-win_amd64.whl (25.8 MB view details)

Uploaded CPython 3.13Windows x86-64

sgspy-1.0.6-cp313-cp313-manylinux_2_28_x86_64.whl (35.7 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.28+ x86-64

sgspy-1.0.6-cp312-cp312-win_amd64.whl (25.8 MB view details)

Uploaded CPython 3.12Windows x86-64

sgspy-1.0.6-cp312-cp312-manylinux_2_28_x86_64.whl (35.7 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.28+ x86-64

sgspy-1.0.6-cp311-cp311-win_amd64.whl (25.8 MB view details)

Uploaded CPython 3.11Windows x86-64

sgspy-1.0.6-cp311-cp311-manylinux_2_28_x86_64.whl (35.7 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.28+ x86-64

sgspy-1.0.6-cp310-cp310-win_amd64.whl (25.8 MB view details)

Uploaded CPython 3.10Windows x86-64

sgspy-1.0.6-cp310-cp310-manylinux_2_28_x86_64.whl (35.6 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.28+ x86-64

File details

Details for the file sgspy-1.0.6-cp314-cp314-win_amd64.whl.

File metadata

  • Download URL: sgspy-1.0.6-cp314-cp314-win_amd64.whl
  • Upload date:
  • Size: 26.5 MB
  • Tags: CPython 3.14, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for sgspy-1.0.6-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 1b5654895681890c63dd10892551d91229cbd84fd102431cc780c5955b920b20
MD5 7e44610a4c2aab5fcc78cfc8aef78478
BLAKE2b-256 9ebbd0b0b9f8f3e6f790507b92cc7e58ea6046d44701063a07a129f13f58d8b6

See more details on using hashes here.

Provenance

The following attestation bundles were made for sgspy-1.0.6-cp314-cp314-win_amd64.whl:

Publisher: deploy.yml on jbmeyer2001/sgsPy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sgspy-1.0.6-cp314-cp314-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for sgspy-1.0.6-cp314-cp314-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 0d5cd059d220d5f1c6662330354538328daff4392e2aa2a332263dbb6a52421f
MD5 03f1c29b49453fe2781a49dc45cd2a5f
BLAKE2b-256 3b4365ca7add73c4c7ea98e0a5402edcc01771eb308c4ebc612cf7cf8ffbd1b3

See more details on using hashes here.

Provenance

The following attestation bundles were made for sgspy-1.0.6-cp314-cp314-manylinux_2_28_x86_64.whl:

Publisher: deploy.yml on jbmeyer2001/sgsPy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sgspy-1.0.6-cp313-cp313-win_amd64.whl.

File metadata

  • Download URL: sgspy-1.0.6-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 25.8 MB
  • Tags: CPython 3.13, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for sgspy-1.0.6-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 2263d901db73cf9f24de0af4b30e6f45d15d31856c0f6bcfb4b25313ea8df808
MD5 f2be998f7fbd8e37f0f05f868c470a11
BLAKE2b-256 150e15a205b2acd632e2e48453e27ec8546f6cc1048188395af02b27576a7093

See more details on using hashes here.

Provenance

The following attestation bundles were made for sgspy-1.0.6-cp313-cp313-win_amd64.whl:

Publisher: deploy.yml on jbmeyer2001/sgsPy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sgspy-1.0.6-cp313-cp313-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for sgspy-1.0.6-cp313-cp313-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 563093de21f2ed5f195908dae9785efaaedf024a332522fec20f38b3f6dc26ca
MD5 11fc9b126f2febf27931d711b9be2f21
BLAKE2b-256 19c8ff9b2ffe434c53e9289fcd269eaf9d715e62157b231ea961e781c2b3621b

See more details on using hashes here.

Provenance

The following attestation bundles were made for sgspy-1.0.6-cp313-cp313-manylinux_2_28_x86_64.whl:

Publisher: deploy.yml on jbmeyer2001/sgsPy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sgspy-1.0.6-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: sgspy-1.0.6-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 25.8 MB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for sgspy-1.0.6-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 51800380ce79f619ba75f3c63bb6d8036ef29fd9e16032097de7b06238086f51
MD5 f900a5be52eddd27f2ddcbc0ad37ba00
BLAKE2b-256 7e937e6909c7f739e3b709ade8eaca80f9501caf70bb7ed90c548ea3c558023a

See more details on using hashes here.

Provenance

The following attestation bundles were made for sgspy-1.0.6-cp312-cp312-win_amd64.whl:

Publisher: deploy.yml on jbmeyer2001/sgsPy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sgspy-1.0.6-cp312-cp312-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for sgspy-1.0.6-cp312-cp312-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 1efa4e00933dfdcffdcf610596dcb2335e94089262ad46912f9ba17f290e1d16
MD5 4ff333bcf3fea93e07f6b56cbf00d570
BLAKE2b-256 beccd73676b2887cc769455b250d5b54d2ed2947c7cceb1e6b9c77b58fc0c97e

See more details on using hashes here.

Provenance

The following attestation bundles were made for sgspy-1.0.6-cp312-cp312-manylinux_2_28_x86_64.whl:

Publisher: deploy.yml on jbmeyer2001/sgsPy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sgspy-1.0.6-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: sgspy-1.0.6-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 25.8 MB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for sgspy-1.0.6-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 375e6988992d8a82341e8f156cfb1da3f6985919bac520fb5f529404cfa5fbda
MD5 279b7b3bbb86269478228d5047271129
BLAKE2b-256 f0b107753df120f8aada93a53c4f325a0bb334cb68b3ccc0de6dd767242f4154

See more details on using hashes here.

Provenance

The following attestation bundles were made for sgspy-1.0.6-cp311-cp311-win_amd64.whl:

Publisher: deploy.yml on jbmeyer2001/sgsPy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sgspy-1.0.6-cp311-cp311-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for sgspy-1.0.6-cp311-cp311-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 c207f441a85780c23e681cbe38b3cda6d39abad4db38f086cc60159196e772ca
MD5 a5a337879ee31c700fc648306fb2e65c
BLAKE2b-256 d49f88a31c507cb14a9a97874e437b9bd8081b3e437167876e52e8edd324b01f

See more details on using hashes here.

Provenance

The following attestation bundles were made for sgspy-1.0.6-cp311-cp311-manylinux_2_28_x86_64.whl:

Publisher: deploy.yml on jbmeyer2001/sgsPy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sgspy-1.0.6-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: sgspy-1.0.6-cp310-cp310-win_amd64.whl
  • Upload date:
  • Size: 25.8 MB
  • Tags: CPython 3.10, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for sgspy-1.0.6-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 88e8b4459606f5caa3c321e8f5d878c8a1315512a18a5e6ed32ea4f12377a458
MD5 a63e53d9923d30b5637a79c0d029dbcd
BLAKE2b-256 27023c3d3176d1fa3fd12fae8f75b906ece7719a65ef20c248512f507123c4d4

See more details on using hashes here.

Provenance

The following attestation bundles were made for sgspy-1.0.6-cp310-cp310-win_amd64.whl:

Publisher: deploy.yml on jbmeyer2001/sgsPy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sgspy-1.0.6-cp310-cp310-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for sgspy-1.0.6-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 1f4eaa61e2695a6bbabc10ca1bd4051bce675e23a048d64db2be3a8495888c83
MD5 7a7a937da6a8ad79a9be7008a7598d01
BLAKE2b-256 b5b49feaadd7b5436d2e9d4e6e1b55e55d8da01832a8d0836a94f628f067b48b

See more details on using hashes here.

Provenance

The following attestation bundles were made for sgspy-1.0.6-cp310-cp310-manylinux_2_28_x86_64.whl:

Publisher: deploy.yml on jbmeyer2001/sgsPy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page