Spatial Operations and rio
Today we are going to use the spatial background we learned yesterday and apply it to dataset exploration of an AVIRIS image. We are going to use the rio
module of xarray
.
First let’s open the datafile. We are going to use the argument engine='rasterio'
because we are using an ENVI file type. This is also the recommended method for opening TIF files.
<xarray.DataArray 'band_data' (band: 224, y: 300, x: 100)> Size: 27MB
[6720000 values with dtype=float32]
Coordinates:
* band (band) int64 2kB 1 2 3 4 5 6 7 ... 218 219 220 221 222 223 224
xc (y, x) float64 240kB ...
yc (y, x) float64 240kB ...
spatial_ref int64 8B ...
Dimensions without coordinates: y, x
Attributes: (12/236)
Band_1: Band 1
Band_10: Band 10
Band_100: Band 100
Band_101: Band 101
Band_102: Band 102
Band_103: Band 103
... ...
description: ./output_data/subset_f131205t01p00r10rdn_e_sc0...
file_type: ENVI Standard
header_offset: 0
interleave: bsq
lines: 300
samples: 100 Coordinates: (4)
Indexes: (1)
PandasIndex
PandasIndex(Index([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
...
215, 216, 217, 218, 219, 220, 221, 222, 223, 224],
dtype='int64', name='band', length=224)) Attributes: (236)
Band_1 : Band 1 Band_10 : Band 10 Band_100 : Band 100 Band_101 : Band 101 Band_102 : Band 102 Band_103 : Band 103 Band_104 : Band 104 Band_105 : Band 105 Band_106 : Band 106 Band_107 : Band 107 Band_108 : Band 108 Band_109 : Band 109 Band_11 : Band 11 Band_110 : Band 110 Band_111 : Band 111 Band_112 : Band 112 Band_113 : Band 113 Band_114 : Band 114 Band_115 : Band 115 Band_116 : Band 116 Band_117 : Band 117 Band_118 : Band 118 Band_119 : Band 119 Band_12 : Band 12 Band_120 : Band 120 Band_121 : Band 121 Band_122 : Band 122 Band_123 : Band 123 Band_124 : Band 124 Band_125 : Band 125 Band_126 : Band 126 Band_127 : Band 127 Band_128 : Band 128 Band_129 : Band 129 Band_13 : Band 13 Band_130 : Band 130 Band_131 : Band 131 Band_132 : Band 132 Band_133 : Band 133 Band_134 : Band 134 Band_135 : Band 135 Band_136 : Band 136 Band_137 : Band 137 Band_138 : Band 138 Band_139 : Band 139 Band_14 : Band 14 Band_140 : Band 140 Band_141 : Band 141 Band_142 : Band 142 Band_143 : Band 143 Band_144 : Band 144 Band_145 : Band 145 Band_146 : Band 146 Band_147 : Band 147 Band_148 : Band 148 Band_149 : Band 149 Band_15 : Band 15 Band_150 : Band 150 Band_151 : Band 151 Band_152 : Band 152 Band_153 : Band 153 Band_154 : Band 154 Band_155 : Band 155 Band_156 : Band 156 Band_157 : Band 157 Band_158 : Band 158 Band_159 : Band 159 Band_16 : Band 16 Band_160 : Band 160 Band_161 : Band 161 Band_162 : Band 162 Band_163 : Band 163 Band_164 : Band 164 Band_165 : Band 165 Band_166 : Band 166 Band_167 : Band 167 Band_168 : Band 168 Band_169 : Band 169 Band_17 : Band 17 Band_170 : Band 170 Band_171 : Band 171 Band_172 : Band 172 Band_173 : Band 173 Band_174 : Band 174 Band_175 : Band 175 Band_176 : Band 176 Band_177 : Band 177 Band_178 : Band 178 Band_179 : Band 179 Band_18 : Band 18 Band_180 : Band 180 Band_181 : Band 181 Band_182 : Band 182 Band_183 : Band 183 Band_184 : Band 184 Band_185 : Band 185 Band_186 : Band 186 Band_187 : Band 187 Band_188 : Band 188 Band_189 : Band 189 Band_19 : Band 19 Band_190 : Band 190 Band_191 : Band 191 Band_192 : Band 192 Band_193 : Band 193 Band_194 : Band 194 Band_195 : Band 195 Band_196 : Band 196 Band_197 : Band 197 Band_198 : Band 198 Band_199 : Band 199 Band_2 : Band 2 Band_20 : Band 20 Band_200 : Band 200 Band_201 : Band 201 Band_202 : Band 202 Band_203 : Band 203 Band_204 : Band 204 Band_205 : Band 205 Band_206 : Band 206 Band_207 : Band 207 Band_208 : Band 208 Band_209 : Band 209 Band_21 : Band 21 Band_210 : Band 210 Band_211 : Band 211 Band_212 : Band 212 Band_213 : Band 213 Band_214 : Band 214 Band_215 : Band 215 Band_216 : Band 216 Band_217 : Band 217 Band_218 : Band 218 Band_219 : Band 219 Band_22 : Band 22 Band_220 : Band 220 Band_221 : Band 221 Band_222 : Band 222 Band_223 : Band 223 Band_224 : Band 224 Band_23 : Band 23 Band_24 : Band 24 Band_25 : Band 25 Band_26 : Band 26 Band_27 : Band 27 Band_28 : Band 28 Band_29 : Band 29 Band_3 : Band 3 Band_30 : Band 30 Band_31 : Band 31 Band_32 : Band 32 Band_33 : Band 33 Band_34 : Band 34 Band_35 : Band 35 Band_36 : Band 36 Band_37 : Band 37 Band_38 : Band 38 Band_39 : Band 39 Band_4 : Band 4 Band_40 : Band 40 Band_41 : Band 41 Band_42 : Band 42 Band_43 : Band 43 Band_44 : Band 44 Band_45 : Band 45 Band_46 : Band 46 Band_47 : Band 47 Band_48 : Band 48 Band_49 : Band 49 Band_5 : Band 5 Band_50 : Band 50 Band_51 : Band 51 Band_52 : Band 52 Band_53 : Band 53 Band_54 : Band 54 Band_55 : Band 55 Band_56 : Band 56 Band_57 : Band 57 Band_58 : Band 58 Band_59 : Band 59 Band_6 : Band 6 Band_60 : Band 60 Band_61 : Band 61 Band_62 : Band 62 Band_63 : Band 63 Band_64 : Band 64 Band_65 : Band 65 Band_66 : Band 66 Band_67 : Band 67 Band_68 : Band 68 Band_69 : Band 69 Band_7 : Band 7 Band_70 : Band 70 Band_71 : Band 71 Band_72 : Band 72 Band_73 : Band 73 Band_74 : Band 74 Band_75 : Band 75 Band_76 : Band 76 Band_77 : Band 77 Band_78 : Band 78 Band_79 : Band 79 Band_8 : Band 8 Band_80 : Band 80 Band_81 : Band 81 Band_82 : Band 82 Band_83 : Band 83 Band_84 : Band 84 Band_85 : Band 85 Band_86 : Band 86 Band_87 : Band 87 Band_88 : Band 88 Band_89 : Band 89 Band_9 : Band 9 Band_90 : Band 90 Band_91 : Band 91 Band_92 : Band 92 Band_93 : Band 93 Band_94 : Band 94 Band_95 : Band 95 Band_96 : Band 96 Band_97 : Band 97 Band_98 : Band 98 Band_99 : Band 99 long_name : ('Band 1', 'Band 2', 'Band 3', 'Band 4', 'Band 5', 'Band 6', 'Band 7', 'Band 8', 'Band 9', 'Band 10', 'Band 11', 'Band 12', 'Band 13', 'Band 14', 'Band 15', 'Band 16', 'Band 17', 'Band 18', 'Band 19', 'Band 20', 'Band 21', 'Band 22', 'Band 23', 'Band 24', 'Band 25', 'Band 26', 'Band 27', 'Band 28', 'Band 29', 'Band 30', 'Band 31', 'Band 32', 'Band 33', 'Band 34', 'Band 35', 'Band 36', 'Band 37', 'Band 38', 'Band 39', 'Band 40', 'Band 41', 'Band 42', 'Band 43', 'Band 44', 'Band 45', 'Band 46', 'Band 47', 'Band 48', 'Band 49', 'Band 50', 'Band 51', 'Band 52', 'Band 53', 'Band 54', 'Band 55', 'Band 56', 'Band 57', 'Band 58', 'Band 59', 'Band 60', 'Band 61', 'Band 62', 'Band 63', 'Band 64', 'Band 65', 'Band 66', 'Band 67', 'Band 68', 'Band 69', 'Band 70', 'Band 71', 'Band 72', 'Band 73', 'Band 74', 'Band 75', 'Band 76', 'Band 77', 'Band 78', 'Band 79', 'Band 80', 'Band 81', 'Band 82', 'Band 83', 'Band 84', 'Band 85', 'Band 86', 'Band 87', 'Band 88', 'Band 89', 'Band 90', 'Band 91', 'Band 92', 'Band 93', 'Band 94', 'Band 95', 'Band 96', 'Band 97', 'Band 98', 'Band 99', 'Band 100', 'Band 101', 'Band 102', 'Band 103', 'Band 104', 'Band 105', 'Band 106', 'Band 107', 'Band 108', 'Band 109', 'Band 110', 'Band 111', 'Band 112', 'Band 113', 'Band 114', 'Band 115', 'Band 116', 'Band 117', 'Band 118', 'Band 119', 'Band 120', 'Band 121', 'Band 122', 'Band 123', 'Band 124', 'Band 125', 'Band 126', 'Band 127', 'Band 128', 'Band 129', 'Band 130', 'Band 131', 'Band 132', 'Band 133', 'Band 134', 'Band 135', 'Band 136', 'Band 137', 'Band 138', 'Band 139', 'Band 140', 'Band 141', 'Band 142', 'Band 143', 'Band 144', 'Band 145', 'Band 146', 'Band 147', 'Band 148', 'Band 149', 'Band 150', 'Band 151', 'Band 152', 'Band 153', 'Band 154', 'Band 155', 'Band 156', 'Band 157', 'Band 158', 'Band 159', 'Band 160', 'Band 161', 'Band 162', 'Band 163', 'Band 164', 'Band 165', 'Band 166', 'Band 167', 'Band 168', 'Band 169', 'Band 170', 'Band 171', 'Band 172', 'Band 173', 'Band 174', 'Band 175', 'Band 176', 'Band 177', 'Band 178', 'Band 179', 'Band 180', 'Band 181', 'Band 182', 'Band 183', 'Band 184', 'Band 185', 'Band 186', 'Band 187', 'Band 188', 'Band 189', 'Band 190', 'Band 191', 'Band 192', 'Band 193', 'Band 194', 'Band 195', 'Band 196', 'Band 197', 'Band 198', 'Band 199', 'Band 200', 'Band 201', 'Band 202', 'Band 203', 'Band 204', 'Band 205', 'Band 206', 'Band 207', 'Band 208', 'Band 209', 'Band 210', 'Band 211', 'Band 212', 'Band 213', 'Band 214', 'Band 215', 'Band 216', 'Band 217', 'Band 218', 'Band 219', 'Band 220', 'Band 221', 'Band 222', 'Band 223', 'Band 224') bands : 224 band_names : Band 1,Band 2,Band 3,Band 4,Band 5,Band 6,Band 7,Band 8,Band 9,Band 10,Band 11,Band 12,Band 13,Band 14,Band 15,Band 16,Band 17,Band 18,Band 19,Band 20,Band 21,Band 22,Band 23,Band 24,Band 25,Band 26,Band 27,Band 28,Band 29,Band 30,Band 31,Band 32,Band 33,Band 34,Band 35,Band 36,Band 37,Band 38,Band 39,Band 40,Band 41,Band 42,Band 43,Band 44,Band 45,Band 46,Band 47,Band 48,Band 49,Band 50,Band 51,Band 52,Band 53,Band 54,Band 55,Band 56,Band 57,Band 58,Band 59,Band 60,Band 61,Band 62,Band 63,Band 64,Band 65,Band 66,Band 67,Band 68,Band 69,Band 70,Band 71,Band 72,Band 73,Band 74,Band 75,Band 76,Band 77,Band 78,Band 79,Band 80,Band 81,Band 82,Band 83,Band 84,Band 85,Band 86,Band 87,Band 88,Band 89,Band 90,Band 91,Band 92,Band 93,Band 94,Band 95,Band 96,Band 97,Band 98,Band 99,Band 100,Band 101,Band 102,Band 103,Band 104,Band 105,Band 106,Band 107,Band 108,Band 109,Band 110,Band 111,Band 112,Band 113,Band 114,Band 115,Band 116,Band 117,Band 118,Band 119,Band 120,Band 121,Band 122,Band 123,Band 124,Band 125,Band 126,Band 127,Band 128,Band 129,Band 130,Band 131,Band 132,Band 133,Band 134,Band 135,Band 136,Band 137,Band 138,Band 139,Band 140,Band 141,Band 142,Band 143,Band 144,Band 145,Band 146,Band 147,Band 148,Band 149,Band 150,Band 151,Band 152,Band 153,Band 154,Band 155,Band 156,Band 157,Band 158,Band 159,Band 160,Band 161,Band 162,Band 163,Band 164,Band 165,Band 166,Band 167,Band 168,Band 169,Band 170,Band 171,Band 172,Band 173,Band 174,Band 175,Band 176,Band 177,Band 178,Band 179,Band 180,Band 181,Band 182,Band 183,Band 184,Band 185,Band 186,Band 187,Band 188,Band 189,Band 190,Band 191,Band 192,Band 193,Band 194,Band 195,Band 196,Band 197,Band 198,Band 199,Band 200,Band 201,Band 202,Band 203,Band 204,Band 205,Band 206,Band 207,Band 208,Band 209,Band 210,Band 211,Band 212,Band 213,Band 214,Band 215,Band 216,Band 217,Band 218,Band 219,Band 220,Band 221,Band 222,Band 223,Band 224 byte_order : 0 coordinate_system_string : PROJCS["unnamed",GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137.0,298.257223563]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Transverse_Mercator"],PARAMETER["False_Easting",500000.0],PARAMETER["False_Northing",0.0],PARAMETER["Central_Meridian",-123.0],PARAMETER["Scale_Factor",0.9996],PARAMETER["Latitude_Of_Origin",0.0],UNIT["Meter",1.0]] data_type : 2 description : ./output_data/subset_f131205t01p00r10rdn_e_sc01_ort_img file_type : ENVI Standard header_offset : 0 interleave : bsq lines : 300 samples : 100
When we are opening TIF or ENVI files using the rasterio
engine is that we get an additional set of attributes and methods that give us information about the image. We access this using .rio
(577666.150342955, 4150999.70969225, 579276.150342955, 4155829.70969225)
EPSG:32610
300
100
None
| 14.08, 7.81, 577666.15|
| 7.81,-14.08, 4150999.71|
| 0.00, 0.00, 1.00|
These metadata can be really helpful for orienting you to your data.
One field I want to make a note about is the nodata field. The nodata value is sometimes not set, but that doesn’t mean that there isn’t a nodata value in the dataset. There still may be a nodata value present in the dataset even if one is not set.
📝 Check your understanding
Look at the EPSG code of this dataset. Reference the figure below from the spatial data lecture to figure out approximately where in the world the dataset is located. Is the image in the northern or southern hemisphere?
Although the EPSG code is useful for general orientation and an overall sanity check, we usually want to know more specifically where our data is located. We can do this by extracting the bounds and converting the coordinates to EPSG 4326.
The general syntax for that is:
t = Transformer . from_crs ( INPUT_PROJ , OUTPUT_PROJ , always_xy = True ) . transform
transform ( t , SHAPELY_POINT )
Where everything in ALL_CAPS is a variable you will be inserting yourself.
Here’s an example
'POLYGON ((-122.10256826050976 37.5460596647619, -122.10308958299767 37.50252918542879, -122.12130273640575 37.50266607712935, -122.12079199682005 37.546196770577296, -122.10256826050976 37.5460596647619))'