Skip to content

Instantly share code, notes, and snippets.

@soiqualang
Last active March 12, 2021 04:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save soiqualang/cd312816ea7c52996355bbc2b5830970 to your computer and use it in GitHub Desktop.
Save soiqualang/cd312816ea7c52996355bbc2b5830970 to your computer and use it in GitHub Desktop.
GEE - Extract value from image
var img = ee.Image('LANDSAT/LC08/C01/T1/LC08_044034_20140318');
var dem = ee.Image('USGS/SRTMGL1_003');
var arr1 = ee.FeatureCollection(ee.List([]));
var xy = ee.Geometry.Point([86.9250, 27.9881]);
var p1 = ee.Geometry.Point([142.36083984375, -37.466138602344046])
var p2 = ee.Geometry.Point([143.23974609375, -37.04640889969956])
var pts = ee.FeatureCollection(ee.List([ee.Feature(p1),ee.Feature(p2)]))
print(dem);
print(img);
var date = img.date().format();
print(date);
//var value = img.reduceRegion(ee.Reducer.first(), xy, 30).get('B1')
var value = dem.reduceRegion(ee.Reducer.first(), xy, 30).get('elevation')
print(value);
@soiqualang
Copy link
Author

function loadDoc() {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
     console.log(this.responseText);
    }
  };
  xhttp.open("GET", "http://dev.dothanhlong.org/tmp/points.geojson", true);
  xhttp.send();
}

var dem = ee.Image('USGS/SRTMGL1_003')
var xy = ee.Geometry.Point([86.9250, 27.9881])
var elev = dem.sample(xy, 30).first().get('elevation').getInfo()
print('Mount Everest elevation (m):', elev)



var dem = ee.Image('USGS/SRTMGL1_003');
//var xy = ee.Geometry.Point([86.9250, 27.9881]);
//var elev = dem.sample(xy, 30).first().get('elevation').getInfo();
//print('Mount Everest elevation (m):', elev);

var i=0;
var xy='';
var elev='';

//console.log(t1.features[0].geometry.coordinates);
for(i;i<t1.features.length;i++){
	xy = ee.Geometry.Point(t1.features[i].geometry.coordinates);
	elev = dem.sample(xy, 30).first().get('elevation').getInfo();
	print('Mount Everest elevation (m):', elev);
}

@soiqualang
Copy link
Author

// As I can't access your FusionTable,
// I make random points and create a FeatureCollection
var p1 = ee.Geometry.Point([142.36083984375, -37.466138602344046])
var p2 = ee.Geometry.Point([143.23974609375, -37.04640889969956])
var pts = ee.FeatureCollection(ee.List([ee.Feature(p1),ee.Feature(p2)]))

//IMPORT LANDSAT IMAGE
var L82014pre = ee.ImageCollection('LANDSAT/LC8_SR') //Landsat 8 Surface   reflectance
.filter(ee.Filter.eq('wrs_path', 94))
.filter(ee.Filter.eq('wrs_row', 86)) 
.filterDate(ee.Date.fromYMD(2013,12,13), ee.Date.fromYMD(2014,1,15))

// Empty Collection to fill
var ft = ee.FeatureCollection(ee.List([]))

var fill = function(img, ini) {
  // type cast
  var inift = ee.FeatureCollection(ini)

  // gets the values for the points in the current img
  var ft2 = img.reduceRegions(pts, ee.Reducer.first(),30)

  // gets the date of the img
  var date = img.date().format()

  // writes the date in each feature
  var ft3 = ft2.map(function(f){return f.set("date", date)})

  // merges the FeatureCollections
  return inift.merge(ft3)
}

// Iterates over the ImageCollection
var newft = ee.FeatureCollection(L82014pre.iterate(fill, ft))

print(newft);

// Export
Export.table.toDrive(newft,
"anyDescription",
"anyFolder",
"anyNameYouWant")

@soiqualang
Copy link
Author

Đang làm thử

var points={
"type": "FeatureCollection",
"name": "points",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{ "type": "Feature", "properties": { "id": 0 }, "geometry": { "type": "Point", "coordinates": [ 104.777702331543082, 10.961941719055176 ] } },
{ "type": "Feature", "properties": { "id": 1 }, "geometry": { "type": "Point", "coordinates": [ 104.877702331543077, 10.961941719055176 ] } },
{ "type": "Feature", "properties": { "id": 2 }, "geometry": { "type": "Point", "coordinates": [ 104.977702331543071, 10.961941719055176 ] } },
{ "type": "Feature", "properties": { "id": 3 }, "geometry": { "type": "Point", "coordinates": [ 105.077702331543065, 10.961941719055176 ] } },
{ "type": "Feature", "properties": { "id": 4 }, "geometry": { "type": "Point", "coordinates": [ 105.17770233154306, 10.961941719055176 ] } },
{ "type": "Feature", "properties": { "id": 5 }, "geometry": { "type": "Point", "coordinates": [ 105.277702331543054, 10.961941719055176 ] } },
{ "type": "Feature", "properties": { "id": 6 }, "geometry": { "type": "Point", "coordinates": [ 105.377702331543048, 10.961941719055176 ] } },
{ "type": "Feature", "properties": { "id": 7 }, "geometry": { "type": "Point", "coordinates": [ 105.477702331543043, 10.961941719055176 ] } },
{ "type": "Feature", "properties": { "id": 8 }, "geometry": { "type": "Point", "coordinates": [ 104.777702331543082, 10.861941719055176 ] } },
{ "type": "Feature", "properties": { "id": 9 }, "geometry": { "type": "Point", "coordinates": [ 104.877702331543077, 10.861941719055176 ] } },
{ "type": "Feature", "properties": { "id": 10 }, "geometry": { "type": "Point", "coordinates": [ 104.977702331543071, 10.861941719055176 ] } },
{ "type": "Feature", "properties": { "id": 11 }, "geometry": { "type": "Point", "coordinates": [ 105.077702331543065, 10.861941719055176 ] } },
{ "type": "Feature", "properties": { "id": 12 }, "geometry": { "type": "Point", "coordinates": [ 105.17770233154306, 10.861941719055176 ] } },
{ "type": "Feature", "properties": { "id": 13 }, "geometry": { "type": "Point", "coordinates": [ 105.277702331543054, 10.861941719055176 ] } },
{ "type": "Feature", "properties": { "id": 14 }, "geometry": { "type": "Point", "coordinates": [ 105.377702331543048, 10.861941719055176 ] } },
{ "type": "Feature", "properties": { "id": 15 }, "geometry": { "type": "Point", "coordinates": [ 105.477702331543043, 10.861941719055176 ] } },
{ "type": "Feature", "properties": { "id": 16 }, "geometry": { "type": "Point", "coordinates": [ 104.777702331543082, 10.761941719055176 ] } },
{ "type": "Feature", "properties": { "id": 17 }, "geometry": { "type": "Point", "coordinates": [ 104.877702331543077, 10.761941719055176 ] } },
{ "type": "Feature", "properties": { "id": 18 }, "geometry": { "type": "Point", "coordinates": [ 104.977702331543071, 10.761941719055176 ] } },
{ "type": "Feature", "properties": { "id": 19 }, "geometry": { "type": "Point", "coordinates": [ 105.077702331543065, 10.761941719055176 ] } },
{ "type": "Feature", "properties": { "id": 20 }, "geometry": { "type": "Point", "coordinates": [ 105.17770233154306, 10.761941719055176 ] } },
{ "type": "Feature", "properties": { "id": 21 }, "geometry": { "type": "Point", "coordinates": [ 105.277702331543054, 10.761941719055176 ] } },
{ "type": "Feature", "properties": { "id": 22 }, "geometry": { "type": "Point", "coordinates": [ 105.377702331543048, 10.761941719055176 ] } },
{ "type": "Feature", "properties": { "id": 23 }, "geometry": { "type": "Point", "coordinates": [ 105.477702331543043, 10.761941719055176 ] } },
{ "type": "Feature", "properties": { "id": 24 }, "geometry": { "type": "Point", "coordinates": [ 104.777702331543082, 10.661941719055177 ] } },
{ "type": "Feature", "properties": { "id": 25 }, "geometry": { "type": "Point", "coordinates": [ 104.877702331543077, 10.661941719055177 ] } },
{ "type": "Feature", "properties": { "id": 26 }, "geometry": { "type": "Point", "coordinates": [ 104.977702331543071, 10.661941719055177 ] } },
{ "type": "Feature", "properties": { "id": 27 }, "geometry": { "type": "Point", "coordinates": [ 105.077702331543065, 10.661941719055177 ] } },
{ "type": "Feature", "properties": { "id": 28 }, "geometry": { "type": "Point", "coordinates": [ 105.17770233154306, 10.661941719055177 ] } },
{ "type": "Feature", "properties": { "id": 29 }, "geometry": { "type": "Point", "coordinates": [ 105.277702331543054, 10.661941719055177 ] } },
{ "type": "Feature", "properties": { "id": 30 }, "geometry": { "type": "Point", "coordinates": [ 105.377702331543048, 10.661941719055177 ] } },
{ "type": "Feature", "properties": { "id": 31 }, "geometry": { "type": "Point", "coordinates": [ 105.477702331543043, 10.661941719055177 ] } },
{ "type": "Feature", "properties": { "id": 32 }, "geometry": { "type": "Point", "coordinates": [ 104.777702331543082, 10.561941719055177 ] } },
{ "type": "Feature", "properties": { "id": 33 }, "geometry": { "type": "Point", "coordinates": [ 104.877702331543077, 10.561941719055177 ] } },
{ "type": "Feature", "properties": { "id": 34 }, "geometry": { "type": "Point", "coordinates": [ 104.977702331543071, 10.561941719055177 ] } },
{ "type": "Feature", "properties": { "id": 35 }, "geometry": { "type": "Point", "coordinates": [ 105.077702331543065, 10.561941719055177 ] } },
{ "type": "Feature", "properties": { "id": 36 }, "geometry": { "type": "Point", "coordinates": [ 105.17770233154306, 10.561941719055177 ] } },
{ "type": "Feature", "properties": { "id": 37 }, "geometry": { "type": "Point", "coordinates": [ 105.277702331543054, 10.561941719055177 ] } },
{ "type": "Feature", "properties": { "id": 38 }, "geometry": { "type": "Point", "coordinates": [ 105.377702331543048, 10.561941719055177 ] } },
{ "type": "Feature", "properties": { "id": 39 }, "geometry": { "type": "Point", "coordinates": [ 105.477702331543043, 10.561941719055177 ] } },
{ "type": "Feature", "properties": { "id": 40 }, "geometry": { "type": "Point", "coordinates": [ 104.777702331543082, 10.461941719055178 ] } },
{ "type": "Feature", "properties": { "id": 41 }, "geometry": { "type": "Point", "coordinates": [ 104.877702331543077, 10.461941719055178 ] } },
{ "type": "Feature", "properties": { "id": 42 }, "geometry": { "type": "Point", "coordinates": [ 104.977702331543071, 10.461941719055178 ] } },
{ "type": "Feature", "properties": { "id": 43 }, "geometry": { "type": "Point", "coordinates": [ 105.077702331543065, 10.461941719055178 ] } },
{ "type": "Feature", "properties": { "id": 44 }, "geometry": { "type": "Point", "coordinates": [ 105.17770233154306, 10.461941719055178 ] } },
{ "type": "Feature", "properties": { "id": 45 }, "geometry": { "type": "Point", "coordinates": [ 105.277702331543054, 10.461941719055178 ] } },
{ "type": "Feature", "properties": { "id": 46 }, "geometry": { "type": "Point", "coordinates": [ 105.377702331543048, 10.461941719055178 ] } },
{ "type": "Feature", "properties": { "id": 47 }, "geometry": { "type": "Point", "coordinates": [ 105.477702331543043, 10.461941719055178 ] } },
{ "type": "Feature", "properties": { "id": 48 }, "geometry": { "type": "Point", "coordinates": [ 104.777702331543082, 10.361941719055178 ] } },
{ "type": "Feature", "properties": { "id": 49 }, "geometry": { "type": "Point", "coordinates": [ 104.877702331543077, 10.361941719055178 ] } },
{ "type": "Feature", "properties": { "id": 50 }, "geometry": { "type": "Point", "coordinates": [ 104.977702331543071, 10.361941719055178 ] } },
{ "type": "Feature", "properties": { "id": 51 }, "geometry": { "type": "Point", "coordinates": [ 105.077702331543065, 10.361941719055178 ] } },
{ "type": "Feature", "properties": { "id": 52 }, "geometry": { "type": "Point", "coordinates": [ 105.17770233154306, 10.361941719055178 ] } },
{ "type": "Feature", "properties": { "id": 53 }, "geometry": { "type": "Point", "coordinates": [ 105.277702331543054, 10.361941719055178 ] } },
{ "type": "Feature", "properties": { "id": 54 }, "geometry": { "type": "Point", "coordinates": [ 105.377702331543048, 10.361941719055178 ] } },
{ "type": "Feature", "properties": { "id": 55 }, "geometry": { "type": "Point", "coordinates": [ 105.477702331543043, 10.361941719055178 ] } },
{ "type": "Feature", "properties": { "id": 56 }, "geometry": { "type": "Point", "coordinates": [ 104.777702331543082, 10.261941719055178 ] } },
{ "type": "Feature", "properties": { "id": 57 }, "geometry": { "type": "Point", "coordinates": [ 104.877702331543077, 10.261941719055178 ] } },
{ "type": "Feature", "properties": { "id": 58 }, "geometry": { "type": "Point", "coordinates": [ 104.977702331543071, 10.261941719055178 ] } },
{ "type": "Feature", "properties": { "id": 59 }, "geometry": { "type": "Point", "coordinates": [ 105.077702331543065, 10.261941719055178 ] } },
{ "type": "Feature", "properties": { "id": 60 }, "geometry": { "type": "Point", "coordinates": [ 105.17770233154306, 10.261941719055178 ] } },
{ "type": "Feature", "properties": { "id": 61 }, "geometry": { "type": "Point", "coordinates": [ 105.277702331543054, 10.261941719055178 ] } },
{ "type": "Feature", "properties": { "id": 62 }, "geometry": { "type": "Point", "coordinates": [ 105.377702331543048, 10.261941719055178 ] } },
{ "type": "Feature", "properties": { "id": 63 }, "geometry": { "type": "Point", "coordinates": [ 105.477702331543043, 10.261941719055178 ] } }
]
}

var dem = ee.Image('USGS/SRTMGL1_003');
print(dem);

var point=points.features[0].geometry.coordinates;
console.log(point);

var xy = ee.Geometry.Point([86.9250, 27.9881]);
var value = dem.reduceRegion(ee.Reducer.first(), xy, 30).get('elevation');
print([value]);

print('----------------------');

var i=0;
var xy='';
var arr1=[];
//[toa do, value]

for(i;i<points.features.length;i++){
	point=points.features[i].geometry.coordinates;
	xy = ee.Geometry.Point(point);
	value = dem.reduceRegion(ee.Reducer.first(), xy, 30).get('elevation');
	//print(value);
	//value=Math.random();
	value=ee.String(value);
	arr1[i]=[point,value];
	
	/* ee.Feature(ee.Algorithms.If(value,
                                       newf.set(date, ee.String(value)),
                                       newf.set(date, ee.String('No data')))) */
}

//print(JSON.stringify(arr1));
print(arr1);

var collect1 = ee.FeatureCollection(ee.List(arr1));

/* Export.table.toAsset({
  collection: collect1,
  description:'exportToTableAssetExample',
  assetId: 'exampleAssetId',
}); */

Export.table.toDrive({
  collection: collect1,
  folder: 'Test_Folder',
  description:'test_chartArray',
  fileFormat: 'CSV'
});

@soiqualang
Copy link
Author

soiqualang commented Mar 12, 2021

GEE add map

var im1 = ee.Image('COPERNICUS/S2/20160422T084804_20160422T123809_T36TVK')

// Point
var p = ee.Geometry.Point(32.3, 40.3)

// Extract the data
var data = im1
.select("B3")
.reduceRegion(ee.Reducer.first(),p,10)
.get("B3")

// Convert to Number for further use
var dataN = ee.Number(data)

// Show data
print(dataN)

// Add Layers
Map.centerObject(im1)
Map.addLayer(im1,{bands:["B4","B3","B2"],min:0,max:5000})
Map.addLayer(p)

https://krstn.eu/analyze-Sentinel-1-time-series-in-Google-Earth-Engine/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment