GEE Tutorials

Google Earth Engine Tutorial: Import Shapefile and Clip Raster

Credit: Youtube Channel “Terra Spatial, Guide on importing study area shapefiles and clipping raster layers to specific boundaries.”

You can see all the tutorials from here: Techgeo Academy.

Import Shapefile and Clip Raster Using Google Earth Engine

Google Earth Engine (GEE) allows you to work with geospatial data by integrating remote sensing imagery with vector data, such as shapefiles. This tutorial outlines how to import a shapefile from Google Drive and use it to clip a raster dataset.

Step 1: Upload Shapefile to Google Drive

1. Convert the shapefile to a GeoJSON format or upload the ZIP file directly to your Google Drive.

2. Ensure the shapefile contains the necessary geometry (e.g., polygons, points) to define the area of interest.

Step 2: Access Shapefile in Google Earth Engine

Use the following code snippet to import the shapefile:

  
// Import the shapefile from Google Drive  
var shapefile = ee.FeatureCollection('users/yourusername/your_shapefile_name');  
// Replace 'yourusername' and 'your_shapefile_name' with your actual Drive path  

Step 3: Prepare Raster Data for Clipping

Load a raster dataset, such as a Landsat image or Sentinel-2 data, and ensure it is in a compatible projection:

  
// Load a Landsat 8 image  
var image = ee.Image('LANDSAT/LC08/C01/T1_SR');  
// Filter by date and location  
var filtered = image.filterDate('2020-01-01', '2020-12-31')  
                   .filter(ee.Filter.eq('WRS_PATH', 137))  
                   .filter(ee.Filter.eq('WRS_ROW', 34));  

Step 4: Clip the Raster Using the Shapefile

Use the `clip` method to apply the shapefile’s geometry to the raster:

  
// Clip the image using the shapefile geometry  
var clippedImage = image.clip(shapefile.geometry());  

This ensures the raster is limited to the area defined by the shapefile.

Step 5: Visualize or Export the Clipped Raster

To visualize the result in the GEE Code Editor:

  
// Add the clipped image to the map  
Map.addLayer(clippedImage, {bands: ['SR_B5', 'SR_B4', 'SR_B3'], min: 0, max: 3000}, 'Clipped Image');  

To export the clipped image as a GeoTIFF:

  
// Export the clipped image to Google Drive  
Export.image.toDrive({  
  image: clippedImage,  
  description: 'clipped_image',  
  folder: 'GEE_Exports',  
  fileNamePrefix: 'clipped_image',  
  region: shapefile.geometry(),  
  scale: 30,  
  crs: 'EPSG:4326',  
  maxPixels: 1e9  
});  

FAQ

How do I handle projection issues when clipping?

Ensure the shapefile and raster data use the same coordinate reference system (CRS). If not, reproject the image using .reproject() before clipping.

What if the shapefile is not found in Google Drive?

Verify the file name, path, and permissions. Check the GEE documentation for correct syntax.

Can I clip multiple shapefiles at once?

Yes. Combine feature collections using merge() or filter them to select multiple geometries before clipping.

How do I check the geometry of the shapefile?

Use print(shapefile.geometry()) to inspect the shapefile’s geometry and ensure it aligns with the raster data.

What is the purpose of maxPixels in the export?

maxPixels defines the maximum number of pixels that can be processed. Increase it if the clipped area exceeds the default limit.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *