Plot markers on map

16 次查看(过去 30 天)
Eli
Eli 2023-9-21
Dear all,
I require help as I am new at this. How do I do the following?
  1. I have plotted the map of Malaysia and it's districts. The districts are obtained from the shapefile "polbnda_mys.shp". However, there is plenty of excess empty space surrounding the map. How do I get the red box in the figure?
  2. I would like to plot a marker at a lat and lon. I use geoplot for this but I am getting an error "Unable to read 'polbnda_mys.shp'. Format may not be supported, file may be corrupt, or a supporting file may have been specified." I am confused about this error as "geoshow" managed to plot the shapefile.
I have attached the figure for your reference.
Thank you very much!
% % Part 1 ========================================
figure;
worldmap('Malaysia');
geoshow("polbnda_mys.shp");
% % Part 2 ========================================
hold on;
states = readgeotable("polbnda_mys.shp");
geoplot(states,Color=[1 0 0]);
geoplot(2.6028,103.0569,'r');
  1 个评论
Walter Roberson
Walter Roberson 2023-9-21
When I test in R2023b:
  • there is no error about being unable to read the file
  • an error is given because states turns out to be a Polygon and there is no Color property for Polygon: you have to change the Color to FaceColor to avoid that problem
  • an error is given because geoshow() creates a patch object in a traditional (non-geographic) axes, but geoplot() is for plotting in a geographic axes. At the moment I do not know how to handle this (sorry, I don't use the Mapping toolbox much)

请先登录,再进行评论。

回答(1 个)

R
R 2023-11-15
编辑:R 2023-11-21
Hi Eli,
I understand that you are facing issue while plotting data from a .SHP file.
To resolve the error "Unable to read 'polbnda_mys.shp'", ensure that all the files from the downloaded .ZIP file "data.zip" are present in your working directory. The "polbnda_mys.shp" files also needs "polbnda_mys.shx" and other supporting files. You can execute the following code after you unzip all the supporting files in your current working folder:
states = readgeotable("polbnda_mys.shp");
geoplot(states,FaceColor=[1 0 0]);
hold on;
geoplot(2.6028,103.0569,"om",MarkerFaceColor="r");
hold off;
Additionally, to plot the red box, refer to the following example from the "geoplot" documentation:
Hope this helps!
  3 个评论
Walter Roberson
Walter Roberson 2023-11-21
编辑:Walter Roberson 2023-11-21
%but unfortunately Raghvi's code does not solve one of the two major
%problems
sourcefile = "https://stacks.stanford.edu/file/druid:zd362bc5680/data.zip";
TD = tempdir();
unzip(sourcefile, TD)
shpfile = fullfile(TD, "polbnda_mys.shp");
% % Part 1 ========================================
figure;
worldmap('Malaysia');
geoshow(shpfile);
% % Part 2 ========================================
hold on;
states = readgeotable( shpfile );
geoplot(states,FaceColor=[1 0 0]);
Error using matlab.graphics.internal.prepareCoordinateSystem
Adding GeographicAxes to axes is not supported. Turn hold off.

Error in matlab.graphics.internal.maps.prepareAxesParent

Error in map.graphics.internal.geotableplot (line 47)
parent = matlab.graphics.internal.maps.prepareAxesParent(parent, pvpairs);

Error in geoplot (line 80)
obj = map.graphics.internal.geotableplot(gx, args);
hold on;
geoplot(2.6028,103.0569,"om",MarkerFaceColor="r");
hold off;
Walter Roberson
Walter Roberson 2023-11-21
  1. That FaceColor needed to be used instead of Color. You made this change in your code
  2. That geoplot() needs an axes that is incompatible with the existing axes created by geoshow(). You do not address that in your post.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Geographic Plots 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by