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.

2
  • 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 Apr 19, 2016 at 7: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. Apr 19, 2016 at 7:48

1 Answer 1

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");
            }
        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.