Skip to content

Instantly share code, notes, and snippets.

@KMarkert
Created August 15, 2020 16:09
Show Gist options
  • Save KMarkert/308d2d81eb460603a7ecfa118907ad23 to your computer and use it in GitHub Desktop.
Save KMarkert/308d2d81eb460603a7ecfa118907ad23 to your computer and use it in GitHub Desktop.
using PyCall;
# import the ee Python module into Julia
# and initialize the EE API
ee = pyimport("ee");
ee.Initialize();
# load in ee.Image and sample a point
dem = ee.Image("USGS/SRTMGL1_003");
xy = ee.Geometry.Point([86.9250, 27.9881]...);
# notice the ellipsis after the coordinate list
# this is splatting, allowing for users pass
# unknown number of arguments as an array from an iterable
elev = dem.sample(xy, 30).first().get("elevation").getInfo();
println("Mount Everest elevation (m): ", elev);
# result should be: 'Mount Everest elevation (m): 8729'
using PyCall, Plots, Colors, FileIO;
# import the ee Python module into Julia
# and initialize the EE API
ee = pyimport("ee");
ee.Initialize();
# produce tabular data that can be plotted by data visualization in Julia
# Fetch a Landsat image.
img = ee.Image("LANDSAT/LT05/C01/T1_SR/LT05_034033_20000913");
# define a function to calcuate NDVI from an LT5 image
function ndvi(img)
img.normalizedDifference(["B4","B3"]);
end
# apply function to image
ndvi_img = ndvi(img);
# define a color map to use for visualization
# choose green gradient and convert to hex color code
color_map = map(x -> hex(x,:RRGGBB), cgrad(:Greens_9));
# get a link to the thumbnail NDVI image
thumburl = ndvi_img.getThumbUrl(
# define parameters to visualize image
Dict(
"min" => 0,
"max" => 0.8,
"dimensions" => 1024,
"format" => "png",
"palette" => color_map
)
);
# download the image from the url returned
localpath = download(thumburl);
# load the image into an Array
img = FileIO.load(localpath);
# display the image
plot(img, ticks = nothing, border = :none)
using PyCall, Plots;
theme(:bright);
# import the ee Python module into Julia
# and initialize the EE API
ee = pyimport("ee");
ee.Initialize();
# produce tabular data that can be plotted by data visualization in Julia
# Fetch a Landsat image.
img = ee.Image("LANDSAT/LT05/C01/T1_SR/LT05_034033_20000913");
# Select Red and NIR bands, scale them, and sample 500 points.
samp_fc = img.select(["B3","B4"]).divide(10000).sample(scale=30, numPixels=500);
# Arrange the sample as a list of lists.
samp_dict = samp_fc.reduceColumns(ee.Reducer.toList().repeat(2), ["B3", "B4"]);
samp_list = ee.List(samp_dict.get("list"));
# Save server-side ee.List as a client-side Python list.
samp_data = samp_list.getInfo();
# unpack the red/nir rows into Vectors
red,nir = [samp_data[x,:] for x in 1:size(samp_data,1)];
# plot the data
scatter(red,nir,markersize=5,alpha=0.5,xlabel="Red",ylabel="NIR",leg=false,dpi=200)
# you can save the figure using: 'savefig("<path-to-file>,png")'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment