Change the geospatial data in a geotiff file.
Show older comments
Hello,
my goal is to change the geospatial data of a geotiff file.
In other words, I want to relocate the image to another coodrinates.
More precisely, we have a test-track and I want to bring a piece of a real world road on it.
So when I import the image to the Roadrunner, it shows up where I need it.
How I did it:
1. I read both the image and data separately
data = geotiffinfo(filename);
image = imread(filename);
2. I defined a matrix that I needed to apply to the image
matrix = [0.990950218730870 0.136163909329197 0
-0.136163909329197 0.990950218730870 0
741682.197901774 -43966.3723596139 1];
3. I created a tform object
tform = affine2d(matrix);
4. I created an object that connects the spatial data and pixels.
xWorldLimits = [data.BoundingBox(1, 1) data.BoundingBox(2, 1)];
yWorldLimits = [data.BoundingBox(1, 2) data.BoundingBox(2, 2)];
ref_data = imref2d(size(image), xWorldLimits, yWorldLimits);
5.I applied the matrix to the image and geo-spatial data
[upd_image, upd_data] = imwarp(image, ref_data, tform);
6. Reference object is needed to create a new tiff image.
Rmap = maprefcells(upd_data.XWorldLimits, upd_data.YWorldLimits, size(upd_image), ...
'ColumnsStartFrom','north', 'RowsStartFrom','west');
7. Finally create a file.
new_filename = ['ra_transformed' '.tif'];
geotiffwrite(new_filename, upd_image, Rmap, 'CoordRefSysCode', 25832);
In the end, it didn't work.
The image is rotated in another direction and moved not precisely.
It's close but not perfectly.
The matrix is correct, I've checked it twice.
Is there anything that I've missed?
Or maybe the overall approach of dividing, changing and gluing it back is wrong?
Answers (1)
Rushan Abdrakhimov
on 9 Feb 2022
0 votes
Categories
Find more on Create Plots on Maps in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!