how to set background of the color map with shape file/image in hand

45 views (last 30 days)
Hi every one; I want to make color map from text file which contains three columns( 1st is latitude, 2nd is longitude, 3rd is temperature). I have accessed to this code which reading and creating color map from text file(1-1000.text)
fid = fopen('1-1000.txt');
C = textscan(fid, '%f %f %f')
fclose(fid);
f = {'lat', 'long', 'temp'}
S = cell2struct(C,f,2);
N = 1000;
[Xi, Yi] = meshgrid(linspace(60,80,N),linspace(20,40,N));
Ci = griddata(S.long, S.lat, S.temp, Xi, Yi);
colormap ('hot')
imagesc(linspace(60,80,N),linspace(20,40,N),Ci);
xlabel('Longitude');
ylabel('Latitude');
colorbar
it has output
The data in text file represent region , i have shape file of that region i want to set this region as the background of this color map. I have attached with this post text data, my requried output map and shape file of the region. Please set my code so that background of the map will become region of my shapefile. Thanks in advance for assistance. Regards
  15 Comments
Muhammad Usman Saleem
Muhammad Usman Saleem on 24 Jul 2015
@Walter thanks for your contributions. Please vote up if my question was interesting :)

Sign in to comment.

Accepted Answer

Amy Haskins
Amy Haskins on 21 Jul 2015
Once you've gotten your data into a regular grid of latitudes and longitudes as you've done above, the geoshow function in the Mapping Toolbox can help with the rest.
First use geoshow to get your surface plot into a map axes. If you don't already have a map open, geoshow will create a default one. If you would perfer a different projection, take a look at the doc for axesm.
h = geoshow(Yi,Xi,Ci,'DisplayType','surface');
This creates an actual surface plot, but you're after a 2D map, so let's flatten out the ZData.
h.ZData = zeros(size(h.XData));
Your shape file contains polygons. You have a couple of options here. I think it looks nice to overlay just the edge lines, but you could play with the transparency and fill in the polygons.
p = geoshow('pakistanshapefile\PAK_adm1.shp', ...
'DisplayType','polygon','FaceColor','none','EdgeColor','w')
Or
p = geoshow('pakistanshapefile\PAK_adm1.shp',...
'DisplayType','polygon','FaceAlpha',0.25)
Alternately, you could move the polygons to the bottom using uistack(p,'bottom') and change the FaceAlpha property of the surface if you really want the shape file in the background.
  4 Comments
Muhammad Usman Saleem
Muhammad Usman Saleem on 24 Jul 2015
@Amy Haskins thank you very much for resolving this problem :) Please vote up my question

Sign in to comment.

More Answers (1)

Nitin Khola
Nitin Khola on 21 Jul 2015
As per my understanding, you wish to set the background of the given colormap to a map outline imported from a shapefile. I am assuming you have a license for the MAPPING TOOLBOX and are using MATLAB R2015a. To achieve it, as per this assumption, you can use the function "shaperead":
to import the shapefile and then overlay the given colormap on a plot of data from the shapefile. The transparency of the colormap can be changed using "alpha" command so that the outline is visible. I was able to do it using the following code:
im = shaperead('PAK_adm1.shp'); % import the shapefile
plot([im.X],[im.Y],'k','LineWidth',2.0); % plot X,Y data
hold on;
fid = fopen('1-1000.txt');
C = textscan(fid, '%f %f %f');
fclose(fid);
f = {'lat', 'long', 'temp'};
S = cell2struct(C,f,2);
N = 1000;
[Xi, Yi] = meshgrid(linspace(60,80,N),linspace(20,40,N));
Ci = griddata(S.long, S.lat, S.temp, Xi, Yi);
colormap ('hot')
imagesc(linspace(60,80,N),linspace(20,40,N),Ci);
xlabel('Longitude');
ylabel('Latitude');
colorbar
alpha(0.85) % Change transparency of the colormap so that map is visible
You might want to change some of the values in the above code as per your needs.
  7 Comments
Muhammad Usman Saleem
Muhammad Usman Saleem on 25 Jul 2015
@Nitin Khola , If i want to label features( actually want to label districts in PAK.shp). Kindly guide me how i correct this in 1st line of your code? Thanks in advance for this kind assistance

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!