Select to view content in your preferred language

Get Cell Value in Python

1115
7
06-22-2010 10:57 AM
SalvatoreLarosa
Deactivated User
Hello everyone,
I need a DEM elevation using a pair of coordinates.
I have tested with gp.GetCellValue () but as output does not give me the elevation.
This is a piece of code:
........
quota = gp.GetCellValue(dem, "284889 4420904", "1")
print gp.AddMessage("La quota è  "  + str(quota))

and as message output get me:
Start Time: Tue Jun 22 20:53:51 2010
Running script 1...
La quota è <geoprocessing server result object object at 0x17FCA7E8>
Completed script 1...
Executed (1) successfully.
End Time: Tue Jun 22 20:53:58 2010 (Elapsed Time: 7,00 seconds)



Can anyone give me some advice?
0 Kudos
7 Replies
CedricWannaz
Emerging Contributor
What happens if you change your code for:

........
gpResult = gp.GetCellValue(dem, "284889 4420904", "1")
print gp.AddMessage("La quota è  "  + str( gpResult.GetOutput(0) ))


See http://webhelp.esri.com/arcgisdesktop/9.3/index.cfm?id=921&pid=914&topicname=Getting_results_from_a_... for more information.

Regards,

Cedric
0 Kudos
SalvatoreLarosa
Deactivated User
Thanks, now work!
Sorry, but I could not find the link you gave me!

Regards,

Salvatore
0 Kudos
CedricWannaz
Emerging Contributor
My pleasure!

Regards,

Cedric
0 Kudos
SalvatoreLarosa
Deactivated User
I would like to take advantage of your kindness .....

How can I make arithmetic operations such as:

dh = float(quota) - float(quotaNear)

I get this error:

<type 'exceptions.ValueError'>: invalid literal for float(): 1598,7365


If I change the string in

dh = quota - quotaNear

I get this:

<type 'exceptions.TypeError'>: unsupported operand type(s) for -: 'unicode' and 'unicode'
Failed to execute (Script).


Thank you in advance.

Salvatore
0 Kudos
CedricWannaz
Emerging Contributor
Hi,

The quota variables that you are using are strings, so you can't subtract them (which explains the second error). The first error comes from the fact that your decimal separator is a comma, and the typecast to float wants a period instead. There are a lot of ways to change this .. a simple one would be basic string replacement. Being strings or string objects, your quota variables have methods, and in particular one for sub-string replacement: replace(). You can use it simply as follows:

quota_period = quota.replace(',', '.')

which would give, applied to your case and without creating intermediary variables:
dh = float( quota.replace(',', '.') ) - float( quotaNear.replace(',', '.') )


Best regards,

Cedric
0 Kudos
SalvatoreLarosa
Deactivated User
THANKS!

IT'S WORK!
0 Kudos
CedricWannaz
Emerging Contributor
You're welcome!

Cedric
0 Kudos