Plot markers on map

18 vues (au cours des 30 derniers jours)
Eli
Eli le 21 Sep 2023
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 commentaire
Walter Roberson
Walter Roberson le 21 Sep 2023
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)

Connectez-vous pour commenter.

Réponses (1)

R
R le 15 Nov 2023
Modifié(e) : R le 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 commentaires
Walter Roberson
Walter Roberson le 21 Nov 2023
Modifié(e) : Walter Roberson le 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]);
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 le 21 Nov 2023
  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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Geographic Plots dans Help Center et File Exchange

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by