2

I am looking for a "clean" way to load rasters to my c# code. By raster I mean any (or at least some) XYZ file that QGis or ArcGis are able to load: .tif, .rrd ...

I am using NetTopologySuite. This library works perfectly for shapefiles, which makes me think there could be a raster reader. I have been trying to focus my research under the NetTopologySuite.IO namespace, which contains quite a few Readers.

I have tagged this post with NetTopologySuite hoping that some c#-savvy knows a bit more than me about this particular library.

Xavier Peña
  • 7,399
  • 9
  • 57
  • 99
  • I'm voting to close this question as off-topic because I doubt this question has anything to do with programming and in particular c#, thus I recommend post it here: gis.stackexchange.com – MakePeaceGreatAgain Apr 19 '16 at 07:41
  • 4
    @HimBromBeere In that case I'll try to keep it up by focusing the question about `NetTopologySuite` only (which Stackoverflow has a tag for). My problem is purely related to c#, and this is why I chose Stackoverflow. I am not saying I completely disagree, but I think it is at least a bit nuanced. – Xavier Peña Apr 19 '16 at 07:48

1 Answers1

2

I used LibTiff.Net for .tif, but I had no requirement for any other format. The UpperLeft and BottomRight properties are used to position the Image on a world map. I don't deal with the model transform case because it's very complicated and - again - no requirement to do so at the moment.

        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");
            }
        }
metao
  • 2,784
  • 1
  • 18
  • 15