You can add a shapefile to Google Earth Engine by using the ee.FeatureCollection
constructor and providing the path to your shapefile.
Here's a step-by-step guide:
- Upload your shapefile: Go to the Google Earth Engine Code Editor and click on "Assets" in the left sidebar. Then, click on "Upload" and select your shapefile.
- Import the shapefile: In your Earth Engine script, use the following code to import the shapefile:
var shapefile = ee.FeatureCollection('users/your_username/your_shapefile_name');
Replace your_username
with your Google Earth Engine username and your_shapefile_name
with the name you assigned to your shapefile when uploading it.
-
Use the shapefile: Now you can use the
shapefile
variable to perform various operations like:- Visualize it:
Map.addLayer(shapefile, {color: 'red'}, 'Shapefile');
- Extract data from other datasets:
var image = ee.Image('MODIS/006/MOD13A2'); var extractedData = image.reduceRegions({ collection: shapefile, reducer: ee.Reducer.mean() });
- Visualize it:
Practical Insights:
- Make sure your shapefile uses a supported projection, such as WGS84.
- If your shapefile is large, you might need to split it into smaller parts before uploading.
- You can use tools like QGIS to convert your shapefile to GeoJSON format, which is a common format supported by Google Earth Engine.
Example:
Let's say you have a shapefile named "cities.shp" that you uploaded to your Google Earth Engine account. You can then import and visualize it using the following code:
// Import the shapefile
var cities = ee.FeatureCollection('users/your_username/cities');
// Visualize the shapefile
Map.addLayer(cities, {color: 'blue'}, 'Cities');
This code will display the cities on the map in blue.