Code covered by the BSD License  

Highlights from
geotiffwrite

4.9

4.9 | 10 ratings Rate this file 56 Downloads (last 30 days) File Size: 24 KB File ID: #27959

geotiffwrite

by

 

19 Jun 2010 (Updated )

Write a 2D or 3D array to a single or multi-band GeoTIFF file

| Watch this File

File Information
Description

This program can write a 2D or 3D array to a single or multi-band GeoTIFF file. For more information and examples, please visit:
http://research.baycrest.org/~jimmy/geotiffwrite
or type: help geotiffwrite

MATLAB's Mapping Toolbox only provides a "geotiffread" function, but it does not have a "geotiffwrite" function (Note). This is the MATLAB program to write a 2D or 3D array to a single or multi-band GeoTIFF file, where data can be either 1-bit monochrome data (i.e. binary or bilevel), 8-bit unsigned integer, 16-bit signed / unsigned integer, 32-bit signed integer, or 32-bit floating point.

Note: Starting from version R2011a, MATLAB also provides its own "geotiffwrite" function
(http://www.mathworks.com/help/toolbox/map/rn/bsq4us7-1.html#bsu1ro5-1).

This program is based on GeoTIFF Format Specification under:
http://www.remotesensing.org/geotiff/spec/geotiffhome.html
or http://download.osgeo.org/geotiff/spec

It does not need MATLAB's Mapping Toolbox, or any other library.

Usage:
[option bbox] = make_option([bbox]);
geotiffwrite(filename, bbox, image, [bit_depth, option]);

For more information on arguments and detail examples, please visit:
http://research.baycrest.org/~jimmy/geotiffwrite
or type: help geotiffwrite

Acknowledgements

This file inspired Utm2 Lat Long Geo Tiff Converter.

MATLAB release MATLAB 5.3.1 (R11.1)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (29)
06 Jun 2014 Sharon Veerayah

Do i need a special toolbox to access geotiffread and geotiffwrite?
I can't find either using help geotiffread or write.
I get this error: Undefined function 'geotiffread'
I am using R2014a

Thank you.

31 Aug 2013 yann

Jimmy,

sorry i just realized my mistake. I read your code too fast and kept using -8 for 8-bit unsigned integer instead of 8 (i thought that the 'minus' sign meant unsigned).

Everything works fine now. Thanks again for this great piece of work!

31 Aug 2013 yann

Jimmy, thanks for your answer.

I got confused because the code runs into an error at line 1108 when the bit depth is set to -8. This seems to be because this case is not considered at lines 828-837, and the 'ifd.SampleFormat' variable is not created.
However, if i replace line 831 with 'case {-8, 8, -16}' then the code seems to run just fine.

Is it correct or did i miss something?

Thanks
Yann

30 Aug 2013 Jimmy Shen

No, it is not a typo.

"bit_depth" value -16 & -32 were designed to expand datatype to "16-bit unsigned integer" & "32-bit signed integer".

If "bit_depth" is -8, it will be treated as 8 anyway, since only abstract value is considered. However, if it is 1 or -1, according to TIFF Revision 6.0 (Final-June 3, 1992), "SampleFormat" field has to be set to "undefined data format", since it is binary monochrome data.

If you look further down a little bit to line 932, you will find how I write data strip based on "bit_depth" value.

30 Aug 2013 yann

Hi Jimmy,

Thanks for the great code! It's really useful for those who have not switched to the new Matlab version.

I just would like to suggest a correction. I'm not sure but I think that there is a typo at line 829. Shouldn't it be 'case {1, -8}' instead of 'case {1, -1}' ?

Thanks,
Yann

07 Aug 2013 Jimmy Shen

Please check: http://research.baycrest.org/~jimmy/geotiffwrite

You need to add:

option.ColorMap [Nx3 array]

Where N = 2^bit_depth, and bit_depth can only be 8 or 16. Values in option.ColorMap range from 0 to 65535, and value 0 in image points to the first row in the ColorMap. Columns in option.ColorMap are [R G B]. Black is represented by [0 0 0], and white is represented by [65535 65535 65535].

07 Aug 2013 Val Schmidt

Jimmy,
I'm having some trouble with your code and need some advice. I'm trying to read a JPEG image, and write it as a GeoTiff. Specifically, I'm unable to properly specify a colormap to retain the color in the resulting tif. I suspect, I simply don't understand how to properly specify the "options".

I'm not explicitly specifying a colormap (see code snippet below) and the resulting tiff is black and white (from an original color image).

I've tried using rgb2ind to create a colormap and apply it and I either get an unreadable image or a black and white one whose colors are scrambled.

Do you have any suggestions on how to do this properly?

Thanks,
Val

% Read the image
I = imread('inputimage,jpg');
outfile = 'outputimage.tif');

% Set the options:
option.GTModelTypeGeoKey = 1;
option.ModelTiepointTag = [0, 0, 0, P(1,:),...
I_info.Height, 0,0, P(2,:),...
I_info.Height, I_info.Width, 0, P(3,:),...
0, I_info.Width, 0, P(4,:)];
option.GeogEllipsoidGeoKey = 7030; % WGS-84
option.PCSCitationGeoKey = ['UTM Zone ' zone ' with WGS84'];

if zone(end) >= 'N'
option.ProjectedCSTypeGeoKey = str2double(['326' zone(1:2)]);
else
option.ProjectedCSTypeGeoKey = str2double(['327' zone(1:2)]);
end

% Write the file:
geotiffwrite(outfile,[],I,8,option)

12 Dec 2012 Sunke

thanks for this export option! It helps sharing my MatLab data with colleagues that do not use MatLab, but ArcGIS, a lot !

23 Jul 2012 Kyle Mayer  
25 May 2012 Jesse Angle

In order to make this code fully support the ModelTransformationTag the following changes are necessary:

line 873: comment out
error('ModelPixelScale is required...');

as this isn't necessarily a true statement and will cause problems when trying to use only the ModelTransformationTag.

line 1213 - 1214:
fwrite(fid, gifd.ModelPixelScaleTag,'double'); and
fwrite(fid, gifd.ModelTiePointTag, 'double');

should be surrounded by
if isfield(gifd,'Model...Tag')

end

statements for when these tags are not being used as is the case with ModelTransformationTag.

Finally, add in the code that actually writes the ModelTransformationTag portion (previously missing). Mine looks like this:

if isfield(gifd,'ModelPixelScaleTag')
fwrite(fid,gifd.ModelPixelScaleTag,'double');
end
if isfield(gifd,'ModelTiePointTag')
fwrite(fid,gifd.ModelTiePointTag,'double');
end
if isfield(gifd,'ModelTransformationTag')
fwrite(fid,gifd.ModelTransformationTag,'double')
end

That being said, great work Jimmy getting this put together and thank you for your efforts.

27 Apr 2012 Carlo M

thank you very much Jimmy

26 Apr 2012 Jimmy Shen

You need to use MATLAB's geotiffread to read L7 file into matrix, with all attributes. Then find out the index of your sub-region, and assign the sub-region to a new matrix. You can now use the make_option option in my package to select optional fields. However, you need to match the attributes that you read, and select the correct fields by yourself in order to create the optional fields to be used in my geotiffwrite. Since I don't have Mapping toolbox, I am afraid that I cannot provide you with more details.

26 Apr 2012 Carlo M

thank you for your response... i'm trying to select a region of a Landsat 7 geotiff image and then to save it with all its atributes. Please, how can I select the rectangular window?

26 Apr 2012 Jimmy Shen

My geotiffwrite can only write a rectangular dataset into the file. As long as your sub-region is a rectangular dataset, you should be able to write it into a separate file.

26 Apr 2012 Carlo M

there is possible to select a sub-region of a geotiff image? i'm trying to do that but it seems not possible

28 Mar 2012 Jimmy Shen

MATLAB finally has its own "geotiffwrite" function. So I suggest you use its geotiffread / geotiffwrite pair to load your Antarctica background, and superimpose your ship track data on top, and then re-write the entire data into GeoTIFF format using Matlab's geotiffwrite function.

I am currently no longer working in GeoScience field, so I am afraid that I cannot provide you with more useful advise.

27 Mar 2012 Kaycee

Hi Jimmy, I found your post very informative, so I was hoping you could help me with a problem. I'm having issues attempting to plot a ship track on top of an already made geotiff image from Antarctica. The issue i believe is that it is stereographic, so i cannot use geotiff2mstruct. I am able to get the track on the ice image using profwd however the track is not exactly in the right spot. Also I cannot figure out how to get the latitude and longitude on the axes, the file has intrinsic or world x,y. Any advice?

22 Nov 2011 Evan

Thanks for the fast response. I changed the colormap to include 256 elements and got it working. Seems to be a very useful script although it requires some troubleshooting with the 'option' inputs. Thank you!

22 Nov 2011 Jimmy Shen

Please notice that the values in colormap range from 0 to 65535 (instead of from 0 to 1). So black is represented by [0 0 0], and white is represented by [65535 65535 65535]. If you put [1 1 1], it will be black. Please see more details from my web site: http://www.rotman-baycrest.on.ca/~jimmy/geotiffwrite

22 Nov 2011 Evan

I've been playing around with this code for a bit, but I'm having trouble with colormaps. If I use a standard colormap (which I am currently using to write standard tifs of my data), I get a solid black image. Any advice of support on the specific data format/colormap format required to successfully created a colored geotiff?

05 Jul 2011 Jimmy Shen

If Model Type is "projected", you cannot use "bbox" variable directly, and you have to use "make_option.m" program to generate "option" variable.

05 Jul 2011 Matthew

great program. If the Model Type is not geographic, it clears the bbox variable ? My model type is "projected". So what should happen there? Thanks Matt

24 Apr 2011 E

Excelent job! Works with Octave too. Just replace '&' with '&&' in lines 257 and 1165.

13 Sep 2010 Jimmy Shen

Hi David:

SRTM (Shuttle Radar Topography Mission) data are nothing but Big-Endian, 16-bit Signed Integer binary numbers with either in 1201x1201 (SRTM3) or in 3601x3601 (SRTM1) dimension. You can simply use the following script to do the job:

function srtm=load_srtm(fn)
fid=fopen(fn,'rb','ieee-be');
srtm.dat=fread(fid,inf,'*int16');
if length(srtm.dat)==1442401
srtm.dat=reshape(srtm.dat,[1201 1201]);
elseif length(srtm.dat)==12967201
srtm.dat=reshape(srtm.dat,[3601 3601]);
end
srtm.dat=rot90(srtm.dat);

Please let me know if you encounter any problem.

Jimmy

09 Sep 2010 David Davenish

Very helpful program, Thank you!

I have a question though, where can I find the "load_srtm" command that you examplhttp://www.mathworks.com/matlabcentral/fileexchange/27959-geotiffwrite#ed in link http://www.rotman-baycrest.on.ca/~jimmy/geotiffwrite in "Description"?

17 Aug 2010 Jimmy Shen

Hi David:

Thanks for you comments and rating.

Yes, you can use some of the fields contents returned by geotiffinfo to provide information for my option struct when you run the "make_option.m" program. However, please be careful, since my struct fields are DIRECTLY correspond to GeoTIFF Format Specification (like IDL software does); while struct fields from geotiffinfo in MATLAB software are NOT DIRECTLY correspond to GeoTIFF Format Specification. Here's a simple example: you can use geotiffinfo.TiePoints.WorldPoints for TiepointXYZ; but you cannot use geotiffinfo.TiePoints.ImagePoints for TiepointIJK.

I have just submitted 1-bit GeoTIFF support, so please come back in 2~3 days to see if they make my program available for you.

Jimmy

17 Aug 2010 David

Great capability that Matlab has needed for a long time. I found that I can use the struct returned by geotiffinfo as the option struct for the input. I'd like to write a 1-bit geotiff. Any pointers would be appreciated.

07 Aug 2010 Karl

MATLAB has been needing this for years. Thanks Jimmy. I really like the flexibility of this program.

22 Jul 2010 Zina  
Updates
25 Jun 2010

It now supports to write any kind of data info a GeoTIFF file. Examples are provided.

20 Jul 2010

1. 3D array is supported to write Multi-band GeoTIFF data; 2. "make_option" program is created to provide a GUI window to generate option argument for "geotiffwrite" program;

17 Aug 2010

1-bit GeoTIFF is supported

06 Mar 2012

Full Color (24-bit RGB) GeoTIFF is supported

04 Dec 2012

Thanks to Jesse Angle for the detail fix of ModelTransformationTag bug.

Contact us