2

我正在寻找一种“干净”的方式将栅格加载到我的 c# 代码中。我所说的栅格是指 QGis 或 ArcGis 能够加载的任何(或至少一些)XYZ 文件:.tif、.rrd ...

我正在使用NetTopologySuite. 这个库非常适合 shapefile,这让我觉得可能有一个光栅阅读器。我一直试图将我的研究集中在NetTopologySuite.IO包含很多读者的命名空间下。

我已经标记了这篇文章,NetTopologySuite希望一些精通 c# 的人比我更了解这个特定的库。

4

1 回答 1

2

我将 LibTiff.Net 用于 .tif,但我不需要任何其他格式。UpperLeft 和 BottomRight 属性用于在世界地图上定位图像。我不处理模型转换案例,因为它非常复杂,而且 - 再次 - 目前不需要这样做。

        var imageStreamSource = new FileStream(Filename, FileMode.Open, FileAccess.Read, FileShare.Read);
        var decoder = new TiffBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
        var bitmapSource = decoder.Frames[0];

        // Draw the Image
        Image = bitmapSource.ToBitmap();

        using (var tiff = Tiff.Open(Filename, "r"))
        {
            Height = tiff.GetField(TiffTag.IMAGELENGTH)[0].ToInt();
            Width = tiff.GetField(TiffTag.IMAGEWIDTH)[0].ToInt();

            // see http://www.remotesensing.org/geotiff/spec/geotiff2.6.html#2.6.1

            var modelPixelScaleTag = tiff.GetField(TiffTag.GEOTIFF_MODELPIXELSCALETAG);
            var modelTiepointTag = tiff.GetField(TiffTag.GEOTIFF_MODELTIEPOINTTAG);
            //var modelTransformTag = tiff.GetField(TiffTag.GEOTIFF_MODELTRANSFORMATIONTAG);

            var modelPixelScale = modelPixelScaleTag[1].GetBytes();
            var ScaleX = BitConverter.ToDouble(modelPixelScale, 0);
            var ScaleY = BitConverter.ToDouble(modelPixelScale, 8) * -1;

            if (modelTiepointTag != null)
            {
                var modelTransformation = modelTiepointTag[1].GetBytes();
                var originLon = BitConverter.ToDouble(modelTransformation, 24);
                var originLat = BitConverter.ToDouble(modelTransformation, 32);

                // origin is the center of the pixel, so offset to
                var startLat = originLat + (ScaleY / 2.0);
                var startLon = originLon + (ScaleX / 2.0);

                UpperLeft = new Position(startLat, startLon);
                BottomRight = new Position(startLat + ScaleY * Height, startLon + ScaleX * Width);
            }
            //else if (modelTransformTag != null)
            //{
            // this is very complicated
            //}
            else
            {
                throw new Exception("Couldn't understand the TIFF format");
            }
        }
于 2016-08-05T03:01:03.647 回答