MissionPlanner.srtm.getAltitude C# (CSharp) Method

getAltitude() public static method

public static getAltitude ( double lat, double lng, double zoom = 16 ) : altresponce
lat double
lng double
zoom double
return altresponce
        public static altresponce getAltitude(double lat, double lng, double zoom = 16)
        {
            short alt = 0;

            var trytiff = Utilities.GeoTiff.getAltitude(lat, lng);

            if (trytiff.currenttype == tiletype.valid)
                return trytiff;

            var trydted = Utilities.DTED.getAltitude(lat, lng);

            if (trydted.currenttype == tiletype.valid)
                return trydted;

            //lat += 1 / 1199.0;
            //lng -= 1 / 1201f;

            // 		lat	-35.115676879882812	double
            //		lng	117.94178754638671	double
            // 		alt	70	short

            var filename = GetFilename(lat, lng);

            if (String.IsNullOrEmpty(filename))
                return altresponce.Invalid;

            try
            {

                if (cache.ContainsKey(filename) || File.Exists(datadirectory + Path.DirectorySeparatorChar + filename))
                {
                    // srtm hgt files

                    int size = -1;

                    // add to cache
                    if (!cache.ContainsKey(filename))
                    {
                        using (
                            FileStream fs = new FileStream(datadirectory + Path.DirectorySeparatorChar + filename,
                                FileMode.Open, FileAccess.Read, FileShare.Read))
                        {

                            if (fs.Length == (1201*1201*2))
                            {
                                size = 1201;
                            }
                            else if (fs.Length == (3601*3601*2))
                            {
                                size = 3601;
                            }
                            else
                                return srtm.altresponce.Invalid;

                            byte[] altbytes = new byte[2];
                            short[,] altdata = new short[size, size];


                            int altlat = 0;
                            int altlng = 0;

                            while (fs.Read(altbytes, 0, 2) != 0)
                            {
                                altdata[altlat, altlng] = (short) ((altbytes[0] << 8) + altbytes[1]);

                                altlat++;
                                if (altlat >= size)
                                {
                                    altlng++;
                                    altlat = 0;
                                }
                            }

                            cache[filename] = altdata;
                        }
                    }

                    if (cache[filename].Length == (1201*1201))
                    {
                        size = 1201;
                    }
                    else if (cache[filename].Length == (3601*3601))
                    {
                        size = 3601;
                    }
                    else
                        return srtm.altresponce.Invalid;

                    int x = (lng < 0) ? (int) (lng - 1) : (int) lng;
                    int y = (lat < 0) ? (int) (lat - 1) : (int) lat;

                    // remove the base lat long
                    lat -= y;
                    lng -= x;

                    // values should be 0-1199, 1200 is for interpolation
                    double xf = lng*(size - 2);
                    double yf = lat*(size - 2);

                    int x_int = (int) xf;
                    double x_frac = xf - x_int;

                    int y_int = (int) yf;
                    double y_frac = yf - y_int;

                    y_int = (size - 2) - y_int;

                    double alt00 = GetAlt(filename, x_int, y_int);
                    double alt10 = GetAlt(filename, x_int + 1, y_int);
                    double alt01 = GetAlt(filename, x_int, y_int + 1);
                    double alt11 = GetAlt(filename, x_int + 1, y_int + 1);

                    double v1 = avg(alt00, alt10, x_frac);
                    double v2 = avg(alt01, alt11, x_frac);
                    double v = avg(v1, v2, -y_frac);

                    if (v < -1000)
                        return altresponce.Invalid;

                    return new altresponce()
                    {
                        currenttype = tiletype.valid,
                        alt = v,
                        altsource = "SRTM"
                    };
                }

                string filename2 = "srtm_" + Math.Round((lng + 2.5 + 180)/5, 0).ToString("00") + "_" +
                                   Math.Round((60 - lat + 2.5)/5, 0).ToString("00") + ".asc";

                if (File.Exists(datadirectory + Path.DirectorySeparatorChar + filename2))
                {
                    using (
                        StreamReader sr =
                            new StreamReader(readFile(datadirectory + Path.DirectorySeparatorChar + filename2)))
                    {

                        int nox = 0;
                        int noy = 0;
                        float left = 0;
                        float top = 0;
                        int nodata = -9999;
                        float cellsize = 0;

                        int rowcounter = 0;

                        float wantrow = 0;
                        float wantcol = 0;


                        while (!sr.EndOfStream)
                        {
                            string line = sr.ReadLine();

                            if (line.StartsWith("ncols"))
                            {
                                nox = int.Parse(line.Substring(line.IndexOf(' ')));

                                //hgtdata = new int[nox * noy];
                            }
                            else if (line.StartsWith("nrows"))
                            {
                                noy = int.Parse(line.Substring(line.IndexOf(' ')));

                                //hgtdata = new int[nox * noy];
                            }
                            else if (line.StartsWith("xllcorner"))
                            {
                                left = float.Parse(line.Substring(line.IndexOf(' ')));
                            }
                            else if (line.StartsWith("yllcorner"))
                            {
                                top = float.Parse(line.Substring(line.IndexOf(' ')));
                            }
                            else if (line.StartsWith("cellsize"))
                            {
                                cellsize = float.Parse(line.Substring(line.IndexOf(' ')));
                            }
                            else if (line.StartsWith("NODATA_value"))
                            {
                                nodata = int.Parse(line.Substring(line.IndexOf(' ')));
                            }
                            else
                            {
                                string[] data = line.Split(new char[] {' '});

                                if (data.Length == (nox + 1))
                                {
                                    wantcol = (float) ((lng - Math.Round(left, 0)));

                                    wantrow = (float) ((lat - Math.Round(top, 0)));

                                    wantrow = (int) (wantrow/cellsize);
                                    wantcol = (int) (wantcol/cellsize);

                                    wantrow = noy - wantrow;

                                    if (rowcounter == wantrow)
                                    {
                                        Console.WriteLine("{0} {1} {2} {3} ans {4} x {5}", lng, lat, left, top,
                                            data[(int) wantcol], (nox + wantcol*cellsize));

                                        return new altresponce()
                                        {
                                            currenttype = tiletype.valid,
                                            alt = int.Parse(data[(int) wantcol])
                                        };
                                    }

                                    rowcounter++;
                                }
                            }
                        }
                    }
                    return new altresponce()
                    {
                        currenttype = tiletype.valid,
                        alt = alt,
                        altsource = "ASCII"
                    };
                }
                else // get something
                {
                    if (filename.Contains("S00W000") || filename.Contains("S00W001") ||
                        filename.Contains("S01W000") || filename.Contains("S01W001"))
                    {
                        return altresponce.Ocean;
                    }

                    if (oceantile.Contains(filename))
                        return altresponce.Ocean;

                    if (zoom >= 7)
                    {
                        if (!Directory.Exists(datadirectory))
                            Directory.CreateDirectory(datadirectory);

                        if (requestThread == null)
                        {
                            log.Info("Getting " + filename);
                            lock (objlock)
                            {
                                queue.Add(filename);
                            }

                            requestThread = new Thread(requestRunner);
                            requestThread.IsBackground = true;
                            requestThread.Name = "SRTM request runner";
                            requestThread.Start();
                        }
                        else
                        {
                            lock (objlock)
                            {
                                if (!queue.Contains(filename))
                                {
                                    log.Info("Getting " + filename);
                                    queue.Add(filename);
                                }
                            }
                        }
                    }
                }
            }
            catch
            {
                return altresponce.Invalid;
            }

            return altresponce.Invalid;
        }