Hi there! and welcome back.
In this video I will talk to you about the difference between the Client and the Server
And how you can get the Server to execute your code.
It is very useful to know how these two interact since I didn't at first,
and now when I think about it, a lot of errors I made could have been easily avoided.
Since we use the API with JavaScript, all functionalities of this language can still be
used, like arrays, strings, dictionaries and so on.
But since we are also dealing with cloud computing, new functions and variables must come in place
to connect the client, which is your computer, to the server.
Whenever you use objects or functions that also work in other regular javascript environments,
These will get stored and computed on your device, the client.
But when you want to Earth Engine's servers to run code you have to use server objects
and server functions.
These variables and functions are generally initialized with the dubble "e", and these
are the objects that will send the information to the servers.
So for example, I can create two numbers, a and b.
And I can perform calculations with this, for example a + b
and when I print this out, we should be able to get the result.
So as you see, the console has printed the number, but when we look at the profiler,
We can see nothing is there, there is nothing there, no action has been taken by the server.
The entire calculation happened on the client side.
When instead, we define a and b as server-side variables we should be able to get the same
command executed by the server.
The server-side variable for numbers can be declared with the function ee.Number().
Capitals are important here, so watch out for that.
And as you see, the double "e" informs that this is a server side object.
So we could try to run this, but it will not work.
These variables are no longer numbers, but objects, and the plus operation does not recognize the objects.
We can check the type of the variables by printing it.
And indeed, we find that the variables are objects.
When we want to add these two variables, we will have to call a server side function,
since this are server side variables.
We can easily find all the available functions by using the docs tab.
There you find all server side variable types and by expanding them, you can find all
possible functions.
So we'll go down to ee.Number
And this are all possible operations for this type of object.
In this example we wanted to add two numbers, so the function .add() should do the trick.
And we can try to run this again.
And now we have a number.
This time we got the proper result, but the 8 you see is not just an integer.
It is also an object, ee.Number to be precice but the print command is one of the few that
can recognize Earth Engine objects
and will give you a proper .print()
When looking at the profiler we can verify the server's actions again.
And indeed, this time the server executed code.
So the first thing that happened on the server is the Algorithm number.add, and second item,
the plumbing, represents the transfer of data from the server back to the client.
It is the safest to use server side variables as often as possible since mixing the two can
cause problems.
For example if we write this piece of code, similar to the previous example, and we want
to check if "a" equals 4.
We will use another function
"c" should be set to False since the number "a"
does not equal 4
But when we check for "c" in a regular JavaScript "if" statement
we find thatit is still set to true.
This happens because the comparison does not see the variable c as a value, but as an object,
and it does not know what to make of that object.
a print statement on the other hand, or a server side function does know how to interpret
it and run the function.
To do this same operation, a server side "if" function should have been used.
This is an important thing to note when you get started with the google earth engine,
I know it would have saved me a lot of time and frustration so I hope I can spare you
of this now.
If you want to find more information about a certain function, you can visit the complete
docs online.
I will put a link to that in the description.
Here you find a manual with more advanced explanations and examples for the more common
tasks, and the reference guide contains all possible functions similar to de docs tab.
You can also find video tutorials here, but they are really long, often longer than an
hour and the quality is not always ideal.
We will come in contact with more of the different types of variables and functions in the following videos,
so stay tuned for that.
Không có nhận xét nào:
Đăng nhận xét