plot a map with labels for city names in MATLAB - matlab

I want to plot a map with labels for city names. I know how to plot polygons from a shapefile but how do I label it with place names? Would these be in the shapefile or one of the other files included in the zipfile?
my_map = shaperead('myshapefile.shp');
Zone1 = rgb('Red');
Zone2 = rgb('Purple');
Zone3 = rgb('Orange');
Zone4 = rgb('Yellow');
Zone5 = rgb('SpringGreen');
Zone6 = rgb('DeepSkyBlue');
mapColors = makesymbolspec('Polygon',{'GRID_CODE',1,'Facecolor',Zone1,'FaceAlpha',0.5},...
{'GRID_CODE',2,'Facecolor',Zone2,'FaceAlpha',0.5},{'GRID_CODE',3,'Facecolor',Zone3,'FaceAlpha',0.5},{'GRID_CODE',4,'Facecolor',Zone4},...
{'GRID_CODE',5,'Facecolor',Zone5,'FaceAlpha',0.5},{'GRID_CODE',6,'Facecolor',Zone6,'FaceAlpha',0.5});
mapshow(my_map,'SymbolSpec',mapColors,my_labels);
axis off

Related

Python - join/understand .txt in .nc

I need to plot together the salinity in background and ocean coordinates as a polygon (identifying the basin). Find a manner the .nc recognize my .txt, and separete only the basin identificate.
For example, I have salinity (.nc) all the oceans and the coordinates of Indian Ocean (.txt). So, I need identify only the coordinates the Indian Ocean at the .nc and separate this basin/data to my future analysis
I have two files:
salinity.nc (georeferenced salinity data in Latxlon)
#salinity (.nc)
salinity = xr.open_dataset('...salinity.nc')
salinity
salinity.coords['lon'] = ((salinity.coords['lon'] + 180) % 360) - 180
salinity = salinity.sortby(salinity.lon)
lon = salinity['lon']
lat = salinity['lat']
fig=plt.figure(figsize=(10,8))
#plot
ax = fig.add_subplot(111, projection=crs.PlateCarree())
im = ax.contourf(lon, lat, salinity, cmap="Spectral_r", extend="both")
ocean.txt (a list with 666 rows x 2 columns, in each row a latxlon point)
#indianocean (.txt)
indian = pd.read_csv('.....indianocean.txt', sep=';')
indian
#plot
fig = plt.figure(figsize = (12,12))
m = Basemap()
m.plot(indian.lon, indian.lat, marker=None,color='blue')
Updating...
I'm using a shapefile to test, could plot the shape in a map, but I need to extract the salinity data only from my shapefile coordinates.
salinity = xr.open_dataset('...salinity.nc')
indshp = gpd.read_file('....indcoord.shp')
#plot
fig=plt.figure(figsize=(10,8))
ax = fig.add_subplot(111, projection=ccrs.PlateCarree())
ax.add_geometries(indshp.geometry, crs=ccrs.PlateCarree(), facecolor='none', edgecolor='red', lw =2)
im = ax.contourf(lon, lat, salinity, cmap="Spectral_r", extend="both")
Now, how I could use the salinity data only of the shapefile?
Thanks so much!

Add multiply shapefiles in one figure (MATLAB)

I have 4 shape file. I know how to read them using this code:
% first shape file is for whole country
Country = 'country.shp'
S_country = shaperead (country);
M_country = mapshow (S_country); % >>>> I want to be in a white with black border <<<< can you please tell me how to?
% Second shape file for a sea in the north
north_sea = 'caspian sea.shp'
S_north_sea = shaperead (north_sea);
M_north_sea = mapshow (S_north_sea ); % I want to be in blue with no border
% Third shape file for a gulf in the south
Sout_gulf = 'persiangulf.shp'
S_Sout_gulf = shaperead (Sout_gulf);
M_Sout_gulf = mapshow (S_Sout_gulf); % I want to be in blue with no border
% Fourth Shape file for a sea in the south
south_sea = 'oman sea.shp'
S_south_sea = shaperead (south_sea);
M_south_sea = mapshow (S_south_sea); % I want to be in blue with no border
But I don't know how to have all of them in the one figure to have a nicely figure of a country with sea in north and south with colors that I mentioed in the comments of code.
I attach all 4 shape file here in my google drive.
Thank you.
Update
Thanks to your guidance in a comment below, I used this:
But still, I can't have all the figures in one
country = 'country.shp'
S_country = shaperead (country);
north_sea = 'caspian sea.shp'
S_north_sea = shaperead (north_sea);
figure
mapshow (S_country);
hold on
mapshow (S_north_sea );

Region mask in the center - Matlab

In the script below I create a mask based on coordinates and plot them in the original position and also starting at position 0,0. How can I plot another region mask (mask1venter) center?
Code:
xCoord = [354 500 100 363];
yCoord = [309 500 600 360];
if max(xCoord)>max(yCoord)
matrixLength = max(xCoord);
else
matrixLength = max(yCoord);
end
xCoordMin = xCoord-min(xCoord);
yCoordMin = yCoord-min(yCoord);
xCoordCenter = xCoord-round((max(xCoord))/2);
yCoordCenter = yCoord-round((max(yCoord))/2);
mask1 = poly2mask(yCoord,xCoord,matrixLength,matrixLength);
mask1Min = poly2mask(yCoordMin,xCoordMin,matrixLength,matrixLength);
mask1Center = poly2mask(yCoordCenter,xCoordCenter,matrixLength,matrixLength);
imshowpair(mask1,mask1Min)
You can either use subplot, add the two masks or logical OR the two masks
Subplot
figure
subplot(2,2,1)
imshow(mask1)
subplot(2,2,2)
imshow(mask1Min)
subplot(2,2,3)
imshow(mask1Center)
Add the two image
figure
imshowpair(mask1,mask1Min + mask1Center)
Logical OR the two masks
figure
imshowpair(mask1,mask1Min | mask1Center)

Saving the image names and bounding box coordinates in different file formats in Matlab

I've written a Matlab code to crop an image with respect to a mask.
function Crop_Img = Crop_Xray(Img,Mask)
% Find borders
vertical_profile = sum(Mask,2);
horizontal_profile = sum(Mask);
indexes = find(vertical_profile >0);
upper = indexes(1);
lower = indexes(end);
indexes = find(horizontal_profile > 0);
left = indexes(1);
right = indexes(end);
% Crop Img
Crop_Img = Img(upper:lower, left:right);
end
I would like to save the individual image file names (*.png) and their respective bounding box co-ordinates [upper: lower, left: right] exactly in the format I have mentioned, in adjacent columns to a .xls, .txt, and .mat file. How should I do that?

unable to draw a line correctly using hough transformation in matlab

I used this code for drawing lines using Hough tranform. In this code, there is a draw of the image in Polar space . I want to draw detected lines in Cartesian space. Thus, with the help of this explanation I wrote my code like this:
imBinary = flipud(edge(rgb2gray(im),'sobel'));
[imWidth,imLenght]=size(imBinary);
[interestPointY , interestPointX] = find(imBinary);
numberOfInterestingPoints=numel(interestPointX);
maxRadius=sqrt(imWidth^2 + imLenght^2);
polarRadius=(-maxRadius:1:maxRadius);
polarTheta=(-pi/2:(1/200):pi/2);
numberOfPolarThetas=numel(polarTheta);
houghMatrix=zeros(numel(polarRadius),numberOfPolarThetas);
polarAccumulator = zeros(numberOfInterestingPoints,numberOfPolarThetas);
cosine = (0:imWidth-1)'*cos(polarTheta);
sine = (0:imLenght-1)'*sin(polarTheta);
polarAccumulator((1:numberOfInterestingPoints),:) =cosine(interestPointY,:) + sine(interestPointX,:);
for i = (1:numberOfPolarThetas)
houghMatrix(:,i) = hist(polarAccumulator(:,i),polarRadius);
end
radiusDetect=[];angleDetect=[];
houghBinaryMax = imregionalmax(houghMatrix);
[Potential_radius ,Potential_angle] = find(houghBinaryMax == 1);
%[radiusIndex angleIndex] = find(houghMatrix > 0);
houghTmp= houghMatrix - lineThreshold;
for cnt = 1:numel(Potential_radius)
if houghTmp(Potential_radius(cnt),Potential_angle(cnt)) >= 0
radiusDetect = [radiusDetect;Potential_radius(cnt)];
angleDetect = [angleDetect;Potential_angle(cnt)];
end
end
radius=polarRadius(radiusDetect);
deg=polarTheta(angleDetect);
%find two points in cartesian space
x=radius.*cos(deg);
y=radius.*sin(deg);
x1=x+5.*cos(deg);
y1=y+5.*cos(deg);
imshow(imBinary);
hold on;
plot([x x1],[y y1]);
Unfortunately I did not get the correct lines and my image in like this:the blue lines are detected
My question is what the mistake is that I cannot draw lines correctly?