Skip to main content

Extract raster data at input coordinates. Meant for processing many rasters at once.

Project description

Raster Extraction Functions

This Python script provides functions to process raster files in chunks and extract values at specified coordinates using multiprocessing. The script is designed to handle large raster datasets efficiently by dividing the workload across multiple CPU cores.

NOTE

Note: GDAL cannot be installed via pip on Windows. Download the wheel for your python version here:

https://github.com/cgohlke/geospatial-wheels/releases

Keep in mind your python version (for example for python 3.11, download a wheel with "cp311".)

Then install with: pip install "path/to/.whl"

Usage

  1. Create a virtual environment with pythom -m venv venv'
  2. Activate the virtual environment with venv\scripts\activate
  3. Install using pip install raster-extraction-tools
  4. Install GDAL (For Windows, see the note above)
  5. Copy the code from example_run.py or from Example down below
  6. Carefully change the parameters keeping in mind the commented instructions (all rasters should be in 1 directory with no subdirectories)
  7. Save and run the python file

Example

import raster_extraction_functions as ref

if __name__ == "__main__":
    ref.extract_values(
        input_csv="path/to/csv.csv",  # file with at least 2 coordinate columns called "X" and "Y".
        raster_folder='path/to/raster/folder',  # directory where rasters to be extracted are saved, does not read subdirs.
        output_csv='path/to/output.csv',  # output file to be created.
        in_crs='EPSG:28992',  # Coordinate Reference System of input csv.
        raster_crs='EPSG:3035',  # Coordinate Reference System of rasters to be used (EPSG:3035 in case of EXPANSE rasters).
        sep=';',  # column separator in input file, output will always be semi-colon.
        decimal=',',  # decimal separator in input file.
        writemethod='concat',  # method to write output to csv, "concat" is fast but memory-intensive, "rows" is slow but requires no extra memory.
    )

Requirements

  • GDAL
  • NumPy
  • Pandas
  • PyProj

Installation

Make sure you have the required libraries installed. You can install them using pip:

pip install numpy pandas pyproj 

Functions

extract_values

Main function to orchestrate the multiprocessing of raster files for extracting values at specified coordinates.

Parameters:

  • input_csv (str): Path to the input CSV file containing coordinates.
  • raster_folder (str): Directory containing the raster files.
  • output_csv (str): Path to the output CSV file where results will be saved.
  • in_crs (str or int): Coordinate reference system of the input coordinates.
  • raster_crs (str or int): Coordinate reference system of the raster files.
  • sep (str, optional): Delimiter to use in the input CSV file. Default is ';'.
  • decimal (str, optional): Character to recognize as decimal point in the input CSV file. Default is '.'.
  • writemethod (str, optional): Method to write output CSV ('concat' or 'rows'). Default is 'concat'.

Returns:

  • None

process_raster

Processes a single raster file in chunks and extracts values at specified coordinates.

Parameters:

  • x_coords (numpy.ndarray): Array of x coordinates (longitude) for points of interest.
  • y_coords (numpy.ndarray): Array of y coordinates (latitude) for points of interest.
  • raster_folder (str): Directory containing the raster files.
  • raster_file (str): Name of the raster file to process.
  • buffer_size (int, optional): Buffer size around each coordinate to consider. Default is 0.

Returns:

  • tuple: (column_name, values) where column_name is the raster file name without extension, and values is a numpy array of extracted values at the specified coordinates.

Helper functions

calculate_chunk_size

Helper function. Calculates the chunk size for reading the raster based on the desired maximum memory footprint. Parameters:

  • dataset (gdal.Dataset): The GDAL dataset representing the raster.
  • max_chunk_memory (int, optional): Maximum memory (in bytes) to be used for a chunk. Default is 500 MB.

Returns:

  • tuple: (chunk_width, chunk_height) representing the dimensions of the chunk.

update_progress

Helper function. Callback function to update progress. Parameters:

  • result: The result from the multiprocessing pool.
  • progress_counter (multiprocessing.Value): The progress counter.
  • total_rasters (int): Total number of rasters to process.

Project details


Download files

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

Source Distribution

raster_extraction_tool-0.1.3.tar.gz (6.6 kB view details)

Uploaded Source

Built Distribution

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

raster_extraction_tool-0.1.3-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

Details for the file raster_extraction_tool-0.1.3.tar.gz.

File metadata

  • Download URL: raster_extraction_tool-0.1.3.tar.gz
  • Upload date:
  • Size: 6.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.7

File hashes

Hashes for raster_extraction_tool-0.1.3.tar.gz
Algorithm Hash digest
SHA256 2cef1deb01b5e7468472c05354c2ffd0a0b1c7e55c5f320b4c420ca0ec757c40
MD5 c329eb01a58d6fb31d2261b6d6efcfa8
BLAKE2b-256 41edde69d38d018bba87cadb897177ca73a9bea70a810df3f759dc55ffefc407

See more details on using hashes here.

File details

Details for the file raster_extraction_tool-0.1.3-py3-none-any.whl.

File metadata

File hashes

Hashes for raster_extraction_tool-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 7709c9896bc6a18922837d086eaf3bdcf446473abc460c762416537850040cad
MD5 9589396759bbec562cd2e1254d607134
BLAKE2b-256 ee64d8cfe0a0eebe1a7738f433fe31bab875538c5c57c908096c28a1f82311ab

See more details on using hashes here.

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