3

In Google Earth Engine I can clip the image using a shape drawn on the map canvas or by supplying a vector through Fusion table. This basically take the form.

var boundary = ee.FeatureCollection('ft:1W78nK9f3rkXBG3iuDdBidFHVGVnzvAqh7UEjRbeq');
var cliped = median_L8_2016.clip(boundary);
Export.image(median_L8_2016,'filename',

Suppose I have a vector file upload to Fusion table that contain three polygons and an attribute field called "district" with value as A, B, C for each polygon.

Is there any method in Google Earth Engine to clip the image output into different area and name the output file using the attribute. In this case I my expected output is three images named A, B, C.

The use case for this is plenty, for example, clip the study area into different province and district for further processing.

Currently I finish the image processing in GEE then download the result and do the clipping in FME which is working fine. But having the whole process in GEE would add much advantages to the workflow.

  • You can .map() over each feature in your FC and do the processing district wise and later join all districts to export a single image. – Kersten Apr 24 '17 at 8:57
  • I add Setting imagery viz params (min: 0, max: 2000} but the images don't display viz. I got errors. Could you please tell me why? collection = ee.ImageCollection('COPERNICUS/S2').filterDate('2016-01-01', '2017-01-01').filterBounds(boundvn) img = collection.mosaic().select('B4','B3','B2').getMapId({min: 0, max: 2000}) # Setting imagery viz params – hana Jun 21 '17 at 9:31
3

I show you two options: first make an ImageCollection in which each image has a property named 'district', and the second is what you asked, download every image. Replace YOUR_IMAGE with the name of your image, YOUR_FOLDER with the name of the folder you want the images in, and IMG_NAME with the base name you want to give to the images (it will attach the property afterwards).

EXPORTING is independent from TO IMAGECOLLECTION and viceversa.

As I couldn't access to your FeatureCollection, I tried with mine which had numbers, so it could be that you have to replace disS.toString() with only disS

// Feature Collection
var fc = ee.FeatureCollection("ft:1W78nK9f3rkXBG3iuDdBidFHVGVnzvAqh7UEjRbeq")

// Image
var img = YOUR_IMAGE

/////////////// TO IMAGECOLLECTION //////////////////////// 
// Function to make the Image Collection
var colFunc = function(feat) {
  var dis = feat.get("district")
  var clipped = img.clip(feat).set("district", dis)
  return clipped
}
// Map the FC to an IC 
var imcol = ee.ImageCollection(fc.map(colFunc))
///////////////////////////////////////////////////////////

////////////// EXPORTING ///////////////////////
var featlist = fc.getInfo()["features"]
for (var f in featlist) {
  var feat = ee.Feature(featlist[f])
  var dis = feat.get("district")
  var disS = dis.getInfo()
  var name = "IMG_NAME"      
  Export.image.toDrive({
    image: img,
    description: name+disS.toString(),
    folder: "YOUR_FOLDER",
    fileNamePrefix: name+disS.toString(),
    region: feat.geometry().bounds(),
    scale: 30
  })
}
////////////////////////////////////////////////
  • Thanks Rodrigo: I try your solution and it look like almost work. However when I print the the "district name" the console return "null". Could you check the code. Thanks code.earthengine.google.com/ae095d649dd62c2c30b01d2907bcd49e – anh.hv May 5 '17 at 10:53
  • Your fusion table is not right, first of all I think you have more than 1 field assigned as "geometry", and second, it seems it has not a "district" field.. – Rodrigo E. Principe May 5 '17 at 13:51
  • Rodrigo: Thanks, I found the mistake now. GEE don't except user define column for naming polygon. Once I replace the value in description field with value in my "district" field, it work. Currently I can print the polygon name to the console. Still working on the clipping part and will report my progress. – anh.hv May 9 '17 at 4:44
  • @anh.hv When you export, the clipping is done with the 'region' parameter. No need to use Image.clip() – Rodrigo E. Principe May 9 '17 at 10:22
  • link I can have the cliped result output to the task panel. Still need to click on the run for each task to archive the result. Did you be able to directly export multiple cliped image to Gdrive or external storage. Thanks – anh.hv May 18 '17 at 4:18
2

I am glad to help, I have a repository that may be helpful and I'll put this script on it: https://github.com/gee-community/gee_tools

The scritp:

# -*- coding: utf-8 -*-
"""
Created on Thu May 18 12:09:25 2017

@author: Rodrigo E. Principe

email: fitoprincipe82 at gmail
github: https://github.com/gee-community/gee_tools
twitter: @fitoprincipe
"""
import ee
ee.Initialize()

fc = ee.FeatureCollection("ft:1oWq2NBh5F2VGdbdXDDmj-l2JpuKNP8WySYoOmtQH")

img = YOUR_IMAGE

featlist = fc.getInfo()["features"]

#print featlist

def unpack(thelist):
    unpacked = []
    for i in thelist:
        unpacked.append(i[0])
        unpacked.append(i[1])
    return unpacked

for f in featlist:
    geomlist = unpack(f["geometry"]["coordinates"][0])
    geom = ee.Geometry.Polygon(geomlist)

    feat = ee.Feature(geom)
    disS = f["properties"]["district"]
    name = "YOUR NAME"

    task= ee.batch.Export.image.toDrive(
    image=img,
    description= name+str(disS),
    folder= "HANSEN_SO",
    fileNamePrefix= name+str(disS),
    region= feat.geometry().bounds().getInfo()["coordinates"],
    scale= 30)

    task.start()
    print "exporting {0} {1}..".format(name,disS)

Enjoy!

  • It is now in the repository: github.com/gee-community/gee_tools/tree/master/python – Rodrigo E. Principe May 18 '17 at 16:51
  • so @anh.hv, have you tried it? did it work? you can put the function on a separte file and import it from your script, to keep the code clean and take advantage of using the python API – Rodrigo E. Principe May 19 '17 at 11:29
  • thanks you very much. I still working on the code give. Need to adapt it a bit to our situation but in general it look very promising. I will report when we get it run from my side. Sorry I was a bit busy and did not respond fast enough. Highly appreciated with your help. – anh.hv May 23 '17 at 13:02
  • no worries, I'm busy too. If you have any trouble adapting it, just ask. Cheers – Rodrigo E. Principe May 23 '17 at 14:15
1

I try the solution by Rodrigo and so far almost get the expected result

GEE code bellow. The only down side is that the result is output to the task panel, so i need to click to run individual clip. Is there any one successfully clip multiple region directly to Google Drive (or other cloud storage)

enter image description here

https://code.earthengine.google.com/a21e62bb07bc279f470e16c1e21d49d1

// Feature Collection ("description" is attribute of kml file (name of districts) var fc = ee.FeatureCollection('ft:19b9lU0WPVJqqH1Z40RsMZnRMwmLUJcz1t_HMf4pt')

var S2_2017 = S2.filterDate('2016-01-04', '2017-05-10').filterBounds(geometry); var img = S2_2017.select('B4','B3','B2'); var composite = ['B4','B3','B2']

Map.addLayer(img, {bands: composite, max: 2000}, 'S2')

////////////// EXPORTING /////////////////////// var featlist = fc.getInfo()["features"] //print (featlist) for (var f in featlist) { var feat = ee.Feature(featlist[f]) var dis = feat.get("description") var disS = dis.getInfo() var name = "IMG_NAME_"

//export images

Export.image.toDrive({ image: img, description: name+disS,//.toString(), folder: "SENTINEL_VN", fileNamePrefix: name+disS,//.toString(), region: feat.geometry().bounds(), maxPixels:1e13, scale: 10 }) }

  • I don't think you should post it as an answer, the only solution is what I mention before, use the Python API. – Rodrigo E. Principe May 18 '17 at 12:06
  • I agree. Actually I don't mean to post it as a complete solution. Just want to show what I have archived so far and what remained to be solved. I know this not follow the Stack Exchange rule. Could you show an example where Python API is used in GEE code. Thanks a lot. – anh.hv May 18 '17 at 12:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.