POST
|
Hi, I'm trying to calculate one field in the attribute table of a feature class based on another field with an if/else clause (please see script). As a test, I used the values of "10" and "20" as results and it works. Now I need to replace that with two more complex expressions using other fields within the attribute table. That is: Instead of "10" : 0.0006*B*(Q 0.31 ) *(Area_km2 0.5 ) *(Z_MAX/1000)*MAT and Instead of "20" : 0.02B(Q) -0.31 *Area_km2 0.5 *(Z_MAX/1000) "B"; "Q"; "Area_km2"; "Z_MAX"; "MAT" being those other fields in the attribute table. I'm not quiet sure how to include those fields as well as the Power function within the codeblock part of the script. Any help would be greatly appreciate it. Many thanks # Add "BQART_QS" field to Nodes FC
arcpy.AddField_management(nodes, "BQART_QS", "Double")
BQART_QS_expression = "calc(!MAT!)"
codeblock = """def calc(val):
if val >= 2:
return 10
else:
return 20"""
arcpy.CalculateField_management(nodes, "BQART_QS", BQART_QS_expression, "PYTHON",codeblock)
... View more
10-17-2016
08:15 AM
|
0
|
4
|
1571
|
POST
|
Hi, I'm getting this window error when double clicking an mxd from ArcCatalog. Anyone knows about this? Many thanks
... View more
11-19-2015
05:00 AM
|
0
|
3
|
7608
|
POST
|
Thanks Dan. It actually gives the same results within the script so I think it should be fine. Cheers
... View more
11-09-2015
01:25 AM
|
1
|
1
|
1484
|
POST
|
Thanks Dan. I guess when it comes to rasters it should be something like this? It seems to be working. raster1 = Raster(env.workspace + "\\" + "Maas_hvel_jan")
raster2 = Raster(env.workspace + "\\" + "Maas_hvel_feb")
raster3 = Raster(env.workspace + "\\" + "Maas_hvel_mar")
raster4 = Raster(env.workspace + "\\" + "Maas_hvel_apr")
raster5 = Raster(env.workspace + "\\" + "Maas_hvel_may")
raster6 = Raster(env.workspace + "\\" + "Maas_hvel_jun")
raster7 = Raster(env.workspace + "\\" + "Maas_hvel_jul")
raster8 = Raster(env.workspace + "\\" + "Maas_hvel_aug")
raster9 = Raster(env.workspace + "\\" + "Maas_hvel_sep")
raster10 = Raster(env.workspace + "\\" + "Maas_hvel_oct")
raster11 = Raster(env.workspace + "\\" + "Maas_hvel_nov")
raster12 = Raster(env.workspace + "\\" + "Maas_hvel_dec")
array1 = arcpy.RasterToNumPyArray(raster1,nodata_to_value=10000000000)
array2 = arcpy.RasterToNumPyArray(raster2,nodata_to_value=10000000000)
array3 = arcpy.RasterToNumPyArray(raster3,nodata_to_value=10000000000)
array4 = arcpy.RasterToNumPyArray(raster4,nodata_to_value=10000000000)
array5 = arcpy.RasterToNumPyArray(raster5,nodata_to_value=10000000000)
array6 = arcpy.RasterToNumPyArray(raster6,nodata_to_value=10000000000)
array7 = arcpy.RasterToNumPyArray(raster7,nodata_to_value=10000000000)
array8 = arcpy.RasterToNumPyArray(raster8,nodata_to_value=10000000000)
array9 = arcpy.RasterToNumPyArray(raster9,nodata_to_value=10000000000)
array10 = arcpy.RasterToNumPyArray(raster10,nodata_to_value=10000000000)
array11 = arcpy.RasterToNumPyArray(raster11,nodata_to_value=10000000000)
array12 = arcpy.RasterToNumPyArray(raster12,nodata_to_value=10000000000)
marray = numpy.ma.masked_values([(array1), (array2), (array3), (array4), (array5), (array6), (array7),(array8), (array9), (array10), (array11), (array12)],10000000000)
per = 20
percentile_val = numpy.percentile(marray.compressed(),(per))
print percentile_val
... View more
11-06-2015
05:13 AM
|
1
|
2
|
1484
|
POST
|
Hi there, I've been calculating percentiles out of monthly raster values, that is one month/raster at a time. I've been using the RasterToNumPyArray function and all good. Like this: array = arcpy.RasterToNumPyArray((raster),nodata_to_value=10000000000)
marray = numpy.ma.masked_values(array,10000000000)
per = 20
percentile_val = numpy.percentile(marray.compressed(),(per))
print percentile_val Now, I need to do the same but taking all 12 months into the percentile calculation. That is, using all cell values from all 12 rasters to calculate the percentile. Like putting all values from the 12 raster months into a single pool of values. I tried this: array = arcpy.RasterToNumPyArray([(raster1), (raster2), (raster3), (raster4), (raster5), (raster6), (raster7),
(raster8), (raster9), (raster10), (raster11), (raster12)],nodata_to_value=10000000000)
marray = numpy.ma.masked_values(array,10000000000)
per = 20
percentile_val = numpy.percentile(marray.compressed(),(per))
print percentile_val But I think the RasterToNumPyArray function does not allow multiple rasters. I wonder if someone could give me some tips to overcome this issue. Many thanks in advance Tony
... View more
11-06-2015
03:36 AM
|
0
|
7
|
4340
|
POST
|
(I meant "Thanks Dan" on my previous reply) I'll keep an eye on your 3rd example. Cheers
... View more
10-26-2015
05:32 AM
|
0
|
1
|
529
|
POST
|
Thanks Darren, That seems a very elegant way of doing it. I'm trying to adapt your script to my set of 12 rasters using the RasterToNumpyArray. Thanks again!
... View more
10-26-2015
03:28 AM
|
0
|
3
|
529
|
POST
|
Thanks Darren, That works nicely. The only thing is that the Max value you get is 24 so that needs to be set back to 12 in the final output. Other than that, that's good. Thanks again Tony
... View more
10-26-2015
03:25 AM
|
0
|
0
|
1042
|
POST
|
It is something that I have in excel but it seems not that straight forward to code in Python (or maybe it is...) A function in python that calculates the "last occurrence"; in this case the last occurrence of 0 so it can start counting from there, I guess it would do the job. However, I cannot find in the documentation anything related to that.
... View more
10-23-2015
01:11 AM
|
0
|
0
|
1042
|
POST
|
Thanks Neil/Darren, Darren, WHEN running the code it is not relevant here. I'll try to graphically explain again: I have a stack of 12 cells each one representing presence or absence of data (1/0) for 12 months. I need to produce an output raster cell that tells me the maximum number of cumulative months with data. In example 1 above the output is 5 because there is data for sep, oct, nov, dec and jan In example 2 above the output is 3 because there is data for apr, may and jun Cheers
... View more
10-23-2015
01:06 AM
|
0
|
4
|
1042
|
POST
|
Thanks Neil. Sorry, I probably didn't explain myself properly. I'm not dealing with years here; just months of any year. I need to calculate cumulative number of months that hold data (in this case value 1). The calculation should loop through the 12 months with no fixed starting point. Hope it is a bit more clear now. Thanks again!
... View more
10-22-2015
08:45 AM
|
0
|
1
|
1042
|
Title | Kudos | Posted |
---|---|---|
1 | 11-06-2015 05:13 AM | |
1 | 11-09-2015 01:25 AM | |
1 | 11-12-2014 05:27 AM |
Online Status |
Offline
|
Date Last Visited |
07-12-2022
09:52 AM
|