# Cut hole in raster with shapefile and sample at closest edges

Geographic Information Systems Asked on January 3, 2022

I have a raster in which I’d like to cut a “hole” with a shapefile. I have points close to the boundary, but inside the hole, and I would like to find the closest point on the edge. To give a concrete example, I have a global dataset (1′ resolution) and a shapefile of the coast of Africa. My data points are locations of cities near the coast, but some of these points are not exactly on the coast, so I’d like to find the closest point outside the shapefile, i.e. closest point over water.

Normally, I’d use: gdalwarp -of -GTiff -cutline Coast.shp -crop_to_cutline -dstnodata 0.0 Data.tif Clipped_Data.tif but this obviously only gives me what’s inside the shapefile; I can’t find an inverse clipping for this command.

I did some digging and found this, which allowed me to set everything inside the shapefile to 0, but when I sample using gmt grdtrack it just sets the values to 0 (which makes sense). Also, when I tried crop=True, invert=True I got: ValueError: crop and invert cannot both be True. So I reverted to using crop=False, invert=True.

So, how can I sample this raster at the closest point outside the shapefile? I think if I can get the hole to be set to NaN, I can use the -T flag in gmt grdtrack.

Take a look at mapproject -L option. I think it does what you want and no need to use a grid.

Answered by J Luis on January 3, 2022

## Related Questions

### GeoServer WMS layers not working on Azure app service

1  Asked on August 4, 2020 by tvrtko

### Extracting “World Reference Base (2006) Soil Groups” data in R and searching for the legend of the extracted values

1  Asked on August 3, 2020

### Using Photo points to “geotag” old images in QGIS

1  Asked on August 3, 2020 by john10710

### Filter geometry based on map extent using OpenLayers

1  Asked on August 3, 2020 by massimiliano-moraca

### Finding closest point to shapefile coastline Python

1  Asked on August 3, 2020 by dannytg

### How do I create a rectangular feature in ArcGIS Pro given only 2 coordinates and a width?

1  Asked on August 2, 2020 by maps_spam

### How to plot NDVI time series for two features on same plot?

0  Asked on August 2, 2020 by sankarshan-rastogi

### Can I copy files using QGIS browser?

1  Asked on August 2, 2020 by user1521655

### gdal_merge with python for PDS4 does not generate xml

0  Asked on August 1, 2020 by shen

### Access SpatiaLite connections with PyQGIS QgsProviderMetadata (QGIS 3.10.7 LTR)

1  Asked on July 31, 2020 by jochen-schwarze

### Display .kml file in Leaflet

2  Asked on July 31, 2020

### Error message qgis-ltr-bin.exe

1  Asked on July 31, 2020 by sarath

### st_intersection only if specific conditions are satisfied

0  Asked on July 30, 2020

### R cartography package colored pencil – Get Pencil Layer problem

1  Asked on July 30, 2020 by james-holland

### Copy & Paste Feature in same Layer but not editable seperately?

0  Asked on July 29, 2020 by gmuerovic

### GeoServer 2.18. Setting Min & Max Scales

1  Asked on July 29, 2020 by zakariah1

### Buffer zone intersection proportion using ArcMap

0  Asked on July 29, 2020 by nigel-lawton

### Displaying features by multi-attributes using proportional symbols

0  Asked on July 29, 2020 by florian-mahl

### Is there a command line tool like gdalinfo for vector files?

1  Asked on July 29, 2020