%This document is distributed under the terms of the GNU Free Documentation
%License. Please, see http://www.gnu.org/licenses/
\documentclass[10pt,a4paper]{article}
\usepackage{tocbibind}
\usepackage{hyperref}
\hypersetup{colorlinks,citecolor=red,linkcolor=red,urlcolor=red}

\newcommand{\octproj}{\texttt{OctPROJ}}
\newcommand{\proj}{\texttt{PROJ}}
\newcommand{\octave}{GNU Octave}

\title{\octproj\\Calling \proj{} from \octave\footnote{This document is
       distributed under the terms of the GNU Free Documentation License.
       Please, see \url{http://www.gnu.org/licenses/}.}}
\author{Jos\'e Luis Garc\'ia Pallero\footnote{ETSI en Topograf\'ia, Geodesia y
        Cartograf\'ia, Universidad Polit\'ecnica de Madrid.
        \texttt{jlg.pallero@upm.es}, \texttt{jgpallero@gmail.com}.}}
\date{November 7, 2022 (version 3.0.2)\\
      October 26, 2021 (version 3.0.1)\\
      October 26, 2021 (version 3.0.0)\\
      May 16, 2020 (version 2.0.1)\\
      May 9, 2020 (version 2.0.0)\\
      April 2, 2019 (version 1.1.6)\\
      June 16, 2015 (version 1.1.5)\\
      February 13, 2015 (version 1.1.4)\\
      June 20, 2013 (version 1.1.3)\\
      October 1, 2012 (version 1.1.1)\\
      April 13, 2012 (version 1.1.0)\\
      May 13, 2011 (version 1.0.2)\\
      November 29, 2010 (version 1.0.1)\\
      February 9, 2010 (version 1.0.0)}

\begin{document}
\maketitle
% \tableofcontents

\begin{abstract}
This is a small introduction to using the \octproj{} package. In this text, you
can overview the basic usage of the functions in
\octave\footnote{\url{http://www.octave.org}.}. If you need a detailed
description about the options, available projections and coordinate reference
systems, please visit the \proj{} website\footnote{\url{https://proj.org/}.}.
Starting with \octproj{} 2.0.0 the code was upgraded to \proj{} version 6.3.0,
so older versions of the library could not work.
\end{abstract}

\tableofcontents

\section{Overview}

\octproj{} allows you to perform cartographic projections and coordinate
reference systems transformation in \octave{} using the \proj{} library. You can
take the power of \proj{} using the facilities that \octave{} provides, without
know the internals of the \proj{} C API. You can use the conversion programs
coming with \proj{} distribution, but for use them in \octave{} you must make
system calls. With \octproj{}, \proj{} can be integrated in \octave{} scripts in
a natural way.

\section{Installation}

As several \octave{} packages, \octproj{} installation consists in compiling the
C++ kernel sources (see section \ref{op-kw}), link them against \octave{}
library to generate \texttt{*.oct} functions and copy this \texttt{*.oct}
executables and other \texttt{*.m} functions into a working directory.

The automagic procedure can be easily done by running the command:

\begin{verbatim}
octave:1> pkg install octproj-x.x.x.tar.gz
\end{verbatim}
where \texttt{x.x.x} is the version number.

After that, the functions and documentation are installed in your machine and
you are ready for use the package.

\section{Kernel wrapper}
\label{op-kw}

The main functions (the functions which make the actual computations) are
written in separate files: \texttt{projwrap.h} and \texttt{projwrap.c}.

The files contain three functions:
\begin{itemize}
\item \texttt{proj\_fwd}: forward computation of geodetic to projected
      coordinates.
\item \texttt{proj\_inv}: inverse computation of projected to geodetic
      coordinates.
\item \texttt{proj\_transform}: general transformations. It is possible to make
      forward, inverse and other transformations using only one function (see
      \proj{} documentation).
\end{itemize}

\subsection{Error handling}

Error handling in the kernel wrapper is based on error codes from \proj.
Functions in \texttt{projwrap} return the \proj{} error code and the \proj{}
text string error message, which can be catched in order to work in this case.

The functions can stop the execution in presence of errors depending on the
nature of the error.
\begin{itemize}
\item If exist an error involving a general parameter of the projection, the
      execution stops.
\item If an error is found due to a wrong or out of domain input coordinate, the
      execution don't stops, but the error code is activated and the output
      coordinate corresponding to the error position have a special value.
\end{itemize}

\section{\octave{} functions}

Two types of functions are programmed for \octave: \texttt{*.oct} functions and
\texttt{*.m} functions.

\subsection{\texttt{*.oct} functions}
\label{op-of}

These functions are linked with \texttt{projwrap}. You can use it, but it is not
recommended because the input arguments are more strict (always column vectors)
than \texttt{*.m} functions and don't check for errors.

The functions are:
\begin{itemize}
\item \texttt{\_op\_geod2geoc}: transformation between geodetic and cartesian
      tridimensional geocentric coordinates.
\item \texttt{\_op\_geoc2geod}: transformation between cartesian tridimensional
      geocentric and geodetic coordinates.
\item \texttt{\_op\_fwd}: forward projection (calls the function
      \texttt{proj\_trans\_generic} with \texttt{PJ\_FWD} option).
\item \texttt{\_op\_inv}: inverse projection (calls the function
      \texttt{proj\_trans\_generic} with \texttt{PJ\_INV} option).
\item \texttt{\_op\_transform}: general transformation (calls
      \texttt{proj\_trans\_generic} with \texttt{PJ\_FWD} option).
\end{itemize}

\subsection{\texttt{*.m} functions}

These functions make the computations by calling the \texttt{*.oct} functions.
You must call these functions because you can use various types of input
(scalars, vectors or matrices) and checking of input arguments (data type,
dimensions) is performed.

The functions are the same as in section \ref{op-of} (without the \texttt{\_} at
the beginning of the name):
\begin{itemize}
\item \texttt{op\_geod2geoc}: calls \texttt{\_op\_geod2geoc}.
\item \texttt{op\_geoc2geod}: calls \texttt{\_op\_geoc2geod}.
\item \texttt{op\_fwd}: calls \texttt{\_op\_fwd}.
\item \texttt{op\_inv}: calls \texttt{\_op\_inv}.
\item \texttt{op\_transform}: calls \texttt{\_op\_transform}.
\end{itemize}

\subsection{Error handling}

\texttt{*.oct} and \texttt{*.m} functions can emit errors or warnings, some due
to errors in input arguments and other due to errors in functions from
\texttt{projwrap} kernel execution (see section \ref{op-kw}).

Errors due to wrong input arguments (data types, dimensions, etc.) can be only
given for \texttt{*.m} functions and this is the reason because the use of these
functions are recommended. In this case the execution is aborted and nothing is
stored in output arguments.

Errors due to the execution of \texttt{projwrap} kernel can be emitted for both
\texttt{*.oct} and \texttt{*.m} functions. If the error is due to an erroneous
projection parameter the execution is aborted and nothing is stored in output
arguments; but if the error is due to a wrong or out of domain input coordinate,
a warning is emitted and the execution has a normal end.

\section{Examples}

\subsection{Geodetic to geocentric and vice versa}

\begin{verbatim}
lon=-6*pi/180;lat=43*pi/180;h=1000;
[x,y,z]=op_geod2geoc(lon,lat,h,6378388,1/297)
x =  4647300.723262573
y = -488450.9885681378
z =  4328259.364257743

[lon,lat,h]=op_geoc2geod(x,y,z,6378388,1/297);
lon*=180/pi,lat*=180/pi,h
lon = -6
lat =  42.99999999999999
h =  1000
\end{verbatim}

\subsection{Forward and inverse projection}

\begin{verbatim}
lon=-6*pi/180;lat=43*pi/180;
[x,y]=op_fwd(lon,lat,'+proj=utm +zone=30 +ellps=GRS80')
x =  255466.9805506805
y =  4765182.932683380

[lon,lat]=op_inv(x,y,'+proj=utm +zone=30 +ellps=GRS80');
lon*=180/pi,lat*=180/pi
lon = -5.999999999999999
lat =  43
\end{verbatim}

\subsection{Forward and inverse transformation: \texttt{op\_transform}}

This function takes into account some of the new \proj{} capabilities. The main
of them are:
\begin{itemize}
\item Projection and coordinate reference systems can be defined not only via
      the classical \texttt{+proj=} format, but also using
      EPSG\footnote{\url{http://www.epsg-registry.org/}.} codes.
\item Time coordinate for dynamical coordinate reference systems can be used.
\end{itemize}

\subsubsection{With altitude and time}

\begin{verbatim}
lon=-6;lat=43;h=1000;t=1;
[x,y,h,t]=op_transform(lon,lat,h,t,...
                       '+proj=latlong +ellps=GRS80',...
                       '+proj=utm +zone=30 +ellps=GRS80')
x =  255466.9805506804
y =  4765182.932683380
h =  1000
t =  1

[lon,lat,h,t]=op_transform(x,y,h,t,...
                           '+proj=utm +zone=30 +ellps=GRS80',...
                           '+proj=latlong +ellps=GRS80')
lon = -6
lat =  43
h =  1000
t =  1
\end{verbatim}

Note that in this case, where the \texttt{+proj=latlong} was used, the input
geodetic coordinates are provided in degrees instead of radians. This is a
feature of \proj{} when \texttt{+proj=latlong} is used in coordinate reference
system transformation, but not in cartographic projection, i.e., when it is used
in \texttt{op\_transform}, but not in \texttt{op\_fwd} nor  \texttt{op\_inv}.

\subsubsection{With altitude}

\begin{verbatim}
lon=-6;lat=43;h=1000;
[x,y,h]=op_transform(lon,lat,h,...
                     '+proj=latlong +ellps=GRS80',...
                     '+proj=utm +zone=30 +ellps=GRS80')
x =  255466.9805506804
y =  4765182.932683380
h =  1000

[lon,lat,h]=op_transform(x,y,h,...
                         '+proj=utm +zone=30 +ellps=GRS80',...
                         '+proj=latlong +ellps=GRS80')
lon = -6
lat =  43
h =  1000
\end{verbatim}

\subsubsection{Without altitude}

\begin{verbatim}
lon=-6;lat=43;
[x,y]=op_transform(lon,lat,'+proj=latlong +ellps=GRS80',...
                   '+proj=utm +zone=30 +ellps=GRS80')
x =  255466.9805506804
y =  4765182.932683380

[lon,lat]=op_transform(x,y,'+proj=utm +zone=30 +ellps=GRS80',...
                       '+proj=latlong +ellps=GRS80')
lon = -6
lat =  43
\end{verbatim}

In all the preceding examples the input coordinates can be scalars, vectors or
matrices, and height and/or time can be provided as empty matrices.

\subsubsection{Using EPSG codes}

Supose a transformation from UTM zone 30 coordinates to UTM zone 31 in the
ETRS89 coordinate reference system. Using the classical definition the operation
is performed as

\begin{verbatim}
x1=[600000;700000];y1=[4800000;4900000];z1=100;
[x2,y2,z2]=op_transform(x1,y1,z1,...
                        '+proj=utm +zone=30 +ellps=GRS80',...
                        '+proj=utm +lon_0=3e +ellps=GRS80')
x2 =

   113677.9843623374
   220776.6200746754

y2 =

   4810303.384473779
   4902896.002979981

z2 =

   100
   100
\end{verbatim}

The same transformation can be carried out using the corresponding EPSG
codes, i.e., \texttt{EPSG:25830} and
\texttt{EPSG:25831}\footnote{See
\url{https://spatialreference.org/ref/epsg/25830/},
and \url{https://spatialreference.org/ref/epsg/25831/}.}:

\begin{verbatim}
x1=[600000;700000];y1=[4800000;4900000];z1=100;
[x2,y2,z2]=op_transform(x1,y1,z1,'EPSG:25830','EPSG:25831')
x2 =

   113677.9843623374
   220776.6200746754

y2 =

   4810303.384473779
   4902896.002979981

z2 =

   100
   100
\end{verbatim}

\subsection{Error due to an erroneous parameter}

\begin{verbatim}
lon=-6*pi/180;lat=43*pi/180;
[x,y]=op_fwd(lon,lat,'+proj=utm +zone=30 +ellps=GRS8')
proj_create: Error -9: unknown elliptical parameter name
error:
        In function op_fwd:
        In function _op_fwd:
        Error in projection parameters
        unknown elliptical parameter name
        +proj=utm +zone=30 +ellps=GRS8
error: called from
    op_fwd at line 96 column 5
\end{verbatim}

\subsection{Error due to latitude too big}

\begin{verbatim}
lon=[-6*pi/180;-6*pi/180];lat=[43*pi/180;43];
[x,y]=_op_fwd(lon,lat,'+proj=utm +zone=30 +ellps=GRS80')
warning: _op_fwd:

warning: Projection error in point 2 (index starts at 1)
x =

   255466.98055
            Inf

y =

   4765182.93268
             Inf
\end{verbatim}

\section{Notes}

Apart from \url{http://octave.sourceforge.net/octproj/index.html}, an up to date
version of \octproj{} can be downloaded from
\url{https://bitbucket.org/jgpallero/octproj/}.

\begin{thebibliography}{99}
\bibitem{eat-om} \textsc{Eaton}, John W.; \textsc{Bateman}, David;
                 \textsc{Hauberg}, S\o{}ren; and \textsc{Wehbring}, Rik;
                 GNU Octave. A high-level interactive language for numerical
                 computations; Edition 5 for Octave version 5.2.0, January 2020;
                 \url{https://www.gnu.org/software/octave/octave.pdf};
                 Permanently updated at
                 \url{https://www.gnu.org/software/octave/support.html}.
\bibitem{projman} \textsc{Evenden}, Gerald I.; Cartographic Projection
                  Procedures for the UNIX Environment---A User's Manual; USGS
                  Open-File Report 90-284; 2003;
                  \url{ftp://ftp.remotesensing.org/proj/OF90-284.pdf}.
\bibitem{projir1} \textsc{Evenden}, Gerald I.; Cartographic Projection
                  Procedures Release 4, Interim Report; 2003;
                  \url{ftp://ftp.remotesensing.org/proj/proj.4.3.pdf}.
\bibitem{projir2} \textsc{Evenden}, Gerald I.; Cartographic Projection
                  Procedures Release 4, Second Interim Report; 2003;
                  \url{ftp://ftp.remotesensing.org/proj/proj.4.3.I2.pdf}.
\bibitem{sny-wm} \textsc{Snyder}, John Parr; Map Projections: A Working Manual;
                 USGS series, Professional Paper 1395; Geological Survey
                 (U. S.), 1987;
                 \url{http://pubs.er.usgs.gov/usgspubs/pp/pp1395}.
\end{thebibliography}

\end{document}

%Copyright (C) 2009-2020, José Luis García Pallero, <jgpallero@gmail.com>
%This document is distributed under the terms of the GNU Free Documentation
%License. Please, see http://www.gnu.org/licenses/
