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

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

                        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++)
                                {
                                    altdata[row, col] = (short)((scanline[col * 2 + 1] << 8) + scanline[col * 2]);
                                }
                            }
                        }

                        cache[geotiffdata.FileName] = altdata;
                    }

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

                    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.FileName, x_int, y_int);
                    double alt10 = GetAlt(geotiffdata.FileName, x_int + 1, y_int);
                    double alt01 = GetAlt(geotiffdata.FileName, x_int, y_int + 1);
                    double alt11 = GetAlt(geotiffdata.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;
                    return(answer);
                }
            }

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

            var answer = new srtm.altresponce();

            foreach (var geotiffdata in index.ToArray())
            {
                if (geotiffdata.Area.Contains(lat, lng))
                {
                    // 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);

                    //wgs84 && etrs89
                    if (geotiffdata.ProjectedCSTypeGeoKey >= 3038 && geotiffdata.ProjectedCSTypeGeoKey <= 3051 ||
                        geotiffdata.ProjectedCSTypeGeoKey >= 32601 && geotiffdata.ProjectedCSTypeGeoKey <= 32760 ||
                        geotiffdata.ProjectedCSTypeGeoKey >= 25828 && geotiffdata.ProjectedCSTypeGeoKey <= 25838)
                    {
                        var pnt = PointLatLngAlt.ToUTM((geotiffdata.UTMZone) * 1, lat, lng);

                        xf = map(pnt[1], geotiffdata.y, geotiffdata.y - geotiffdata.height * geotiffdata.yscale, 0,
                                 geotiffdata.height - 1);
                        yf = map(pnt[0], geotiffdata.x, geotiffdata.x + geotiffdata.width * geotiffdata.xscale, 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;


                    //could be on one of the other images
                    if (x_int < 0 || y_int < 0 || x_int >= geotiffdata.width - 1 || y_int >= geotiffdata.height - 1)
                    {
                        continue;
                    }

                    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;
                    }
                    if (alt00 < -1000 || alt10 < -1000 || alt01 < -1000 || alt11 < -1000)
                    {
                        answer.currenttype = srtm.tiletype.invalid;
                    }
                    answer.alt       = v;
                    answer.altsource = "GeoTiff";
                    return(answer);
                }
            }

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

            var answer = new srtm.altresponce();

            foreach (var geotiffdata in index.ToArray())
            {
                if (geotiffdata.Area.Contains(lat, lng))
                {
                    // add to cache
                    if (!cache.ContainsKey(geotiffdata.FileName) && geotiffdata.cacheable)
                    {
                        if (!File.Exists(geotiffdata.FileName))
                        {
                            continue;
                        }

                        lock (cacheloading)
                        {
                            if (cacheloading.Contains(geotiffdata.FileName))
                            {
                                return(srtm.altresponce.Invalid);
                            }

                            cacheloading.Add(geotiffdata.FileName);
                        }

                        Task.Run(() => {
                            try
                            {
                                float[,] altdata = new float[geotiffdata.height, geotiffdata.width];

                                using (Tiff tiff = Tiff.Open(geotiffdata.FileName, "r"))
                                {
                                    if (tiff.GetField(TiffTag.TILEWIDTH) != null &&
                                        tiff.GetField(TiffTag.TILEWIDTH).Length >= 1)
                                    {
                                        FieldValue[] value = tiff.GetField(TiffTag.IMAGEWIDTH);
                                        int imageWidth     = value[0].ToInt();

                                        value           = tiff.GetField(TiffTag.IMAGELENGTH);
                                        int imageLength = value[0].ToInt();

                                        value         = tiff.GetField(TiffTag.TILEWIDTH);
                                        int tileWidth = value[0].ToInt();

                                        value          = tiff.GetField(TiffTag.TILELENGTH);
                                        int tileLength = value[0].ToInt();

                                        byte[] buf = new byte[tiff.TileSize()];
                                        for (int y = 0; y < imageLength; y += tileLength)
                                        {
                                            for (int x = 0; x < imageWidth; x += tileWidth)
                                            {
                                                tiff.ReadTile(buf, 0, x, y, 0, 0);

                                                for (int row = 0; row < tileLength; row++)
                                                {
                                                    for (int col = 0; col < tileWidth; col++)
                                                    {
                                                        if (x + col >= imageWidth || y + row >= imageLength)
                                                        {
                                                            break;
                                                        }

                                                        if (geotiffdata.bits == 16)
                                                        {
                                                            altdata[y + row, x + col] =
                                                                (short)((buf[row * tileWidth * 2 + col * 2 + 1] << 8) +
                                                                        buf[row * tileWidth * 2 + col * 2]);
                                                        }
                                                        else if (geotiffdata.bits == 32)
                                                        {
                                                            altdata[y + row, x + col] =
                                                                (float)BitConverter.ToSingle(buf,
                                                                                             row * tileWidth * 4 + col * 4);
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    else
                                    {
                                        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;
                            }
                            finally
                            {
                                lock (cacheloading)
                                {
                                    cacheloading.Remove(geotiffdata.FileName);
                                }
                            }
                        });

                        return(srtm.altresponce.Invalid);
                    }

                    // 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;
                    }
                    if (alt00 < -1000 || alt10 < -1000 || alt01 < -1000 || alt11 < -1000)
                    {
                        answer.currenttype = srtm.tiletype.invalid;
                    }
                    answer.alt       = v;
                    answer.altsource = "GeoTiff";
                    return(answer);
                }
            }

            return(srtm.altresponce.Invalid);
        }
Ejemplo n.º 4
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);
        }