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.8-cp314-cp314-win_amd64.whl (26.5 MB view details)

Uploaded CPython 3.14Windows x86-64

sgspy-1.0.8-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.8-cp313-cp313-win_amd64.whl (25.8 MB view details)

Uploaded CPython 3.13Windows x86-64

sgspy-1.0.8-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.8-cp312-cp312-win_amd64.whl (25.8 MB view details)

Uploaded CPython 3.12Windows x86-64

sgspy-1.0.8-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.8-cp311-cp311-win_amd64.whl (25.8 MB view details)

Uploaded CPython 3.11Windows x86-64

sgspy-1.0.8-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.8-cp310-cp310-win_amd64.whl (25.8 MB view details)

Uploaded CPython 3.10Windows x86-64

sgspy-1.0.8-cp310-cp310-manylinux_2_28_x86_64.whl (35.7 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.28+ x86-64

File details

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

File metadata

  • Download URL: sgspy-1.0.8-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.8-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 bcf318d147f6480f3cd6010cb8e016a031a5575f564828c76b7d3e2dbe811bb8
MD5 01a2a0a5f54d3f61806b07cacbb456eb
BLAKE2b-256 1abdd96a30b4ea508757b8f082569e34c322185e28417d9bf57d30479e0db0d2

See more details on using hashes here.

Provenance

The following attestation bundles were made for sgspy-1.0.8-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.8-cp314-cp314-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for sgspy-1.0.8-cp314-cp314-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 53a05d52375e8176074fd28a7662ab0d21be93bb9949ce2826f8244d49dd7a7d
MD5 b05a3789236c9091bec988901502d051
BLAKE2b-256 e23af2c0c24e6727e235904ddf36d7b43f926d94fd287d74a3a3de2f89021625

See more details on using hashes here.

Provenance

The following attestation bundles were made for sgspy-1.0.8-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.8-cp313-cp313-win_amd64.whl.

File metadata

  • Download URL: sgspy-1.0.8-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.8-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 3a52728327561fae1e80b5f0a7b7bb07df889e271e03b5aca95a700b8fd53408
MD5 810c9d561f017a45f3832518beb58655
BLAKE2b-256 40a968b0a383233f3adbea3b2621d02670e595ef9c66a794608c2bb5a270e575

See more details on using hashes here.

Provenance

The following attestation bundles were made for sgspy-1.0.8-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.8-cp313-cp313-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for sgspy-1.0.8-cp313-cp313-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 987e17888d0bc57c84c341f03074a131621441bfa671912d427a8948880a3e35
MD5 5e641c26d550d00c0c811ca6ce58e7a2
BLAKE2b-256 0c3cf7f337c9bec01d430928b5ee521c3054f9f9fc9c315d01e57927ef8fbb0b

See more details on using hashes here.

Provenance

The following attestation bundles were made for sgspy-1.0.8-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.8-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: sgspy-1.0.8-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.8-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 659d7f58c32929840ae0b143fa62ec08a37ecdd9af5f1ccc0c944658d24e7121
MD5 948c5276cca63e73eb84a46eb23f4e3a
BLAKE2b-256 8ccef91a8c3a088e0abc70572fe5315fc48adf7d54066e9245e4aae7c8fed47b

See more details on using hashes here.

Provenance

The following attestation bundles were made for sgspy-1.0.8-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.8-cp312-cp312-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for sgspy-1.0.8-cp312-cp312-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 603f52c02aca3a0706183ea0b37e7b680906b04fdbd27601d3b9227ec95a12ab
MD5 7af562bc88d2ec761d8fc50a9f5f9dba
BLAKE2b-256 45a32f57ea946191b363d9a5619160253adfbca9acef8d013513e7a0403a3ec1

See more details on using hashes here.

Provenance

The following attestation bundles were made for sgspy-1.0.8-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.8-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: sgspy-1.0.8-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.8-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 a3bf1e02da0d3bbe310bb33c584b775cb30d93d85a7ebe06dcea2d345133e290
MD5 13c3639123224cdc0268e700ce931129
BLAKE2b-256 3285a5c4a6a2eddaab55a8f4c5b385ee6cc1d9412c57d27fb6bb2d916df23141

See more details on using hashes here.

Provenance

The following attestation bundles were made for sgspy-1.0.8-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.8-cp311-cp311-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for sgspy-1.0.8-cp311-cp311-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 6652ec48de91b37374941e0b351b1bb233caa9fdb30ea5932fafc1dbcfa875aa
MD5 02b2b7860cc5c53a86cadceda6e97f1b
BLAKE2b-256 7dafa024ef2f8e94f96174854e68c4028ac2ebc732538ce724885819337729fc

See more details on using hashes here.

Provenance

The following attestation bundles were made for sgspy-1.0.8-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.8-cp310-cp310-win_amd64.whl.

File metadata

  • Download URL: sgspy-1.0.8-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.8-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 20ef435b46b94cb3de8567910c8e02e4b1b3c24937b37afa8a5e3d3d0764e4ef
MD5 3a50bbe2beb641327a18c02a8c0496e3
BLAKE2b-256 c3a7a202305902ab53f2786b513206e73b369439e04569e0b7b7bccf032f42e8

See more details on using hashes here.

Provenance

The following attestation bundles were made for sgspy-1.0.8-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.8-cp310-cp310-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for sgspy-1.0.8-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 866689250567aeb0a212065386290ec1b64512785be1c384d64324c5ee267894
MD5 6e8f92bf7c88be0e9792be23f18f12b3
BLAKE2b-256 f0bfc0836ac7098f21dee7886e700c9ad1572482763667a1e582ae9732432e66

See more details on using hashes here.

Provenance

The following attestation bundles were made for sgspy-1.0.8-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