Ejemplo n.º 1
0
        public static srtm.altresponce getAltitude(double lat, double lng, double zoom = 16)
        {
            if (index.Count == 0)
            {
                return(srtm.altresponce.Invalid);
            }

            var answer = new srtm.altresponce();

            foreach (var DTEDdata in index)
            {
                if (DTEDdata.Area.Contains(lat, lng))
                {
                    // add to cache
                    if (!cache.ContainsKey(DTEDdata.FileName))
                    {
                        short[,] altdata = new short[DTEDdata.width, DTEDdata.height];

                        using (var stream = File.OpenRead(DTEDdata.FileName))
                        {
                            stream.Seek(3428, SeekOrigin.Begin);

                            for (int b = 0; b < (DTEDdata.height); b++)
                            {
                                var buffer = new byte[DTEDdata.height * 2 + 12]; // header + checksum + data(shorts)
                                stream.Read(buffer, 0, buffer.Length);

                                if (buffer[0] == 0xaa)
                                {
                                    int blockno   = ((int)buffer[1] << 16) + ((int)buffer[2] << 8) + buffer[3];
                                    int longcount = ((int)buffer[4] << 8) + buffer[5];
                                    int latcount  = ((int)buffer[6] << 8) + buffer[7];

                                    for (int a = 0; a < DTEDdata.width; a++)
                                    {
                                        altdata[longcount, latcount + a] =
                                            (short)(((int)buffer[8 + a * 2] << 8) + buffer[8 + a * 2 + 1]);
                                    }
                                }
                            }
                        }
                        cache[DTEDdata.FileName] = altdata;
                    }

                    // get answer
                    var xf = map(lng, DTEDdata.Area.Left, DTEDdata.Area.Right - DTEDdata.xscale, 0, DTEDdata.width - 1);
                    var yf = map(lat, DTEDdata.Area.Bottom, DTEDdata.Area.Top - DTEDdata.yscale, 0, DTEDdata.height - 1);

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

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

                    double alt00 = GetAlt(DTEDdata.FileName, x_int, y_int);
                    double alt10 = GetAlt(DTEDdata.FileName, x_int + 1, y_int);
                    double alt01 = GetAlt(DTEDdata.FileName, x_int, y_int + 1);
                    double alt11 = GetAlt(DTEDdata.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)
                    {
                        answer.currenttype = srtm.tiletype.valid;
                    }
                    answer.alt       = v;
                    answer.altsource = "DTED";
                    return(answer);
                }
            }

            return(srtm.altresponce.Invalid);
        }
Ejemplo n.º 2
0
        public static srtm.altresponce getAltitude(double lat, double lng, double zoom = 16)
        {
            if (index.Count == 0)
            {
                return(srtm.altresponce.Invalid);
            }

            var answer = new srtm.altresponce();

            foreach (var geotiffdata in index)
            {
                if (geotiffdata.Area.Contains(lat, lng))
                {
                    // add to cache
                    if (!cache.ContainsKey(geotiffdata.FileName) && geotiffdata.cacheable)
                    {
                        float[,] altdata = new float[geotiffdata.height, geotiffdata.width];

                        using (Tiff tiff = Tiff.Open(geotiffdata.FileName, "r"))
                        {
                            byte[] scanline = new byte[tiff.ScanlineSize()];

                            for (int row = 0; row < geotiffdata.height; row++)
                            {
                                tiff.ReadScanline(scanline, row);

                                for (int col = 0; col < geotiffdata.width; col++)
                                {
                                    if (geotiffdata.bits == 16)
                                    {
                                        altdata[row, col] = (short)((scanline[col * 2 + 1] << 8) + scanline[col * 2]);
                                    }
                                    else if (geotiffdata.bits == 32)
                                    {
                                        altdata[row, col] = (float)BitConverter.ToSingle(scanline, col * 4);
                                    }
                                }
                            }
                        }

                        cache[geotiffdata.FileName] = altdata;
                    }

                    // get answer
                    var xf = map(lat, geotiffdata.Area.Top, geotiffdata.Area.Bottom, 0, geotiffdata.height - 1);
                    var yf = map(lng, geotiffdata.Area.Left, geotiffdata.Area.Right, 0, geotiffdata.width - 1);

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

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

                    // y_int = (geotiffdata.height - 2) - y_int;

                    double alt00 = GetAlt(geotiffdata, x_int, y_int);
                    double alt10 = GetAlt(geotiffdata, x_int + 1, y_int);
                    double alt01 = GetAlt(geotiffdata, x_int, y_int + 1);
                    double alt11 = GetAlt(geotiffdata, 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)
                    {
                        answer.currenttype = srtm.tiletype.valid;
                    }
                    answer.alt       = v;
                    answer.altsource = "GeoTiff";
                    return(answer);
                }
            }

            return(srtm.altresponce.Invalid);
        }