How do I find the x/y coordinates that correspond to a given latitude and longitude for my plot?

조회 수: 5 (최근 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
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
이동: 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
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-
I hope this resolves your query and you are able to proceed further!

카테고리

Help CenterFile Exchange에서 Geographic Plots에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by