API Reference¶
NickySpatial: An open-source object-based image analysis library for remote sensing¶
NickySpatial is a Python package for object-based image analysis, providing functionality similar to commercial software like eCognition.
Key features: - Multiresolution segmentation - Object-based analysis - Rule-based classification - Statistics calculation - Integration with geospatial data formats
nickyspatial.core
¶
The core package encompasses fundamental data structures and algorithms for nickyspatial.
It helps define the building blocks like layers, segmentation methods, and rule-based logic for object analysis workflows.
Implements supervised classification algorithms to classify the segments.
SupervisedClassifier
¶
Implementation of Supervised Classification algorithm.
Source code in nickyspatial/core/classifier.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
|
__init__(name=None, classifier_type='Random Forests', classifier_params=None)
¶
Initialize the segmentation algorithm.
Parameters:¶
scale : str classifier type name eg: RF for Random Forest, SVC for Support Vector Classifier classifier_params : dict additional parameters relayed to classifier
Source code in nickyspatial/core/classifier.py
execute(source_layer, samples, layer_manager=None, layer_name=None, features=None)
¶
Execute the supervised classification workflow on the source layer.
This method creates a new layer by copying the input source layer, training a classifier using provided samples, predicting classifications, and storing the results in a new layer. Optionally, the resulting layer can be added to a layer manager.
Parameters¶
source_layer : Layer The input layer containing spatial objects and metadata (transform, CRS, raster). samples : dict A dictionary of training samples where keys are class labels and values are lists of segment IDs or features used for training. Default is an empty dictionary. layer_manager : LayerManager, optional An optional layer manager object used to manage and store the resulting layer. layer_name : str, optional The name to assign to the resulting classified layer.
Returns:¶
Layer A new Layer object containing the predicted classifications, copied metadata from the source layer, and updated attributes.
Source code in nickyspatial/core/classifier.py
Defines the Layer class and related functionality for organizing geospatial data.
A layer can represent a conceptual container for a vector object which is tightly coupled with underlying raster data, allowing additional metadata or processing logic to be attached making sure heirarchical relationships are maintained. This module provides the Layer and LayerManager classes, which manage layers of geospatial data, including segmentation results, classifications,and filters. Layers can be created, copied, and manipulated, and they support attaching functions to calculate additional properties.
Layer
¶
A Layer represents a set of objects (segments or classification results) with associated properties.
Layers can be derived from segmentation, rule application, or filters. Each layer can have functions attached to calculate additional properties.
Source code in nickyspatial/core/layer.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
|
__init__(name=None, parent=None, type='generic')
¶
Initialize a Layer.
Parameters:¶
name : str, optional Name of the layer. If None, a unique name will be generated. parent : Layer, optional Parent layer that this layer is derived from. type : str Type of layer: "segmentation", "classification", "filter", or "generic"
Source code in nickyspatial/core/layer.py
__str__()
¶
String representation of the layer.
Source code in nickyspatial/core/layer.py
attach_function(function, name=None, **kwargs)
¶
Attach a function to this layer and execute it.
Parameters:¶
function : callable Function to attach and execute name : str, optional Name for this function. If None, uses function.name **kwargs : dict Arguments to pass to the function
Returns:¶
self : Layer Returns self for chaining
Source code in nickyspatial/core/layer.py
copy()
¶
Create a copy of this layer.
Returns:¶
layer_copy : Layer Copy of this layer
Source code in nickyspatial/core/layer.py
get_function_result(function_name)
¶
Get the result of an attached function.
Parameters:¶
function_name : str Name of the attached function
Returns:¶
result : any Result of the function
Source code in nickyspatial/core/layer.py
LayerManager
¶
Manages a collection of layers and their relationships.
Source code in nickyspatial/core/layer.py
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
|
__init__()
¶
add_layer(layer, set_active=True)
¶
Add a layer to the manager.
Parameters:¶
layer : Layer Layer to add set_active : bool Whether to set this layer as the active layer
Returns:¶
layer : Layer The added layer
Source code in nickyspatial/core/layer.py
get_layer(layer_id_or_name)
¶
Get a layer by ID or name.
Parameters:¶
layer_id_or_name : str Layer ID or name
Returns:¶
layer : Layer The requested layer
Source code in nickyspatial/core/layer.py
remove_layer(layer_id_or_name)
¶
Remove a layer from the manager.
Parameters:¶
layer_id_or_name : str Layer ID or name
Source code in nickyspatial/core/layer.py
Provides a rule engine for object-based analysis, where segments or layers are processed according to custom logic.
Main idea here is to allow encode expert rules that can be applied to object segments which are layers in a nickyspatial context. So rules are tied up to the layers , they can be attached or revoked or executed multiple items
Developers can define domain-specific rules to classify or merge features based on attributes. This module includes the Rule and RuleSet classes, which allow users to create, manage, and apply rules to layers. The RuleSet class can be used to group multiple rules together, and the execute method applies these rules to a given layer. The rules can be defined using string expressions that can be evaluated using the numexpr library for performance.
CommonBase
¶
A shared utility base class for spatial rule sets.
This class provides common methods used by multiple rule sets to preprocess layer data and determine spatial relationships between segments.
Source code in nickyspatial/core/rules.py
EnclosedByRuleSet
¶
Bases: CommonBase
A rule set to reclassify segments based on spatial enclosure.
This rule set identifies segments of one class (A) that are entirely surrounded by segments of another class (B), and reclassifies them into a new class.
Source code in nickyspatial/core/rules.py
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 |
|
__init__(name=None)
¶
execute(source_layer, class_column_name, class_value_a, class_value_b, new_class_name, layer_manager=None, layer_name=None)
¶
Apply enclosed-by logic to identify and reclassify segments.
Parameters:¶
source_layer : Layer The source spatial layer containing segments. class_column_name : str The name of the column containing class labels (e.g., "veg_class"). class_value_a : str The class value to check for enclosure (target to reclassify). class_value_b : str The class value expected to surround class A segments. new_class_name : str The new class name to assign to enclosed segments. layer_manager : LayerManager, optional Optional manager to register the resulting layer. layer_name : str, optional Optional name for the result layer.
Returns:¶
result_layer : Layer A new layer with updated class values for enclosed segments.
Source code in nickyspatial/core/rules.py
MergeRuleSet
¶
Bases: CommonBase
A rule set for merging segments of the same class based on specified class values.
Source code in nickyspatial/core/rules.py
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 |
|
__init__(name=None)
¶
execute(source_layer, class_column_name, class_value, layer_manager=None, layer_name=None)
¶
Merge segments of the same class in a layer.
Parameters:¶
source_layer : Layer Source layer with segments to merge class_value : str or list of str One or more attribute field names to group and merge segments layer_manager : LayerManager, optional Layer manager to add the result layer to layer_name : str, optional Name for the result layer
Returns:¶
result_layer : Layer Layer with merged geometries
Source code in nickyspatial/core/rules.py
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 |
|
Rule
¶
A rule defines a condition to classify segments.
Source code in nickyspatial/core/rules.py
__init__(name, condition, class_value=None)
¶
Initialize a rule.
Parameters:¶
name : str Name of the rule condition : str Condition as a string expression that can be evaluated using numexpr class_value : str, optional Value to assign when the condition is met. If None, uses the rule name.
Source code in nickyspatial/core/rules.py
RuleSet
¶
A collection of rules to apply to a layer.
Source code in nickyspatial/core/rules.py
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
|
add_rule(name, condition, class_value=None)
¶
Add a rule to the rule set.
Parameters:¶
name : str Name of the rule condition : str Condition as a string expression that can be evaluated using numexpr class_value : str, optional Value to assign when the condition is met
Returns:¶
rule : Rule The added rule
Source code in nickyspatial/core/rules.py
execute(source_layer, layer_manager=None, layer_name=None, result_field='classification')
¶
Apply rules to classify segments in a layer.
Parameters:¶
source_layer : Layer Source layer with segments to classify layer_manager : LayerManager, optional Layer manager to add the result layer to layer_name : str, optional Name for the result layer result_field : str Field name to store classification results
Returns:¶
result_layer : Layer Layer with classification results
Source code in nickyspatial/core/rules.py
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
|
get_rules()
¶
Get the list of rules in the rule set.
Returns:¶
list of tuples List of (name, condition) tuples for each rule
wrap_condition_parts_simple(condition)
staticmethod
¶
Wrap condition parts with parentheses for evaluation.
TouchedByRuleSet
¶
Bases: CommonBase
A rule set to reclassify segments based on spatial enclosure.
This rule set identifies segments of one class (A) that are entirely surrounded by segments of another class (B), and reclassifies them into a new class.
Source code in nickyspatial/core/rules.py
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 |
|
__init__(name=None)
¶
execute(source_layer, class_column_name, class_value_a, class_value_b, new_class_name, layer_manager=None, layer_name=None)
¶
Executes the merge rule set by identifying and updating segments of a given class that are adjacent to another class!
- source_layer: Layer The input layer containing segment geometries and attributes.
- class_column_name: str The name of the column containing class labels.
- class_value_a: str or int The class value of segments to be checked for touching neighbors.
- class_value_b: str or int The class value of neighboring segments that would trigger a merge.
- new_class_name: str The new class value to assign to segments of class_value_a that touch class_value_b.
- layer_manager: optional An optional manager for adding the resulting layer to a collection or interface.
- layer_name: optional
Optional custom name for the resulting layer. Defaults to "
_ ".
- result_layer: Layer A new Layer object with updated segment classifications where applicable.
Logic: - Copies the source layer and initializes a new result layer. - Preprocesses the source layer to build geometry and class lookup maps. - Iterates through each segment of class_value_a, checking if any of its neighbors belong to class_value_b. - If so, updates the segment's class to new_class_name. - Stores the modified DataFrame in the result layer and optionally registers it via the layer_manager.
Source code in nickyspatial/core/rules.py
Implements segmentation algorithms to partition images into meaningful region objects.
The functions here might apply clustering or region-growing techniques, aiding object-based remote sensing analysis. This module includes the SlicSegmentation class, which implements a bottom-up region-growing algorithm
SlicSegmentation
¶
Implementation of Multiresolution segmentation algorithm.
This algorithm segments an image using a bottom-up region-growing approach that optimizes the homogeneity of pixel values within segments while considering shape compactness.
Source code in nickyspatial/core/segmentation.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
|
__init__(scale=15, compactness=0.6)
¶
Initialize the segmentation algorithm.
Parameters:¶
scale : float Scale parameter that influences the size of the segments. Higher values create larger segments. shape : float, range [0, 1] Weight of shape criterion vs. color criterion. Higher values give more weight to shape. compactness : float, range [0, 1] Weight of compactness criterion vs. smoothness criterion. Higher values create more compact segments.
Source code in nickyspatial/core/segmentation.py
execute(image_data, transform, crs, layer_manager=None, layer_name=None)
¶
Perform segmentation and create a layer with the results.
Parameters:¶
image_data : numpy.ndarray Array with raster data values (bands, height, width) transform : affine.Affine Affine transformation for the raster crs : rasterio.crs.CRS Coordinate reference system layer_manager : LayerManager, optional Layer manager to add the result layer to layer_name : str, optional Name for the result layer
Returns:¶
layer : Layer Layer containing the segmentation results
Source code in nickyspatial/core/segmentation.py
nickyspatial.filters
¶
The filters package provides modules for applying transformations to raster data.
It includes spatial filters (e.g., smoothing) as well as spectral filters (e.g., band math). Main idea is to further manipulate the objects such as merging segments or applying pre-defined rules to filter objects based on their attributes.
Implements spatial operations like smoothing and morphological transformations.
These filters can modify the geometry or arrangement of pixel values to enhance or simplify data for object analysis. The functions here include smoothing boundaries, merging small segments, and selecting segments based on area. These operations are essential for preparing data for object-based image analysis, especially in remote sensing applications. The functions are designed to work with raster data and can be applied to layers created from segmentation algorithms.
merge_small_segments(source_layer, min_size, attribute='area_pixels', layer_manager=None, layer_name=None)
¶
Merge small segments with their largest neighbor.
Parameters:¶
source_layer : Layer Source layer with segments to merge min_size : float Minimum segment size threshold attribute : str Attribute to use for size comparison layer_manager : LayerManager, optional Layer manager to add the result layer to layer_name : str, optional Name for the result layer
Returns:¶
result_layer : Layer Layer with merged segments
Source code in nickyspatial/filters/spatial.py
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
|
select_by_area(source_layer, min_area=None, max_area=None, area_column='area_units', layer_manager=None, layer_name=None)
¶
Select segments based on area.
Parameters:¶
source_layer : Layer Source layer with segments to filter min_area : float, optional Minimum area threshold max_area : float, optional Maximum area threshold area_column : str Column containing area values layer_manager : LayerManager, optional Layer manager to add the result layer to layer_name : str, optional Name for the result layer
Returns:¶
result_layer : Layer Layer with filtered segments
Source code in nickyspatial/filters/spatial.py
smooth_boundaries(source_layer, iterations=1, layer_manager=None, layer_name=None)
¶
Smooth segment boundaries by applying morphological operations.
Parameters:¶
source_layer : Layer Source layer with segments to smooth iterations : int Number of smoothing iterations to apply layer_manager : LayerManager, optional Layer manager to add the result layer to layer_name : str, optional Name for the result layer
Returns:¶
result_layer : Layer Layer with smoothed segment boundaries
Source code in nickyspatial/filters/spatial.py
Performs spectral-based manipulations of imagery, including band arithmetic and transformations.
It supports generating new spectral bands or combinations to highlight specific features. It also includes functions for enhancing contrast and applying spectral filters based on mathematical expressions. This module is designed to work with raster . The functions here include contrast enhancement, spectral filtering, and band arithmetic. Not a great fan of these but might be handy sometime
enhance_contrast(source_layer, percentile_min=2, percentile_max=98, layer_manager=None, layer_name=None)
¶
Enhance contrast in source layer raster data.
Parameters:¶
source_layer : Layer Source layer with raster data percentile_min : float Lower percentile for contrast stretching percentile_max : float Upper percentile for contrast stretching layer_manager : LayerManager, optional Layer manager to add the result layer to layer_name : str, optional Name for the result layer
Returns:¶
result_layer : Layer Layer with enhanced contrast
Source code in nickyspatial/filters/spectral.py
spectral_filter(source_layer, expression, layer_manager=None, layer_name=None)
¶
Apply a spectral filter based on a mathematical expression.
Parameters:¶
source_layer : Layer Source layer with segment statistics expression : str Mathematical expression to apply (e.g., "NDVI > 0.5") layer_manager : LayerManager, optional Layer manager to add the result layer to layer_name : str, optional Name for the result layer
Returns:¶
result_layer : Layer Layer with filtered segments
Source code in nickyspatial/filters/spectral.py
nickyspatial.io
¶
The io package contains modules for reading and writing both raster and vector data.
It abstracts file operations and coordinate system handling to facilitate I/O tasks.
Handles raster input and output operations, including reading and saving multi-band images.
Functions in this module may also provide metadata parsing and coordinate transform tools.
layer_to_raster(layer, output_path, column=None, nodata=0)
¶
Save a layer to a raster file.
Parameters:¶
layer : Layer Layer to save output_path : str Path to the output raster file column : str, optional Column to rasterize (if saving from vector objects) nodata : int or float, optional No data value
Source code in nickyspatial/io/raster.py
read_raster(raster_path)
¶
Read a raster file and return its data, transform, and CRS.
Parameters:¶
raster_path : str Path to the raster file
Returns:¶
image_data : numpy.ndarray Array with raster data values transform : affine.Affine Affine transformation for the raster crs : rasterio.crs.CRS Coordinate reference system
Source code in nickyspatial/io/raster.py
write_raster(output_path, data, transform, crs, nodata=None)
¶
Write raster data to a file.
Parameters:¶
output_path : str Path to the output raster file data : numpy.ndarray Array with raster data values transform : affine.Affine Affine transformation for the raster crs : rasterio.crs.CRS Coordinate reference system nodata : int or float, optional No data value
Source code in nickyspatial/io/raster.py
Manages vector data I/O, supporting formats like Shapefile and GeoJSON.
This module typically offers utilities for handling attributes, geometries, and coordinate reference systems.
layer_to_vector(layer, output_path)
¶
Save a layer's objects to a vector file.
Parameters:¶
layer : Layer Layer to save output_path : str Path to the output vector file
Source code in nickyspatial/io/vector.py
read_vector(vector_path)
¶
write_vector(gdf, output_path)
¶
Write a GeoDataFrame to a vector file.
Parameters:¶
gdf : geopandas.GeoDataFrame GeoDataFrame to write output_path : str Path to the output vector file
Source code in nickyspatial/io/vector.py
nickyspatial.stats
¶
The stats package includes modules for calculating statistical metrics on objects.
This is to mimic the stats module in ecognition because they will be necessary in order to apply rules later on and extremely essential to work with objects
Basic statistics for layers in NickySpatial.
attach_basic_stats(layer, column, prefix=None)
¶
Attach basic statistics for a column to a layer.
Parameters:¶
layer : Layer Layer to attach statistics to column : str Column to calculate statistics for prefix : str, optional Prefix for result names
Returns:¶
stats : dict Dictionary with calculated statistics
Source code in nickyspatial/stats/basic.py
attach_class_distribution(layer, class_column='classification')
¶
Calculate the distribution of classes in a layer.
Parameters:¶
layer : Layer Layer to analyze class_column : str Column containing class values
Returns:¶
distribution : dict Dictionary with class counts and percentages
Source code in nickyspatial/stats/basic.py
attach_count(layer, class_column='classification', class_value=None)
¶
Count objects in a layer, optionally filtered by class.
Parameters:¶
layer : Layer Layer to count objects in class_column : str Column containing class values class_value : str, optional Class value to filter by
Returns:¶
count : int Number of objects
Source code in nickyspatial/stats/basic.py
Spatial statistics for layers in NickySpatial.
attach_area_stats(layer, area_column='area_units', by_class=None)
¶
Calculate area statistics for objects in a layer.
Parameters:¶
layer : Layer Layer to calculate statistics for area_column : str Column containing area values by_class : str, optional Column to group by (e.g., 'classification')
Returns:¶
stats : dict Dictionary with area statistics
Source code in nickyspatial/stats/spatial.py
attach_neighbor_stats(layer)
¶
Calculate neighborhood statistics for objects in a layer.
Parameters:¶
layer : Layer Layer to calculate statistics for
Returns:¶
stats : dict Dictionary with neighborhood statistics
Source code in nickyspatial/stats/spatial.py
attach_shape_metrics(layer)
¶
Calculate shape metrics for objects in a layer.
Parameters:¶
layer : Layer Layer to calculate metrics for
Returns:¶
metrics : dict Dictionary with shape metrics
Source code in nickyspatial/stats/spatial.py
Spectral indices calculation module.
attach_ndvi(layer, nir_column='NIR_mean', red_column='Red_mean', output_column='NDVI')
¶
Calculate NDVI (Normalized Difference Vegetation Index) for objects in a layer.
Parameters:¶
layer : Layer Layer to calculate NDVI for nir_column : str Column containing NIR band values red_column : str Column containing Red band values output_column : str Column to store NDVI values
Returns:¶
ndvi_stats : dict Dictionary with NDVI statistics
Source code in nickyspatial/stats/spectral.py
attach_spectral_indices(layer, bands=None)
¶
Calculate multiple spectral indices for objects in a layer.
Parameters:¶
layer : Layer Layer to calculate indices for bands : dict, optional Dictionary mapping band names to column names
Returns:¶
indices : dict Dictionary with calculated indices
Source code in nickyspatial/stats/spectral.py
nickyspatial.utils
¶
This modules contains utility functions for handling attributes, geometries, and coordinate reference systems.
Helpers , Aren't they useful ?
calculate_statistics_summary(layer_manager, output_file=None)
¶
Calculate summary statistics for all layers in a layer manager.
Parameters:¶
layer_manager : LayerManager Layer manager containing layers output_file : str, optional Path to save the summary to (as JSON)
Returns:¶
summary : dict Dictionary with summary statistics
Source code in nickyspatial/utils/helpers.py
create_sample_data()
¶
Create a synthetic 4-band (B, G, R, NIR) image for testing.
Returns:¶
image_data : numpy.ndarray Synthetic image data transform : affine.Affine Affine transformation for the raster crs : rasterio.crs.CRS Coordinate reference system
Source code in nickyspatial/utils/helpers.py
get_band_statistics(image_data, band_names=None)
¶
Calculate statistics for each band in a raster image.
Parameters:¶
image_data : numpy.ndarray Raster image data (bands, height, width) band_names : list of str, optional Names of the bands
Returns:¶
stats : dict Dictionary with band statistics
Source code in nickyspatial/utils/helpers.py
memory_usage(layer)
¶
Estimate memory usage of a layer in MB.
Parameters:¶
layer : Layer Layer to calculate memory usage for
Returns:¶
memory_mb : float Estimated memory usage in MB
Source code in nickyspatial/utils/helpers.py
nickyspatial.viz
¶
Alrighty , let's get this visualization party started!
No matter what you do you need to see it and present it , this is the module for it to contain all code about visualizaing the layers , object rule results etc.
Visualization functions for plotting histograms, statistics, and scatter plots.
plot_histogram(layer, attribute, bins=20, figsize=(10, 6), by_class=None)
¶
Plot a histogram of attribute values.
Parameters:¶
layer : Layer Layer containing data attribute : str Attribute to plot bins : int Number of bins figsize : tuple Figure size by_class : str, optional Column to group by (e.g., 'classification')
Returns:¶
fig : matplotlib.figure.Figure Figure object
Source code in nickyspatial/viz/charts.py
plot_scatter(layer, x_attribute, y_attribute, color_by=None, figsize=(10, 8))
¶
Create a scatter plot of two attributes.
Parameters:¶
layer : Layer Layer containing data x_attribute : str Attribute for x-axis y_attribute : str Attribute for y-axis color_by : str, optional Attribute to color points by figsize : tuple Figure size
Returns:¶
fig : matplotlib.figure.Figure Figure object
Source code in nickyspatial/viz/charts.py
plot_statistics(layer, stats_dict, figsize=(12, 8), kind='bar', y_log=False)
¶
Plot statistics from a statistics dictionary.
Parameters:¶
layer : Layer Layer the statistics are calculated for stats_dict : dict Dictionary with statistics (from attach_* functions) figsize : tuple Figure size kind : str Plot type: 'bar', 'line', or 'pie' y_log : bool Whether to use logarithmic scale for y-axis
Returns:¶
fig : matplotlib.figure.Figure Figure object
Source code in nickyspatial/viz/charts.py
Functions to create maps and visualize layers.
plot_classification(layer, class_field='classification', figsize=(12, 10), legend=True, class_color=None)
¶
Plot classified segments with different colors for each class.
Source code in nickyspatial/viz/maps.py
plot_comparison(before_layer, after_layer, attribute=None, class_field=None, figsize=(16, 8), title=None)
¶
Plot before and after views of layers for comparison.
Source code in nickyspatial/viz/maps.py
plot_layer(layer, image_data=None, attribute=None, title=None, rgb_bands=(2, 1, 0), figsize=(12, 10), cmap='viridis', show_boundaries=False)
¶
Plot a layer, optionally with an attribute or image backdrop.
Source code in nickyspatial/viz/maps.py
plot_layer_interactive(layer, image_data=None, figsize=(10, 8))
¶
Interactive plot of a layer with widgets and working click.
Source code in nickyspatial/viz/maps.py
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
|
plot_layer_interactive_plotly(layer, image_data, rgb_bands=(0, 1, 2), show_boundaries=True, figsize=(800, 400))
¶
Display an interactive RGB image with segment boundaries and hoverable segment IDs using Plotly.
Run in google collab as well.
Parameters:¶
layer : object
An object with a .raster
attribute representing the labeled segmentation layer
(e.g., output from a segmentation algorithm, such as SLIC).
image_data : image data to be visualized.
rgb_bands : tuple of int, optional
Tuple of three integers specifying which bands to use for the RGB composite (default is (0, 1, 2)).
show_boundaries : bool, optional
Whether to overlay the segment boundaries on the RGB image (default is True).
figsize : tuple of int, optional
Tuple specifying the width and height of the interactive Plotly figure in pixels (default is (800, 400)).
Returns:¶
None The function displays the interactive plot directly in the output cell in a Jupyter Notebook.
Notes:¶
- Segment boundaries are drawn using
skimage.segmentation.mark_boundaries
. - Hovering over the image displays the segment ID from
layer.raster
.
Source code in nickyspatial/viz/maps.py
plot_sample(layer, image_data=None, transform=None, rgb_bands=None, class_field='classification', figsize=(8, 6), class_color=None, legend=True)
¶
Plot classified segments on top of RGB or grayscale image data.
Parameters: - layer: Layer object with .objects (GeoDataFrame) - image_data: 3D numpy array (bands, height, width) - transform: Affine transform for the image (needed to compute extent) - red_band, green_band, blue_band: indices for RGB bands
Source code in nickyspatial/viz/maps.py
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 |
|