Plot markers on map

조회 수: 16 (최근 30일)
Eli
Eli 2023년 9월 21일
편집: Walter Roberson 2023년 11월 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