-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathraster_summary.py
More file actions
68 lines (52 loc) · 1.7 KB
/
raster_summary.py
File metadata and controls
68 lines (52 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
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
#!/usr/bin/python3
""" raster_summary.py
Provides a summary of the raster metadata and plot the image.
User ensures the raster file path is known and pass into this function.
Note the naming scheme, '.tif' is the common type when raster is originally created.
https://rasterio.readthedocs.io/en/latest/topics/reading.html
This function opens the file for reading then calls the close() method to end
the process. If this function gets called many times, this could slow down
the process. Remove the close() at the end or redesign the code to avoid
process slowdown.
Libraries:
OS
Pprint
Rasterio
API Calls:
os.path.join()
rasterio.open()
pprint()
show()
External Dependency:
None
"""
import os
from pprint import pprint
import rasterio
from rasterio.plot import show
def raster_summary(raster_dir: str, raster_file: str):
"""Reads a raster file and outputs the metadata and image.
Args:
raster_dir: dir path location of the raster file
raster_file: raster file name
Returns:
None
"""
# for single file operation, use line by line commands
# open file for rasterio
fp = os.path.join(raster_dir, raster_file)
# Open the file:
raster = rasterio.open(fp)
# from rasterio doc: attributes
print(f'Raster shape:\t\t {raster.shape}')
print(f'Raster band count:\t {raster.count}')
print(f'Raster data types:\t {raster.dtypes} ')
print(f'Raster valid data mask:\t {raster.nodatavals}')
print(f'Raster not valid mask:\t {raster.nodata}')
print(f'Raster metadata:\n')
pprint(raster.meta)
# the plot dimensions show the longitude, x, and lattitude, y
show(raster);
raster.close()
return
# end raster_summary