How to crop the area outside the shaperfile on a map

Illustration
Robson Passos - 2023-12-25T13:20:49+00:00
Question: How to crop the area outside the shaperfile on a map

Dear,   I have a .txt file with three columns: longitude, latitude and another variable (i.e. altitude, temperature, precipitation), the data are from weather stations, I have a value per station.   As the seasons are sparse I used the griddata function to interpolate the values and then plotted a shaperfile over the image. The generated figure is as follows:: And the script I used is as follows: clear all close all load dados_dd.txt d = (dados_dd); lat = 0 - d(:,2); %negative because I am in the southern hemisphere lon = 0 - d(:,1); cota = d(:,3); N = 1000; [Xi, Yi] = meshgrid(linspace(-49,-44,N),linspace(-24,-20,N)); Ci = griddata(lon, lat, cota, Xi, Yi); imagesc(linspace(-49,-44,N),linspace(-24,-20,N),Ci); xlabel('Longitude'); ylabel('Latitude'); colorbar S = shaperead('shaperfile.shp') geoshow(S) I would like to know if there is a way to remove the parts that are outside the shaperfile   I do not understand much of interpolation, is it possible to interpolate the data so that they fill the entire Shaperfile?   Sorry for the inconvenience, but I really need help.  

Expert Answer

Profile picture of Kshitij Singh Kshitij Singh answered . 2025-11-20

Have a look on inpolygon. This will give you the indices, which are lying inside the given polygon (shape file here). From this, you can add NaN to the points lying outside and plot. This will solve your purpose.
 
Or have a look on _ScatteredInterpolant, here you can specify not to extrapolation. YOu will get only the region lying inside the closed region.


Not satisfied with the answer ?? ASK NOW

Get a Free Consultation or a Sample Assignment Review!