geocube
repository·master·Indexed 18 days ago
https://github.com/corteva/geocubeA Python library used to convert geopandas vector data into rasterized xarray data. It provides tools for rasterizing point and vector data, handling categorical data mapping, and performing spatial statistics. The library includes a command-line interface (CLI) with the `make_geocube` command for executing core creation logic from the terminal.
What's inside geocube
- geocube is a tool designed to convert GeoPandas vector data into rasterized xarray data. It bridges the gap between vector-based geospatial datasets and multidimensional raster arrays.
Understand the geocube ecosystem and dependencies
mastergeocubeacts as a bridge between several key geospatial and array libraries. It combines the interfaces of:geopandas(vector data handling)xarray(multi-dimensional arrays)rioxarray(raster operations)
Under the hood, it is powered by
GDALand utilizes:rasteriopyogrioodc-geo
Set up geocube for local development
masterTo contribute to
geocube, follow these steps to set up a local development environment:- Fork and Clone: Fork the repository on GitHub and clone your fork locally.
- Virtual Environment: Create a virtual environment and install the package in editable mode with development dependencies.
- Pre-commit: Install and update pre-commit hooks to ensure code quality.
- Branching: Create a new branch for your specific bugfix or feature.
$ git clone git@github.com:your_name_here/geocube.git $ python -m venv geocube_env $ cd geocube/ $ pip install -e .[dev] $ pre-commit install $ pre-commit autoupdate $ git checkout -b name-of-your-bugfix-or-featureVerify changes with linting and tests
masterBefore committing your changes, ensure they pass linting (flake8), formatting (black), and the test suite. You can use the provided
Makefilecommands or run the tools directly.Using Makefile (Recommended):
$ make check $ make testDirect commands (if Makefile is unavailable):
$ flake8 geocube/ test/ $ black --check . $ pytestInstall geocube via conda
masterTo install
geocubeusingconda, it is recommended to use theconda-forgechannel. It is best practice to install the package into a new environment rather than your base environment to ensure stability and easier debugging.Warning: Avoid using
pip installwithin a conda environment if possible. If a package is missing fromconda-forge, consider submitting a recipe instead.conda config --prepend channels conda-forge conda config --set channel_priority strict conda create -n geocube_env geocube conda activate geocube_envInstall geocube from source
masterYou can install
geocubedirectly from the GitHub repository usingpip:python -m pip install git+git://github.com/corteva/geocube.git#egg=geocubeExplore GeoCube usage examples and notebooks
masterGeoCube provides several Jupyter notebooks demonstrating common geospatial processing workflows. You can find specific examples for the following tasks:
- Handling missing data: Managing timestamps with missing data (
timestamp_missing_data.ipynb). - Categorical data: Working with categorical raster values (
categorical.ipynb). - Rasterization:
- Converting point data to rasters (
rasterize_point_data.ipynb). - Using custom functions for rasterization (
rasterize_function.ipynb).
- Converting point data to rasters (
- Vectorization and Mapping:
- Converting grids to vector maps (
grid_to_vector_map.ipynb). - Converting raster data to vector formats (
vectorize.ipynb).
- Converting grids to vector maps (
- Spatial Statistics: Calculating statistics within specific areas (
zonal_statistics.ipynb).
- Handling missing data: Managing timestamps with missing data (
Pull Request Guidelines
masterWhen submitting a pull request for
geocube, ensure the following requirements are met:- Tests: The pull request must include relevant tests.
- Documentation: If adding new functionality, update the documentation. New functionality should be encapsulated in a function with a docstring, and the feature should be added to the list in
README.rst. - Python Compatibility: The code must work for Python versions 3.12, 3.13, and 3.14.
Install geocube for local development
masterIf you are developing on
geocubeand want to install it in editable mode with development dependencies, follow these steps:git clone git@github.com:corteva/geocube.git cd geocube python -m pip install -e .[dev]Install geocube via pip
masterTo install the stable release of
geocubeusingpip, run the following command in your terminal:python -m pip install geocubeRasterize vector data with make_geocube
masterThe primary entry point for
geocubeisgeocube.api.core.make_geocube. You can use it to rasterize vector files (like GeoPackage) orgeopandas.GeoDataFrameobjects.To rasterize a single column from a file, provide the file path to
vector_data, specify the column(s) inmeasurements, and define the pixelresolution.Once the
xarray.Datasetis created, you can export the result usingrioxarray's.rio.to_raster()method for GeoTIFFs orxarray's.to_netcdf()method for netCDF files.from geocube.api.core import make_geocube out_grid = make_geocube( vector_data="path_to_file.gpkg", measurements=["column_name"], resolution=(-0.0001, 0.0001), ) # Export to GeoTIFF using rioxarray out_grid["column_name"].rio.to_raster("my_rasterized_column.tif")Handle temporal data in VectorToCube
masterTo include time-series data in your cube, specify the attribute names in the
datetime_measurementsparameter duringVectorToCubeinitialization.VectorToCubewill:- Convert the specified columns to
pandas.to_datetime. - Attempt to convert them to UTC.
- Localize them to
Noneand cast them todatetime64[ns]. - If these measurements are used in a grouped context, the resulting
xarray.Datasetwill include attributes for units ("seconds from 1970-01-01T00:00:00") and set the_FillValueto0.
- Convert the specified columns to