示例#1
0
    public override bool Read(string filename, ProgressInfo progress, out PatchData data)
    {
        data = null;

        using (var hgt = Hgt.Open(filename))
        {
            if (hgt == null)
            {
                return(false);
            }

            var bounds = hgt.Bounds;

            GridData grid = new GridData
            {
                north = bounds.north,
                east  = bounds.east,
                west  = bounds.west,
                south = bounds.south,
            };

            // Substract 1 to width/height to avoid overlaps
            grid.countX = hgt.Width - 1;
            grid.countY = hgt.Height - 1;
            grid.InitGridValues(false);

            // Read raster values
            var   buffer    = hgt.Buffer;
            int   pos       = 0;
            int   index     = 0;
            float invCountY = 1f / grid.countY;
            for (int y = 0; y < grid.countY; y++)
            {
                for (int x = 0; x < grid.countX; x++)
                {
                    short value = (short)((buffer[pos++] << 8) | buffer[pos++]);
                    grid.values[index++] = value;
                }
                // Advance another 2 positions to skip the last column
                pos += 2;

                progress.value += invCountY;
            }

            progress.value = 1;

            grid.AddMaskValue(Hgt.DataVoid);
            grid.units = SuggestedUnits;

            data = grid;
        }

        return(true);
    }
示例#2
0
    public override bool Read(string filename, ProgressInfo progress, out PatchData data)
    {
        data = null;

        using (var geoTiff = new GeoTiff(filename))
        {
            if (geoTiff == null)
            {
                return(false);
            }

            var bouds = geoTiff.Bounds;

            GridData grid = new GridData
            {
                north = bouds.north,
                east  = bouds.east,
                west  = bouds.west,
                south = bouds.south,
            };

            grid.countX = geoTiff.Width;
            grid.countY = geoTiff.Height;
            grid.InitGridValues(false);

            // Read raster values
            int width          = grid.countX;
            int height         = grid.countY;
            int bufferSize     = width * height;
            int bytesPerSample = geoTiff.BitsPerSample / 8;

            var dataType = geoTiff.DataType;
            if (dataType == null)
            {
                switch (geoTiff.BitsPerSample)
                {
                case 8:
                    dataType = typeof(byte);
                    break;

                case 16:
                    dataType = typeof(UInt16);
                    break;

                case 32:
                    dataType = typeof(UInt32);
                    break;

                case 64:
                    dataType = typeof(UInt64);
                    break;
                }
            }
            ValueConverter convert = ConverterMap[dataType];

            if (geoTiff.Tiff.IsTiled())
            {
                int tileCount       = geoTiff.Tiff.NumberOfTiles();
                int bytesPerTileRow = geoTiff.Tiff.TileRowSize();

                int tileWidth = 0, tileHeight = 0;
                tileWidth  = bytesPerTileRow / bytesPerSample;
                tileHeight = bytesPerTileRow / bytesPerSample;

                int tilesPerRow = Mathf.CeilToInt((float)width / tileWidth);

                int    bytesPerTile = geoTiff.Tiff.TileSize();
                byte[] buffer       = new byte[bytesPerTile];

                int   offset, index = 0;
                float progressStep = 1f / tileCount;
                for (int tile = 0; tile < tileCount; tile++)
                {
                    geoTiff.Tiff.ReadEncodedTile(tile, buffer, 0, bytesPerTile);

                    int tileX   = tile % tilesPerRow;
                    int tileY   = tile / tilesPerRow;
                    int xOffset = tileX * tileWidth;
                    int yOffset = tileY * tileHeight;
                    index = yOffset * width + xOffset;

                    offset = 0;
                    int w = Math.Min(width, xOffset + tileWidth) - xOffset;
                    int h = Math.Min(height, yOffset + tileHeight) - yOffset;
                    for (int r = 0; r < h; r++)
                    {
                        for (int c = 0; c < w; c++)
                        {
                            grid.values[index++] = convert(buffer, offset);
                            offset += bytesPerSample;
                        }
                        index  += width - w;
                        offset += (tileWidth - w) * bytesPerSample;
                    }

                    progress.value += progressStep;
                }
            }
            else
            {
                int    scanlineSize = width * bytesPerSample;
                byte[] buffer       = new byte[scanlineSize];

                int   offset, index = 0;
                float progressStep = 1f / grid.countY;
                for (int r = 0; r < height; r++)
                {
                    offset = 0;
                    geoTiff.Tiff.ReadScanline(buffer, r);
                    for (int c = 0; c < width; c++)
                    {
                        grid.values[index++] = convert(buffer, offset);
                        offset += bytesPerSample;
                    }

                    progress.value += progressStep;
                }
            }

            progress.value = 1;

            grid.AddMaskValue((float)geoTiff.NoDataValue);

            var citation = geoTiff.Citation;
            if (!string.IsNullOrWhiteSpace(citation))
            {
                grid.AddMetadata("Citation", citation);
            }

            data = grid;
        }

        return(true);
    }