Skip to content

Commit

Permalink
answer discussion 12
Browse files Browse the repository at this point in the history
  • Loading branch information
HowcanoeWang committed Aug 6, 2022
1 parent b9d5154 commit 5913fdf
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 18 deletions.
11 changes: 6 additions & 5 deletions easyidp/metashape.py
Original file line number Diff line number Diff line change
Expand Up @@ -1027,22 +1027,23 @@ def convert_proj3d(points_np, crs_origin, crs_target, is_xyz=True):
if crs_target.is_geocentric:
x, y, z = ts.transform(*points_np.T)
out = np.vstack([x, y, z]).T

if crs_target.is_geographic:
elif crs_target.is_geographic:
lat, lon, alt = ts.transform(*points_np.T)
# the pyproj output order is reversed
out = np.vstack([lon, lat, alt]).T
else:
raise TypeError(f"Given crs is neither `crs.is_geocentric=True` nor `crs.is_geographic`")
else:
lon, lat, alt = points_np[:,0], points_np[:,1], points_np[:,2]

if crs_target.is_geocentric:
x, y, z = ts.transform(lat, lon, alt)
out = np.vstack([x, y, z]).T

if crs_target.is_geographic:
elif crs_target.is_geographic:
lat, lon, alt = ts.transform(lat, lon, alt)
out = np.vstack([lon, lat, alt]).T

else:
raise TypeError(f"Given crs is neither `crs.is_geocentric=True` nor `crs.is_geographic`")
if is_single:
return out[0, :]
else:
Expand Down
12 changes: 10 additions & 2 deletions easyidp/shp.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,14 +306,22 @@ def convert_proj(shp_dict, origin_proj, target_proj):
transformer = pyproj.Transformer.from_proj(origin_proj, target_proj)
trans_dict = {}
for k, coord_np in shp_dict.items():
transformed = transformer.transform(coord_np[:, 0], coord_np[:, 1])
if len(coord_np.shape) == 1:
transformed = transformer.transform(coord_np[0], coord_np[1])
elif len(coord_np.shape) == 2:
transformed = transformer.transform(coord_np[:, 0], coord_np[:, 1])
else:
raise IndexError(
f"The input coord should be either [x, y, z] -> shape=(3,) "
f"or [[x,y,z], [x,y,z], ...] -> shape=(n, 3)"
f"not current {coord_np.shape}")
coord_np = np.asarray(transformed).T

# judge if has inf value, means convert fail
if True in np.isinf(coord_np):
raise ValueError(
f'Fail to convert points from "{origin_proj.name}" to '
f'"{target_proj.name}"(dsm projection), '
f'"{target_proj.name}", '
f'this may caused by the uncertainty of .prj file strings, '
f'please check the coordinate manually via QGIS Layer Infomation, '
f'get the EPGS code, and specify the function argument'
Expand Down
13 changes: 6 additions & 7 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import os
import sys
import pathlib
from pathlib import Path

sys.path.insert(0, ".")

# check if output path exists
out_dir = pathlib.Path("./tests/out")
if not os.path.exists(out_dir):
os.makedirs(out_dir)
out_dir = Path("./tests/out")
if not out_dir.exists():
out_dir.mkdir()

out_folders = ["json_test", "pcd_test", "cv_test", "tiff_test", "visual_test"]

for o in out_folders:
sub_dir = out_dir / o
if not os.path.exists(sub_dir):
os.mkdir(sub_dir)
if not sub_dir.exists():
sub_dir.mkdir()
26 changes: 25 additions & 1 deletion tests/test_metashape.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,4 +200,28 @@ def test_class_back2raw_and_crs():
out_all = ms.back2raw(roi)

assert len(out_all) == 2
assert isinstance(out_all["N1W2"], dict)
assert isinstance(out_all["N1W2"], dict)


def test_debug_discussion_12():
ms = idp.Metashape()

pos = np.array([-3943837.80419438, 3363533.48603071, 3704402.1784295 ])
ms.crs = pyproj.CRS.from_epsg(32654)

'''
The following code will raise the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "D:\OneDrive\Program\GitHub\EasyIDP\easyidp\metashape.py", line 99, in _world2crs
return convert_proj3d(points_np, self.world_crs, self.crs)
File "D:\OneDrive\Program\GitHub\EasyIDP\easyidp\metashape.py", line 1047, in convert_proj3d
return out[0, :]
raise UnboundLocalError: local variable 'out' referenced before assignment
Reasons:
is_xyz = True -> crs_target(epsg32654) neither is_geocentric nor is_geographic
'''
with pytest.raises(TypeError, match=re.escape("Given crs is neither `crs.is_geocentric=True` nor `crs.is_geographic`")):
out = ms._world2crs(pos)
3 changes: 1 addition & 2 deletions tests/test_pix4d.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import os
import pytest
import numpy as np
import pyproj
Expand Down Expand Up @@ -93,7 +92,7 @@ def test_parse_p4d_param_folder():
param = idp.pix4d.parse_p4d_param_folder(param_folder)

assert param["project_name"] == "maize_tanashi_3NA_20190729_Ins1Rgb_30m_pix4d"
assert os.path.basename(param["xyz"]) == "maize_tanashi_3NA_20190729_Ins1Rgb_30m_pix4d_offset.xyz"
assert Path(param["xyz"]).name == "maize_tanashi_3NA_20190729_Ins1Rgb_30m_pix4d_offset.xyz"


def test_read_params():
Expand Down
1 change: 0 additions & 1 deletion tests/test_reconstruct.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import sys
import re
import pytest
import numpy as np
Expand Down

0 comments on commit 5913fdf

Please sign in to comment.