Skip to content

webcache

plantdb.server.webcache Link

This module provides three utility functions that are used in combination with the DB interface to create downsized versions of images, point clouds, and mesh resources. The resources are identified using the Scan, Fileset, and File IDs. The downsized versions are cached in the 'webcache' directory in the scan directory.

The following size specifications are available:

  • Images: 'thumb' (max. 150x150), 'large' (max. 1500x1500), and 'orig' (original size).
  • Point clouds: 'preview' (max. 10k points), and 'orig' (original size).
  • Mesh: 'orig' (original size). TODO: add remeshing

Examples:

>>> from plantdb.server import webcache
>>> from os import environ
>>> from plantdb.commons.fsdb import FSDB
>>> db = FSDB(environ.get('ROMI_DB', "/data/ROMI/DB/"))
>>> db.connect()
>>> # Get the path to the original image:
>>> webcache.image_path(db,'sango36','images','00000_rgb','orig')
>>> # Get the path to the thumb image (resized and cached):
>>> webcache.image_path(db,'sango36','images','00000_rgb','thumb')
>>> # Get the path to the original pointcloud:
>>> webcache.pointcloud_path(db,'sango_90_300_36','PointCloud_1_0_0_0_10_0_ca07eb2790','PointCloud','orig')
>>> # Get the path to the preview pointcloud (resized and cached):
>>> webcache.pointcloud_path(db,'sango_90_300_36','PointCloud_1_0_0_0_10_0_ca07eb2790','PointCloud','preview')
>>> # Get the path to a downsampled pointcloud (resized and cached):
>>> webcache.pointcloud_path(db,'sango_90_300_36','PointCloud_1_0_0_0_10_0_ca07eb2790','PointCloud','2.3')
>>> db.disconnect()

__file_path Link

__file_path(db, scan_id, fileset_id, file_id)

Return the path to a file.

Parameters:

Name Type Description Default
db FSDB

The database object.

required
scan_id str

The ID of the scan in the database.

required
fileset_id str

The ID of the fileset in the scan.

required
file_id str

The ID of the file in the fileset.

required

Returns:

Type Description
Path

The path to the file.

Source code in plantdb/server/webcache.py
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
def __file_path(db, scan_id, fileset_id, file_id):
    """Return the path to a file.

    Parameters
    ----------
    db : plantdb.commons.fsdb.FSDB
        The database object.
    scan_id : str
        The ID of the scan in the database.
    fileset_id : str
        The ID of the fileset in the scan.
    file_id : str
        The ID of the file in the fileset.

    Returns
    -------
    pathlib.Path
        The path to the file.
    """
    scan = db.get_scan(scan_id, create=False)
    fs = scan.get_fileset(fileset_id, create=False)
    f = fs.get_file(file_id, create=False)
    return db.basedir / scan.id / fs.id / f.filename

__hash Link

__hash(resource_type, scan_id, fileset_id, file_id, size)

Create a hash for a resource.

Parameters:

Name Type Description Default
resource_type str

The name of the resource type, e.g. "image" or "pointcloud".

required
scan_id str

The ID of the scan in the database.

required
fileset_id str

The ID of the fileset in the scan.

required
file_id str

The ID of the file in the fileset.

required
size str

The requested size.

required

Returns:

Type Description
str

The hash of the resource.

Source code in plantdb/server/webcache.py
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
def __hash(resource_type, scan_id, fileset_id, file_id, size):
    """Create a hash for a resource.

    Parameters
    ----------
    resource_type : str
        The name of the resource type, *e.g.* "image" or "pointcloud".
    scan_id : str
        The ID of the scan in the database.
    fileset_id : str
        The ID of the fileset in the scan.
    file_id : str
        The ID of the file in the fileset.
    size : str
        The requested size.

    Returns
    -------
    str
        The hash of the resource.
    """
    m = hashlib.sha1()
    key = f"{resource_type}|{scan_id}|{fileset_id}|{file_id}|{size}"
    m.update(key.encode('utf-8'))
    return m.hexdigest()

__image_cache Link

__image_cache(db, scan_id, fileset_id, file_id, size)

Create a cache for an image resource.

Parameters:

Name Type Description Default
db FSDB

The database object.

required
scan_id str

The ID of the scan in the database.

required
fileset_id str

The ID of the fileset in the scan.

required
file_id str

The ID of the file in the fileset.

required
size (orig, large, thumb)

If an integer, use it as the size of the cached image to create and return. Else, should be a string in the given list.

'orig'

Returns:

Type Description
Path

The path to the cached image.

Source code in plantdb/server/webcache.py
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
def __image_cache(db, scan_id, fileset_id, file_id, size):
    """Create a cache for an image resource.

    Parameters
    ----------
    db : plantdb.commons.fsdb.FSDB
        The database object.
    scan_id : str
        The ID of the scan in the database.
    fileset_id : str
        The ID of the fileset in the scan.
    file_id : str
        The ID of the file in the fileset.
    size : {'orig', 'large', 'thumb'} or int, optional
        If an integer, use  it as the size of the cached image to create and return.
        Else, should be a string in the given list.

    Returns
    -------
    pathlib.Path
        The path to the cached image.
    """
    # Get the path to the original image:
    src = __file_path(db, scan_id, fileset_id, file_id)

    # Get the path to the 'webcache' directory:
    cache_dir = __webcache_path(db, scan_id)
    dst = cache_dir / __image_hash(scan_id, fileset_id, file_id, size)

    # Load the image and resize it:
    image = Image.open(src)
    image.load()
    if isinstance(size, int):
        maxsize = size
    else:
        maxsize = IMG_RESOLUTIONS.get(size)
    image = __image_resize(image, maxsize)
    # Make sure we have an RGB image to be able to save in this format:
    if image.mode != "RGB":
        image = image.convert(mode="RGB")
    save_kwargs = {'quality': 84}
    # Save the resized image in the "webcache" directory:
    image.save(dst, **save_kwargs)
    print(f"Converted '{src}' to '{dst}', using size '{maxsize}'")

    return dst

__image_cached_path Link

__image_cached_path(db, scan_id, fileset_id, file_id, size)

Get The path to the cached image.

Parameters:

Name Type Description Default
db FSDB

The database object.

required
scan_id str

The ID of the scan in the database.

required
fileset_id str

The ID of the fileset in the scan.

required
file_id str

The ID of the file in the fileset.

required
size (orig, large, thumb)

If an integer, use it as the size of the cached image to create and return. Else, should be a string in the given list.

'orig'

Returns:

Type Description
Path

The path to the cached image.

Source code in plantdb/server/webcache.py
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
def __image_cached_path(db, scan_id, fileset_id, file_id, size):
    """Get The path to the cached image.

    Parameters
    ----------
    db : plantdb.commons.fsdb.FSDB
        The database object.
    scan_id : str
        The ID of the scan in the database.
    fileset_id : str
        The ID of the fileset in the scan.
    file_id : str
        The ID of the file in the fileset.
    size : {'orig', 'large', 'thumb'} or int, optional
        If an integer, use  it as the size of the cached image to create and return.
        Else, should be a string in the given list.

    Returns
    -------
    pathlib.Path
        The path to the cached image.
    """
    cache_dir = __webcache_path(db, scan_id)
    img_path = cache_dir / __image_hash(scan_id, fileset_id, file_id, size)
    if not img_path.is_file():
        __image_cache(db, scan_id, fileset_id, file_id, size)
    return img_path

__image_hash Link

__image_hash(scan_id, fileset_id, file_id, size)

Create a file name for an image resource using a hash value.

Parameters:

Name Type Description Default
scan_id str

The ID of the scan in the database.

required
fileset_id str

The ID of the fileset in the scan.

required
file_id str

The ID of the file in the fileset.

required
size (orig, large, thumb)

If an integer, use it as the size of the cached image to create and return. Else, should be a string in the given list.

'orig'

Returns:

Type Description
str

The image resource file name.

Source code in plantdb/server/webcache.py
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
def __image_hash(scan_id, fileset_id, file_id, size):
    """Create a file name for an image resource using a hash value.

    Parameters
    ----------
    scan_id : str
        The ID of the scan in the database.
    fileset_id : str
        The ID of the fileset in the scan.
    file_id : str
        The ID of the file in the fileset.
    size : {'orig', 'large', 'thumb'} or int, optional
        If an integer, use  it as the size of the cached image to create and return.
        Else, should be a string in the given list.

    Returns
    -------
    str
        The image resource file name.
    """
    return __hash("image", scan_id, fileset_id, file_id, size) + ".jpeg"

__image_resize Link

__image_resize(img, max_size)

Resize a Pillow image.

Parameters:

Name Type Description Default
img Image

A Pillow image to resize.

required
max_size int

The requested max width or height size, in pixels.

required

Returns:

Type Description
Image

The resized image.

Source code in plantdb/server/webcache.py
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
def __image_resize(img, max_size):
    """Resize a ``Pillow`` image.

    Parameters
    ----------
    img : PIL.Image.Image
        A ``Pillow`` image to resize.
    max_size : int
        The requested max width or height size, in pixels.

    Returns
    -------
    PIL.Image.Image
        The resized image.
    """
    img.thumbnail((max_size, max_size))
    return img

__pointcloud_cache Link

__pointcloud_cache(db, scan_id, fileset_id, file_id, size)

Create a cache for a pointcloud resource.

Parameters:

Name Type Description Default
db FSDB

The database object.

required
scan_id str

The ID of the scan in the database.

required
fileset_id str

The ID of the fileset in the scan.

required
file_id str

The ID of the file in the fileset.

required
size (orig, preview)

The requested size of the point cloud. Obviously 'orig' preserve the original point cloud. 'preview' will resize the point cloud to a 1.8 voxel size. A float will resize the point cloud to given voxel size.

'orig'
See Also

__pointcloud_resize

Returns:

Type Description
Path

The path to the cached pointcloud.

Source code in plantdb/server/webcache.py
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
def __pointcloud_cache(db, scan_id, fileset_id, file_id, size):
    """Create a cache for a pointcloud resource.

    Parameters
    ----------
    db : plantdb.commons.fsdb.FSDB
        The database object.
    scan_id : str
        The ID of the scan in the database.
    fileset_id : str
        The ID of the fileset in the scan.
    file_id : str
        The ID of the file in the fileset.
    size : {'orig', 'preview'} or float
        The requested size of the point cloud.
        Obviously 'orig' preserve the original point cloud.
        'preview' will resize the point cloud to a `1.8` voxel size.
        A float will resize the point cloud to given voxel size.

    See Also
    --------
    __pointcloud_resize

    Returns
    -------
    pathlib.Path
        The path to the cached pointcloud.
    """
    read_pointcloud = o3d.io.read_point_cloud
    write_pointcloud = o3d.io.write_point_cloud
    # Get the path to the 'webcache' directory:
    cache_dir = __webcache_path(db, scan_id)
    dst = cache_dir / __pointcloud_hash(scan_id, fileset_id, file_id, size)

    # Load the pointcloud and resize it:
    src = __file_path(db, scan_id, fileset_id, file_id)
    pcd = read_pointcloud(str(src))
    pcd_npts = len(pcd.points)  # get the number of points
    if isinstance(size, float):
        vxs = size
    else:
        vxs = 1.8
    pcd_lowres = __pointcloud_resize(pcd, vxs)
    pcd_lowres_npts = len(pcd_lowres.points)  # get the number of points
    write_pointcloud(str(dst), pcd_lowres)

    print(f"Converted '{src}' to '{dst}', using voxelsize '{vxs}'")
    print(f"  - Original number of points: {pcd_npts}")
    print(f"  - Resized number of points: {pcd_lowres_npts}")

    return dst

__pointcloud_cached_path Link

__pointcloud_cached_path(db, scan_id, fileset_id, file_id, size)

Get The path to the cached pointcloud.

Parameters:

Name Type Description Default
db FSDB

The database object.

required
scan_id str

The ID of the scan in the database.

required
fileset_id str

The ID of the fileset in the scan.

required
file_id str

The ID of the file in the fileset.

required
size (orig, preview)

The requested size of the point cloud. Obviously 'orig' preserve the original point cloud. 'preview' will resize the point cloud to a 1.8 voxel size. A float will resize the point cloud to given voxel size.

'orig'

Returns:

Type Description
Path

The path to the cached pointcloud.

Source code in plantdb/server/webcache.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
def __pointcloud_cached_path(db, scan_id, fileset_id, file_id, size):
    """Get The path to the cached pointcloud.

    Parameters
    ----------
    db : plantdb.commons.fsdb.FSDB
        The database object.
    scan_id : str
        The ID of the scan in the database.
    fileset_id : str
        The ID of the fileset in the scan.
    file_id : str
        The ID of the file in the fileset.
    size : {'orig', 'preview'} or float
        The requested size of the point cloud.
        Obviously 'orig' preserve the original point cloud.
        'preview' will resize the point cloud to a `1.8` voxel size.
        A float will resize the point cloud to given voxel size.

    Returns
    -------
    pathlib.Path
        The path to the cached pointcloud.
    """
    cache_dir = __webcache_path(db, scan_id)
    pcd_path = cache_dir / __pointcloud_hash(scan_id, fileset_id, file_id, size)
    if not pcd_path.is_file():
        __pointcloud_cache(db, scan_id, fileset_id, file_id, size)
    return pcd_path

__pointcloud_hash Link

__pointcloud_hash(scan_id, fileset_id, file_id, size)

Create a hash for a pointcloud resource.

Parameters:

Name Type Description Default
scan_id str

The ID of the scan in the database.

required
fileset_id str

The ID of the fileset in the scan.

required
file_id str

The ID of the file in the fileset.

required
size (orig, preview)

The requested size ('orig' or 'preview').

'orig'

Returns:

Type Description
str

The hash of the pointcloud resource.

Source code in plantdb/server/webcache.py
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
def __pointcloud_hash(scan_id, fileset_id, file_id, size):
    """Create a hash for a pointcloud resource.

    Parameters
    ----------
    scan_id : str
        The ID of the scan in the database.
    fileset_id : str
        The ID of the fileset in the scan.
    file_id : str
        The ID of the file in the fileset.
    size : {'orig', 'preview'}
        The requested size ('orig' or 'preview').

    Returns
    -------
    str
        The hash of the pointcloud resource.
    """
    return __hash("pointcloud", scan_id, fileset_id, file_id, size) + ".ply"

__pointcloud_resize Link

__pointcloud_resize(pointcloud, voxel_size)

Resize a pointcloud to given voxelsize.

Parameters:

Name Type Description Default
pointcloud PointCloud

A pointcloud to resize to given voxelsize.

required
voxel_size float

The voxelsize to use for resampling.

required

Returns:

Type Description
PointCloud

The resized pointcloud.

Source code in plantdb/server/webcache.py
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
def __pointcloud_resize(pointcloud, voxel_size):
    """Resize a pointcloud to given voxelsize.

    Parameters
    ----------
    pointcloud : open3d.geometry.PointCloud
        A pointcloud to resize to given voxelsize.
    voxel_size : float
        The voxelsize to use for resampling.

    Returns
    -------
    open3d.geometry.PointCloud
        The resized pointcloud.
    """
    return pointcloud.voxel_down_sample(voxel_size)

__webcache_path Link

__webcache_path(db, scan_id)

Creates a 'webcache' directory in the scan directory.

Parameters:

Name Type Description Default
db FSDB

The database object.

required
scan_id str

The ID of the scan in the database.

required

Returns:

Type Description
Path

The path to the 'webcache' directory for the given scan.

Source code in plantdb/server/webcache.py
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
def __webcache_path(db, scan_id):
    """Creates a 'webcache' directory in the scan directory.

    Parameters
    ----------
    db : plantdb.commons.fsdb.FSDB
        The database object.
    scan_id : str
        The ID of the scan in the database.

    Returns
    -------
    pathlib.Path
        The path to the 'webcache' directory for the given scan.
    """
    directory = db.basedir / scan_id / "webcache"
    directory.mkdir(exist_ok=True)
    return directory

image_path Link

image_path(db, scan_id, fileset_id, file_id, size='orig')

Get the path to an image file.

Parameters:

Name Type Description Default
db FSDB

The database object.

required
scan_id str

The ID of the scan in the database.

required
fileset_id str

The ID of the fileset in the scan.

required
file_id str

The ID of the file in the fileset.

required
size (orig, large, thumb)

If an integer, use it as the size of the cached image to create and return. Otherwise, should be one of the following strings, default to 'orig':

  • 'thumb': image max width and height to 150.
  • 'large': image max width and height to 1500;
  • 'orig': original image, no chache;
'orig'

Returns:

Type Description
Path

The path to the original or cached image.

Examples:

>>> from plantdb.server.webcache import image_path
>>> from plantdb.commons.test_database import test_database
>>> db = test_database('real_plant_analyzed')
>>> db.connect()
>>> # Example 1: Get the original image:
>>> image_path(db, 'real_plant_analyzed', 'images', '00000_rgb', 'orig')
PosixPath('/tmp/ROMI_DB/real_plant_analyzed/images/00000_rgb.jpg')
>>> # Example 2: Get a thumbnail of the image:
>>> image_path(db, 'real_plant_analyzed', 'images', '00000_rgb', 'thumb')
PosixPath('/tmp/ROMI_DB/real_plant_analyzed/webcache/6fbae08f195837c511af7c2864d075dd5cd153bc.jpeg')
>>> db.disconnect()
Source code in plantdb/server/webcache.py
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
def image_path(db, scan_id, fileset_id, file_id, size='orig'):
    """Get the path to an image file.

    Parameters
    ----------
    db : plantdb.commons.fsdb.FSDB
        The database object.
    scan_id : str
        The ID of the scan in the database.
    fileset_id : str
        The ID of the fileset in the scan.
    file_id : str
        The ID of the file in the fileset.
    size : {'orig', 'large', 'thumb'} or int, optional
        If an integer, use  it as the size of the cached image to create and return.
        Otherwise, should be one of the following strings, default to `'orig'`:

          - `'thumb'`: image max width and height to `150`.
          - `'large'`: image max width and height to `1500`;
          - `'orig'`: original image, no chache;

    Returns
    -------
    pathlib.Path
        The path to the original or cached image.

    Examples
    --------
    >>> from plantdb.server.webcache import image_path
    >>> from plantdb.commons.test_database import test_database
    >>> db = test_database('real_plant_analyzed')
    >>> db.connect()
    >>> # Example 1: Get the original image:
    >>> image_path(db, 'real_plant_analyzed', 'images', '00000_rgb', 'orig')
    PosixPath('/tmp/ROMI_DB/real_plant_analyzed/images/00000_rgb.jpg')
    >>> # Example 2: Get a thumbnail of the image:
    >>> image_path(db, 'real_plant_analyzed', 'images', '00000_rgb', 'thumb')
    PosixPath('/tmp/ROMI_DB/real_plant_analyzed/webcache/6fbae08f195837c511af7c2864d075dd5cd153bc.jpeg')
    >>> db.disconnect()
    """
    try:
        size = int(size)
    except ValueError:
        pass

    if size == "orig":
        return __file_path(db, scan_id, fileset_id, file_id)
    elif size == "large" or size == "thumb" or isinstance(size, int):
        return __image_cached_path(db, scan_id, fileset_id, file_id, size)
    else:
        raise ValueError(f"Unknown image size specification: {size}")

mesh_path Link

mesh_path(db, scan_id, fileset_id, file_id, size='orig')

Get the path to a mesh file.

Parameters:

Name Type Description Default
db FSDB

The database object.

required
scan_id str

The ID of the scan in the database.

required
fileset_id str

The ID of the fileset in the scan.

required
file_id str

The ID of the file in the fileset.

required
size any

UNUSED.

'orig'

Returns:

Type Description
Path

The path to the original mesh.

Examples:

>>> from plantdb.server.webcache import mesh_path
>>> from plantdb.commons.test_database import test_database
>>> db = test_database('real_plant_analyzed')
>>> db.connect()
>>> # Example 1: Get the original pointcloud:
>>> mesh_path(db, 'real_plant_analyzed', 'TriangleMesh_9_most_connected_t_open3d_00e095c359', 'TriangleMesh', 'orig')
PosixPath('/tmp/ROMI_DB/real_plant_analyzed/TriangleMesh_9_most_connected_t_open3d_00e095c359/TriangleMesh.ply')
>>> db.disconnect()
Source code in plantdb/server/webcache.py
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
def mesh_path(db, scan_id, fileset_id, file_id, size='orig'):
    """Get the path to a mesh file.

    Parameters
    ----------
    db : plantdb.commons.fsdb.FSDB
        The database object.
    scan_id : str
        The ID of the scan in the database.
    fileset_id : str
        The ID of the fileset in the scan.
    file_id : str
        The ID of the file in the fileset.
    size : any, optional
        UNUSED.

    Returns
    -------
    pathlib.Path
        The path to the original mesh.

    Examples
    --------
    >>> from plantdb.server.webcache import mesh_path
    >>> from plantdb.commons.test_database import test_database
    >>> db = test_database('real_plant_analyzed')
    >>> db.connect()
    >>> # Example 1: Get the original pointcloud:
    >>> mesh_path(db, 'real_plant_analyzed', 'TriangleMesh_9_most_connected_t_open3d_00e095c359', 'TriangleMesh', 'orig')
    PosixPath('/tmp/ROMI_DB/real_plant_analyzed/TriangleMesh_9_most_connected_t_open3d_00e095c359/TriangleMesh.ply')
    >>> db.disconnect()
    """
    print("Using original mesh file")
    return __file_path(db, scan_id, fileset_id, file_id)

pointcloud_path Link

pointcloud_path(db, scan_id, fileset_id, file_id, size='orig')

Get the path to a point cloud file.

Parameters:

Name Type Description Default
db FSDB

The database object.

required
scan_id str

The ID of the scan in the database.

required
fileset_id str

The ID of the fileset in the scan.

required
file_id str

The ID of the file in the fileset.

required
size (orig, preview)

The requested size of the point cloud. Obviously 'orig' preserve the original point cloud. 'preview' will resize the point cloud to a 1.8 voxel size. A float will resize the point cloud to given voxel size. Default to 'orig'.

'orig'

Returns:

Type Description
Path

The path to the original or cached pointcloud.

Examples:

>>> from plantdb.server.webcache import pointcloud_path
>>> from plantdb.commons.test_database import test_database
>>> db = test_database('real_plant_analyzed')
>>> db.connect()
>>> # Example 1: Get the original pointcloud:
>>> pointcloud_path(db, 'real_plant_analyzed', 'PointCloud_1_0_1_0_10_0_7ee836e5a9', 'PointCloud', 'orig')
PosixPath('/tmp/ROMI_DB/real_plant_analyzed/PointCloud_1_0_1_0_10_0_7ee836e5a9/PointCloud.ply')
>>> # Example 2: Get a down-sampled version of the pointcloud:
>>> pointcloud_path(db, 'real_plant_analyzed', 'PointCloud_1_0_1_0_10_0_7ee836e5a9', 'PointCloud', 'preview')
PosixPath('/tmp/ROMI_DB/real_plant_analyzed/webcache/77e25820ddd8facd7d7a4bc5b17ad3c81046becc.ply')
>>> db.disconnect()
Source code in plantdb/server/webcache.py
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
def pointcloud_path(db, scan_id, fileset_id, file_id, size='orig'):
    """Get the path to a point cloud file.

    Parameters
    ----------
    db : plantdb.commons.fsdb.FSDB
        The database object.
    scan_id : str
        The ID of the scan in the database.
    fileset_id : str
        The ID of the fileset in the scan.
    file_id : str
        The ID of the file in the fileset.
    size : {'orig', 'preview'} or float, optional
        The requested size of the point cloud.
        Obviously 'orig' preserve the original point cloud.
        'preview' will resize the point cloud to a `1.8` voxel size.
        A float will resize the point cloud to given voxel size.
        Default to 'orig'.

    Returns
    -------
    pathlib.Path
        The path to the original or cached pointcloud.

    Examples
    --------
    >>> from plantdb.server.webcache import pointcloud_path
    >>> from plantdb.commons.test_database import test_database
    >>> db = test_database('real_plant_analyzed')
    >>> db.connect()
    >>> # Example 1: Get the original pointcloud:
    >>> pointcloud_path(db, 'real_plant_analyzed', 'PointCloud_1_0_1_0_10_0_7ee836e5a9', 'PointCloud', 'orig')
    PosixPath('/tmp/ROMI_DB/real_plant_analyzed/PointCloud_1_0_1_0_10_0_7ee836e5a9/PointCloud.ply')
    >>> # Example 2: Get a down-sampled version of the pointcloud:
    >>> pointcloud_path(db, 'real_plant_analyzed', 'PointCloud_1_0_1_0_10_0_7ee836e5a9', 'PointCloud', 'preview')
    PosixPath('/tmp/ROMI_DB/real_plant_analyzed/webcache/77e25820ddd8facd7d7a4bc5b17ad3c81046becc.ply')
    >>> db.disconnect()
    """
    if size == "orig":
        print("Using original pointcloud file")
        return __file_path(db, scan_id, fileset_id, file_id)
    elif size == "preview":
        print("Using cached pointcloud file")
        return __pointcloud_cached_path(db, scan_id, fileset_id, file_id, size)
    else:
        try:
            path = __pointcloud_cached_path(db, scan_id, fileset_id, file_id, float(size))
        except ValueError:
            raise ValueError(f"Unknown pointcloud size specification: {size}")
        else:
            return path