Skip to content

Commit b8c4fe9

Browse files
committed
fix for reading gcp_file.txt
1 parent 0435df6 commit b8c4fe9

2 files changed

Lines changed: 28 additions & 28 deletions

File tree

dm/opendm/gcp.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,21 @@ def __init__(self, gcp_path):
1414

1515
def read(self):
1616
if self.exists():
17+
# Read the gcp_file.txt
1718
with open(self.gcp_path, 'r') as f:
18-
contents = f.read().decode('utf-8-sig').encode('utf-8').strip()
19-
20-
lines = map(str.strip, contents.split('\n'))
21-
if lines:
22-
self.raw_srs = lines[0] # SRS
23-
self.srs = location.parse_srs_header(self.raw_srs)
24-
25-
for line in lines[1:]:
26-
if line != "" and line[0] != "#":
27-
parts = line.split()
28-
if len(parts) >= 6:
29-
self.entries.append(line)
30-
else:
31-
log.MM_WARNING("Malformed GCP line: %s" % line)
19+
contents = [line.rstrip() for line in f]
20+
21+
# Get the spatial reference system (SRS) header from the first line
22+
self.raw_srs = contents[0].strip()
23+
self.srs = location.parse_srs_header(self.raw_srs)
24+
25+
for line in contents[1:]:
26+
if line != "" and line[0] != "#":
27+
parts = line.strip().split()
28+
if len(parts) >= 6:
29+
self.entries.append(line)
30+
else:
31+
log.MM_WARNING("Malformed GCP line: %s" % line)
3232

3333
def iter_entries(self):
3434
for entry in self.entries:

dm/opendm/location.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
def extract_utm_coords(photos, images_path, output_coords_file):
77
"""
8-
Create a coordinate file containing the GPS positions of all cameras
8+
Create a coordinate file containing the GPS positions of all cameras
99
to be used later in the ODM toolchain for automatic georeferecing
1010
:param photos ([ODM_Photo]) list of photos
1111
:param images_path (str) path to dataset images
@@ -14,7 +14,7 @@ def extract_utm_coords(photos, images_path, output_coords_file):
1414
"""
1515
if len(photos) == 0:
1616
raise Exception("No input images, cannot create coordinates file of GPS positions")
17-
17+
1818
utm_zone = None
1919
hemisphere = None
2020
coords = []
@@ -23,20 +23,20 @@ def extract_utm_coords(photos, images_path, output_coords_file):
2323
if photo.latitude is None or photo.longitude is None or photo.altitude is None:
2424
log.MM_ERROR("Failed parsing GPS position for %s, skipping" % photo.filename)
2525
continue
26-
26+
2727
if utm_zone is None:
2828
utm_zone, hemisphere = get_utm_zone_and_hemisphere_from(photo.longitude, photo.latitude)
2929

3030
try:
3131
coord = convert_to_utm(photo.longitude, photo.latitude, photo.altitude, utm_zone, hemisphere)
3232
except:
3333
raise Exception("Failed to convert GPS position to UTM for %s" % photo.filename)
34-
34+
3535
coords.append(coord)
3636

3737
if utm_zone is None:
3838
raise Exception("No images seem to have GPS information")
39-
39+
4040
# Calculate average
4141
dx = 0.0
4242
dy = 0.0
@@ -54,7 +54,7 @@ def extract_utm_coords(photos, images_path, output_coords_file):
5454
f.write("%s %s\n" % (dx, dy))
5555
for coord in coords:
5656
f.write("%s %s %s\n" % (coord[0] - dx, coord[1] - dy, coord[2]))
57-
57+
5858
def transform2(from_srs, to_srs, x, y):
5959
return transformer(from_srs, to_srs).TransformPoint(x, y, 0)[:2]
6060

@@ -73,14 +73,14 @@ def proj_srs_convert(srs):
7373
else:
7474
proj4 = srs.to_proj4()
7575
res.ImportFromProj4(proj4)
76-
76+
7777
return res
7878

7979
def transformer(from_srs, to_srs):
8080
src = proj_srs_convert(from_srs)
8181
tgt = proj_srs_convert(to_srs)
8282
return osr.CoordinateTransformation(src, tgt)
83-
83+
8484
def get_utm_zone_and_hemisphere_from(lon, lat):
8585
"""
8686
Calculate the UTM zone and hemisphere that a longitude/latitude pair falls on
@@ -106,7 +106,7 @@ def convert_to_utm(lon, lat, alt, utm_zone, hemisphere):
106106
p = Proj(proj='utm',zone=utm_zone,ellps='WGS84', preserve_units=True)
107107
else:
108108
p = Proj(proj='utm',zone=utm_zone,ellps='WGS84', preserve_units=True, south=True)
109-
109+
110110
x,y = p(lon, lat)
111111
return [x, y, alt]
112112

@@ -116,17 +116,17 @@ def parse_srs_header(header):
116116
:param header (str) line
117117
:return Proj object
118118
"""
119-
log.MM_INFO('Parsing SRS header: %s' % header)
119+
log.MM_INFO(f'Parsing spatial reference system (SRS) header: {header}')
120120
header = header.strip()
121121
ref = header.split(' ')
122122
try:
123123
if ref[0] == 'WGS84' and ref[1] == 'UTM':
124124
datum = ref[0]
125125
utm_pole = (ref[2][len(ref[2]) - 1]).upper()
126-
utm_zone = int(ref[2][:len(ref[2]) - 1])
127-
126+
utm_zone = int(ref[2][:-1])
127+
128128
proj_args = {
129-
'zone': utm_zone,
129+
'zone': utm_zone,
130130
'datum': datum
131131
}
132132

@@ -140,7 +140,7 @@ def parse_srs_header(header):
140140
elif header.lower().startswith("epsg:"):
141141
srs = CRS.from_epsg(header.lower()[5:])
142142
else:
143-
log.MM_ERROR('Could not parse coordinates. Bad SRS supplied: %s' % header)
143+
log.MM_ERROR(f'Could not parse coordinates. Bad SRS supplied: {header}')
144144
except RuntimeError as e:
145145
log.MM_ERROR('Uh oh! There seems to be a problem with your coordinates/GCP file.\n\n'
146146
'The line: %s\n\n'

0 commit comments

Comments
 (0)