Utilities
estimate_average_bounds
estimate_average_bounds(gdf, quantile=0.9)
Estimates the average size of all features in a GeoDataFrame.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
gdf |
GeoDataFrame
|
GeoDataFrame that contains all features (i.e. shapely.Geometry objects) |
required |
quantile |
float
|
what quantile will represent the feature population |
0.9
|
Returns:
Type | Description |
---|---|
Tuple[float, float]
|
a tuple of floats representing average width and height |
Source code in geococo\utils.py
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
|
estimate_schema
estimate_schema(
gdf,
src,
quantile=0.9,
window_bounds=[(256, 256), (512, 512)],
)
Attempts to find a schema that is able to represent the average GeoDataFrame feature (i.e. sufficient overlap) but within the bounds given by window_bounds.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
gdf |
GeoDataFrame
|
GeoDataFrame that contains features that determine the degree of overlap |
required |
src |
DatasetReader
|
The rasterio DataSource associated with the resulting schema (i.e. bounds and pixelsizes) |
required |
quantile |
float
|
what quantile will represent the feature population |
0.9
|
window_bounds |
List[Tuple[int, int]]
|
a list of possible limits for the window generators |
[(256, 256), (512, 512)]
|
Returns:
Type | Description |
---|---|
WindowSchema
|
(if found) a viable WindowSchema with sufficient overlap within the window_bounds |
Source code in geococo\utils.py
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 |
|
generate_window_offsets
generate_window_offsets(window, schema)
Computes an array of window offsets bound by a given window.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
window |
Window
|
the bounding window (i.e. offsets will be within its bounds) |
required |
schema |
WindowSchema
|
the parameters for the window generator |
required |
Returns:
Type | Description |
---|---|
ndarray
|
an array of window offsets within the bounds of window |
Source code in geococo\utils.py
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
|
generate_window_polygon
generate_window_polygon(datasource, window)
Turns the spatial bounds of a given window to a shapely.Polygon object in a given dataset's CRS.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
datasource |
DatasetReader
|
a rasterio DatasetReader object that provides the affine transformation |
required |
window |
Window
|
bounds to represent as Polygon |
required |
Returns:
Type | Description |
---|---|
Polygon
|
shapely Polygon representing the spatial bounds of a given window in a given CRS |
Source code in geococo\utils.py
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
|
get_date_created
get_date_created(raster_source)
Get the creation date of the input image, represented as a datetime object. If no such information is available in the image's metadata, we return the date the file itself was last modified.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
raster_source |
DatasetReader
|
reader for input image |
required |
Returns:
Type | Description |
---|---|
datetime
|
datetime object representing date_created |
Source code in geococo\utils.py
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 |
|
mask_label
mask_label(input_raster, label)
Masks out an label from input_raster and flattens it to a 2D binary array. If it doesn't overlap, the resulting mask will only consist of False bools.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_raster |
DatasetReader
|
open rasterio DatasetReader for the input raster |
required |
label |
Union[Polygon, MultiPolygon]
|
Polygon object representing the area to be masked (i.e. label) |
required |
Returns:
Type | Description |
---|---|
ndarray
|
A 2D binary array representing the label |
Source code in geococo\utils.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
|
reshape_image
reshape_image(img_array, shape, padding_value=0)
Reshapes 3D numpy array to match given 3D shape, done through slicing or padding.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
img_array |
ndarray
|
the numpy array to be reshaped |
required |
shape |
Tuple[int, int, int]
|
the desired shape (bands, rows, cols) |
required |
padding_value |
int
|
what value to pad img_array with (if too small) |
0
|
Returns:
Type | Description |
---|---|
ndarray
|
numpy array in desired shape |
Source code in geococo\utils.py
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
|
update_labels
update_labels(
labels,
categories,
id_attribute="category_id",
name_attribute=None,
)
Updates labels with validated (super)category names and ids from given Category instances (i.e. source of truth created from current and previous labels). This ultimately just matches a given key (name or id) with keys in each Category instance and maps the associated (and validated) values to labels.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
labels |
GeoDataFrame
|
GeoDataFrame containing labels and category attributes (validated by validate_labels) |
required |
categories |
List[Category]
|
list of Category instances created from current and previous labels |
required |
id_attribute |
Optional[str]
|
Column name that holds category_id values |
'category_id'
|
name_attribute |
Optional[str]
|
Column name that holds category_name values |
None
|
Returns:
Type | Description |
---|---|
GeoDataFrame
|
labels with name, id and supercategory attributes from all given Category instances |
Source code in geococo\utils.py
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 |
|
validate_labels
validate_labels(
labels,
id_attribute="category_id",
name_attribute=None,
super_attribute=None,
)
Validates all necessary attributes for a geococo-viable GeoDataFrame. It also checks for the presence of either category_id or category_name values and ensures valid geometry.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
labels |
GeoDataFrame
|
GeoDataFrame containing labels and category attributes |
required |
id_attribute |
Optional[str]
|
Column name that holds category_id values |
'category_id'
|
name_attribute |
Optional[str]
|
Column name that holds category_name values |
None
|
super_attribute |
Optional[str]
|
Column name that holds supercategory values |
None
|
Returns:
Type | Description |
---|---|
GeoDataFrame
|
Validated GeoDataFrame with coerced dtypes |
Source code in geococo\utils.py
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 270 271 272 273 274 275 276 277 278 279 |
|
window_factory
window_factory(parent_window, schema, boundless=True)
Generator that produces rasterio.Window objects in predetermined steps, within the given Window.
:yield: a rasterio.Window used for windowed reading/writing
Parameters:
Name | Type | Description | Default |
---|---|---|---|
parent_window |
Window
|
the window that provides the bounds for all child_window objects |
required |
schema |
WindowSchema
|
the parameters that determine the window steps |
required |
boundless |
bool
|
whether the child_window should be clipped by the parent_window or not |
True
|
Source code in geococo\utils.py
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 |
|
window_intersect
window_intersect(input_raster, input_vector)
Generates a Rasterio Window from the intersecting extents of the input data. It also verifies if the input data share the same CRS and if they physically overlap.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_raster |
DatasetReader
|
rasterio dataset (i.e. input image) |
required |
input_vector |
GeoDataFrame
|
geopandas geodataframe (i.e. input labels) |
required |
Returns:
Type | Description |
---|---|
Window
|
rasterio window that represent the intersection between input data extents |
Source code in geococo\utils.py
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 |
|