How do I map/plot my polygon onto a Europe map in MATLAB? - matlab

I have a shapefile with a transverse mercator projection. It also has false northings/eastings values associated with it in ArcGIS.
I am trying to map the polygons onto a Europe map, but they are not showing up on it. When I use worldmap world the polygons show up in Africa, even though they are supposed to be in Ireland.
How can I plot my polygons in the right place? They have latitudes and longitudes associated with them, so I think it has to be the projection.
S = shaperead(polygons.shp','UseGeoCoords', true)
h = axesm('tranmerc','FalseEasting',60000,'FalseNorthing',750000,'Grid','on','Frame','on', 'MlabelParallel',0,'PlabelMeridian',0)
When I try to plot the latitudes and longitudes, they end up in Africa:
h = plot([S.Lat],[S.Lon], 'k:');
Or when I try and use geoshow they fill up the map completely with a color:
geoshow(S,'FaceColor',[1 1 .5],'EdgeColor',[.6 .6 .6]);

Related

How to attribute intersection points between poly and lineseg to poly

I'm trying to plot an animal's trajectory from a set of coordinates as a line segment. I want to see how many coordinates are plotted inside a circular zone vs outside. I think that coordinates which intersect with the circle are being counted as both inside and outside, but I would like them to be strictly counted as inside. This is what I have so far:
[in,out] = intersect(circle_poly,trajectory);
plot(circle_poly)
hold on
plot(in(:,1),in(:,2),'b',out(:,1),out(:,2),'r')
legend('Polygon','Inside','Outside','Location','NorthWest')
num_frames_in = numel([in]) %count num elements/frames in polygon
num_frames_out = numel([out]) %count num element/frames outside polygon
total_frames = num_frames_in + num_frames_out
Any help would be really appreciated since I'm new to Matlab!

I would like to label the points with values in polar plot in Matlab

Example:
I have
radians = [0:2pi];
and radius
values = [-200 -180 -160.....-100]; polarplot(radians,values)
So i have values after each 18 degrees. I want to label the plotted points with values instead of one value on different radius circles.
Sample illustration:
Have a look at this link. You can label the points individually with the function text().

How to convert from the image coordinates to Cartesian coordinates

I have this 3D image generated from the simple code below.
% Input Image size
imageSizeY = 200;
imageSizeX = 120;
imageSizeZ = 100;
%# create coordinates
[rowsInImage, columnsInImage, pagesInImage] = meshgrid(1:imageSizeY, 1:imageSizeX, 1:imageSizeZ);
%# get coordinate array of vertices
vertexCoords = [rowsInImage(:), columnsInImage(:), pagesInImage(:)];
centerY = imageSizeY/2;
centerX = imageSizeX/2;
centerZ = imageSizeZ/2;
radius = 28;
%# calculate distance from center of the cube
sphereVoxels = (rowsInImage - centerY).^2 + (columnsInImage - centerX).^2 + (pagesInImage - centerZ).^2 <= radius.^2;
%# Now, display it using an isosurface and a patch
fv = isosurface(sphereVoxels,0);
patch(fv,'FaceColor',[0 0 .7],'EdgeColor',[0 0 1]); title('Binary volume of a sphere');
view(45,45);
axis equal;
grid on;
xlabel('x-axis [pixels]'); ylabel('y-axis [pixels]'); zlabel('z-axis [pixels]')
I have tried plotting the image with isosurface and some other volume visualization tools, but there remains quite a few surprises for me from the plots.
The code has been written to conform to the image coordinate system (eg. see: vertexCoords) which is a left-handed coordinate system I presume. Nonetheless, the image is displayed in the Cartesian (right-handed) coordinate system. I have tried to see this displayed as the figure below, but that’s simply not happening.
I am wondering if the visualization functions have been written to display the image the way they do.
Image coordinate system:
Going forward, there are other aspects of the code I am to write for example if I have an input image sphereVoxels as in above, in addition to visualizing it, I would want to find north, south east, west, top and bottom locations in the image, as well as number and count the coordinates of the vertices, plus more.
I foresee this would likely become confusing for me if I don’t stick to one coordinate system, and considering that the visualization tools predominantly use the right-hand coordinate system, I would want to stick with that from the onset. However, I really do not know how to go about this.
Right-hand coordinate system:
Any suggestions to get through this?
When you call meshgrid, the dimensions x and y axes are switched (contrary to ndgrid). For example, in your case, it means that rowsInImage is a [120x100x200] = [x,y,z] array and not a [100x120x200] = [y,x,z] array even if meshgrid was called with arguments in the y,x,z order. I would change those two lines to be in the classical x,y,z order :
[columnsInImage, rowsInImage, pagesInImage] = meshgrid(1:imageSizeX, 1:imageSizeY, 1:imageSizeZ);
vertexCoords = [columnsInImage(:), rowsInImage(:), pagesInImage(:)];

convert pixel coordinates to map coordinates

I have an image A of dimension p x q. If I know the UTM coordinate of A(1,1) and A(p,q) and pixel size in meters.
How to convert the pixel coordinates to map coordinates in MATLAB?
Xsize = (1:p)*PixelSizeInMeter+UTM_x_onA11;
Ysize = (1:q)*PixelSizeInMeter+UTM_y_onA11;
figure;
surface(Xsize,Ysize,A);
Now you can plot your map using Xsize and Ysize. Since UTM is a Cartesian grid, life's quite easy: get the correct number of elements, multiply with the grid size and add the lower corner's coordinates to shift the plot to the correct location.

Plotting intersection of planar surfaces in matlab

I am looking to plot the intersection of two surfaces(patches) lying the same plane in MATLAB.
As you can see in the above picture the green circle intersects four red rectangles.I want to plot out(or patch) only the four intersections.How do I proceed?
I tried to plot points on the circular patch which lies outside the intersection of one rectangle and circle using conditional statements.But MATLAB throws an error.Here is the code snippet.
[p,q] = size(points);
for s=1:1:q;
t = points(1,s);
if (points(1,s) >= Pa3(1,1)) && (points(1,s) <= Pa2(1,1)) && (points(2,s) >= Pa3(1,2)) && (points(2,s) <= Pa4(1,2))
points(1,s) = 0;points(2,s) = 0;
end
end
fill3(points(1,:), points(2,:), points(3,:), 'g');
The above code throws an error at if statement.Basically in the code "points" represent all the points in the green circle.Pa1,Pa2,Pa3,Pa4 represent the vertices of the left top corner rectangle with Pa1 being the left corner top vertex and Pa2,Pa3,Pa4 following in clockwise manner.
Thanks
Patches are defined by polygons, and what you are looking for is the intersection of two polygons which itself is a polygon.
If you have the Mapping Toolbox, you can use polybool to compute the intersection and other logical operations on polygons.
If not, have a look at the submission Polygon_Intersection on the Matlab File Exchange.
See also intersection and union of polygons.