-
Notifications
You must be signed in to change notification settings - Fork 0
/
cs2cs.m
84 lines (75 loc) · 2.35 KB
/
cs2cs.m
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
function [x,y]=cs2cs(u, v, prj4_params)
%CS2CS - cartographic coordinate system filter
% This is a wrapper-function for cs2cs.
%
% Syntax: [x,y] = cs2cs(u, v, prj4_params)
%
% Inputs:
% u - vector with horizontal input coordinates
% v - vector with vertical input coordinates
% prj4_params - parameters of cs2cs
%
% Outputs:
% x - vector with horicontal output coordinates
% y - vector with vertical output coordinates
%
% Author: Erwin Nindl
% Email: [email protected]
% Website: https://github.com/nine/matlab-cs2cs
% April 2013; Last revision: 12-May-2004
% call cs2cs from matlab
% inspired by
% http://marinescience.wiki.otago.ac.nz/Cs2csFromMatlab
%------------------------------------------------------------------------
% input validation
%------------------------------------------------------------------------
if nargin<3
error('please provide all params');
end
u = u(:);
v = v(:);
if length(u)~=length(v)
error('input coordinates must have the same length');
end
if ~isempty(u)
% cs2cs: operating system dependent stuff
%----------------------------------------------------------------------
if ispc() % MS windows
f = filesep();
[pathstr,~,~] = fileparts(mfilename('fullpath'));
proj_path = [pathstr f 'util' f 'proj' f 'bin'];
proj_lib_path = [pathstr f 'util' f 'proj' f 'nad'];
clear pathstr;
clear f;
setenv('PATH', [getenv('PATH') ';' proj_path]);
setenv('PROJ_LIB', proj_lib_path);
else
if isunix() || ismac()
if unix('which cs2cs')
error('binary of cs2cs not found in path');
end
else % unknown OS
error('operating system not supported');
end
end
% call cs2cs
%----------------------------------------------------------------------
tmp_file1 = tempname(); % infile
tmp_file2 = tempname(); % outfile
dlmwrite(tmp_file1, [u, v], 'delimiter', ' ', 'precision', '%.6f');
[status,result] = system(['cs2cs ' prj4_params ' < ' tmp_file1 ' > ' tmp_file2]);
if status~=0
error(['Calling cs2cs: ' result]);
end
M = dlmread(tmp_file2);
x = M(:,1);
y = M(:,2);
% cleanup
%----------------------------------------------------------------------
delete(tmp_file1);
delete(tmp_file2);
else
x = [];
y = [];
end
end %eof