cropmaps package
Submodules
cropmaps.clipper module
- class cropmaps.clipper.Clipper
Bases:
object- static clipByMask(image, shapefile, store=None, band=None, new=None, resize=False, method=None, ext='tif', verbose=False, compress=False, force_update=False)
Mask image based on a shapefile mask.
- Parameters:
image (senimage) – senimage object
shapefile (str) – Path to shapefile mask
store (str, optional) – Path to store data. If None stores the masked data inside the default satellite data structure. Defaults to None
band (str, optional) – Band to be applied. Note that the band name must be the same with the object attribute name. Defaults to None
new (str, optional) – Piece of string added to the end of the new filename. Defaults to None. If None then it names the new data with the shapefile name
resize (bool, optional) – If True resizes the output resize_val times. Defaults to False
method (rasterio.enum.Resampling, optional) – Resampling method. Defaults to None
ext (str, optional) – Extention of the image. Defaults to ‘tif’
- Raises:
PathError – Raises when cannot find a path to store the new image
BandNotFound – Raises when the image has no attribute band selected by the user
cropmaps.cube module
- cropmaps.cube.convert(x)
- cropmaps.cube.generate_cube_paths(eodata: sentimeseries, bands: list, mask: str | None = None) list
Get all the paths from all the available images in a timeseries.
- Parameters:
eodata (sentimeseries) – Sentinel 2 data timeseries
bands (list) – List of the bands to extract data
mask (str, optional) – If provided then searches for attribute with this specific name. Defaults to None
- Returns:
List of paths
- Return type:
list
- cropmaps.cube.make_cube(listOfPaths: List[str], searchPath: str, newFilename: str, dtype: dtype, nodata: float = -9999, gap_fill: bool = True, harmonize: bool = True, alpha: float = 0.0001, beta: float = 0.0, force_new: bool = False, compress=False) Tuple[List[str], Dict]
Stack satellite images (FROM DIFFERENT FILES) as timeseries cube, without loading them in memory. If there is a datetime field in filename, could enable sort=True, to sort cube layers by date, ascending. Also, if sort=True, dates are written at .txt file which will be saved with the same output name, as cube.
- Parameters:
listOfPaths (List[str]) – Paths of images which will participate in cube.
searchPath (str) – Where the result will be saved. Fullpath, ending to dir.
newFilename (str) – Not a full path. Only the filename, without format ending.
dtype (np.dtype) – Destination datatype.
nodata (float, optional) – No data value. Defaults to -9999
gap_fill (bool, optional) – Fill original nodata values where cloud exists and in feasible. Defaults to True
harmonize (bool, optional) – Apply DN to reflectance convertion. User need to provide alpha and beta from alpha * x + beta. Defaults to True
alpha (float, optional) – Alpha component of alpha in alpha * x + beta. Defaults to 0.0001
beta (float, optional) – Beta component of beta in alpha * x + beta. Defaults to 0
force_new (bool, optional) – Update cube with force if exists. Defaults to False
- Returns:
Bands description in stacked order, metadata of written cube.
- Return type:
Tuple[List[str], Dict]
cropmaps.exceptions module
- exception cropmaps.exceptions.BandNotFound
Bases:
imageErrorError raised when an attribute is not found.
- exception cropmaps.exceptions.GeometryError
Bases:
ExceptionBase class for exceptions in this module.
- exception cropmaps.exceptions.MinMaxCloudBoundError
Bases:
timeseriesErrorError raised when the provided maximum cloud coverage value is less than the minimum cloud coverage value.
- exception cropmaps.exceptions.MinMaxDateError
Bases:
timeseriesErrorError raised when the provided maximum date value is less or equal than the minimum date value.
- exception cropmaps.exceptions.NameNotInTimeSeriesError
Bases:
timeseriesErrorError raised when the provided name is not in the time series object.
- exception cropmaps.exceptions.NoDataError
Bases:
timeseriesErrorError raised when no satellite data was found.
- exception cropmaps.exceptions.NotSameGeometryError
Bases:
GeometryErrorError raised when two geometry objects should be the same and their not.
- exception cropmaps.exceptions.PathError
Bases:
imageErrorRaise when the path is not correct.
- exception cropmaps.exceptions.VegetationIndexNotInList
Bases:
imageErrorError raised when the provided Vegetation Index name is not in the list.
- exception cropmaps.exceptions.imageError
Bases:
ExceptionBase class for exceptions in this module.
- exception cropmaps.exceptions.timeseriesError
Bases:
ExceptionBase class for exceptions in this module.
cropmaps.get_creodias module
cropmaps.logger module
- cropmaps.logger.setup(name=None, level=20, propag=False)
Logger configuration for the project.
cropmaps.models module
- cropmaps.models.LandCover_Masking(landcover_mask: str, predicted: str, results_to: str, fname: str = 'Predictions_Crops.tif')
Masks classification results with WorldCover product to keep only areas with crops.
- Parameters:
landcover_mask (str) – Path to land cover map (returned from sts.sentimeseries.LandCover)
predicted (str) – Path to predicted image
results_to (str) – Store the results
fname (str, optional) – New image name. Defaults to “Predictions_Crops.tif”.
- Returns:
Path of the new image
- Return type:
str
- cropmaps.models.fill_confMatrix(ct: DataFrame, labels: List) DataFrame
Fill a confusion matrix with data.
- Parameters:
ct (pd.DataFrame) – Cross-tabulate object
labels (List) – Labels of the categories
- Returns:
Filled confusion matrix
- Return type:
pd.DataFrame
- cropmaps.models.importance(band_desc: list, feature_importance: ndarray, store_to: str | None = None)
Get the importance of the data per band and per date.
- Parameters:
band_desc (list) – List of the bands
feature_importance (np.ndarray) – Feature importance from sklearn model.features_importance_
store_to (str, optional) – Folder to save results. If None then the importance is printed as information from the logger. Defaults to None
- cropmaps.models.load_model(fname: str) RandomForestClassifier | SVC
Loads a saved model to memory.
- Parameters:
fname (str) – Path to model path
- Returns:
Restored model
- Return type:
Union[RandomForestClassifier, SVC]
- cropmaps.models.random_forest_predict(cube_path: str, model: RandomForestClassifier, results_to: str, fname: str = 'Predictions.tif') str
Use to make predictions using a Random Forest model.
- Parameters:
cube_path (str) – Path to datacube
model (RandomForestClassifier) – The model
results_to (str) – Path to save results
fname (str, optional) – Name of the output predicted image. Defaults to “Predicts.tif”
- Returns:
Path of the predicted image
- Return type:
str
- cropmaps.models.random_forest_predict_patches(cube_path: str, model: RandomForestClassifier, results_to: str, patch_size: tuple = (512, 512), fname: str = 'Predictions.tif') str
Use to make predictions using a RandomForestClassifier model with image patches.
- Parameters:
cube_path (str) – Path to datacube
model (RandomForestClassifier) – The model
results_to (str) – Path to save results
patch_size (tuple, optional) – Size of the image patches. Defaults to (512, 512).
fname (str, optional) – Name of the output predicted image. Defaults to “Predictions.tif”
- Returns:
Path of the predicted image
- Return type:
str
- cropmaps.models.random_forest_train(cube_path: str, gt_fpath: str, results_to: str, test_size: float = 0.33, n_jobs=-1, gridsearch: bool = False, parameters: dict = {'n_estimators': 200}) RandomForestClassifier
Train a model with Random Forest classifier.
- Parameters:
cube_path (str) – Path to datacube
gt_fpath (str) – Path to ground truth data (As a raster. Use cropmaps.prepare_vector.burn to transform vector data to raster.)
results_to (str) – Path to store results
test_size (float, optional) – Test sample size. Defaults to 0.33
gridsearch (bool, optional) – Hyperparameter Tuning using GridSearchCV. Check here: https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.GridSearchCV.html#sklearn.model_selection.GridSearchCV. Defaults to False.
parameters (dict, optional) – RF parameters. Check here: https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.RandomForestClassifier.html#sklearn.ensemble.RandomForestClassifier.
- Returns:
The model
- Return type:
RandomForestClassifier
- cropmaps.models.save_model(model, spath: str, save_as: str = 'model.save') str
Save model to disk.
- Parameters:
model (sklearn.object) – Model to be saved
spath (str) – Path to save the model
save_as (str, optional) – Name of the file with .save extension. Defaults to “model.save”
- Returns:
Path of the saved file
- Return type:
str
- cropmaps.models.svm_predict(cube_path: str, model: SVC, results_to: str, fname: str = 'Predictions.tif') str
Use to make predictions using a SVM model.
- Parameters:
cube_path (str) – Path to datacube
model (RandomForestClassifier) – The model
results_to (str) – Path to save results
fname (str, optional) – Name of the output predicted image. Defaults to “Predicts.tif”
- Returns:
Path of the predicted image
- Return type:
str
- cropmaps.models.svm_predict_patches(cube_path: str, model: SVC, results_to: str, patch_size: tuple = (512, 512), fname: str = 'Predictions.tif') str
Use to make predictions using a SVM model with image patches.
- Parameters:
cube_path (str) – Path to datacube
model (svm.SVC) – The model
results_to (str) – Path to save results
patch_size (tuple, optional) – Size of the image patches. Defaults to (512, 512)
fname (str, optional) – Name of the output predicted image. Defaults to “Predictions.tif”
- Returns:
Path of the predicted image
- Return type:
str
- cropmaps.models.svm_train(cube_path: str, gt_fpath: str, results_to: str, test_size: float = 0.33, gridsearch: bool = False, parameters: dict = {'C': 1.0}) str
Train a model with Random Forest classifier.
- Parameters:
cube_path (str) – Path to datacube
gt_fpath (str) – Path to ground truth data (As a raster. Use cropmaps.prepare_vector.burn to transform vector data to raster.)
results_to (str) – Path to store results
test_size (float, optional) – Test sample size. Defaults to 0.33
gridsearch (bool, optional) – Hyperparameter Tuning using GridSearchCV. Check here: https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.GridSearchCV.html#sklearn.model_selection.GridSearchCV. Defaults to False.
parameters (dict, optional) – SVM parameters. Check here: https://scikit-learn.org/stable/modules/generated/sklearn.svm.SVC.html
- Returns:
The model
- Return type:
svm.SVC
cropmaps.prepare_vector module
- cropmaps.prepare_vector.buffer()
- cropmaps.prepare_vector.burn(shapefile: str, classes: str, metadata: dict, classes_id: str | None = None, save_nomenclature: bool = True, save_to: str | None = None, outfname: str = 'gt.tif') str
Burns vector ground truth data to raster.
- Parameters:
shapefile (str) – Path to shapefile ground truth data
classes (str) – Column name with the categories
metadata (dict) – A dictionary with width, height, crs, transform (Can retrieved from a base image with rasterio.open().meta)
classes_id (str, optional) – Column name with ID for each class. If None then this function creates an ID for each unique category. Defaults to None
save_nomenclature (bool, optional) – Writes the nomenclature data. Defaults to True
save_to (str, optional) – Save path of the results. If None then the data will be written at the same path as the shapefile. Defaults to None
outfname (str, optional) – Name of the ground truth image. Defaults to “gt.tif”
- Returns:
Path to ground truth image data
- Return type:
str
cropmaps.sentinels module
- cropmaps.sentinels.convert(x)
- class cropmaps.sentinels.sentinel2(path, name)
Bases:
objectA Sentinel 2 image.
- apply_cloud_mask(band: str | None = None, store: str | None = None, subregion: str | None = None, resolution: str | None = None, new: str = 'CLOUDMASK', compress=False) None
Apply default SCL mask to S2 images.
- Parameters:
band (str, optional) – Band to apply SCL mask. If None then applies mask to all default bands. Defaults to None.
store (str, optional) – Path to store the new images. If None then the results are saved in the default S2 path. Defaults to None.
subregion (str, optional) – Apply mask to subregion. If subregion is None then applies the mask to the default raw images. Defaults to None.
resolution (str, optional) – Resolution to apply cloud mask. If None the applies to default resolution else tries to apply to the highest 10m resolution. Defaults to None.
new (str, optional) – New name extension. Defaults to “CLOUDMASK”.
- calcVI(index, store=None, subregion=None, verbose: bool = False, compress=False)
Calculates a selected vegetation index (NDVI, NDBI, NDWI). :param index: Vegetation index to be calculated and saved. Currently only NDVI, NDBI, NDWI are supported :type index: str
- getBands()
Finds all the available bands of an image and sets new attributes for each band.
- getmetadata()
Searching for metadata (XML) files.
- static reproj_match(image: str, base: str, to_file: bool = False, outfile: str = 'output.tif', resampling: Resampling = Resampling.nearest, compress=False) None
Reprojects/Resamples an image to a base image. :param image: Path to input file to reproject/resample :type image: str :param base: Path to raster with desired shape and projection :type base: str :param outfile: Path to saving Geotiff :type outfile: str
- static setResolution(band)
Getting band resolution for Sentinel 2. :param band: Band short name as string :type band: str
- Returns:
Band resolution
- Return type:
str
- property show_metadata
Prints metadata using __dict__
- upsample(band=None, store=None, new=None, subregion=None, method=None, ext='tif')
Upsample 20 meters spatial resolution bands to 10 meters.
- Parameters:
store (str, optional) – Path to store data. If None then stores the results to default image path. Defaults to None
band (str, optional) – Band to apply upsample. If None then raises error. Defaults to None
new (str, optional) – Name extension. If None then adds new resolution and _Upsampled. Defaults to None
subregion (str, optional) – Perform upsample to subregion. Defaults to None.
method (Resampling, optional) – Resampling method. If None then applies Resampling.nearest. Defaults to None
ext (str, optional) – Image extension. Defaults to ‘tif’.
- static writeResults(path: str, name: str, array: array, metadata: dict)
Writing a new image with the use of rasterio module. :param path: Path to image :type path: str :param name: Image name :type name: str :param array: Image numpy array :type array: np.ndarray :param metadata: Metadata dictionary :type metadata: dict
cropmaps.sts module
- class cropmaps.sts.sentimeseries(name: str)
Bases:
timeseriesSentinel 2 time series.
- LandCover(store, aoi: str | None = None, outname='LC_mosaic.tif') str
Download and add WorldCover masking data to the object.
- Parameters:
aoi (str) – Path to AOI
write (bool, optional) – Write result to disk. Defaults to False
- Returns:
Path with LC data
- Return type:
str
- apply_SCL(image: sentinel2 | None = None, band: str | None = None, store: str | None = None, subregion: str | None = None, resolution: str | None = None)
Apply default SCL mask to S2 images.
- Parameters:
image (sentinel2, optional) – Apply SCL mask to one image. If None then applies the mask to all the timeseries. Defaults to None.
band (str, optional) – Band to apply SCL mask. If None then applies mask to all default bands. Defaults to None.
store (str, optional) – Path to store the new images. If None then the results are saved in the default S2 path. Defaults to None.
subregion (str, optional) – Apply mask to subregion. If subregion is None then applies the mask to the default raw images. Defaults to None.
resolution (str, optional) – Resolution to apply cloud mask. If None the applies to default resolution else tries to apply to the highest 10m resolution. Defaults to None.
- clipbyMask(shapefile, image=None, band=None, resize=False, method=None, new=None, store=None, force_update=False)
Masks an image or the complete time series with a shapefile.
- Parameters:
shapefile (path-like, str) – Path to shapefile mask
image (senimage, optional) – Masks a specific image. Defaults to None
band (str, optional) – Masks a specific band. Defaults to None
resize (bool, optional) – Resize band. Defaults to False
method (rasterio.enums.Resampling) – Available resampling methods. If None the Nearest is used. Defaults to None
- find(path: str, level: str = 'L2A')
Finds automatically all the available data in a provided path based on the S2 level product (L1C or L2A) that the user provides.
- Parameters:
path (str, path-like) – Search path
level (str, optional) – Level of the S2 time series (L1C or L2A). Defaults to ‘L2A’.
- Raises:
NoDataError – Raises when no data were found in the provided path
- find_DIAS(DIAS_data: list)
- getVI(index: str, store: str | None = None, image: sentinel2 | None = None, subregion=None, verbose=False)
Calculates a vegetation index for an image if the user provides an image or for all the time series.
- Parameters:
index (str) – Vegetation index. Currently works only for NDVI, NDWI, NDBI
image (sentinel2) – If an sentinel2 object is provided calculates VI for this image only
- remove_orbit(orbit)
Remove images with specific orbit.
- Parameters:
orbit (str) – Number of orbit
- upsample(image=None, band=None, method=None, new=None, store=None, subregion=None)
Upsample lower resolution bands to the highest available resolution.
- Parameters:
image (sentinel2, optional) – Image to perform upsampling. If None, then performs upsample to all images in the timeseries. Defaults to None
band (str, optional) – Name of the band to upsample. If None, then performs upsample to all 20m bands. Defaults to None
method (Resampling, optional) – Upsampling method. Defaults to None
new (str, optional) – Name extension. If None the by default adds _Upsampled. Defaults to None
store (str, optional) – Path to store data. If None then stores the data to default image path. Defaults to None
subregion (str, optional) – Perform upsample to subregion. Defaults to None
cropmaps.ts module
- class cropmaps.ts.timeseries(name)
Bases:
objectBase class of time series objects.
- filter_dates(max_date, min_date=None)
Removes from the list of data all the instances with date more than max_date and less than min_date. Date format: DDMMYYYY
- Parameters:
max_date (str) – Maximum date in DDMMYYY format
min_date (str, optional) – Minimum date in DDMMYYY format. Defaults to None
- Raises:
TypeError – Raises if max_date is not a str
ValueError – Raises if max_date is not 8 length str
TypeError – Raises if min_date is not a str
ValueError – Raises if min_date is not 8 length str
MinMaxDateError – Raises if maximum date is sooner than minimum date
- keep_timerange(start_time, end_time)
Keeps all the data that are inside the range of a start_time (HHMMSS format) and an end time (HHMMSS format).
- Parameters:
start_time (str) – Start time in HHMMSS format
end_time (str) – End time in HHMMSS format
- remove_cloudy(max_cloud, min_cloud=0)
Removes from the list of data all the images with cloud coverage that overcomes the maximum and minimum bounds provided by the user.
- Parameters:
max_cloud (int) – Maximum allowed cloud coverage
min_cloud (int, optional) – Mimimum allowed cloud coverage. Defaults to 0
- Raises:
MinMaxCloudBoundError – Raises when maximum cloud coverage is less than the minimum coverage
- remove_date(date)
Removes from the list of data all the instances with a specific date(s) provided by the user. Date format: DDMMYYYY
- Parameters:
date (str, list) – Date(s) to remove in DDMMYYYY format
- Raises:
TypeError – Raises if date is not a str or a list
ValueError – Raises if date value is not a 8 length str
- show_metadata(name=None)
Prints all the metadata of all available data of the time series or of a selected image.
- Parameters:
name (str, optional) – Name of the image to print its metadata. Defaults to None
- Raises:
NameNotInTimeSeriesError – Raises when the provided name is not in the list of data
- sort_images(cloud_coverage=False, date=False)
Sort images based on name. If cloud coverage is True then sorts based on cloud coverage. If date is True then sorts based on dates.
- Parameters:
cloud_coverage (bool, optional) – Sort by cloud coverage. Defaults to False
date (bool, optional) – Sort by dates. Defaults to False
cropmaps.utils module
- cropmaps.utils.worldcover(geometry: Polygon, savepath: str) GeoDataFrame
Downloads landcover maps from worldcover project :param geometry: Path to AOI file to download data or a list with geometries :type geometry: shapely.geometry.polygon.Polygon :param savepath: Path to store data :type savepath: str
- Returns:
Downloaded tiles
- Return type:
gpd.GeoDataFrame
cropmaps.vi module
- class cropmaps.vi.vi
Bases:
objectA collection of functions for Vegetation Incices in Python. Created for use with Sentinel 2. Sources: http://www.sentinel-hub.com/eotaxonomy/indices
- static ndbi(swir, nir)
Normalized Difference Buildings Index
- General formula:
- \[(SWIR — NIR)/(SWIR + NIR)\]
- Sentinel 2:
- \[(B11 - B08) / (B11 + B08)\]
- Parameters:
swir (ndarray) – SWIR numpy array
nir (ndarray) – NIR numpy array
- Returns:
NDBI numpy array
- Return type:
ndarray
- static ndvi(red, nir)
Normalized Difference Vegetation Index General formula:
\[(NIR — VIS)/(NIR + VIS)\]- Sentinel 2:
- \[(B08 - B04) / (B08 + B04)\]
- Parameters:
red (ndarray) – Red numpy array
nir (ndarray) – NIR numpy array
- Returns:
NDVI numpy array
- Return type:
ndarray
- static ndwi(green, nir)
Normalized Difference Water Index
- General formula:
- \[(GREEN — NIR)/(GREEN + NIR)\]
- Sentinel 2:
- \[(B03 - B08) / (B03 + B08)\]
- Parameters:
green (ndarray) – Green numpy array
nir (ndarray) – NIR numpy array
- Returns:
NDWI numpy array
- Return type:
ndarray