Ejemplo n.º 1
0
        public static altresponce getAltitude(double lat, double lng, double zoom = 16)
        {
            short alt = 0;
            var answer = new altresponce();

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

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

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

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

            int x = (lng < 0) ? (int)(lng - 1) : (int)lng;//(int)Math.Floor(lng);
            int y = (lat < 0) ? (int)(lat - 1) : (int)lat; ;//(int)Math.Floor(lat);

            string ns;
            if (y > 0)
                ns = "N";
            else
                ns = "S";

            string ew;
            if (x > 0)
                ew = "E";
            else
                ew = "W";

            // running tostring at a high rate was costing cpu
            if (fnamecache[y] == null)
                fnamecache[y] = Math.Abs(y).ToString("00");
            if (fnamecache[1000 + x] == null)
                fnamecache[1000 + x] = Math.Abs(x).ToString("000");

            string filename = ns + fnamecache[y] + ew + fnamecache[1000 + x] + ".hgt";

            try
            {

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

                    int size = -1;

                    // add to cache
                    if (!cache.ContainsKey(filename))
                    {
                        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 answer;

                        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;
                            }
                        }

                        fs.Close();

                        cache[filename] = altdata;
                    }

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

                    // 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);

                    answer.currenttype = tiletype.valid;
                    answer.alt = v;
                    return answer;
                }

                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))
                {
                    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));

                                    answer.currenttype = tiletype.valid;
                                    answer.alt = int.Parse(data[(int)wantcol]);
                                    return answer;
                                }

                                rowcounter++;
                            }
                        }



                    }

                    //sr.Close();
                    answer.currenttype = tiletype.valid;
                    answer.alt = alt;
                    return answer;
                }
                else // get something
                {
                    if (filename.Contains("S00W000") || filename.Contains("S00W001") ||
                        filename.Contains("S01W000") || filename.Contains("S01W001"))
                    {
                        answer.currenttype = tiletype.ocean;
                        return answer;
                    }

                    if (oceantile.Contains(filename))
                        answer.currenttype = tiletype.ocean;

                    if (zoom >= 12)
                    {
                        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 { answer.alt = 0; answer.currenttype = tiletype.invalid; }

            return answer;
        }
Ejemplo n.º 2
0
        public static altresponce getAltitude(double lat, double lng, double zoom = 16)
        {
            short alt    = 0;
            var   answer = new altresponce();

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

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

            int x = (lng < 0) ? (int)(lng - 1) : (int)lng;  //(int)Math.Floor(lng);
            int y = (lat < 0) ? (int)(lat - 1) : (int)lat;; //(int)Math.Floor(lat);

            string ns;

            if (y > 0)
            {
                ns = "N";
            }
            else
            {
                ns = "S";
            }

            string ew;

            if (x > 0)
            {
                ew = "E";
            }
            else
            {
                ew = "W";
            }

            // running tostring at a high rate was costing cpu
            if (fnamecache[y] == null)
            {
                fnamecache[y] = Math.Abs(y).ToString("00");
            }
            if (fnamecache[1000 + x] == null)
            {
                fnamecache[1000 + x] = Math.Abs(x).ToString("000");
            }

            string filename = ns + fnamecache[y] + ew + fnamecache[1000 + x] + ".hgt";

            try
            {
                if (cache.ContainsKey(filename) || File.Exists(datadirectory + Path.DirectorySeparatorChar + filename))
                { // srtm hgt files
                    int size = -1;

                    // add to cache
                    if (!cache.ContainsKey(filename))
                    {
                        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(answer);
                        }

                        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;
                            }
                        }

                        fs.Close();

                        cache[filename] = altdata;
                    }

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

                    // 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);

                    answer.currenttype = tiletype.valid;
                    answer.alt         = v;
                    return(answer);
                }

                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))
                {
                    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));

                                    answer.currenttype = tiletype.valid;
                                    answer.alt         = int.Parse(data[(int)wantcol]);
                                    return(answer);
                                }

                                rowcounter++;
                            }
                        }
                    }

                    //sr.Close();
                    answer.currenttype = tiletype.valid;
                    answer.alt         = alt;
                    return(answer);
                }
                else // get something
                {
                    if (oceantile.Contains(filename))
                    {
                        answer.currenttype = tiletype.ocean;
                    }

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

                        if (requestThread == null)
                        {
                            Console.WriteLine("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))
                                {
                                    Console.WriteLine("Getting " + filename);
                                    queue.Add(filename);
                                }
                            }
                        }
                    }
                }
            }
            catch { answer.alt = 0; answer.currenttype = tiletype.invalid; }

            return(answer);
        }