Wednesday 26 October 2016

python - Specifying region of interest for google earth engine to calculate Jeffries-Matusita separability?


My objective is to compute Jeffries-Matusita separability using google earth engine python api. I have never worked with ee before, so I am trying to follow this github.


In it, to import roi it says:


table = ee.FeatureCollection('users/mortcanty/supervisedclassification/train')

trainData = image.sampleRegions(table,['CLASS_ID'])

What surprised me here is that the file here has no extension. So I tried to use shp (and only shp,not its supporting files).


However, subsequently when I try to parse 'table' in this function


def jmsep(class1,class2,image,table):
# Jeffries-Matusita separability
table1 = table.filter(
ee.Filter.eq('CLASS_ID',str(class1-1)))
m1 = image.reduceRegion(ee.Reducer.mean(),table1)\
.toArray()

s1 = image.toArray() \
.reduceRegion(ee.Reducer.covariance(),table1)\
.toArray()
table2 = table.filter(
ee.Filter.eq('CLASS_ID',str(class2-1)))
m2 = image.reduceRegion(ee.Reducer.mean(),table2)\
.toArray()
s2 = image.toArray() \
.reduceRegion(ee.Reducer.covariance(),table2,15)\
.toArray()

m12 = m1.subtract(m2)
m12 = ee.Array([m12.toList()]) # makes 2D matrix
s12i = s1.add(s2).divide(2).matrixInverse()
# first term in Bhattacharyya distance
B1 = m12.matrixMultiply(
s12i.matrixMultiply(m12.matrixTranspose())) \
.divide(8)
ds1 = s1.matrixDeterminant()
ds2 = s2.matrixDeterminant()
ds12 = s1.add(s2).matrixDeterminant()

# second term
B2 = ds12.divide(2).divide(ds1.multiply(ds2).sqrt())\
.log().divide(2)
B = ee.Number(B1.add(B2).project([0]).toList().get(0))
# J-M separability
return ee.Number(1).subtract(ee.Number(1) \
.divide(B.exp())) \
.multiply(2)



a = jmsep(5,9,image,table).getInfo()

Gives error:



Collection asset 'C:\Users\train.shp' not found



I suspect this is due to 'shp' file not being appropriate.



Answer



The problem is that the table needs to be on the server side for Earth Engine to be able to use it.


The step prior to this script is to add the .shp as an asset in your Earth Engine user.



If you look at the "Assets" tab, there is a red NEW button. Here you are able to load your shapefile as a table asset that you can use later in your script.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...