POST
|
Thank you! Now it is working and I've changed all my raster processing to NumpyArray. Much better!
... View more
12-17-2018
02:24 AM
|
1
|
1
|
659
|
POST
|
And how can I do that in arcpy? I was also wondering if there is a way to have the raster attribute tables in numpy array. I have tried the RasterToNumpyArray, but without success. The thing I want to do seems quite easy but I'm really struggling with it =/. Thanks again!
... View more
12-11-2018
01:48 AM
|
0
|
3
|
659
|
POST
|
Hi there! I have some raster files in grid format with signed integer pixels in which I want to add some fields in their table then compute values with some formulas in the new fields. I managed to add the fields but I'm struggling with the second part. I tried with arcpy.da.UpdateCursor but it returns a RuntimeError: 'in_table' is not a table or a feature class. It seems clear for me that I can not use this function with raster file. Is there a way to compute the values directly in the raster or do I have to try a different approach? How? The code is: import arcpy as ap
import os
import random_function as rf # change name after
from arcpy.sa import *
if ap.CheckExtension('Spatial') == 'Available':
ap.CheckOutExtension('Spatial')
else:
print('Spatial Analyst extension is not available')
# Setting environments & paths
ap.env.workspace = os.path.join("D:\\", "master", "_thesis_data")
ap.env.scratchWorkspace = os.path.join(ap.env.workspace, "temp")
ap.env.overwriteOutput = True
inp_ws = ap.env.workspace
temp_ws = ap.env.scratchWorkspace
lu_folder = os.path.join(inp_ws, "lu")
lu_scenarios = []
for dirpath, dirnames, filenames in ap.da.Walk(lu_folder, datatype='RasterDataset'):
for f in filenames:
lu_scenarios.append(os.path.join(dirpath,f))
# Setting input data
climate = Raster("climate_c")
clim_soil = Raster("rc_climsoil_c")
tbl_uSOCr = os.path.join(inp_ws, "uSOCr.txt")
tbl_uSOCf = os.path.join(inp_ws, "uSOCf.txt")
tbl_uBCi = os.path.join(inp_ws, "uBCi.txt")
for (sc, lu_sc) in enumerate(lu_scenarios):
lu = Raster(lu_sc)
clim_lu = climate + lu
soc_f1k = ReclassByTable(clim_lu, tbl_uSOCf, "id", "id", "soc_factor")
soc_r1k = ReclassByTable(clim_soil, tbl_uSOCr, "id", "id", "soc_ref")
soc_1k = (soc_f1k*soc_r1k)/1000
bc_1k = ReclassByTable(clim_lu, tbl_uBCi, "id", "id", "bcs")
tc_1k = (soc_1k + bc_1k)
tc_1k_new_fields = ["tc", "tot_ha", "tot_cs"]
for field in tc_1k_new_fields:
ap.AddField_management(tc_1k, field, "FLOAT", 12, 4)
with ap.da.UpdateCursor(tc_1k, "*") as cursor:
tc_all_fields = [f.name for f in arcpy.ListFields(tc_1k)]
VALUE, COUNT, tc, tot_ha, tot_cs = row[1], row[2], row[3], row[4], row[5]
for row in cursor:
tc = VALUE/1000
tot_ha = COUNT*2500
tot_cs = tc*tot_ha
cursor.updateRow(row)
if ap.GetRasterProperties_management(tc_1k, "ALLNODATA"):
tc_1k.save(temp_ws + "\\" + "0sc" + str(sc) + "_tc" + str(mc_counter+1)) # ==> TESTING DATA!!!
print tc_1k Thanks again!
... View more
12-05-2018
03:08 AM
|
0
|
6
|
971
|
POST
|
Line 22 was a copy error. My worskpace in inp_ws but i'm saving in temp_ws, And yeah, the problem was the missing *.shp! My bad. My bad, thanks. Now is saving, but it takes 9x more time to finish... If someone has a wiser idea to reduce it, I would appreciate Thanks again!
... View more
11-27-2018
03:20 AM
|
0
|
1
|
652
|
POST
|
Hi! I have a script with a set of environment properties. I'm having a problem to save my outputs due to Error 999998. After some attempts i have figured out that the problem occurs when I set a mask for the environment (a shapefile). If I remove the mask, then my outputs are saved. Any idea how can I solve this issue? The error (RuntimeError: Error 999998: Unexpected Error) happens in line 60 of the following script: import os
import arcpy
from arcpy.sa import *
# Checking spatial analyst extension
if arcpy.CheckExtension('Spatial') == 'Available':
arcpy.CheckOutExtension('Spatial')
else:
print('Spatial Analyst extension is not available')
# Setting paths
inp_ws = os.path.join("D:\\", "master", "data", "cb")
out_ws = os.path.join(inp_ws, "results")
temp_ws = os.path.join(inp_ws, "temp")
lu_folder = os.path.join(inp_ws, "lu")
# Setting geoprocessing environments
arcpy.env.workspace = inp_ws
arcpy.env.scratchWorkspace = temp_ws
arcpy.env.overwriteOutput=True
arcpy.env.rasterStatistics = "STATISTICS 1 1"
arcpy. env.snapRaster = "soc_default"
arcpy.env.cartographicCoordinateSystem = "PROJCS['South_America_Albers_Equal_Area_Conic',GEOGCS['GCS_South_American_1969',DATUM['D_South_American_1969',SPHEROID['GRS_1967_Truncated',6378160.0,298.25]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Albers'],PARAMETER['False_Easting',0.0],PARAMETER['False_Northing',0.0],PARAMETER['Central_Meridian',-60.0],PARAMETER['Standard_Parallel_1',-5.0],PARAMETER['Standard_Parallel_2',-42.0],PARAMETER['Latitude_Of_Origin',-32.0],UNIT['Meter',1.0]]"
arcpy.env.mask = "BRA_adm0"
# Describing rasters file extension
for dirpath, dirnames, filenames in arcpy.da.Walk(inp_ws, datatype="RasterDataset"):
dir_lu = [d for d in dirnames if d == 'lu']
dirnames = dir_lu
if dirpath == inp_ws or dirpath == lu_folder:
print"\nThe formats of input rasters in folder {} are:".format(dirpath)
for f in filenames:
path = os.path.join(dirpath, f)
ext = arcpy.Describe(path).format
print "< {0} > for {1}".format(ext, f)
# Cataloging LU maps in a list
lu_scenarios = []
for dirpath, dirnames, filenames in arcpy.da.Walk(lu_folder, datatype='RasterDataset'):
for f in filenames:
lu_scenarios.append(os.path.join(dirpath,f))
# Setting input data
climate = Raster("climate")
soc_default = Raster("soc_default")
codes_soc = "soc_random_test.txt"
codes_bc = "bc_stock.txt"
for (i, lu_scenario) in enumerate(lu_scenarios):
lu = Raster(lu_scenario)
# map algebra: combine climate + lu files
clim_lu = climate + lu
# reclass by soc factor
soc_factor = ReclassByTable(clim_lu, codes_soc, "Code", "Code", "Value")
# map algebra: create soc stock
soc = (soc_factor*soc_default)/1000 #tonne C/ha
# reclass by bc stock
bc = ReclassByTable(clim_lu, codes_bc, "Code", "Code", "Value") # map algebra: soc stock + bc stock
tc = soc + bc
if str(tc) != "":
tc_output = (temp_ws + "\\" + str(i)+"-tc-test")
tc.save(tc_output)
print tc Thank you very much!!
... View more
11-27-2018
02:34 AM
|
0
|
4
|
845
|
Title | Kudos | Posted |
---|---|---|
1 | 12-17-2018 02:24 AM |
Online Status |
Offline
|
Date Last Visited |
04-01-2022
11:27 AM
|