How do I find the x/y coordinates that correspond to a given latitude and longitude for my plot?
30 次查看(过去 30 天)
显示 更早的评论
Hi I am plotting a netCDF file and an trying to plot a rectangle over an area of interest within the netCDF plot. I have tried using plot() and inputing x/y coordinates but it isn't as accurate as I want because I only have the longitude and latitude. If I input the longitude and latitude, the rectangle does not appear. I have also tried m_line() and used the latitude/longitude coordinates but that also doesn't appear in my plot.
Here is my code:
file = 'NetCDF Files/2013/snapshot-2013-04-15T00_00_00Z.nc';
ncdisp(file);
lon = ncread(file, 'lon');
lat = ncread(file, 'lat');
band1 = uint8(ncread(file, 'Band1'));
band2 = uint8(ncread(file, 'Band2'));
band3 = uint8(ncread(file, 'Band3'));
close all
figure('pos', [200, 50, 1100, 700])
minlon = -66.3;
maxlon = -65.4;
minlat = 65.6;
maxlat = 66.4;
m_proj('mercator', 'lon', [minlon maxlon], 'lat', [minlat maxlat]); hold on
m_pcolor(lon, lat, band2'); shading flat;
%m_gshhs_f('save','gshhs_f_Pang')
m_usercoast('gshhs_f_Pang','patch',[0.820,0.703,0.547]);
m_grid('box','fancy','tickdir','in')
colorbar
%this is what I was messing with but it doesn't give me the accuracy I need
%plot([-0.0035 -0.0003 -0.0003 -0.0035 -0.0035], [1.5515 1.5515 1.5536 1.5536 1.5515],'-r')
%this doesn't show up on the map
bndry_lon = [66 66.15 66.15 66 66];
bndry_lat = [-66.1 -66.1 -65.89 -65.89 -66.1];
m_line(bndry_lon, bndry_lat, 'linewi', 1, 'color', 'r');
4 个评论
Mrutyunjaya Hiremath
2023-7-23
The issue with your code is that you are mixing up the longitude and latitude coordinates while plotting the rectangle using m_line. The correct order should be [longitude, latitude], but you have used [latitude, longitude] instead.
华纳公司注册网址hn6660.com
2023-7-23
移动:Voss
2023-7-23
I am not able to figure out the error here.
I have been struggling with this from months. I sincerely appreciate any sort of help/guidance on this
回答(1 个)
Raj
2023-11-10
编辑:Raj
2023-11-10
Hello Noah,
I understand you essentially want to find x and y coordinates that correspond to a given latitude and longitude. To do so you can use the MATLAB function ‘projfwd’ provided in the Mapping Toolbox in MATLAB
Here is a sample code. You can replace the latitude and longitude with the ‘lat’ and ‘lon’ values you receive from ‘ncread’ function and adapt accordingly
% Define the latitude and longitude coordinates
latitude = 40.7128;
longitude = -74.0060;
%Create a map projection object
projection = defaultm('mercator'); % Example projection (Mercator)
% Convert latitude and longitude to x/y coordinates
[x, y] = projfwd(projection, latitude, longitude)
With the x and y coordinates, you can proceed to draw a rectangle over the required area within the plot
Additionally refer to the documentation of the respective functions for better understanding-
- ‘defaultm’- https://www.mathworks.com/help/map/ref/defaultm.html
- ‘projfwd’- https://www.mathworks.com/help/map/ref/projcrs.projfwd.html
I hope this resolves your query and you are able to proceed further!
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Geographic Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!