Пример #1
0
        // get parameter value
        internal static double pj_param_d(projCtx ctx, List <string> pl, string name)
        {
            if (ctx == null)
            {
                ctx = pj_get_default_ctx();
            }

            foreach (string p in pl)
            {
                if (p == name)
                {
                    return(0);
                }
                if (p.StartsWith(name + "="))
                {
                    if (p.Length == name.Length + 1)
                    {
                        return(0);
                    }
                    string val = p.Substring(name.Length + 1);

                    try
                    {
                        return(double.Parse(val, nc));
                    }
                    catch
                    {
                        Libc.errno = (int)ERRORNUMBER.EINVAL;
                        return(0);
                    }
                }
            }

            return(0);
        }
Пример #2
0
        //**********************************************************************
        //								get_init()
        //**********************************************************************
        static string get_init(projCtx ctx, List <string> start, string name, out bool found_def)
        {
            found_def = false;

            int opt = name.LastIndexOf(':');

            if (opt == -1)
            {
                pj_ctx_set_errno(ctx, -3); return(null);
            }
            if (opt == name.Length - 1)
            {
                pj_ctx_set_errno(ctx, -3); return(null);
            }                                                                              // last char ':' => no name given

            string next = null;

            try
            {
                FileStream fs = pj_open_lib(ctx, name.Substring(0, opt), FileAccess.Read);
                if (fs == null)
                {
                    return(null);
                }

                StreamReader fid = new StreamReader(fs);
                next = get_opt(ctx, start, fid, name.Substring(opt + 1), out found_def);
                fid.Close();
            }
            catch
            {
            }

            return(next);
        }
Пример #3
0
        // get parameter value
        internal static double pj_param_d(projCtx ctx, List<string> pl, string name)
        {
            if(ctx==null) ctx=pj_get_default_ctx();

            foreach(string p in pl)
            {
                if(p==name) return 0;
                if(p.StartsWith(name+"="))
                {
                    if(p.Length==name.Length+1) return 0;
                    string val=p.Substring(name.Length+1);

                    try
                    {
                        return double.Parse(val, nc);
                    }
                    catch
                    {
                        Libc.errno=(int)ERRORNUMBER.EINVAL;
                        return 0;
                    }
                }
            }

            return 0;
        }
Пример #4
0
        // get parameter value
        internal static double pj_param_r(projCtx ctx, List <string> pl, string name)
        {
            if (ctx == null)
            {
                ctx = pj_get_default_ctx();
            }

            foreach (string p in pl)
            {
                if (p == name)
                {
                    return(0);
                }
                if (p.StartsWith(name + "="))
                {
                    if (p.Length == name.Length + 1)
                    {
                        return(0);
                    }
                    string val = p.Substring(name.Length + 1);
                    return(dmstor_ctx(ctx, val, 0));
                }
            }

            return(0);
        }
Пример #5
0
        // get parameter value
        internal static bool pj_param_b(projCtx ctx, List<string> pl, string name)
        {
            if(ctx==null) ctx=pj_get_default_ctx();

            foreach(string p in pl)
            {
                if(p==name) return true;

                if(p.StartsWith(name+"="))
                {
                    if(p.Length==name.Length+1) return true;
                    char val=p[name.Length+1];

                    switch(val)
                    {
                        case 'f':
                        case 'F':
                            return false;
                        case 't':
                        case 'T':
                            return true;
                        default:
                            pj_ctx_set_errno(ctx, -8);
                            return false;
                    }
                }
            }

            return false;
        }
Пример #6
0
 //***********************************************************************
 //*								pj_log()								*
 //***********************************************************************
 public static void pj_log(projCtx ctx, PJ_LOG level, string format, params object[] args)
 {
     if (level > ctx.debug_level)
     {
         return;
     }
     ctx.logger(ctx.app_data, level, string.Format(format, args));
 }
Пример #7
0
        public static double aasin(projCtx ctx, double v)
        {
            double av=Math.Abs(v);

            if(av>=1.0)
            {
                if(av>aasincos_ONE_TOL) pj_ctx_set_errno(ctx, -19);
                return v<0.0?-HALFPI:HALFPI;
            }

            return Math.Asin(v);
        }
Пример #8
0
        //**********************************************************************
        //							get_defaults()
        //**********************************************************************
        static string get_defaults(projCtx ctx, List <string> start, string name)
        {
            string next = null;

            try
            {
                FileStream fs = pj_open_lib(ctx, "proj_def.dat", FileAccess.Read);
                if (fs != null)
                {
                    StreamReader fid = new StreamReader(fs);
                    bool         found_def;
                    next = get_opt(ctx, start, fid, "general", out found_def);
                    fid.BaseStream.Seek(0, SeekOrigin.Begin);
                    next = get_opt(ctx, start, fid, name, out found_def);
                    fid.Close();
                }
                else
                {
                    // if proj_def.dat not found
                    start.Add(pj_mkparam("ellps=WGS84"));

                    if (name == "aea")
                    {
                        start.Add(pj_mkparam("lat_1=29.5"));
                        start.Add(pj_mkparam("lat_2=45.5"));
                    }
                    else if (name == "lcc")
                    {
                        start.Add(pj_mkparam("lat_1=33"));
                        start.Add(pj_mkparam("lat_2=45"));
                    }
                    else if (name == "lagrng")
                    {
                        start.Add(pj_mkparam("W=2"));
                    }
                }
            }
            catch
            {
            }

            if (Libc.errno != 0)
            {
                Libc.errno = 0;                         // don't care if can't open file
            }
            ctx.last_errno = 0;

            return(next);
        }
Пример #9
0
        public static double aasin(projCtx ctx, double v)
        {
            double av = Math.Abs(v);

            if (av >= 1.0)
            {
                if (av > aasincos_ONE_TOL)
                {
                    pj_ctx_set_errno(ctx, -19);
                }
                return(v < 0.0?-HALFPI:HALFPI);
            }

            return(Math.Asin(v));
        }
Пример #10
0
        //**********************************************************************
        //								pj_param()
        //
        //		Test for presence or get parameter value. The last
        //		character in function name is a parameter type which can take
        //		the values:
        //
        //			t - test for presence, return true/false as bool
        //			i - integer value returned as int
        //			d - simple valued real input returned as double
        //			r - degrees (DMS translation applied), returned as
        //				radians as double
        //			s - string returned as string
        //			b - test for t/T/f/F, return as bool
        //
        //**********************************************************************

        // test for presence
        internal static bool pj_param_t(projCtx ctx, List <string> pl, string name)
        {
            foreach (string p in pl)
            {
                if (p == name)
                {
                    return(true);
                }
                if (p.StartsWith(name + "="))
                {
                    return(true);
                }
            }

            return(false);
        }
Пример #11
0
        public static double pj_inv_mlfn(projCtx ctx, double arg, double es, double[] en)
        {
            const int MAX_ITER=10;

            double k=1.0/(1.0-es);
            double phi=arg;
            for(int i=MAX_ITER; i>0; --i) // rarely goes over 2 iterations
            {
                double s=Math.Sin(phi);
                double t=1.0-es*s*s;
                t=(pj_mlfn(phi, s, Math.Cos(phi), en)-arg)*t*Math.Sqrt(t)*k;
                phi-=t;
                if(Math.Abs(t)<EPS11) return phi;
            }

            pj_ctx_set_errno(ctx, -17);
            return phi;
        }
Пример #12
0
        // get parameter value
        internal static string pj_param_s(projCtx ctx, List <string> pl, string name)
        {
            foreach (string p in pl)
            {
                if (p == name)
                {
                    return("");
                }
                if (p.StartsWith(name + "="))
                {
                    if (p.Length == name.Length + 1)
                    {
                        return("");
                    }
                    return(p.Substring(name.Length + 1));
                }
            }

            return("");
        }
Пример #13
0
        // get parameter value
        internal static bool pj_param_b(projCtx ctx, List <string> pl, string name)
        {
            if (ctx == null)
            {
                ctx = pj_get_default_ctx();
            }

            foreach (string p in pl)
            {
                if (p == name)
                {
                    return(true);
                }

                if (p.StartsWith(name + "="))
                {
                    if (p.Length == name.Length + 1)
                    {
                        return(true);
                    }
                    char val = p[name.Length + 1];

                    switch (val)
                    {
                    case 'f':
                    case 'F':
                        return(false);

                    case 't':
                    case 'T':
                        return(true);

                    default:
                        pj_ctx_set_errno(ctx, -8);
                        return(false);
                    }
                }
            }

            return(false);
        }
Пример #14
0
        public double proj_inv_mdist(projCtx ctx, double dist)
        {
            double k   = 1.0 / (1.0 - es);
            int    i   = MAX_ITER_mdist;
            double phi = dist;

            while (i-- > 0)
            {
                double s = Math.Sin(phi);
                double t = 1.0 - es * s * s;
                phi -= t = (proj_mdist(phi, s, Math.Cos(phi)) - dist) * (t * Math.Sqrt(t)) * k;
                if (Math.Abs(t) < TOL14)
                {
                    return(phi);                                  // that is no change
                }
            }

            // convergence failed
            Proj.pj_ctx_set_errno(ctx, -17);
            return(phi);
        }
Пример #15
0
        public static double pj_inv_mlfn(projCtx ctx, double arg, double es, double[] en)
        {
            const int MAX_ITER = 10;

            double k   = 1.0 / (1.0 - es);
            double phi = arg;

            for (int i = MAX_ITER; i > 0; --i)        // rarely goes over 2 iterations
            {
                double s = Math.Sin(phi);
                double t = 1.0 - es * s * s;
                t    = (pj_mlfn(phi, s, Math.Cos(phi), en) - arg) * t * Math.Sqrt(t) * k;
                phi -= t;
                if (Math.Abs(t) < EPS11)
                {
                    return(phi);
                }
            }

            pj_ctx_set_errno(ctx, -17);
            return(phi);
        }
Пример #16
0
        // determine latitude angle phi-2
        public static double pj_phi2(projCtx ctx, double ts, double e)
        {
            const int N_ITER = 15;

            double eccnth = 0.5 * e;
            double Phi    = HALFPI - 2.0 * Math.Atan(ts);
            int    i      = N_ITER;
            double dphi   = 0;

            do
            {
                double con = e * Math.Sin(Phi);
                dphi = HALFPI - 2.0 * Math.Atan(ts * Math.Pow((1.0 - con) / (1.0 + con), eccnth)) - Phi;
                Phi += dphi;
            } while(Math.Abs(dphi) > TOL10 && (--i) != 0);

            if (i <= 0)
            {
                pj_ctx_set_errno(ctx, -18);
            }

            return(Phi);
        }
Пример #17
0
        //**********************************************************************
        //							pj_open_lib()
        //**********************************************************************
        public static FileStream pj_open_lib(projCtx ctx, string name, FileAccess mode)
        {
            string sysname;
            string dir_chars = "/\\";

            if (name == null || name.Length < 3)
            {
                return(null);
            }

            // check if ~/name
            if (name[0] == '~' && dir_chars.IndexOf(name[1]) != -1)
            {
                sysname = Environment.GetEnvironmentVariable("HOME");
                if (sysname != null)
                {
                    sysname += DIR_CHAR + name.Substring(1);
                }
                else
                {
                    return(null);
                }
            }
            else
            {
                // or fixed path: "/name", "./name", "../name" or "?:\name"
                if (dir_chars.IndexOf(name[0]) != -1 || (name[0] == '.' && dir_chars.IndexOf(name[1]) != -1) ||
                    (name[0] == '.' && name[1] == '.' && dir_chars.IndexOf(name[2]) != -1) ||
                    (name[1] == ':' && dir_chars.IndexOf(name[2]) != -1))
                {
                    sysname = name;
                }
                else
                {
                    // or try to use application provided file finder
                    if (pj_finder != null && pj_finder(name) != null)
                    {
                        sysname = pj_finder(name);
                    }
                    else
                    {
                        // or is environment PROJ_LIB defined
                        sysname = Environment.GetEnvironmentVariable(PROJ_LIB);
                        if (sysname != null)
                        {
                            sysname += DIR_CHAR + name;
                        }
                        else
                        {
                            try
                            {
                                FileInfo fi = new FileInfo(System.Reflection.Assembly.GetEntryAssembly().Location);
                                if (fi.DirectoryName.Length == 3)
                                {
                                    sysname = fi.DirectoryName + PROJ_LIB + DIR_CHAR + name;
                                }
                                else
                                {
                                    sysname = fi.DirectoryName + DIR_CHAR + PROJ_LIB + DIR_CHAR + name;
                                }
                            }
                            catch
                            {
                                sysname = PROJ_LIB + DIR_CHAR + name;
                                //else sysname=name; // just try it bare bones
                            }
                        }
                    }
                }
            }

            FileStream fid = null;

            try
            {
                fid = File.Open(sysname, FileMode.Open, mode);
            }
            catch
            {
                fid = null;
            }
            if (fid != null)
            {
                Libc.errno = 0;
            }

            // If none of those work and we have a search path, try it
            if (fid == null && search_path != null)
            {
                foreach (string path in search_path)
                {
                    sysname = path + DIR_CHAR + name;
                    try
                    {
                        fid = File.Open(sysname, FileMode.Open, mode);
                    }
                    catch
                    {
                        fid = null;
                    }
                    if (fid != null)
                    {
                        break;
                    }
                }
                if (fid != null)
                {
                    Libc.errno = 0;
                }
            }

            if (ctx.last_errno == 0 && Libc.errno != 0)
            {
                pj_ctx_set_errno(ctx, Libc.errno);
            }

#if DEBUG
            Console.Error.WriteLine("pj_open_lib({0}): call fopen({1}) - {2}", name, sysname, fid == null?"failed":"succeeded");
#endif

            return(fid);
        }
Пример #18
0
        public static double dmstor_ctx(projCtx ctx, string nptr)
        {
            string dummy = "";

            return(dmstor_ctx(ctx, nptr, out dummy));
        }
Пример #19
0
        //**********************************************************************
        //							pj_datum_set()
        //**********************************************************************
        static bool pj_datum_set(projCtx ctx, List <string> pl, PJ projdef)
        {
            projdef.datum_type = PJD.UNKNOWN;

            // --------------------------------------------------------------------
            //		Is there a datum definition in the parameters list? If so,
            //		add the defining values to the parameter list. Note that
            //		this will append the ellipse definition as well as the
            //		towgs84= and related parameters. It should also be pointed
            //		out that the addition is permanent rather than temporary
            //		like most other keyword expansion so that the ellipse
            //		definition will last into the pj_ell_set() function called
            //		after this one.
            // --------------------------------------------------------------------
            string name = pj_param_s(ctx, pl, "datum");

            if (name != null && name != "")
            {
                string defn, ellipse_id;
                if (GetDatum(name, out defn, out ellipse_id) == null)
                {
                    pj_ctx_set_errno(ctx, -9); return(true);
                }

                if (ellipse_id != null && ellipse_id.Length > 0)
                {
                    pl.Add(pj_mkparam("ellps=" + ellipse_id));
                }
                if (defn != null && defn.Length > 0)
                {
                    pl.Add(pj_mkparam(defn));
                }
            }

            string nadgrids = pj_param_s(ctx, pl, "nadgrids");
            string towgs84  = pj_param_s(ctx, pl, "towgs84");
            string catalog  = pj_param_s(ctx, pl, "catalog");

            // --------------------------------------------------------------------
            //		Check for nadgrids parameter.
            // --------------------------------------------------------------------
            if (!string.IsNullOrEmpty(nadgrids))
            {
                // We don't actually save the value separately. It will continue
                // to exist in the param list for use in pj_apply_gridshift.cs
                projdef.datum_type = PJD.GRIDSHIFT;
            }

            // --------------------------------------------------------------------
            //		Check for grid catalog parameter, and optional date.
            // --------------------------------------------------------------------
            else if (!string.IsNullOrEmpty(catalog))
            {
                projdef.datum_type   = PJD.GRIDSHIFT;
                projdef.catalog_name = catalog;

                string date = pj_param_s(ctx, pl, "date");
                if (!string.IsNullOrEmpty(date))
                {
                    projdef.datum_date = Free.Ports.Proj4.Gridshift.Grid.pj_gc_parsedate(ctx, date);
                }
            }

            // --------------------------------------------------------------------
            //		Check for towgs84 parameter.
            // --------------------------------------------------------------------
            else if (!string.IsNullOrEmpty(towgs84))
            {
                projdef.datum_params[0] = projdef.datum_params[1] = projdef.datum_params[2] = 0;
                projdef.datum_params[3] = projdef.datum_params[4] = projdef.datum_params[5] = projdef.datum_params[6] = 0;

                // parse out the parameters
                string   s  = towgs84;
                string[] ss = s.Split(',');
                for (int i = 0; i < 7 && i < ss.Length; i++)
                {
                    projdef.datum_params[i] = double.Parse(ss[i], nc);
                }

                if (projdef.datum_params[3] != 0.0 || projdef.datum_params[4] != 0.0 || projdef.datum_params[5] != 0.0 || projdef.datum_params[6] != 0.0)
                {
                    projdef.datum_type = PJD._7PARAM;

                    // transform from arc seconds to radians
                    projdef.datum_params[3] *= SEC_TO_RAD;
                    projdef.datum_params[4] *= SEC_TO_RAD;
                    projdef.datum_params[5] *= SEC_TO_RAD;

                    // transform from parts per million to scaling factor
                    projdef.datum_params[6] = (projdef.datum_params[6] / 1000000.0) + 1;
                }
                else
                {
                    projdef.datum_type = PJD._3PARAM;
                }

                // Note that pj_init() will later switch datum_type to PJD.WGS84 if shifts are all zero, and ellipsoid is WGS84 or GRS80
            }

            return(false);
        }
Пример #20
0
        // initialize geographic shape parameters
        internal static bool pj_ell_set(projCtx ctx, List <string> pl, out double a, out double es)
        {
            const double SIXTH = 0.1666666666666666667;          // 1/6
            const double RA4   = 0.04722222222222222222;         // 17/360
            const double RA6   = 0.02215608465608465608;         // 67/3024
            const double RV4   = 0.06944444444444444444;         // 5/72
            const double RV6   = 0.04243827160493827160;         // 55/1296

            // clear any previous error
            pj_ctx_set_errno(ctx, 0);

            // check for varying forms of ellipsoid input
            a = es = 0.0;
            // R takes precedence
            if (pj_param_t(ctx, pl, "R"))
            {
                a = pj_param_d(ctx, pl, "R");
            }
            else
            {             // probable elliptical figure
                bool removeLast2 = false;
                try
                {
                    // check if ellps present and temporarily append its values to pl
                    string name = pj_param_s(ctx, pl, "ellps");
                    if (!string.IsNullOrEmpty(name))
                    {
                        string major, ell;
                        if (GetEllipsoid(name, out major, out ell) == null)
                        {
                            pj_ctx_set_errno(ctx, -9); return(true);
                        }
                        pl.Add(pj_mkparam(major));
                        pl.Add(pj_mkparam(ell));
                        removeLast2 = true;
                    }

                    double b = 0.0, e;
                    a = pj_param_d(ctx, pl, "a");
                    if (pj_param_t(ctx, pl, "es"))
                    {
                        es = pj_param_d(ctx, pl, "es");                                             // eccentricity squared
                    }
                    else if (pj_param_t(ctx, pl, "e"))
                    {                     // eccentricity
                        e  = pj_param_d(ctx, pl, "e");
                        es = e * e;
                    }
                    else if (pj_param_t(ctx, pl, "rf"))
                    {                     // recip flattening
                        es = pj_param_d(ctx, pl, "rf");
                        if (es == 0)
                        {
                            pj_ctx_set_errno(ctx, -10); return(true);
                        }
                        es = 1.0 / es;
                        es = es * (2.0 - es);
                    }
                    else if (pj_param_t(ctx, pl, "f"))
                    {                     // flattening
                        es = pj_param_d(ctx, pl, "f");
                        es = es * (2.0 - es);
                    }
                    else if (pj_param_t(ctx, pl, "b"))
                    {                     // minor axis
                        b  = pj_param_d(ctx, pl, "b");
                        es = 1.0 - (b * b) / (a * a);
                    }
                    // else es == 0.0 and sphere of radius a

                    if (b == 0)
                    {
                        b = a * Math.Sqrt(1.0 - es);
                    }

                    // following options turn ellipsoid into equivalent sphere
                    if (pj_param_b(ctx, pl, "R_A"))
                    {                     // sphere--area of ellipsoid
                        a *= 1.0 - es * (SIXTH + es * (RA4 + es * RA6));
                        es = 0.0;
                    }
                    else if (pj_param_b(ctx, pl, "R_V"))
                    {                     // sphere--vol. of ellipsoid
                        a *= 1.0 - es * (SIXTH + es * (RV4 + es * RV6));
                        es = 0.0;
                    }
                    else if (pj_param_b(ctx, pl, "R_a"))
                    {                     // sphere--arithmetic mean
                        a  = 0.5 * (a + b);
                        es = 0.0;
                    }
                    else if (pj_param_b(ctx, pl, "R_g"))
                    {                     // sphere--geometric mean
                        a  = Math.Sqrt(a * b);
                        es = 0.0;
                    }
                    else if (pj_param_b(ctx, pl, "R_h"))
                    {                     // sphere--harmonic mean
                        a  = 2.0 * a * b / (a + b);
                        es = 0.0;
                    }
                    else
                    {
                        bool i = pj_param_t(ctx, pl, "R_lat_a"); // sphere--arith.
                        if (i || pj_param_t(ctx, pl, "R_lat_g"))
                        {                                        // or geom. mean at latitude
                            double tmp = Math.Sin(pj_param_r(ctx, pl, i?"R_lat_a":"R_lat_g"));
                            if (Math.Abs(tmp) > HALFPI)
                            {
                                pj_ctx_set_errno(ctx, -11); return(true);
                            }
                            tmp = 1.0 - es * tmp * tmp;
                            a  *= i?0.5 * (1.0 - es + tmp) / (tmp * Math.Sqrt(tmp)):Math.Sqrt(1.0 - es) / tmp;
                            es  = 0.0;
                        }
                    }
                }
                finally
                {
                    if (removeLast2)
                    {
                        pl.RemoveRange(pl.Count - 2, 2);                               // clean up temporary extension of list
                    }
                }

                if (ctx.last_errno != 0)
                {
                    return(true);
                }
            }

            // some remaining checks
            if (es < 0.0)
            {
                pj_ctx_set_errno(ctx, -12); return(true);
            }
            if (a <= 0.0)
            {
                pj_ctx_set_errno(ctx, -13); return(true);
            }

            return(false);
        }
Пример #21
0
        //**********************************************************************
        //                            pj_open_lib()
        //**********************************************************************
        public static FileStream pj_open_lib(projCtx ctx, string name, FileAccess mode)
        {
            string sysname;
            string dir_chars="/\\";

            if(name==null||name.Length<3) return null;

            // check if ~/name
            if(name[0]=='~'&&dir_chars.IndexOf(name[1])!=-1)
            {
                sysname=Environment.GetEnvironmentVariable("HOME");
                if(sysname!=null) sysname+=DIR_CHAR+name.Substring(1);
                else return null;
            }
            else
            {
                // or fixed path: "/name", "./name", "../name" or "?:\name"
                if(dir_chars.IndexOf(name[0])!=-1||(name[0]=='.'&&dir_chars.IndexOf(name[1])!=-1)||
                    (name[0]=='.'&&name[1]=='.'&&dir_chars.IndexOf(name[2])!=-1)||
                    (name[1]==':'&&dir_chars.IndexOf(name[2])!=-1)) sysname=name;
                else
                {
                    // or try to use application provided file finder
                    if(pj_finder!=null&&pj_finder(name)!=null) sysname=pj_finder(name);
                    else
                    {
                        // or is environment PROJ_LIB defined
                        sysname=Environment.GetEnvironmentVariable(PROJ_LIB);
                        if(sysname!=null) sysname+=DIR_CHAR+name;
                        else
                        {
                            try
                            {
                                FileInfo fi=new FileInfo(System.Reflection.Assembly.GetEntryAssembly().Location);
                                if(fi.DirectoryName.Length==3) sysname=fi.DirectoryName+PROJ_LIB+DIR_CHAR+name;
                                else sysname=fi.DirectoryName+DIR_CHAR+PROJ_LIB+DIR_CHAR+name;
                            }
                            catch
                            {
                                sysname=PROJ_LIB+DIR_CHAR+name;
                                //else sysname=name; // just try it bare bones
                            }
                        }
                    }
                }
            }

            FileStream fid=null;
            try
            {
                fid=File.Open(sysname, FileMode.Open, mode);
            }
            catch
            {
                fid=null;
            }
            if(fid!=null) Libc.errno=0;

            // If none of those work and we have a search path, try it
            if(fid==null&&search_path!=null)
            {
                foreach(string path in search_path)
                {
                    sysname=path+DIR_CHAR+name;
                    try
                    {
                        fid=File.Open(sysname, FileMode.Open, mode);
                    }
                    catch
                    {
                        fid=null;
                    }
                    if(fid!=null) break;
                }
                if(fid!=null) Libc.errno=0;
            }

            if(ctx.last_errno==0&&Libc.errno!=0)
                pj_ctx_set_errno(ctx, Libc.errno);

            #if DEBUG
                Console.Error.WriteLine("pj_open_lib({0}): call fopen({1}) - {2}", name, sysname, fid==null?"failed":"succeeded");
            #endif

            return fid;
        }
Пример #22
0
        public static double dmstor_ctx(projCtx ctx, string nptr, int stuff)         // for: ...=dmstor(s, 0);
        {
            string dummy = "";

            return(dmstor_ctx(ctx, nptr, out dummy));
        }
Пример #23
0
        public static double dmstor_ctx(projCtx ctx, string nptr, out string newptr)
        {
            const string sym = "NnEeSsWw";

            newptr = nptr;
            if (nptr == null)
            {
                pj_ctx_set_errno(ctx, -16);
                return(Libc.HUGE_VAL);
            }

            string s = nptr.TrimStart();

            if (s.Length == 0)
            {
                newptr = "";
                pj_ctx_set_errno(ctx, -16);
                return(Libc.HUGE_VAL);
            }

            newptr = s;

            string work = "";

            foreach (char c in s)
            {
                if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '.' || c == '°' || c == '\'' || c == '"' || c == '+' || c == '-')
                {
                    work += c;
                }
                else
                {
                    break;
                }
            }

            s = work;
            char sign = work[0];

            if (sign == '+' || sign == '-')
            {
                s = s.Substring(1);
            }
            else
            {
                sign = '+';
            }

            if ((s.Length == 0) || (!((s[0] >= '0' && s[0] <= '9') || s[0] == '.')))
            {
                pj_ctx_set_errno(ctx, -16);
                return(Libc.HUGE_VAL);
            }

            int    n = 0, p;
            double v = 0, tv;

            for (int nl = 0; nl < 3; nl = n + 1)
            {
                if (s.Length == 0)
                {
                    break;
                }

                char c = s[0];
                if (!((c >= '0' && c <= '9') || c == '.'))
                {
                    break;
                }

                tv = proj_strtod(s, out s);
                if (tv == Libc.HUGE_VAL)
                {
                    return(tv);
                }

                if (s.Length == 0)
                {
                    v += tv * vm[nl];
                    n  = 4;
                    continue;
                }

                switch (s[0])
                {
                case '°':
                case 'D':
                case 'd':
                    n = 0; break;

                case '\'':
                    n = 1; break;

                case '"':
                    n = 2; break;

                case 'r':
                case 'R':
                    if (nl != 0)
                    {
                        pj_ctx_set_errno(ctx, -16);
                        return(Libc.HUGE_VAL);
                    }
                    s = s.Substring(1);
                    v = tv;
                    n = 4;
                    continue;

                default:
                    v += tv * vm[nl];
                    n  = 4;
                    continue;
                }
                if (n < nl)
                {
                    pj_ctx_set_errno(ctx, -16);
                    return(Libc.HUGE_VAL);
                }

                v += tv * vm[n];
                s  = s.Substring(1);
            }

            // postfix sign
            if (s.Length > 0)
            {
                p = sym.IndexOf(s[0]);
                if (p > -1)
                {
                    sign = p >= 4?(sign == '-'?'+':'-'):sign;
                    s    = s.Substring(1);
                }
            }

            if (sign == '-')
            {
                v = -v;
            }

            // return point of next char after valid string
            if (newptr.Length <= (work.Length - s.Length))
            {
                newptr = "";
            }
            else
            {
                newptr = newptr.Substring(work.Length - s.Length);
            }
            return(v);
        }
Пример #24
0
        // get parameter value
        internal static double pj_param_r(projCtx ctx, List<string> pl, string name)
        {
            if(ctx==null) ctx=pj_get_default_ctx();

            foreach(string p in pl)
            {
                if(p==name) return 0;
                if(p.StartsWith(name+"="))
                {
                    if(p.Length==name.Length+1) return 0;
                    string val=p.Substring(name.Length+1);
                    return dmstor_ctx(ctx, val, 0);
                }
            }

            return 0;
        }
Пример #25
0
        //**********************************************************************
        //                                pj_param()
        //
        //        Test for presence or get parameter value. The last
        //        character in function name is a parameter type which can take
        //        the values:
        //
        //            t - test for presence, return true/false as bool
        //            i - integer value returned as int
        //            d - simple valued real input returned as double
        //            r - degrees (DMS translation applied), returned as
        //                radians as double
        //            s - string returned as string
        //            b - test for t/T/f/F, return as bool
        //
        //**********************************************************************
        // test for presence
        internal static bool pj_param_t(projCtx ctx, List<string> pl, string name)
        {
            foreach(string p in pl)
            {
                if(p==name) return true;
                if(p.StartsWith(name+"=")) return true;
            }

            return false;
        }
Пример #26
0
        //***********************************************************************
        //*							pj_adjust_axis()							*
        //*																		*
        //*		Normalize or de-normalized the x/y/z axes. The normal form		*
        //*		is "enu" (easting, northing, up).								*
        //***********************************************************************
        public static int pj_adjust_axis(projCtx ctx, string axis, bool denormalize_flag, long point_count, int point_offset, double[] x, double[] y, double[] z)
        {
            double x_in, y_in, z_in = 0.0;
            int    i, i_axis;

            if (!denormalize_flag)
            {
                for (i = 0; i < point_count; i++)
                {
                    x_in = x[point_offset * i];
                    y_in = y[point_offset * i];
                    if (z != null)
                    {
                        z_in = z[point_offset * i];
                    }

                    for (i_axis = 0; i_axis < 3; i_axis++)
                    {
                        double value;

                        if (i_axis == 0)
                        {
                            value = x_in;
                        }
                        else if (i_axis == 1)
                        {
                            value = y_in;
                        }
                        else
                        {
                            value = z_in;
                        }

                        switch (axis[i_axis])
                        {
                        case 'e': x[point_offset * i] = value; break;

                        case 'w': x[point_offset * i] = -value; break;

                        case 'n': y[point_offset * i] = value; break;

                        case 's': y[point_offset * i] = -value; break;

                        case 'u': if (z != null)
                            {
                                z[point_offset * i] = value;
                            }
                            break;

                        case 'd': if (z != null)
                            {
                                z[point_offset * i] = -value;
                            }
                            break;

                        default: pj_ctx_set_errno(ctx, (int)PJD_ERR.AXIS); return((int)PJD_ERR.AXIS);
                        }
                    }        // i_axis
                }            // i (point)
            }
            else             // denormalize
            {
                for (i = 0; i < point_count; i++)
                {
                    x_in = x[point_offset * i];
                    y_in = y[point_offset * i];
                    if (z != null)
                    {
                        z_in = z[point_offset * i];
                    }

                    for (i_axis = 0; i_axis < 3; i_axis++)
                    {
                        double[] target;

                        if (i_axis == 2 && z == null)
                        {
                            continue;
                        }

                        if (i_axis == 0)
                        {
                            target = x;
                        }
                        else if (i_axis == 1)
                        {
                            target = y;
                        }
                        else
                        {
                            target = z;
                        }

                        switch (axis[i_axis])
                        {
                        case 'e': target[point_offset * i] = x_in; break;

                        case 'w': target[point_offset * i] = -x_in; break;

                        case 'n': target[point_offset * i] = y_in; break;

                        case 's': target[point_offset * i] = -y_in; break;

                        case 'u': target[point_offset * i] = z_in; break;

                        case 'd': target[point_offset * i] = -z_in; break;

                        default: pj_ctx_set_errno(ctx, (int)PJD_ERR.AXIS); return((int)PJD_ERR.AXIS);
                        }
                    }             // i_axis
                }                 // i (point)
            }

            return(0);
        }
Пример #27
0
        //**********************************************************************
        //                            pj_datum_set()
        //**********************************************************************
        static bool pj_datum_set(projCtx ctx, List<string> pl, PJ projdef)
        {
            projdef.datum_type=PJD.UNKNOWN;

            // --------------------------------------------------------------------
            //		Is there a datum definition in the parameters list? If so,
            //		add the defining values to the parameter list. Note that
            //		this will append the ellipse definition as well as the
            //		towgs84= and related parameters. It should also be pointed
            //		out that the addition is permanent rather than temporary
            //		like most other keyword expansion so that the ellipse
            //		definition will last into the pj_ell_set() function called
            //		after this one.
            // --------------------------------------------------------------------
            string name=pj_param_s(ctx, pl, "datum");
            if(name!=null&&name!="")
            {
                string defn, ellipse_id;
                if(GetDatum(name, out defn, out ellipse_id)==null) { pj_ctx_set_errno(ctx, -9); return true; }

                if(ellipse_id!=null&&ellipse_id.Length>0) pl.Add(pj_mkparam("ellps="+ellipse_id));
                if(defn!=null&&defn.Length>0) pl.Add(pj_mkparam(defn));
            }

            string nadgrids=pj_param_s(ctx, pl, "nadgrids");
            string towgs84=pj_param_s(ctx, pl, "towgs84");
            string catalog=pj_param_s(ctx, pl, "catalog");

            // --------------------------------------------------------------------
            //		Check for nadgrids parameter.
            // --------------------------------------------------------------------
            if(!string.IsNullOrEmpty(nadgrids))
            {
                // We don't actually save the value separately. It will continue
                // to exist in the param list for use in pj_apply_gridshift.cs
                projdef.datum_type=PJD.GRIDSHIFT;
            }

            // --------------------------------------------------------------------
            //		Check for grid catalog parameter, and optional date.
            // --------------------------------------------------------------------
            else if(!string.IsNullOrEmpty(catalog))
            {
                projdef.datum_type=PJD.GRIDSHIFT;
                projdef.catalog_name=catalog;

                string date=pj_param_s(ctx, pl, "date");
                if(!string.IsNullOrEmpty(date)) projdef.datum_date=Free.Ports.Proj4.Gridshift.Grid.pj_gc_parsedate(ctx, date);
            }

            // --------------------------------------------------------------------
            //		Check for towgs84 parameter.
            // --------------------------------------------------------------------
            else if(!string.IsNullOrEmpty(towgs84))
            {
                projdef.datum_params[0]=projdef.datum_params[1]=projdef.datum_params[2]=0;
                projdef.datum_params[3]=projdef.datum_params[4]=projdef.datum_params[5]=projdef.datum_params[6]=0;

                // parse out the parameters
                string s=towgs84;
                string[] ss=s.Split(',');
                for(int i=0; i<7&&i<ss.Length; i++) projdef.datum_params[i]=double.Parse(ss[i], nc);

                if(projdef.datum_params[3]!=0.0||projdef.datum_params[4]!=0.0||projdef.datum_params[5]!=0.0||projdef.datum_params[6]!=0.0)
                {
                    projdef.datum_type=PJD._7PARAM;

                    // transform from arc seconds to radians
                    projdef.datum_params[3]*=SEC_TO_RAD;
                    projdef.datum_params[4]*=SEC_TO_RAD;
                    projdef.datum_params[5]*=SEC_TO_RAD;

                    // transform from parts per million to scaling factor
                    projdef.datum_params[6]=(projdef.datum_params[6]/1000000.0)+1;
                }
                else projdef.datum_type=PJD._3PARAM;

                // Note that pj_init() will later switch datum_type to PJD.WGS84 if shifts are all zero, and ellipsoid is WGS84 or GRS80
            }

            return false;
        }
Пример #28
0
        public static PJ pj_init_ctx(projCtx ctx, string[] argv)
        {
            ctx.last_errno=0;

            if(argv.Length<=0) { pj_ctx_set_errno(ctx, -1); return null; }

            List<string> start=new List<string>();

            Libc.errno=pj_errno=0;

            // put arguments into internal linked list
            for(int a=0; a<argv.Length; a++)
            {
                string curr=pj_mkparam(argv[a]);
                if(curr==null) { pj_ctx_set_errno(ctx, Libc.errno); return null; }
                start.Add(curr);
            }

            // check if +init present
            if(pj_param_t(ctx, start, "init"))
            {
                bool found_def;
                string curr=get_init(ctx, start, pj_param_s(ctx, start, "init"), out found_def);
                if(curr==null||!found_def)
                {
                    if(pj_errno!=0||Libc.errno!=0)
                    {
                        if(pj_errno==0) pj_ctx_set_errno(ctx, Libc.errno);
                    }
                    else pj_ctx_set_errno(ctx, -2);

                    return null;
                }
            }

            // find projection selection
            string name=pj_param_s(ctx, start, "proj");
            if(name=="") { pj_ctx_set_errno(ctx, -4); return null; }

            // set defaults, unless inhibited
            if(!pj_param_b(ctx, start, "no_defs")) get_defaults(ctx, start, name);

            // allocate projection structure
            PJ PIN=GetPJ(name);
            if(PIN==null) { pj_ctx_set_errno(ctx, -5); return null; }
            PIN.ctx=ctx;
            PIN.parameters=start;
            PIN.is_latlong=false;
            PIN.is_geocent=false;
            PIN.is_long_wrap_set=false;
            PIN.long_wrap_center=0.0;
            PIN.axis="enu";

            PIN.gridlist=null;
            PIN.vgridlist_geoid=null;

            // set datum parameters
            if(pj_datum_set(ctx, start, PIN)) return null;

            // set ellipsoid/sphere parameters
            if(pj_ell_set(ctx, start, out PIN.a, out PIN.es)) return null;

            PIN.a_orig=PIN.a;
            PIN.es_orig=PIN.es;

            PIN.e=Math.Sqrt(PIN.es);
            PIN.ra=1.0/PIN.a;
            PIN.one_es=1.0-PIN.es;
            if(PIN.one_es==0.0) { pj_ctx_set_errno(ctx, -6); return null; }
            PIN.rone_es=1.0/PIN.one_es;

            // Now that we have ellipse information check for WGS84 datum
            if(PIN.datum_type==PJD._3PARAM&&PIN.datum_params[0]==0.0&&PIN.datum_params[1]==0.0&&PIN.datum_params[2]==0.0&&
                PIN.a==6378137.0&&Math.Abs(PIN.es-0.006694379990)<0.000000000050) PIN.datum_type=PJD.WGS84; //WGS84/GRS80

            // set PIN.geoc coordinate system
            PIN.geoc=PIN.es!=0.0&&pj_param_b(ctx, start, "geoc");

            // over-ranging flag
            PIN.over=pj_param_b(ctx, start, "over");

            // vertical datum geoid grids
            PIN.has_geoid_vgrids=pj_param_t(ctx, start, "geoidgrids");
            if(PIN.has_geoid_vgrids) // we need to mark it as used.
                pj_param_s(ctx, start, "geoidgrids");

            // longitude center for wrapping
            PIN.is_long_wrap_set=pj_param_b(ctx, start, "lon_wrap");
            if(PIN.is_long_wrap_set)
                PIN.long_wrap_center=pj_param_r(ctx, start, "lon_wrap");

            // axis orientation
            string axis_arg=pj_param_s(ctx, start,"axis");
            if(axis_arg!=null&&axis_arg.Length!=0)
            {
                string axis_legal="ewnsud";
                if(axis_arg.Length!=3)
                {
                    pj_ctx_set_errno(ctx, (int)PJD_ERR.AXIS);
                    return null;
                }

                if(axis_legal.IndexOf(axis_arg[0])==-1||axis_legal.IndexOf(axis_arg[1])==-1||(axis_arg.Length>=3&&axis_legal.IndexOf(axis_arg[2])==-1))
                {
                    pj_ctx_set_errno(ctx, (int)PJD_ERR.AXIS);
                    return null;
                }

                // it would be nice to validate we don't have on axis repeated
                PIN.axis=axis_arg;
            }

            // central meridian
            PIN.lam0=pj_param_r(ctx, start, "lon_0");

            // central latitude
            PIN.phi0=pj_param_r(ctx, start, "lat_0");

            // false easting and northing
            PIN.x0=pj_param_d(ctx, start, "x_0");
            PIN.y0=pj_param_d(ctx, start, "y_0");

            // general scaling factor
            if(pj_param_t(ctx, start, "k_0")) PIN.k0=pj_param_d(ctx, start, "k_0");
            else if(pj_param_t(ctx, start, "k")) PIN.k0=pj_param_d(ctx, start, "k");
            else PIN.k0=1.0;

            if(PIN.k0<=0.0) { pj_ctx_set_errno(ctx, -31); return null; }

            // set units
            double to_meter=double.NaN;
            name=pj_param_s(ctx, start, "units");
            if(name!="")
            {
                to_meter=GetUnitFactor(name);
                if(double.IsNaN(to_meter)) { pj_ctx_set_errno(ctx, -7); return null; }
                PIN.to_meter=to_meter;
            }

            if(double.IsNaN(to_meter))
            {
                string s=pj_param_s(ctx, start, "to_meter");
                if(s!="")
                {
                    PIN.to_meter=Libc.strtod(s, out s);
                    if(s.Length>0&&s[0]=='/') PIN.to_meter/=Libc.strtod(s.Substring(1), out s); // ratio number
                    PIN.fr_meter=1.0/PIN.to_meter;
                }
                else PIN.to_meter=PIN.fr_meter=1.0;
            }
            else PIN.fr_meter=1.0/PIN.to_meter;

            // set vertical units
            double vto_meter=double.NaN;
            name=pj_param_s(ctx, start, "vunits");
            if(name!="")
            {
                vto_meter=GetUnitFactor(name);
                if(double.IsNaN(vto_meter)) { pj_ctx_set_errno(ctx, -7); return null; }
                PIN.vto_meter=vto_meter;
            }

            if(double.IsNaN(vto_meter))
            {
                string s=pj_param_s(ctx, start, "vto_meter");
                if(s!="")
                {
                    PIN.vto_meter=Libc.strtod(s, out s);
                    if(s.Length>0&&s[0]=='/') PIN.vto_meter/=Libc.strtod(s.Substring(1), out s); // ratio number
                    PIN.vfr_meter=1.0/PIN.vto_meter;
                }
                else
                {
                    PIN.vto_meter=PIN.to_meter;
                    PIN.vfr_meter=PIN.fr_meter;
                }
            }
            else PIN.vfr_meter=1.0/PIN.vto_meter;

            // prime meridian
            name=pj_param_s(ctx, start, "pm");
            if(name!="")
            {
                string value=GetPrimeMeridian(name);

                string next_str;
                if(value==null)
                {
                    double tmp=dmstor_ctx(ctx, name, out next_str);
                    if((tmp!=0.0||name[0]=='0')&&next_str=="") value=name;
                }

                if(value==null) { pj_ctx_set_errno(ctx, -46); return null; }
                PIN.from_greenwich=dmstor_ctx(ctx, value, out next_str);
            }
            else PIN.from_greenwich=0.0;

            // projection specific initialization
            PIN=PIN.Init();
            if(PIN==null||ctx.last_errno!=0)
            {
                // cleanup error return
                return null;
            }

            return PIN;
        }
Пример #29
0
        public static PJ pj_init_plus_ctx(projCtx ctx, string definition)
        {
            definition = definition.Trim(' ', '\t', '\n', '\r');
            if (definition.Length <= 0)
            {
                pj_ctx_set_errno(ctx, -1); return(null);
            }

            // split on whitespace not in double-quotes-strings (where \" is a double-quote in a string)
            List <string> argv = new List <string>();

            StringBuilder part         = new StringBuilder();
            bool          inString     = false;
            bool          wasBackslash = false;

            for (int i = 0; i < definition.Length; i++)
            {
                if (wasBackslash)
                {
                    wasBackslash = false;

                    if (definition[i] == '"')
                    {
                        part.Append('"');
                        continue;
                    }

                    part.Append('\\');
                }

                switch (definition[i])
                {
                case '"': inString = !inString; break;

                case '\\': wasBackslash = true; break;

                case ' ':
                case '\t':
                case '\n':
                case '\r':
                    if (inString)
                    {
                        part.Append(definition[i]);
                    }
                    else
                    {
                        string str = part.ToString().Trim();
                        if (str.Length > 1 && str.StartsWith("+"))
                        {
                            argv.Add(str.Substring(1));
                        }

                        part.Clear();
                    }
                    break;

                default: part.Append(definition[i]); break;
                }
            }

            string strLast = part.ToString().Trim();

            if (strLast.Length > 1 && strLast.StartsWith("+"))
            {
                argv.Add(strLast.Substring(1));
            }

            // perform actual initialization
            return(pj_init_ctx(ctx, argv.ToArray()));
        }
Пример #30
0
        public static PJ pj_init_ctx(projCtx ctx, string[] argv)
        {
            ctx.last_errno = 0;

            if (argv.Length <= 0)
            {
                pj_ctx_set_errno(ctx, -1); return(null);
            }

            List <string> start = new List <string>();

            Libc.errno = pj_errno = 0;

            // put arguments into internal linked list
            for (int a = 0; a < argv.Length; a++)
            {
                string curr = pj_mkparam(argv[a]);
                if (curr == null)
                {
                    pj_ctx_set_errno(ctx, Libc.errno); return(null);
                }
                start.Add(curr);
            }

            // check if +init present
            if (pj_param_t(ctx, start, "init"))
            {
                bool   found_def;
                string curr = get_init(ctx, start, pj_param_s(ctx, start, "init"), out found_def);
                if (curr == null || !found_def)
                {
                    if (pj_errno != 0 || Libc.errno != 0)
                    {
                        if (pj_errno == 0)
                        {
                            pj_ctx_set_errno(ctx, Libc.errno);
                        }
                    }
                    else
                    {
                        pj_ctx_set_errno(ctx, -2);
                    }

                    return(null);
                }
            }

            // find projection selection
            string name = pj_param_s(ctx, start, "proj");

            if (name == "")
            {
                pj_ctx_set_errno(ctx, -4); return(null);
            }

            // set defaults, unless inhibited
            if (!pj_param_b(ctx, start, "no_defs"))
            {
                get_defaults(ctx, start, name);
            }

            // allocate projection structure
            PJ PIN = GetPJ(name);

            if (PIN == null)
            {
                pj_ctx_set_errno(ctx, -5); return(null);
            }
            PIN.ctx              = ctx;
            PIN.parameters       = start;
            PIN.is_latlong       = false;
            PIN.is_geocent       = false;
            PIN.is_long_wrap_set = false;
            PIN.long_wrap_center = 0.0;
            PIN.axis             = "enu";

            PIN.gridlist        = null;
            PIN.vgridlist_geoid = null;

            // set datum parameters
            if (pj_datum_set(ctx, start, PIN))
            {
                return(null);
            }

            // set ellipsoid/sphere parameters
            if (pj_ell_set(ctx, start, out PIN.a, out PIN.es))
            {
                return(null);
            }

            PIN.a_orig  = PIN.a;
            PIN.es_orig = PIN.es;

            PIN.e      = Math.Sqrt(PIN.es);
            PIN.ra     = 1.0 / PIN.a;
            PIN.one_es = 1.0 - PIN.es;
            if (PIN.one_es == 0.0)
            {
                pj_ctx_set_errno(ctx, -6); return(null);
            }
            PIN.rone_es = 1.0 / PIN.one_es;

            // Now that we have ellipse information check for WGS84 datum
            if (PIN.datum_type == PJD._3PARAM && PIN.datum_params[0] == 0.0 && PIN.datum_params[1] == 0.0 && PIN.datum_params[2] == 0.0 &&
                PIN.a == 6378137.0 && Math.Abs(PIN.es - 0.006694379990) < 0.000000000050)
            {
                PIN.datum_type = PJD.WGS84;                                                                                 //WGS84/GRS80
            }
            // set PIN.geoc coordinate system
            PIN.geoc = PIN.es != 0.0 && pj_param_b(ctx, start, "geoc");

            // over-ranging flag
            PIN.over = pj_param_b(ctx, start, "over");

            // vertical datum geoid grids
            PIN.has_geoid_vgrids = pj_param_t(ctx, start, "geoidgrids");
            if (PIN.has_geoid_vgrids)            // we need to mark it as used.
            {
                pj_param_s(ctx, start, "geoidgrids");
            }

            // longitude center for wrapping
            PIN.is_long_wrap_set = pj_param_b(ctx, start, "lon_wrap");
            if (PIN.is_long_wrap_set)
            {
                PIN.long_wrap_center = pj_param_r(ctx, start, "lon_wrap");
            }

            // axis orientation
            string axis_arg = pj_param_s(ctx, start, "axis");

            if (axis_arg != null && axis_arg.Length != 0)
            {
                string axis_legal = "ewnsud";
                if (axis_arg.Length != 3)
                {
                    pj_ctx_set_errno(ctx, (int)PJD_ERR.AXIS);
                    return(null);
                }

                if (axis_legal.IndexOf(axis_arg[0]) == -1 || axis_legal.IndexOf(axis_arg[1]) == -1 || (axis_arg.Length >= 3 && axis_legal.IndexOf(axis_arg[2]) == -1))
                {
                    pj_ctx_set_errno(ctx, (int)PJD_ERR.AXIS);
                    return(null);
                }

                // it would be nice to validate we don't have on axis repeated
                PIN.axis = axis_arg;
            }

            // central meridian
            PIN.lam0 = pj_param_r(ctx, start, "lon_0");

            // central latitude
            PIN.phi0 = pj_param_r(ctx, start, "lat_0");

            // false easting and northing
            PIN.x0 = pj_param_d(ctx, start, "x_0");
            PIN.y0 = pj_param_d(ctx, start, "y_0");

            // general scaling factor
            if (pj_param_t(ctx, start, "k_0"))
            {
                PIN.k0 = pj_param_d(ctx, start, "k_0");
            }
            else if (pj_param_t(ctx, start, "k"))
            {
                PIN.k0 = pj_param_d(ctx, start, "k");
            }
            else
            {
                PIN.k0 = 1.0;
            }

            if (PIN.k0 <= 0.0)
            {
                pj_ctx_set_errno(ctx, -31); return(null);
            }

            // set units
            double to_meter = double.NaN;

            name = pj_param_s(ctx, start, "units");
            if (name != "")
            {
                to_meter = GetUnitFactor(name);
                if (double.IsNaN(to_meter))
                {
                    pj_ctx_set_errno(ctx, -7); return(null);
                }
                PIN.to_meter = to_meter;
            }

            if (double.IsNaN(to_meter))
            {
                string s = pj_param_s(ctx, start, "to_meter");
                if (s != "")
                {
                    PIN.to_meter = Libc.strtod(s, out s);
                    if (s.Length > 0 && s[0] == '/')
                    {
                        PIN.to_meter /= Libc.strtod(s.Substring(1), out s);                                         // ratio number
                    }
                    PIN.fr_meter = 1.0 / PIN.to_meter;
                }
                else
                {
                    PIN.to_meter = PIN.fr_meter = 1.0;
                }
            }
            else
            {
                PIN.fr_meter = 1.0 / PIN.to_meter;
            }

            // set vertical units
            double vto_meter = double.NaN;

            name = pj_param_s(ctx, start, "vunits");
            if (name != "")
            {
                vto_meter = GetUnitFactor(name);
                if (double.IsNaN(vto_meter))
                {
                    pj_ctx_set_errno(ctx, -7); return(null);
                }
                PIN.vto_meter = vto_meter;
            }

            if (double.IsNaN(vto_meter))
            {
                string s = pj_param_s(ctx, start, "vto_meter");
                if (s != "")
                {
                    PIN.vto_meter = Libc.strtod(s, out s);
                    if (s.Length > 0 && s[0] == '/')
                    {
                        PIN.vto_meter /= Libc.strtod(s.Substring(1), out s);                                         // ratio number
                    }
                    PIN.vfr_meter = 1.0 / PIN.vto_meter;
                }
                else
                {
                    PIN.vto_meter = PIN.to_meter;
                    PIN.vfr_meter = PIN.fr_meter;
                }
            }
            else
            {
                PIN.vfr_meter = 1.0 / PIN.vto_meter;
            }

            // prime meridian
            name = pj_param_s(ctx, start, "pm");
            if (name != "")
            {
                string value = GetPrimeMeridian(name);

                string next_str;
                if (value == null)
                {
                    double tmp = dmstor_ctx(ctx, name, out next_str);
                    if ((tmp != 0.0 || name[0] == '0') && next_str == "")
                    {
                        value = name;
                    }
                }

                if (value == null)
                {
                    pj_ctx_set_errno(ctx, -46); return(null);
                }
                PIN.from_greenwich = dmstor_ctx(ctx, value, out next_str);
            }
            else
            {
                PIN.from_greenwich = 0.0;
            }

            // projection specific initialization
            PIN = PIN.Init();
            if (PIN == null || ctx.last_errno != 0)
            {
                // cleanup error return
                return(null);
            }

            return(PIN);
        }
Пример #31
0
 public static double dmstor_ctx(projCtx ctx, string nptr)
 {
     string dummy="";
     return dmstor_ctx(ctx, nptr, out dummy);
 }
Пример #32
0
        public static double dmstor_ctx(projCtx ctx, string nptr, out string newptr)
        {
            const string sym="NnEeSsWw";

            newptr=nptr;
            if(nptr==null)
            {
                pj_ctx_set_errno(ctx, -16);
                return Libc.HUGE_VAL;
            }

            string s=nptr.TrimStart();
            if(s.Length==0)
            {
                newptr="";
                pj_ctx_set_errno(ctx, -16);
                return Libc.HUGE_VAL;
            }

            newptr=s;

            string work="";
            foreach(char c in s)
            {
                if((c>='0'&&c<='9')||(c>='a'&&c<='z')||(c>='A'&&c<='Z')||c=='.'||c=='°'||c=='\''||c=='"'||c=='+'||c=='-')
                {
                    work+=c;
                }
                else break;
            }

            s=work;
            char sign=work[0];
            if(sign=='+'||sign=='-') s=s.Substring(1);
            else sign='+';

            if((s.Length==0)||(!((s[0]>='0'&&s[0]<='9')||s[0]=='.')))
            {
                pj_ctx_set_errno(ctx, -16);
                return Libc.HUGE_VAL;
            }

            int n=0, p;
            double v=0, tv;

            for(int nl=0; nl<3; nl=n+1)
            {
                if(s.Length==0) break;

                char c=s[0];
                if(!((c>='0'&&c<='9')||c=='.')) break;

                tv=proj_strtod(s, out s);
                if(tv==Libc.HUGE_VAL) return tv;

                if(s.Length==0)
                {
                    v+=tv*vm[nl];
                    n=4;
                    continue;
                }

                switch(s[0])
                {
                    case '°':
                    case 'D':
                    case 'd':
                        n=0; break;
                    case '\'':
                        n=1; break;
                    case '"':
                        n=2; break;
                    case 'r':
                    case 'R':
                        if(nl!=0)
                        {
                            pj_ctx_set_errno(ctx, -16);
                            return Libc.HUGE_VAL;
                        }
                        s=s.Substring(1);
                        v=tv;
                        n=4;
                        continue;
                    default:
                        v+=tv*vm[nl];
                        n=4;
                        continue;
                }
                if(n<nl)
                {
                    pj_ctx_set_errno(ctx, -16);
                    return Libc.HUGE_VAL;
                }

                v+=tv*vm[n];
                s=s.Substring(1);
            }

            // postfix sign
            if(s.Length>0)
            {
                p=sym.IndexOf(s[0]);
                if(p>-1)
                {
                    sign=p>=4?(sign=='-'?'+':'-'):sign;
                    s=s.Substring(1);
                }
            }

            if(sign=='-') v=-v;

            // return point of next char after valid string
            if(newptr.Length<=(work.Length-s.Length)) newptr="";
            else newptr=newptr.Substring(work.Length-s.Length);
            return v;
        }
Пример #33
0
        // initialize geographic shape parameters
        internal static bool pj_ell_set(projCtx ctx, List<string> pl, out double a, out double es)
        {
            const double SIXTH=0.1666666666666666667; // 1/6
            const double RA4=0.04722222222222222222; // 17/360
            const double RA6=0.02215608465608465608; // 67/3024
            const double RV4=0.06944444444444444444; // 5/72
            const double RV6=0.04243827160493827160; // 55/1296

            // clear any previous error
            pj_ctx_set_errno(ctx, 0);

            // check for varying forms of ellipsoid input
            a=es=0.0;
            // R takes precedence
            if(pj_param_t(ctx, pl, "R")) a=pj_param_d(ctx, pl, "R");
            else
            { // probable elliptical figure
                bool removeLast2=false;
                try
                {
                    // check if ellps present and temporarily append its values to pl
                    string name=pj_param_s(ctx, pl, "ellps");
                    if(!string.IsNullOrEmpty(name))
                    {
                        string major, ell;
                        if(GetEllipsoid(name, out major, out ell)==null) { pj_ctx_set_errno(ctx, -9); return true; }
                        pl.Add(pj_mkparam(major));
                        pl.Add(pj_mkparam(ell));
                        removeLast2=true;
                    }

                    double b=0.0, e;
                    a=pj_param_d(ctx, pl, "a");
                    if(pj_param_t(ctx, pl, "es")) es=pj_param_d(ctx, pl, "es"); // eccentricity squared
                    else if(pj_param_t(ctx, pl, "e"))
                    { // eccentricity
                        e=pj_param_d(ctx, pl, "e");
                        es=e*e;
                    }
                    else if(pj_param_t(ctx, pl, "rf"))
                    { // recip flattening
                        es=pj_param_d(ctx, pl, "rf");
                        if(es==0) { pj_ctx_set_errno(ctx, -10); return true; }
                        es=1.0/es;
                        es=es*(2.0-es);
                    }
                    else if(pj_param_t(ctx, pl, "f"))
                    { // flattening
                        es=pj_param_d(ctx, pl, "f");
                        es=es*(2.0-es);
                    }
                    else if(pj_param_t(ctx, pl, "b"))
                    { // minor axis
                        b=pj_param_d(ctx, pl, "b");
                        es=1.0-(b*b)/(a*a);
                    }
                    // else es == 0.0 and sphere of radius a

                    if(b==0) b=a*Math.Sqrt(1.0-es);

                    // following options turn ellipsoid into equivalent sphere
                    if(pj_param_b(ctx, pl, "R_A"))
                    { // sphere--area of ellipsoid
                        a*=1.0-es*(SIXTH+es*(RA4+es*RA6));
                        es=0.0;
                    }
                    else if(pj_param_b(ctx, pl, "R_V"))
                    { // sphere--vol. of ellipsoid
                        a*=1.0-es*(SIXTH+es*(RV4+es*RV6));
                        es=0.0;
                    }
                    else if(pj_param_b(ctx, pl, "R_a"))
                    { // sphere--arithmetic mean
                        a=0.5*(a+b);
                        es=0.0;
                    }
                    else if(pj_param_b(ctx, pl, "R_g"))
                    { // sphere--geometric mean
                        a=Math.Sqrt(a*b);
                        es=0.0;
                    }
                    else if(pj_param_b(ctx, pl, "R_h"))
                    { // sphere--harmonic mean
                        a=2.0*a*b/(a+b);
                        es=0.0;
                    }
                    else
                    {
                        bool i=pj_param_t(ctx, pl, "R_lat_a"); // sphere--arith.
                        if(i||pj_param_t(ctx, pl, "R_lat_g"))
                        { // or geom. mean at latitude
                            double tmp=Math.Sin(pj_param_r(ctx, pl, i?"R_lat_a":"R_lat_g"));
                            if(Math.Abs(tmp)>HALFPI) { pj_ctx_set_errno(ctx, -11); return true; }
                            tmp=1.0-es*tmp*tmp;
                            a*=i?0.5*(1.0-es+tmp)/(tmp*Math.Sqrt(tmp)):Math.Sqrt(1.0-es)/tmp;
                            es=0.0;
                        }
                    }
                }
                finally
                {
                    if(removeLast2) pl.RemoveRange(pl.Count-2, 2); // clean up temporary extension of list
                }

                if(ctx.last_errno!=0) return true;
            }

            // some remaining checks
            if(es<0.0) { pj_ctx_set_errno(ctx, -12); return true; }
            if(a<=0.0) { pj_ctx_set_errno(ctx, -13); return true; }

            return false;
        }
Пример #34
0
        //**********************************************************************
        //                                get_opt()
        //**********************************************************************
        static string get_opt(projCtx ctx, List<string> start, StreamReader fid, string name, out bool found_def)
        {
            bool first=true, done=false;
            string next=null;

            found_def=false;

            while(!fid.EndOfStream&&!done)
            {
                string line=fid.ReadLine();
                if(line.Length<2) continue;
                line=line.TrimStart();
                if(line.Length<2) continue;
                if(line[0]=='#') continue;

                string[] words=line.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
                if(words.Length<1) continue;

                foreach(string word in words)
                {
                    if(word[0]=='#') break;
                    if(word[0]=='<')
                    { // control name
                        if(!first) { done=true; break; }
                        if(first&&word=="<"+name+">")
                        {
                            first=false;
                            found_def=true;
                        }
                        continue;
                    }
                    else if(!first&&!pj_param_t(ctx, start, word))
                    {
                        // don't default ellipse if datum, ellps or any earth model information is set.
                        if(word.StartsWith("ellps=")||
                            (!pj_param_t(ctx, start, "datum")&&!pj_param_t(ctx, start, "ellps")&&!pj_param_t(ctx, start, "a")&&
                            !pj_param_t(ctx, start, "b")&&!pj_param_t(ctx, start, "rf")&&!pj_param_t(ctx, start, "f")))
                        {
                            next=pj_mkparam(word);
                            if(next==null) return null;
                            start.Add(next);
                        }
                    }
                }
            }

            return next;
        }
Пример #35
0
        public static PJ pj_init_plus_ctx(projCtx ctx, string definition)
        {
            definition=definition.Trim(' ', '\t', '\n', '\r');
            if(definition.Length<=0) { pj_ctx_set_errno(ctx, -1); return null; }

            // split on whitespace not in double-quotes-strings (where \" is a double-quote in a string)
            List<string> argv=new List<string>();

            StringBuilder part=new StringBuilder();
            bool inString=false;
            bool wasBackslash=false;
            for(int i=0; i<definition.Length; i++)
            {
                if(wasBackslash)
                {
                    wasBackslash=false;

                    if(definition[i]=='"')
                    {
                        part.Append('"');
                        continue;
                    }

                    part.Append('\\');
                }

                switch(definition[i])
                {
                    case '"': inString=!inString; break;
                    case '\\': wasBackslash=true; break;
                    case ' ':
                    case '\t':
                    case '\n':
                    case '\r':
                        if(inString) part.Append(definition[i]);
                        else
                        {
                            string str=part.ToString().Trim();
                            if(str.Length>1&&str.StartsWith("+")) argv.Add(str.Substring(1));

                            part.Clear();
                        }
                        break;
                    default: part.Append(definition[i]); break;
                }
            }

            string strLast=part.ToString().Trim();
            if(strLast.Length>1&&strLast.StartsWith("+")) argv.Add(strLast.Substring(1));

            // perform actual initialization
            return pj_init_ctx(ctx, argv.ToArray());
        }
Пример #36
0
        //**********************************************************************
        //                                get_init()
        //**********************************************************************
        static string get_init(projCtx ctx, List<string> start, string name, out bool found_def)
        {
            found_def=false;

            int opt=name.LastIndexOf(':');
            if(opt==-1) { pj_ctx_set_errno(ctx, -3); return null; }
            if(opt==name.Length-1) { pj_ctx_set_errno(ctx, -3); return null; } // last char ':' => no name given

            string next=null;
            try
            {
                FileStream fs=pj_open_lib(ctx, name.Substring(0, opt), FileAccess.Read);
                if(fs==null) return null;

                StreamReader fid=new StreamReader(fs);
                next=get_opt(ctx, start, fid, name.Substring(opt+1), out found_def);
                fid.Close();
            }
            catch
            {
            }

            return next;
        }
Пример #37
0
        // get parameter value
        internal static string pj_param_s(projCtx ctx, List<string> pl, string name)
        {
            foreach(string p in pl)
            {
                if(p==name) return "";
                if(p.StartsWith(name+"="))
                {
                    if(p.Length==name.Length+1) return "";
                    return p.Substring(name.Length+1);
                }
            }

            return "";
        }
Пример #38
0
        //**********************************************************************
        //                            get_defaults()
        //**********************************************************************
        static string get_defaults(projCtx ctx, List<string> start, string name)
        {
            string next=null;
            try
            {
                FileStream fs=pj_open_lib(ctx, "proj_def.dat", FileAccess.Read);
                if(fs!=null)
                {
                    StreamReader fid=new StreamReader(fs);
                    bool found_def;
                    next=get_opt(ctx, start, fid, "general", out found_def);
                    fid.BaseStream.Seek(0, SeekOrigin.Begin);
                    next=get_opt(ctx, start, fid, name, out found_def);
                    fid.Close();
                }
                else
                {
                    // if proj_def.dat not found
                    start.Add(pj_mkparam("ellps=WGS84"));

                    if(name=="aea")
                    {
                        start.Add(pj_mkparam("lat_1=29.5"));
                        start.Add(pj_mkparam("lat_2=45.5"));
                    }
                    else if(name=="lcc")
                    {
                        start.Add(pj_mkparam("lat_1=33"));
                        start.Add(pj_mkparam("lat_2=45"));
                    }
                    else if(name=="lagrng") start.Add(pj_mkparam("W=2"));
                }
            }
            catch
            {
            }

            if(Libc.errno!=0) Libc.errno=0; // don't care if can't open file
            ctx.last_errno=0;

            return next;
        }
Пример #39
0
        public double proj_inv_mdist(projCtx ctx, double dist)
        {
            double k=1.0/(1.0-es);
            int i=MAX_ITER_mdist;
            double phi=dist;
            while(i-->0)
            {
                double s=Math.Sin(phi);
                double t=1.0-es*s*s;
                phi-=t=(proj_mdist(phi, s, Math.Cos(phi))-dist)*(t*Math.Sqrt(t))*k;
                if(Math.Abs(t)<TOL14) return phi; // that is no change
            }

            // convergence failed
            Proj.pj_ctx_set_errno(ctx, -17);
            return phi;
        }
Пример #40
0
        //**********************************************************************
        //								get_opt()
        //**********************************************************************
        static string get_opt(projCtx ctx, List <string> start, StreamReader fid, string name, out bool found_def)
        {
            bool   first = true, done = false;
            string next = null;

            found_def = false;

            while (!fid.EndOfStream && !done)
            {
                string line = fid.ReadLine();
                if (line.Length < 2)
                {
                    continue;
                }
                line = line.TrimStart();
                if (line.Length < 2)
                {
                    continue;
                }
                if (line[0] == '#')
                {
                    continue;
                }

                string[] words = line.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
                if (words.Length < 1)
                {
                    continue;
                }

                foreach (string word in words)
                {
                    if (word[0] == '#')
                    {
                        break;
                    }
                    if (word[0] == '<')
                    {                     // control name
                        if (!first)
                        {
                            done = true; break;
                        }
                        if (first && word == "<" + name + ">")
                        {
                            first     = false;
                            found_def = true;
                        }
                        continue;
                    }
                    else if (!first && !pj_param_t(ctx, start, word))
                    {
                        // don't default ellipse if datum, ellps or any earth model information is set.
                        if (word.StartsWith("ellps=") ||
                            (!pj_param_t(ctx, start, "datum") && !pj_param_t(ctx, start, "ellps") && !pj_param_t(ctx, start, "a") &&
                             !pj_param_t(ctx, start, "b") && !pj_param_t(ctx, start, "rf") && !pj_param_t(ctx, start, "f")))
                        {
                            next = pj_mkparam(word);
                            if (next == null)
                            {
                                return(null);
                            }
                            start.Add(next);
                        }
                    }
                }
            }

            return(next);
        }
Пример #41
0
            0, 0, 0, 0, 0, 0, 0, 0, 1, 0}; // 40 to 49

        #endregion Fields

        #region Methods

        //***********************************************************************
        //*							pj_adjust_axis()							*
        //*																		*
        //*		Normalize or de-normalized the x/y/z axes. The normal form		*
        //*		is "enu" (easting, northing, up).								*
        //***********************************************************************
        public static int pj_adjust_axis(projCtx ctx, string axis, bool denormalize_flag, long point_count, int point_offset, double[] x, double[] y, double[] z)
        {
            double x_in, y_in, z_in=0.0;
            int i, i_axis;

            if(!denormalize_flag)
            {
                for(i=0; i<point_count; i++)
                {
                    x_in=x[point_offset*i];
                    y_in=y[point_offset*i];
                    if(z!=null) z_in=z[point_offset*i];

                    for(i_axis=0; i_axis<3; i_axis++)
                    {
                        double value;

                        if(i_axis==0) value=x_in;
                        else if(i_axis==1) value=y_in;
                        else value=z_in;

                        switch(axis[i_axis])
                        {
                            case 'e': x[point_offset*i]=value; break;
                            case 'w': x[point_offset*i]=-value; break;
                            case 'n': y[point_offset*i]=value; break;
                            case 's': y[point_offset*i]=-value; break;
                            case 'u': if(z!=null) z[point_offset*i]=value; break;
                            case 'd': if(z!=null) z[point_offset*i]=-value; break;
                            default: pj_ctx_set_errno(ctx, (int)PJD_ERR.AXIS); return (int)PJD_ERR.AXIS;
                        }
                    } // i_axis
                } // i (point)
            }
            else // denormalize
            {
                for(i=0; i<point_count; i++)
                {
                    x_in=x[point_offset*i];
                    y_in=y[point_offset*i];
                    if(z!=null) z_in=z[point_offset*i];

                    for(i_axis=0; i_axis<3; i_axis++)
                    {
                        double[] target;

                        if(i_axis==2&&z==null) continue;

                        if(i_axis==0) target=x;
                        else if(i_axis==1) target=y;
                        else target=z;

                        switch(axis[i_axis])
                        {
                            case 'e': target[point_offset*i]=x_in; break;
                            case 'w': target[point_offset*i]=-x_in; break;
                            case 'n': target[point_offset*i]=y_in; break;
                            case 's': target[point_offset*i]=-y_in; break;
                            case 'u': target[point_offset*i]=z_in; break;
                            case 'd': target[point_offset*i]=-z_in; break;
                            default: pj_ctx_set_errno(ctx, (int)PJD_ERR.AXIS); return (int)PJD_ERR.AXIS;
                        }
                    } // i_axis
                } // i (point)
            }

            return 0;
        }
Пример #42
0
 //***********************************************************************
 //*								pj_log()								*
 //***********************************************************************
 public static void pj_log(projCtx ctx, PJ_LOG level, string format, params object[] args)
 {
     if(level>ctx.debug_level) return;
     ctx.logger(ctx.app_data, level, string.Format(format, args));
 }