-
Notifications
You must be signed in to change notification settings - Fork 0
/
georeferencing.py
615 lines (372 loc) · 17.4 KB
/
georeferencing.py
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
69
70
71
72
73
74
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
111
112
113
114
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
140
141
142
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
223
224
225
226
227
228
229
230
231
232
233
234
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
262
263
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
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
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
411
412
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
442
443
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
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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
from pyproj import Transformer
import rasterio
import csv
import skimage
import numpy as np
import os
# TODO check if current image_mode and destination image mode are the same
# TODO add indication in file name in case of cube origin mode
class GCPList(list):
SUPPORTED_IMAGE_MODES = ['bin3', 'scale3', 'standard']
SUPPORTED_ORIGIN_MODES = ['qgis', 'cube']
def __init__(self, filename, crs='epsg:4326', image_mode=None, origin_mode=None, cube_height=None, cube_width=None):
super().__init__()
# Split the path from the filename
path, file = os.path.split(filename)
# Convert to string if they aren't already
path = str(path)
file = str(file)
# Drop the file extension
base, extension = file.rsplit('.', 1)
# Find image mode string and remove it from basename
image_mode_indices = []
for im in self.SUPPORTED_IMAGE_MODES:
image_mode_indices.append(base.find('-' + im))
if max(image_mode_indices) < 1:
basename = base
else:
basename = base[:max(image_mode_indices)]
# Set filename info
self.filename = str(filename)
self.path = str(path)
self.extension = str(extension)
self.basename = str(basename)
# Set CRS
self.crs = crs
# Set image mode
self.image_mode = self._check_image_mode(image_mode)
# Set origin mode:
self.origin_mode = self._check_origin_mode(origin_mode)
# Set height:
self.cube_height = cube_height
# Set width:
self.cube_width = cube_width
self._load_gcps()
def _detect_image_mode(self):
for image_mode in self.SUPPORTED_IMAGE_MODES:
if '-' + image_mode in str(self.filename):
detected_image_mode = image_mode
if detected_image_mode:
print('No image mode provided. Detected image mode: ' + detected_image_mode)
else:
print('No image mode provided. Assuming image mode is: ' + detected_image_mode)
return detected_image_mode
def _check_image_mode(self, image_mode):
if not image_mode:
image_mode = self._detect_image_mode()
if image_mode not in self.SUPPORTED_IMAGE_MODES:
print('Invalid image mode ' + image_mode + ' provided. Defaulting to \'standard\' image mode.')
image_mode = 'standard'
return image_mode
def _check_origin_mode(self, origin_mode):
if not origin_mode:
origin_mode = None
return origin_mode
if origin_mode not in self.SUPPORTED_ORIGIN_MODES:
print('Invalid origin mode ' + origin_mode + ' provided.')
origin_mode = None
return origin_mode
def _load_gcps(self):
header, fieldnames, unproc_gcps = PointsCSV(self.filename).read_points_csv()
for unproc_gcp in unproc_gcps:
gcp = GCP(**unproc_gcp, crs=self.crs)
self.append(gcp)
def save(self, filename=None):
if filename is None:
print('Writing .points file to: ' + self.filename)
pcsv = PointsCSV(self.filename)
else:
pcsv = PointsCSV(filename)
pcsv.write_points_csv(gcps=self)
def _update_filename(self):
match self.image_mode:
case 'standard':
self.filename = str(self.path) + '/' + str(self.basename) + '.' + str(self.extension)
case 'bin3':
self.filename = str(self.path) + '/' + str(self.basename) + '-bin3.' + str(self.extension)
case 'scale3':
self.filename = str(self.path) + '/' + str(self.basename) + '-scale3.' + str(self.extension)
case _:
print('Invalid image_mode')
def convert_crs(self, dst_crs=None):
if dst_crs is not None:
for gcp in self:
gcp.convert_gcp_crs(dst_crs)
def change_image_mode(self, dst_image_mode=None):
match self.image_mode:
case 'standard':
match dst_image_mode:
case 'standard':
self._update_filename()
case 'bin3':
self._standard_to_bin3_image_mode()
self._update_filename()
case 'scale3':
self._standard_to_scale3_image_mode()
self._update_filename()
case _:
print('Invalid dst_image_mode')
case 'bin3':
match dst_image_mode:
case 'standard':
self._bin3_to_standard_image_mode()
self._update_filename()
case 'bin3':
self._update_filename()
case 'scale3':
self._bin3_to_scale3_image_mode()
self._update_filename()
case _:
print('Invalid dst_image_mode')
case 'scale3':
match dst_image_mode:
case 'standard':
self._scale3_to_standard_image_mode()
self._update_filename()
case 'bin3':
self._scale3_to_bin3_image_mode()
self._update_filename()
case 'scale3':
self._update_filename()
case _:
print('Invalid dst_image_mode')
case _:
print('Invalid image_mode')
# standard image mode conversion functons
def _standard_to_bin3_image_mode(self):
for idx, gcp in enumerate(self):
# Apply binning
gcp['sourceY'] = gcp['sourceY'] / 3
# Update GCP
self[idx] = GCP(**gcp, crs=gcp.crs)
self.image_mode = 'bin3'
def _standard_to_scale3_image_mode(self):
for idx, gcp in enumerate(self):
# Apply scaling
gcp['sourceX'] = gcp['sourceX'] * 3
# Update GCP
self[idx] = GCP(**gcp, crs=gcp.crs)
self.image_mode = 'scale3'
# bin3 image mode conversion functions
def _bin3_to_standard_image_mode(self):
for idx, gcp in enumerate(self):
# Apply scaling
gcp['sourceY'] = gcp['sourceY'] * 3
# Update GCP
self[idx] = GCP(**gcp, crs=gcp.crs)
self.image_mode = 'standard'
def _bin3_to_scale3_image_mode(self):
for idx, gcp in enumerate(self):
# Apply scaling
gcp['sourceX'] = gcp['sourceX'] * 3
gcp['sourceY'] = gcp['sourceY'] * 3
# Update GCP
self[idx] = GCP(**gcp, crs=gcp.crs)
self.image_mode = 'scale3'
# scale3 image mode conversion functions
def _scale3_to_standard_image_mode(self):
for idx, gcp in enumerate(self):
# Apply scaling
gcp['sourceX'] = gcp['sourceX'] / 3
# Update GCP
self[idx] = GCP(**gcp, crs=gcp.crs)
self.image_mode = 'standard'
def _scale3_to_bin3_image_mode(self):
for idx, gcp in enumerate(self):
# Apply scaling
gcp['sourceX'] = gcp['sourceX'] / 3
gcp['sourceY'] = gcp['sourceY'] / 3
# Update GCP
self[idx] = GCP(**gcp, crs=gcp.crs)
self.image_mode = 'bin3'
def change_origin_mode(self, dst_origin_mode=None):
if self.cube_height is None or self.cube_width is None:
print('No available cube height or width information. Unable to change origin mode.')
return
match self.origin_mode:
case 'qgis':
# convert to cube origin mode
self._qgis_to_cube_origin_mode()
case 'cube':
# convert to qgis origin mode
self._cube_to_qgis_origin_mode()
case _:
print('No origin mode set. Please first provide an origin mode before running this function.')
def _qgis_to_cube_origin_mode(self):
image_mode_height = self._get_image_mode_height()
for idx, gcp in enumerate(self):
# Switch to top left origin
gcp['sourceY'] = gcp['sourceY'] + image_mode_height
# Update GCP
self[idx] = GCP(**gcp, crs=gcp.crs)
self.origin_mode = 'cube'
def _cube_to_qgis_origin_mode(self):
image_mode_height = self._get_image_mode_height()
for idx, gcp in enumerate(self):
# Switch to top left origin
gcp['sourceY'] = gcp['sourceY'] - image_mode_height
# Update GCP
self[idx] = GCP(**gcp, crs=gcp.crs)
self.origin_mode = 'cube'
def _get_image_mode_height(self):
match self.image_mode:
case 'standard':
return self.cube_height
case 'bin3':
return self.cube_height / 3
case 'scale3':
return self.cube_height
case _:
print('Invalid image_mode')
return self.cube_height
def _get_image_mode_width(self):
match self.image_mode:
case 'standard':
return self.cube_width
case 'bin3':
return self.cube_width
case 'scale3':
return self.cube_width * 3
case _:
print('Invalid image_mode')
return self.cube_width
class GCP(dict):
def __init__(self, mapX, mapY, sourceX, sourceY, enable=1, dX=0, dY=0, residual=0, crs='epsg:4326'):
# Initialize dict
super().__init__(mapX=mapX,
mapY=mapY,
sourceX=sourceX,
sourceY=sourceY,
enable=enable,
dX=dX,
dY=dY,
residual=residual)
self.crs=crs
# Add rasterio GCP
self.gcp = rasterio.control.GroundControlPoint(row=self['sourceX'],
col=self['sourceY'],
x=self['mapX'],
y=self['mapY'])
def convert_gcp_crs(self, dst_crs):
src_crs = self.crs
if src_crs.lower() != dst_crs.lower():
# Initialize transformer for CRS conversion
transformer = Transformer.from_crs(src_crs, dst_crs)
# mapX is lon
# mapY is lat
lon = self['mapX']
lat = self['mapY']
lat, lon = transformer.transform(lon, lat)
self['mapX'] = lon
self['mapY'] = lat
# Update rasterio GCP
self.gcp = rasterio.control.GroundControlPoint(row=self['sourceX'],
col=self['sourceY'],
x=self['mapX'],
y=self['mapY'])
self.crs = dst_crs
class PointsCSV():
def __init__(self, filename):
self.filename = str(filename)
self.default_header = '#CRS: GEOGCRS["WGS 84",ENSEMBLE["World Geodetic System 1984 ensemble",MEMBER["World Geodetic System 1984 (Transit)"],MEMBER["World Geodetic System 1984 (G730)"],MEMBER["World Geodetic System 1984 (G873)"],MEMBER["World Geodetic System 1984 (G1150)"],MEMBER["World Geodetic System 1984 (G1674)"],MEMBER["World Geodetic System 1984 (G1762)"],ELLIPSOID["WGS 84",6378137,298.257223563,LENGTHUNIT["metre",1]],ENSEMBLEACCURACY[2.0]],PRIMEM["Greenwich",0,ANGLEUNIT["degree",0.0174532925199433]],CS[ellipsoidal,2],AXIS["geodetic latitude (Lat)",north,ORDER[1],ANGLEUNIT["degree",0.0174532925199433]],AXIS["geodetic longitude (Lon)",east,ORDER[2],ANGLEUNIT["degree",0.0174532925199433]],USAGE[SCOPE["Horizontal component of 3D system."],AREA["World."],BBOX[-90,-180,90,180]],ID["EPSG",4326]]'
self.default_fieldnames = 'mapX,mapY,sourceX,sourceY,enable,dX,dY,residual'
self.default_fieldnames_list = ['mapX', 'mapY', 'sourceX', 'sourceY', 'enable', 'dX', 'dY', 'residual']
def write_points_csv(self, gcps=[]):
with open(str(self.filename), 'w') as csv_file:
csv_file.write(self.default_header)
csv_file.write('\n')
# Open CSV file for writing
writer = csv.DictWriter(csv_file,
fieldnames=self.default_fieldnames_list)
writer.writeheader()
if len(gcps) > 0:
for gcp in gcps:
writer.writerow(gcp)
# Close file
csv_file.close()
def read_points_csv(self):
# Unprocessed GCPs list
unproc_gcps = []
with open(str(self.filename), 'r') as csv_file:
header = csv_file.readline().rstrip()
fieldnames = csv_file.readline().rstrip()
# Open CSV file for reading
reader = csv.DictReader(csv_file,
fieldnames=self.default_fieldnames_list)
# Iterate through rows
for line in reader:
# Convert to int/floats
for key, value in line.items():
try:
line[key] = int(value)
except ValueError:
try:
line[key] = float(value)
except ValueError:
pass
# Add line to unprocessed GCPs list
unproc_gcps.append(line)
# Close file
csv_file.close()
return header, fieldnames, unproc_gcps
class Georeferencer(GCPList):
def __init__(self, filename, cube_height, cube_width, crs='epsg:4326', image_mode=None, origin_mode='qgis'):
super().__init__(filename, crs=crs, image_mode=image_mode, origin_mode=origin_mode, cube_height=cube_height, cube_width=cube_width)
self.cube_height = cube_height
self.cube_width = cube_width
self.img_coords = None
self.geo_coords = None
self.latitudes = None
self.longitudes = None
# Estimate polynomial transform
self._estimate_polynomial_transform()
# Generate latitude and longitude arrays
self._generate_polynomial_lat_lon_arrays()
def _estimate_polynomial_transform(self):
# https://scikit-image.org/docs/stable/api/skimage.transform.html#polynomialtransform
self.img_coords = np.zeros((len(self),2))
self.geo_coords = np.zeros((len(self),2))
# Load image coords and geospatial coords from GCPs.
for i,gcp in enumerate(self):
self.img_coords[i,0] = gcp['sourceX'] + 0.5
self.img_coords[i,1] = -gcp['sourceY']*3 - 0.5
self.geo_coords[i,0] = gcp['mapX']
self.geo_coords[i,1] = gcp['mapY']
# Estimate transform
self.transform = skimage.transform.estimate_transform('polynomial', self.img_coords, self.geo_coords, 2)
# Get coefficients from transform
self.lat_coefficients = self.transform.params[0]
self.lon_coefficients = self.transform.params[1]
def _generate_polynomial_lat_lon_arrays(self):
# Create empty arrays to write lat and lon data
self.latitudes = np.empty((self.cube_height, self.cube_width))
self.longitudes = np.empty((self.cube_height, self.cube_width))
# Generate X and Y coordinates
x_coords, y_coords = np.meshgrid(np.arange(self.cube_height), np.arange(self.cube_width), indexing='ij')
# Combine the X and Y coordinates into a list of (x, y) tuples
image_coordinates = list(zip(x_coords.ravel(), y_coords.ravel()))
# Transform X and Y coordnates to geospatial coordinates
geo_coordinates = self.transform(image_coordinates)
# Copy transformed lat and lon coords into lat and lon arrays
for idx,coord in enumerate(image_coordinates):
self.longitudes[coord] = geo_coordinates[idx,0]
self.latitudes[coord] = geo_coordinates[idx,1]
def _compute_polynomial_transform(self, X, Y, lat_coefficients, lon_coefficients):
## Example usage:
#for Y in range(0, image_height):
# for X in range(0, image_width):
# lon, lat = self.compute_polynomial_transform(Y, X, lat_coefficients, lon_coefficients)
# lats[Y,X] = lat
# lons[Y,X] = lon
#X = sum[j=0:order]( sum[i=0:j]( a_ji * x**(j - i) * y**i ))
#x.T = [a00 a10 a11 a20 a21 a22 ... ann
# b00 b10 b11 b20 b21 b22 ... bnn c3]
#X = (( a_00 * x**(0 - 0) * y**0 ))
#(( a_10 * x**(1 - 0) * y**0 )) + (( a_11 * x**(1 - 1) * y**1 ))
#(( a_20 * x**(2 - 0) * y**0 )) + (( a_21 * x**(2 - 1) * y**1 ))
# + (( a_22 * x**(2 - 2) * y**2 ))
c = lat_coefficients
lat = c[0] + c[1]*X + c[2]*Y + c[3]*X**2 + c[4]*X*Y + c[5]*Y**2
c = lon_coefficients
lon = c[0] + c[1]*X + c[2]*Y + c[3]*X**2 + c[4]*X*Y + c[5]*Y**2
return (lat, lon)