How to create a map mask using coordinates
Show older comments
Hello,
I have data (temperature) and their coordinates associated. I have two vectors with latitude and longitude defining an area, let say it's a square for easiness (in fact I have thousands of coordinates) having the coordinates :
latX = [10.7; 10.7; 20.2; 20.2];
lonX = [-15.3; -5.1; -5.1; -15.3];
And X represents some data I have access to for the whole area.
My coordinates have the from :
lat_t = 0:1:30;
lon_t = -20:1:0;
[lon,lat]=meshgrid(lon_t,lat_t);
Then I apply the poly2mask to return a logical value of 1 for points inside the square:
bw = poly2mask(latX,lonX,size(lat,1),size(lat,2));
It doesn't work because I have negative coordinates, so it can not use the indexs. What could the solution be ? Is there a poly2mask method dedicated to coordinates ?
Accepted Answer
More Answers (1)
Sean de Wolski
on 6 Aug 2021
0 votes
What is your end goal?
You need to project lat and lon into cartesian coordinates. From there, you can use poly2mask. The resulting mask will still be in projected coordinates. You can then inverse-project it back to lat/lon.
Look at projfwd, projinv - you'll need to find an appropriate projection based on your goal (equal area, etc.)
4 Comments
Sean de Wolski
on 9 Aug 2021
Edited: Sean de Wolski
on 9 Aug 2021
it's important because latitude and longitude are on the surface of a sphere, not on a flat cartesian coordinate system. Thus 1 latitude does not equal one longtitude and 1 latitude does not even equal 1 latitude if it's somewhere else. Thus you need to "flatten" it so that you can use cartesian things like poly2mask which assumes that pixels are square (a lat/lon cell is not square!)
MaHa
on 10 Aug 2021
Sean de Wolski
on 10 Aug 2021
Edited: Sean de Wolski
on 10 Aug 2021
The projection relies on where on the earth you are and the size of the area. For example, if working with Massachusetts data (MathWorks' headquarters location) I'd use the Massachusetts State Plane projection which is optimized for MA. Find it by web search literally: massachusetts state plane projection code - Bing
prj = projcrs(26986)
For Alaska, there are a bunch of them, so you'll have to pick one: Spatial Reference List -- Spatial Reference. Search as necessary for wherever in the world you are!
projcrs(3474)
Categories
Find more on Coordinate Reference Systems 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!