Skip to content

API Reference

raster_utilities

Ortophoto(path=None, folder=None, crs=25831)

Raster algorithm containing the different values to be kept.

Parameters:

Name Type Description Default
path str

The path to the image, with the GDAL-accepted formats. Defaults to None.

None
folder str

The default folder for all computations to be performed. Defaults to None, and will be stored in with a name like DATA_DIR/{path}.

None
crs int

CRS of the tile to be loaded. Defaults to 25831.

25831

Raises:

Type Description
Exception

File is not valid

Source code in apb_spatial_computer_vision/raster_utilities.py
 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
def __init__(self,path=None,folder=None,crs=25831):
    """Raster algorithm containing the different values to be kept.

    Args:
        path (str, optional): The path to the image, with the GDAL-accepted formats. Defaults to None.
        folder (str, optional): The default folder for all computations to be performed. Defaults to None, and will be stored in with a name like DATA_DIR/{path}.
        crs (int): CRS of the tile to be loaded. Defaults to 25831.

    Raises:
        Exception: File is not valid
    """
    try:
        #os.path.exists(path):
        self.raster=gdal.Open(path)
    except:
        raise Exception('Name does not exist')

    self.raster_path=path
    self.basename=os.path.basename(path)
    self.folder=self._get_dirname(folder)
    self.GT=[self.X_min, self.X_pixel, self.X_spin, self.Y_max, self.Y_spin, self.Y_pixel]=self.raster.GetGeoTransform()
    self.X_max = self.X_min + self.X_pixel * self.raster.RasterXSize
    self.Y_min = self.Y_max + self.Y_pixel * self.raster.RasterYSize
    self.bounds=(self.X_min,self.Y_min,self.X_max,self.Y_max)
    self.width=self.X_max-self.X_min
    self.height=self.Y_max-self.Y_min
    self.area=self.width*self.height
    self.pixel_width=self.raster.RasterXSize
    self.pixel_height=self.raster.RasterYSize
    self.crs=crs
    self.wkt=self._get_wkt()
    self.dstSRS_wkt=self.getSRS()
    self.pyramid = None
    self.resolutions =None
    self.resolutions_tiles=None
    self.lowest_pixel_size=1024

cloneBand(image, dst_filename, driver_name=None)

Creates a new raster file just like the current, given a matrix

Parameters:

Name Type Description Default
image array

[[row, col],[row,col],...] pixel-level description of the image

required
dst_filename str

Absolute path of the file to be written to

required
driver_name str

GDAL-Driver to run the create command.If not specified it will guess from the dst_filename, or if it fails as GTiff. Defaults to None.

None
Source code in apb_spatial_computer_vision/raster_utilities.py
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
def cloneBand(self,image,dst_filename,driver_name = None):
    """
    Creates a new raster file just like the current, given a matrix 

    Args:
        image (np.array): [[row, col],[row,col],...] pixel-level description of the image
        dst_filename (str): Absolute path of the file to be written to
        driver_name (str, optional): GDAL-Driver to run the create command.If not specified it will guess from the dst_filename, or if it fails as GTiff. Defaults to None.
    """

    if driver_name is None:
        extension=os.path.splitext(dst_filename)[1]
        driver_name=driverDict.get(extension,driverDict['.tif'])
    driver = gdal.GetDriverByName(driver_name)
    ndvi_ds= driver.Create(dst_filename, xsize=image.shape[1], ysize=image.shape[0],
                bands=1, eType=gdal.GDT_Byte)
    ndvi_ds.SetGeoTransform(self.GT)
    ndvi_ds.SetProjection(self.dstSRS_wkt)
    ndvi_ds.GetRasterBand(1).WriteArray(image)
    ndvi_ds=None

create_pyramid(lowest_pixel_size)

Generates a log_2 based image pyramid with the most available tiles.

Parameters:

Name Type Description Default
lowest_pixel_size int

Number of pixels of the size for each tile

required

Returns:

Name Type Description
pyramid_dir

The path to where the pyramid was stored

Source code in apb_spatial_computer_vision/raster_utilities.py
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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
def create_pyramid(self,lowest_pixel_size):
    """
    Generates a log_2 based image pyramid with the most available tiles.

    Args:
        lowest_pixel_size (int): Number of pixels of the size for each tile

    Returns:
        pyramid_dir: The path to where the pyramid was stored
    """

    largest_side=max(self.pixel_width,self.pixel_height)
    smallest_side=min(self.pixel_width,self.pixel_height)    

    #finding the biggest available 2-power aspect relationship
    aspect_relationship=self.pixel_width/self.pixel_height
    quadratic_aspect_relationship=closest_base_power(aspect_relationship)

    largest_tile=closest_base_power(smallest_side)
    depth=int(log(largest_tile,2))-int(log(lowest_pixel_size,2))

    if depth<=0:
        print('LA TESELA PEDIDA NO ES DE UN TAMAÑO SUFICIENTE')

    pyramid_dir=folder_check(os.path.join(self.folder,os.path.basename(self.raster_path).split('.')[0])+'_pyramid')
    pyramid_raster_dir=folder_check(os.path.join(pyramid_dir,'raster'))
    pyramid_vector_dir=folder_check(os.path.join(pyramid_dir,'vector'))  

    dirs=[folder_check(os.path.join(pyramid_raster_dir,f'subset_{str(i).zfill(self.nice_write(depth))}')) for i in range(depth+1)]
    image_loaded_generalization=partial(_simplify_single_raster,raster=self)   
    #image_loaded_generalization=partial(_generalize_single_raster,raster=self) 

    layers=[layer for layer in range(depth,-1,-1)]
    divisors=[int(2**layer) for layer in layers]
    directories=[dirs[layer] for layer in  layers]
    tile_sizes=[largest_tile/divisor for divisor in divisors]
    teselas=list(map(self.tesselation,directories,tile_sizes))
    m=[t[0] for t in teselas]
    blueprint=[np.array(a) for a in m]

    xRes=[self.X_pixel*2**(depth-layer) for layer in layers]
    yRes=[self.Y_pixel*2**(depth-layer) for layer in layers]

    xRes=[np.full_like(sample,x).astype(np.float64).tolist() for (sample,x) in zip(blueprint,xRes)]
    yRes=[np.full_like(sample,y).astype(np.float64).tolist() for (sample,y) in zip(blueprint,yRes)]
    xRes_flattened=[i for inte in xRes for i in inte]
    yRes_flattened=[i for inte in yRes for i in inte]

    name_lists=[t[0] for t in teselas]
    bound_lists=[t[1] for t in teselas]

    bound_gdfs=[gpd.GeoDataFrame({'NAME':name_list},geometry=gpd.GeoSeries.from_wkt([bounds2wkt(bound) for bound in bound_list]),crs=25831) for (name_list,bound_list) in zip(name_lists,bound_lists)]
    [bound_gdf.to_file(os.path.join(pyramid_vector_dir,f'subset_{layer}.geojson')) for (bound_gdf,layer) in zip(bound_gdfs,layers)]
    name_lists_flattened=[t for tesela in teselas for t in tesela[0]]
    bounds_lists_flattened=[t for tesela in teselas for t in tesela[1]]

    with ProcessPoolExecutor() as executor:
            results=list(executor.map(image_loaded_generalization,name_lists_flattened,xRes_flattened,yRes_flattened,bounds_lists_flattened))

    return pyramid_dir

create_resolutions(depth)

Generate images with degraded resolutions using bilineal interpolation

Parameters:

Name Type Description Default
depth int

The amount of numbers to be used in the development of the data.

required

Returns:

Name Type Description
resolutions_dir str

The path where the resolutions are stored (within the ortophoto /project folder)

Source code in apb_spatial_computer_vision/raster_utilities.py
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
def create_resolutions(self,depth):
    """
    Generate images with degraded resolutions using bilineal interpolation

    Args:
        depth (int): The amount of numbers to be used in the development of the data.

    Returns:
        resolutions_dir (str): The path where the resolutions are stored (within the ortophoto /project folder) 
    """
    resolutions_dir=folder_check(os.path.join(self.folder,os.path.basename(self.raster_path).split('.')[0])+'_resolutions')
    gen=partial(_simplify_single_raster,raster=self,bounds=self.bounds)
    args={i:{'Name':os.path.join(folder_check(os.path.join(resolutions_dir,str(int(100*self.X_pixel*2**i))+'cm')),f'{i}.tif'),'xRes':self.X_pixel*2**i,'yRes':self.Y_pixel*2**i} for i in range(depth)}
    df=pd.DataFrame(args)
    df=df.transpose()
    with ProcessPoolExecutor() as executor:
        results=list(executor.map(gen,df['Name'],df['xRes'],df['yRes']))
    return resolutions_dir

create_tiles_duckdb_table(lowest_pixel_size=1024)

Generates a DUCKDB table named tiles, containing the image pyramid data and its information. May trigger pyramid_depth or pyramid calculation if not abailable.

Parameters:

Name Type Description Default
lowest_pixel_size int

Number of pixels for each tile in the pyramid. Defaults to 1024.

1024
Source code in apb_spatial_computer_vision/raster_utilities.py
329
330
331
332
333
334
335
336
337
338
339
340
def create_tiles_duckdb_table(self,lowest_pixel_size=1024):
    """
    Generates a DUCKDB table named tiles, containing the image pyramid data and its information. May trigger pyramid_depth or pyramid calculation if not abailable.

    Args:
        lowest_pixel_size (int, optional): Number of pixels for each tile in the pyramid. Defaults to 1024.
    """

    command = " UNION ALL ".join(
            [f"SELECT *, '{depth}' depth  FROM st_read('{os.path.join(self.get_pyramid(lowest_pixel_size),'vector',f'subset_{depth}.geojson')}')" for depth in range(self.get_pyramid_depth(lowest_pixel_size))])

    DUCKDB.sql('CREATE TABLE IF NOT EXISTS tiles AS '+command)

explore(bound_list, tile_size)

Generates an interactive map in HTML for the tiles

Parameters:

Name Type Description Default
bound_list Iterable[tuple[float]]

list of bounds, like from the tiles

required
tile_size Iterable[str]

tile size with which the bounds have been developed.

required
Source code in apb_spatial_computer_vision/raster_utilities.py
414
415
416
417
418
419
420
421
422
423
424
425
426
def explore(self,bound_list,tile_size):
    """
    Generates an interactive map in HTML for the tiles

    Args:
        bound_list (Iterable [tuple [float]]): list of bounds, like from the tiles
        tile_size (Iterable [str]): tile size with which the bounds have been developed.
    """
    wkts=[bounds2wkt(b)for b in bound_list]
    gdf=gpd.GeoDataFrame(geometry=gpd.GeoSeries.from_wkt(wkts),crs=self.crs)
    gdf['area']=gdf['geometry'].area
    m=gdf.explore()
    m.save(os.path.join(STATIC_DIR,f'{tile_size}.html'))

find_intersection_centroid(gdf)

Intersects an image and a gdf and returns intersection centroid

Parameters:

Name Type Description Default
gdf GeoDataFrame

Geospatial table containing a 'geometry' column

required

Returns:

Name Type Description
coords

The XY coordinates of the centroid of the intersection

Source code in apb_spatial_computer_vision/raster_utilities.py
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
def find_intersection_centroid(self,gdf):
    """ 
    Intersects an image and a gdf and returns intersection centroid

    Args:
        gdf (gpd.GeoDataFrame): Geospatial table containing a 'geometry' column

    Returns:
        coords: The XY coordinates of the centroid of the intersection
    """
    s1=gdf.geometry
    s3=gpd.GeoSeries.from_wkt([self.wkt],crs=self.crs)
    puntos_inside=s1.intersection(s3[0])
    newdf=gpd.GeoDataFrame(geometry=puntos_inside,crs=self.crs)
    geo_prompt=newdf[newdf['geometry'].area==newdf['geometry'].area.max()].reset_index(drop=True)['geometry'][0].centroid
    if geo_prompt.is_empty==False:
        coords= [geo_prompt.x,geo_prompt.y]
        return coords

getSRS()

Get the OSGEO/OSR WKT CRS

Returns:

Name Type Description
str

OSGEO/OSR Well-known Text (WKT) CRS

Source code in apb_spatial_computer_vision/raster_utilities.py
228
229
230
231
232
233
234
235
236
237
238
def getSRS(self):
    """
    Get the OSGEO/OSR WKT CRS 

    Returns:
        str: OSGEO/OSR Well-known Text (WKT) CRS 
    """
    srs = osr.SpatialReference()
    srs.ImportFromEPSG(self.crs)
    dstSRS_wkt = srs.ExportToWkt()
    return dstSRS_wkt

get_pyramid(lowest_pixel_size)

Gets the pyramid directory or creates it if not yet implemented

Parameters:

Name Type Description Default
lowest_pixel_size int

Number of pixels for each tile in the pyramid. Defaults to 1024.

required

Returns:

Type Description

self.pyramid (str) : Path to the pyramid

Source code in apb_spatial_computer_vision/raster_utilities.py
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
def get_pyramid(self,lowest_pixel_size):
    """
    Gets the pyramid directory or creates it if not yet implemented

    Args:
        lowest_pixel_size (int, optional): Number of pixels for each tile in the pyramid. Defaults to 1024.

    Returns:
        self.pyramid (str) : Path to the pyramid
    """
    if self.pyramid is None:
        self.pyramid=self.create_pyramid(lowest_pixel_size=self.lowest_pixel_size)
        return self.pyramid
    else:
        return self.pyramid

get_pyramid_depth(lowest_pixel_size=1024)

Calculates the depth of the image pyramid, creates it too if it hasn't been done yet

Parameters:

Name Type Description Default
lowest_pixel_size int

Number of pixels for each tile in the pyramid. Defaults to 1024.

1024

Returns:

Type Description

self.pyramid_depth (int): Number of levels of the pyramid

Source code in apb_spatial_computer_vision/raster_utilities.py
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
def get_pyramid_depth(self,lowest_pixel_size :int=1024):
    """
    Calculates the depth of the image pyramid, creates it too if it hasn't been done yet

    Args:
        lowest_pixel_size (int, optional): Number of pixels for each tile in the pyramid. Defaults to 1024.

    Returns:
        self.pyramid_depth (int): Number of levels of the pyramid
    """
    if not hasattr(self,"pyramid_depth"):
        self.pyramid=self.get_pyramid(lowest_pixel_size)
        self.raster_pyramid=os.path.join(self.pyramid,'raster')
        self.pyramid_depth=len([i for i in os.listdir(self.raster_pyramid) if os.path.isfile(os.path.join(self.raster_pyramid,i))==False])
        return self.pyramid_depth
    else:
        return self.pyramid_depth

get_pyramid_tiles(lowest_pixel_size=1024)

Fetches the list containing the paths for all files in the pyramid

Parameters:

Name Type Description Default
lowest_pixel_size int

Number of pixels for each tile in the pyramid. Defaults to 1024.

1024

Returns:

Type Description

self.pyramid_tiles (list): Paths for the tiles in the pyramid

Source code in apb_spatial_computer_vision/raster_utilities.py
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
def get_pyramid_tiles(self,lowest_pixel_size=1024):
    """Fetches the list containing the paths for all files in the pyramid

    Args:
        lowest_pixel_size (int, optional): Number of pixels for each tile in the pyramid. Defaults to 1024.

    Returns:
        self.pyramid_tiles (list): Paths for the tiles in the pyramid
    """
    if not hasattr(self,'pyramid_tiles'):
        pyramid_tiles=[]
        for root, dirs, files in os.walk(os.path.join(self.get_pyramid(lowest_pixel_size),'raster')):
            pyramid_tiles+=[os.path.join(root,name) for name in files if os.path.splitext(name)[1]=='.tif']
        self.pyramid_tiles=pyramid_tiles

    return self.pyramid_tiles

mosaic_rasters(im_input, name, fetch=False, pixel_value_to_be_ignored='') staticmethod

Returns the ortophoto object of the addition of the imput elements' iterable

Parameters:

Name Type Description Default
im_input Iterable

a list of Ortophoto objects or paths

required
name str

Output name for the mosaic

required
fetch bool

Wether to return the Ortophoto object. Defaults to False.

False
pixel_value_to_be_ignored str

Pixel value that will not override other previous values in case of overlap. Defaults to ''.

''

Returns:

Name Type Description
outname str

Output file name.

out_orto Ortophoto

Generated ortophoto object for the output file, only created if fetch equals True.

Source code in apb_spatial_computer_vision/raster_utilities.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
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
@staticmethod
def mosaic_rasters(im_input:Iterable,name:str,fetch=False, pixel_value_to_be_ignored=''):
    """Returns the ortophoto object of the addition of the imput elements' iterable

    Args:
        im_input (Iterable): a list of Ortophoto objects or paths
        name (str): Output name for the mosaic
        fetch (bool, optional): Wether to return the Ortophoto object. Defaults to False.
        pixel_value_to_be_ignored (str, optional): Pixel value that will not override other previous values in case of overlap. Defaults to ''.

    Returns:
        outname (str): Output file name.
        out_orto (Ortophoto): Generated ortophoto object for the output file, only created if fetch equals True.
    """


    valid_images=[]

    def check_image(valid_images,im):
        if isinstance(im,str):
            if os.path.exists(im):
                valid_images.append(im)
                return valid_images
        else:
            try:
                if os.path.exists(im.raster_path):
                    valid_images.append(im)
                    return valid_images
            except AttributeError:
                raise (f'{im} COULD NOT BE ADDED AS IT IS NOT A VALID PATH OR ORTOPHOTO/TILE OBJECT.')

    if isinstance(im_input,Iterable) and not isinstance(im_input,str):
        for im in im_input:
            valid_images=check_image(valid_images,im)
    else:
        valid_images=check_image(valid_images,im_input)

    if len(valid_images)<2:
        print('NONE OF THE ITEMS TO ADD WAS GOOD ENOUGH')
        return im_input[0]
    else:
        root,ext=os.path.splitext(name)
        if ext=='':
            ext='.vrt' 

        if not os.path.exists(name):
            outdir=folder_check(os.path.join(DATA_DIR,os.path.dirname(name)))
        else:
            outdir=os.path.dirname(root)
        outname=os.path.join(outdir,f'{os.path.basename(root)}{ext}')

        if pixel_value_to_be_ignored!='':
            pixel_value_to_be_ignored=f'-srcnodata {pixel_value_to_be_ignored} -vrtnodata {pixel_value_to_be_ignored}'

        def create_virtual_tiles(output_virtual_file_path):
            """Generate a VRT file with the configurations to be added

            Args:
                output_virtual_file_path (str): Path to the output VRT file
            """
            with tempfile.NamedTemporaryFile(mode='w',delete=False, suffix=".txt") as tmp:
                tiles_list_file = tmp.name
                for image_path in valid_images:
                    tmp.write(image_path + '\n')
            command = f'''gdalbuildvrt -o "{output_virtual_file_path}" {pixel_value_to_be_ignored} -input_file_list "{tiles_list_file}"'''
            subprocess.call(command)
            os.remove(tiles_list_file)

        if ext=='.vrt':
            create_virtual_tiles(outname)
        else:
            provisional_virtual_file=os.path.join(outdir,f'tmp.vrt')
            create_virtual_tiles(provisional_virtual_file)
            gdal.Translate(destName=outname,srcDS=provisional_virtual_file)
            os.remove(provisional_virtual_file)

        if fetch is True:
            return Ortophoto(outname)
        else:
            return outname

nice_write(num) staticmethod

Computes the needed digits to be applied to zfill for succesful number ordering

Parameters:

Name Type Description Default
num int

input number

required

Returns:

Name Type Description
int

The number of digits to be applied into zfill

Source code in apb_spatial_computer_vision/raster_utilities.py
246
247
248
249
250
251
252
253
254
255
256
257
@staticmethod
def nice_write(num):
    """Computes the needed digits to be applied to zfill for succesful number ordering

    Args:
        num (int): input number

    Returns:
        int: The number of digits to be applied into zfill
    """
    '''Devuelve los ceros necesarios para la función zfill que permiten ordenar los números'''
    return int(log(num,10))+1

polygonize(step_x, step_y=None)

Creates GDAL-based image cropping given an image size. Concurrently-sped up.

Parameters:

Name Type Description Default
step_x int

The size of the resulting elements

required
horizontal_skew int

The pixel units to add from to step in the X direction (cols or i). Defaults to None.

required
vertical_skew int

The pixel units to add from to step in the Y direction (rows or j). Defaults to None.

required
Source code in apb_spatial_computer_vision/raster_utilities.py
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
def polygonize(self,step_x,step_y=None):
    """
    Creates GDAL-based image cropping given an image size. Concurrently-sped up. 

    Args:
        step_x (int): The size of the resulting elements
        horizontal_skew (int, optional): The pixel units to add from to step in the X direction (cols or i). Defaults to None.
        vertical_skew (int, optional): The pixel units to add from to step in the Y direction (rows or j). Defaults to None.
    """
    name_list=[]
    bound_list=[]

    # Generación de las ventanas para los recortes
    tiles_dir=folder_check(os.path.join(self.folder,f'tiles_{os.path.basename(self.raster_path).split(".")[0]}_{step_x}'))

    name_list,bound_list=self.tesselation(tiles_dir,step_x,step_y)

    # Partial application of the function to avoid raster reopening
    processing=partial(_warp_single_raster,raster=self)

    # PARALELIZADO CON PROCESOS CONCURRENTES
    with ProcessPoolExecutor() as executor:
         results = list(executor.map(processing,name_list,bound_list,chunksize=2000))

tesselation(dir, step_x, step_y=None)

Generation of tesselation algorithms

Parameters:

Name Type Description Default
dir str

Output directory

required
step_x int

Pixel width of the image crops.

required
step_y int

Pixel heigh to the image crops if different. Defaults to None.

None

Returns:

Name Type Description
name_list list

Names for the tiles' files

bound_list list[tuple]

Nested bounding box list like [(xmin,ymin,xmax,ymax),...,(...)]

Source code in apb_spatial_computer_vision/raster_utilities.py
342
343
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
def tesselation(self,dir,step_x,step_y=None):
    """
    Generation of tesselation algorithms

    Args:
        dir (str): Output directory
        step_x (int): Pixel width of the image crops.
        step_y (int, optional): Pixel heigh to the image crops if different. Defaults to None.

    Returns:
        name_list (list): Names for the tiles' files
        bound_list (list[tuple]):  Nested bounding box list like [(xmin,ymin,xmax,ymax),...,(...)]
    """
    if step_y is None:
        step_y=step_x
    metric_x=step_x*self.X_pixel
    metric_y=step_y*self.Y_pixel

    cols=abs(int(self.width/metric_x))
    rows=abs(int(self.height/metric_y))

    zcols,zrows =self.nice_write(cols), self.nice_write(rows)

    name_list,bound_list=[],[]
    ncol,nrow=0,0
    step_x=int(step_x)
    for i in range(cols):
        for j in range(rows):
            name=os.path.join(dir,f'tile_{step_x}_grid_{str(nrow).zfill(zrows)}_{str(ncol).zfill(zcols)}.tif')
            bounds=(self.X_min+metric_x*i,self.Y_max+metric_y*(j+1),self.X_min+metric_x*(i+1),self.Y_max+metric_y*j)
            name_list.append(name)
            bound_list.append(bounds) 
            nrow+=1
        ncol+=1
        nrow=0  
    return name_list, bound_list

Tile(path, crs=25831)

Bases: Ortophoto

Class Tile (Ortophoto) Child class from parent Ortophoto

Parameters:

Name Type Description Default
path str

Complete path to the tile as a string

required
crs int

CRS of the tile to be loaded. Defaults to 25831.

25831
Source code in apb_spatial_computer_vision/raster_utilities.py
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
def __init__(self,path:str,crs=25831):
    '''
    Args: 
        path (str): Complete path to the tile as a string
        crs (int): CRS of the tile to be loaded. Defaults to 25831.
    '''
    super().__init__(path,
                     os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(path)))),
                     crs
                     )
    self.original_size,self.row,self.col=self.get_row_col()
    self.pyramid=os.path.dirname(os.path.dirname(os.path.dirname(self.raster_path)))
    self.raster_pyramid=os.path.join(self.pyramid,'raster')
    self.pyramid_layer=int(os.path.dirname(self.raster_path).split('_')[-1])
    self.pyramid_depth=len([i  for i in os.listdir(self.raster_pyramid) if os.path.isfile(os.path.join(self.raster_pyramid,i))==False])
    self.folder=os.path.dirname(self.pyramid)

__eq__(t2)

Parameters:

Name Type Description Default
t2(Ortophoto)

Tile to be compared

required

Returns: bool, True if in the same layer of the pyramid Exceptions: Raise if not an instance of the Ortophoto class

Source code in apb_spatial_computer_vision/raster_utilities.py
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
def __eq__(self,t2: Ortophoto):
    """
    Args:   
        self(Tile)
        t2(Ortophoto): Tile to be compared
    Returns:
        bool, True if in the same layer of the pyramid
    Exceptions:
        Raise if not an instance of the Ortophoto class 
    """              

    if isinstance(t2,(Ortophoto,Tile)):
        return self.pyramid_layer==t2.pyramid_layer
    else:
        raise('NOT AN INSTANCE OF THE CLASS ORTOPHOTO OR TILE')

get_children()

Retrieves the tiles which have a higher resolution for the same point (lower levels of the pyramid for a given tile)

Returns:

Name Type Description
out_list list

2D-list. Each list contains a level of deepness, from closer to the current level until the lowest layer of the pyramid.

Source code in apb_spatial_computer_vision/raster_utilities.py
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
def get_children(self):
    """
    Retrieves the tiles which have a higher resolution for the same point (lower levels of the pyramid for a given tile)

    Returns:
        out_list (list): 2D-list. Each list contains a level of deepness, from closer to the current level until the lowest layer of the pyramid.
    """

    base=self.pyramid_layer
    out_list=[]
    n_row,n_col=self.get_n_rows_cols()

    for k in range(1,self.pyramid_depth-base):
        i_min=self.row*2**k
        i_max=i_min+2**k-1
        j_min=self.col*2**k
        j_max=j_min+2**k-1
        current=[]
        for l in range(j_min,j_max+1): 
            for m in range(i_min,i_max+1):
                current.append((str(m).zfill(self.nice_write((n_row+1)*2**k)),str(l).zfill(self.nice_write((n_col+1)*2**k))))
        out_list.append([os.path.join(self.raster_pyramid,f'subset_{base+k}',f'tile_{int(self.original_size/(2**k))}_grid_{i}_{j}.tif') for i,j in current])
    self.children=out_list
    #self.smallest_children=out_list[-1]
    return out_list

get_n_rows_cols()

Get the number of rows and columns of a certain level in a pyramid

Returns:

Name Type Description
n_row int

number of rows at the current level

n_col int

number of columns at the current level

Source code in apb_spatial_computer_vision/raster_utilities.py
633
634
635
636
637
638
639
640
641
642
643
644
def get_n_rows_cols(self):
    """
    Get the number of rows and columns of a certain level in a pyramid

    Returns:
        n_row (int): number of rows at the current level
        n_col (int): number of columns at the current level
    """
    curdir=os.path.join(self.raster_pyramid,f'subset_{self.pyramid_layer}')
    current_siblings=os.listdir(curdir)
    init_size,n_row,n_col=self.get_row_col(os.path.join(curdir,current_siblings[-1]))
    return n_row,n_col

get_parents()

Retrieves the tiles which have a higher resolution for the same point (lower levels of the pyramid for a given tile)

Returns:

Name Type Description
out_list list

2D-list. Each list contains a level of deepness, from closer to the current level until the lowest layer of the pyramid.

Source code in apb_spatial_computer_vision/raster_utilities.py
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
def get_parents(self):
    """
    Retrieves the tiles which have a higher resolution for the same point (lower levels of the pyramid for a given tile)

    Returns:
        out_list (list): 2D-list. Each list contains a level of deepness, from closer to the current level until the lowest layer of the pyramid.
    """
    base=self.pyramid_layer
    out_list=[]
    n_row,n_col=self.get_n_rows_cols()

    for k in range(1,base+1):
        i=int(self.row/2**k)
        j=int(self.col/2**k)
        current=[]
        current.append((str(i).zfill(self.nice_write((n_row+1)/2**k)),str(j).zfill(self.nice_write((n_col+1)/2**k))))
        out_list.append([os.path.join(self.raster_pyramid,f'subset_{base-k}',f'tile_{int(self.original_size*(2**k))}_grid_{i}_{j}.tif') for i,j in current])
    self.parents=out_list
    #self.biggest_parent=out_list[-1]
    return out_list

get_row_col(path=None)

Get the original size, row and col of a certain tile

Parameters:

Name Type Description Default
raster str

Raster path to be added. Defaults to None.

required

Returns:

Name Type Description
original_size int

original size of the parent class from where it has been resampled

row int

row in the current pyramid layer

col int

col in the current pyramid layer

Source code in apb_spatial_computer_vision/raster_utilities.py
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
def get_row_col(self,path=None):
    """
    Get the original size, row and col of a certain tile

    Args:
        raster (str, optional): Raster path to be added. Defaults to None.

    Returns:
        original_size (int): original size of the parent class from where it has been resampled
        row (int): row in the current pyramid layer
        col (int): col in the current pyramid layer
    """
    raster=self.raster_path
    if path is not None:
        if os.path.exists(path):
            raster=path
        else:
            pass
    metadata_list=os.path.basename(raster).split('.')[0].split('_')
    original_size,row,col=int(metadata_list[1]),int(metadata_list[3]),int(metadata_list[4])
    return  original_size,row,col

get_siblings()

Finds the four tiles which come from one level higher in the pyramid. It is equivalent to finding the first parent and looking at its first children

Returns:

Type Description

self.siblings (list): A list of the four immediate siblings in the current level of the pyramid

Source code in apb_spatial_computer_vision/raster_utilities.py
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
def get_siblings(self):
    """
    Finds the four tiles which come from one level higher in the pyramid. It is equivalent to finding the first parent and looking at its first children 

    Returns:
        self.siblings (list): A list of the four immediate siblings in the current level of the pyramid

    """
    base=self.pyramid_layer
    size=self.original_size
    i_min=self.row+self.row%-2
    j_min=self.col+self.col%-2
    i_max=i_min+1
    j_max=j_min+1
    sibling_list=[(i_min,j_min),(i_max,j_min),(i_min,j_max),(i_max,j_max)]
    n_row,n_col=self.get_n_rows_cols()
    candidates=[os.path.join(self.raster_pyramid,f'subset_{base}',f'tile_{size}_grid_{str(i).zfill(self.nice_write(n_row+1))}_{str(j).zfill(self.nice_write(n_col+1))}.tif') for i,j in sibling_list]

    self.siblings= [c for c in candidates if os.path.exists(c)]
    return self.siblings