Skip to content

Instantly share code, notes, and snippets.

@jdries
Last active October 13, 2017 09:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jdries/33d9945de676f9c793c5e349cdcaaf3d to your computer and use it in GitHub Desktop.
Save jdries/33d9945de676f9c793c5e349cdcaaf3d to your computer and use it in GitHub Desktop.
Client API examples
Some examples of existing API's.
https://nbviewer.jupyter.org/urls/bitbucket.org/vitotap/notebooks/raw/master/QuickstartExample.ipynb
// Compute the Normalized Difference Vegetation Index (NDVI).
var nir = image.select('B5');
var red = image.select('B4');
var ndvi = nir.subtract(red).divide(nir.add(red)).rename('NDVI');
////ZONAL STATS
// Load input imagery: Landsat 7 5-year composite.
var image = ee.Image('LE7_TOA_5YEAR/2008_2012');
// Load a FeatureCollection of counties in Maine.
var maineCounties = ee.FeatureCollection('ft:1S4EB6319wWW2sWQDPhDvmSBIVrD3iEmCLYB7nMM')
.filter(ee.Filter.eq('StateName', 'Maine'));
// Add reducer output to the Features in the collection.
var maineMeansFeatures = image.reduceRegions({
collection: maineCounties,
reducer: ee.Reducer.mean(),
scale: 30,
});
for( key <- temporalKeys ) {
val spatialRDD = timeseriesRDD.toSpatial(key.instant )
val histogram: Map[Int, Histogram[Int]] = spatialRDD.zonalHistogram(zonesRDD)
# simple example for pyGRASS usage: raster processing via modules approach
from grass.pygrass.modules.shortcuts import general as g
from grass.pygrass.modules.shortcuts import raster as r
g.message("Filter elevation map by a threshold...")
# set computational region
input = 'elevation'
g.region(rast=input)
# hardcoded:
# r.mapcalc('elev_100m = if(elevation > 100, elevation, null())', overwrite = True)
# with variables
output = 'elev_100m'
thresh = 100.0
r.mapcalc("%s = if(%s > %d, %s, null())" % (output, input, thresh, input), overwrite = True)
#Zonal stats on timeseries
# Install v.strds.stats add-on
gmod.Module("g.extension", extension="v.strds.stats")
# Extract mean, max and min LST for municipalities
gmod.Module("v.strds.stats", input="geology", output="geology_aggr_lst",
strds="LST_Day_monthly_celsius", method="average,minimum,maximum")
# Save the attribute table of the new vector into a csv file
gmod.Module("v.db.select", map="geology_aggr_lst", file="ts_polygons.csv")
#Compute NDVI (Dask)
red = RioArray('../data/LC81160482016300LGN00_B3.TIF', 1)
nir = RioArray('../data/LC81160482016300LGN00_B4.TIF', 1)
ndvi = (nir - red) / (nir + red)
# store the resultant/intermediate arrays onto disk
da.to_hdf5('ndvi_result.hdf5', '/ndvi', ndvi)
#ZONAL STATS
from rasterstats import zonal_stats
zonal_stats("polygons.shp", "elevation.tif",
stats="count min mean max median")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment