- 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)
Plot markers on map
16 views (last 30 days)
Show older comments
Dear all,
I require help as I am new at this. How do I do the following?
- 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?
- 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 Comment
Walter Roberson
on 21 Sep 2023
When I test in R2023b:
Answers (1)
R
on 15 Nov 2023
Edited: R
on 21 Nov 2023
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 Comments
Walter Roberson
on 21 Nov 2023
Edited: Walter Roberson
on 21 Nov 2023
%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]);
hold on;
geoplot(2.6028,103.0569,"om",MarkerFaceColor="r");
hold off;
Walter Roberson
on 21 Nov 2023
That is, I identified two problems in the original code, https://www.mathworks.com/matlabcentral/answers/2024182-plot-markers-on-map#comment_2895567
- That FaceColor needed to be used instead of Color. You made this change in your code
- That geoplot() needs an axes that is incompatible with the existing axes created by geoshow(). You do not address that in your post.
See Also
Categories
Find more on Geographic Plots 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!