Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when creating BRAT table #109

Closed
kbartelt opened this issue Jul 5, 2018 · 9 comments
Closed

Error when creating BRAT table #109

kbartelt opened this issue Jul 5, 2018 · 9 comments
Labels
help wanted Requests from analysts and BRAT users to help solve specific problems or find work-arounds.

Comments

@kbartelt
Copy link

kbartelt commented Jul 5, 2018

I am coming across the following error when running BRAT Step 2 to create the BRAT Table for the Salmon watershed (18010210) for #97

I have tried restarting arcMap and my computer. Matt looked the files over for any obvious issues and tried running it on his computer which yielded the same error. @bangen @banderson1618 do you have any suggestions for how to resolve this? Thanks!
`Executing: BRAT_table_tool C:\Users\A02295870\Documents\FHC\Salmon_18010210\BRAT PrelimRun01 18010210 Salmon C:\Users\A02295870\Documents\FHC\Salmon_18010210\BRAT\Inputs\02_Network\Network_1\NHD_24k_300mReaches.shp C:\Users\A02295870\Documents\FHC\Salmon_18010210\BRAT\Inputs\03_Topography\DEM_1\NED_DEM_10m.tif # C:\Users\A02295870\Documents\FHC\Salmon_18010210\BRAT\Inputs\01_Vegetation\01_ExistingVegetation\Ex_Veg_1\LANDFIRE_140EVT.tif C:\Users\A02295870\Documents\FHC\Salmon_18010210\BRAT\Inputs\01_Vegetation\02_HistoricVegetation\Hist_Veg_1\LANDFIRE_140BPS.tif C:\Users\A02295870\Documents\FHC\Salmon_18010210\BRAT\Inputs\04_Conflict\01_ValleyBottom\Valley_1\ValleyBottom_Reaches_300m.shp C:\Users\A02295870\Documents\FHC\Salmon_18010210\BRAT\Inputs\04_Conflict\02_Roads\Roads_1\tl_2017_county_roads.shp C:\Users\A02295870\Documents\FHC\Salmon_18010210\BRAT\Inputs\04_Conflict\03_Railroads\Railroads_1\tl_2017_rails.shp C:\Users\A02295870\Documents\FHC\Salmon_18010210\BRAT\Inputs\04_Conflict\04_Canals\Canals_1\NHDCanals.shp C:\Users\A02295870\Documents\FHC\Salmon_18010210\BRAT\Inputs\04_Conflict\05_LandUse\Land_Use_1\LANDFIRE_140EVT.tif BRAT_Table true #
Start Time: Thu Jul 05 15:31:20 2018
Running script BRAT_table_tool...
Adding "iGeo" attributes to network
calculating drainage area
Adding "iVeg" attributes to network
Adding "iPC" attributes to network

Traceback (most recent call last):
File "", line 321, in execute
File "C:\Users\A02295870\Documents\FHC\pyBRAT\BRAT_table.py", line 120, in main
ipc_attributes(seg_network_copy, road, railroad, canal, valley_bottom, buf_30m, buf_100m, landuse, scratch, projPath)
File "C:\Users\A02295870\Documents\FHC\pyBRAT\BRAT_table.py", line 605, in ipc_attributes
for row in cursor:
RuntimeError: A column was specified that does not exist.

Failed to execute (BRAT_table_tool).
Failed at Thu Jul 05 15:39:57 2018 (Elapsed Time: 8 minutes 36 seconds)`

@kbartelt kbartelt added analysis A specific analysis request of ET-AL Analysts. help wanted Requests from analysts and BRAT users to help solve specific problems or find work-arounds. and removed analysis A specific analysis request of ET-AL Analysts. labels Jul 5, 2018
@banderson1618
Copy link
Contributor

I've looked at it, and from what I can tell, it looks like there's something going wrong with the following segment of code:

if "LUI_Class" in fields:
    buf_fields = [f.name for f in arcpy.ListFields(buf_100m)]
    if 'oArea' not in buf_fields:
        arcpy.AddField_management(buf_100m, 'oArea', 'DOUBLE')
        with arcpy.da.UpdateCursor(buf_100m, ['SHAPE@AREA', 'oArea']) as cursor:
            for row in cursor:
                row[1] = row[0]
                cursor.updateRow(row)
    landuse_poly = arcpy.RasterToPolygon_conversion(landuse, os.path.join(scratch, 'landuse_poly'), 'NO_SIMPLIFY', "LUI_Class")
    landuse_int = arcpy.Intersect_analysis([landuse_poly, buf_100m], os.path.join(scratch, 'landuse_int'))
    arcpy.AddField_management(landuse_int, 'propArea', 'DOUBLE')
    with arcpy.da.UpdateCursor(landuse_int, ['SHAPE@AREA', 'oArea', 'propArea']) as cursor:
        for row in cursor:
            row[2] = row[0]/row[1]
            cursor.updateRow(row)
    areaTbl = arcpy.Statistics_analysis(landuse_int, os.path.join(scratch, 'areaTbl'), [['propArea', 'SUM']], ['ReachID', 'LUI_CLASS'])
    areaPivTbl = arcpy.PivotTable_management(areaTbl, ['ReachID'], 'LUI_CLASS', 'SUM_propArea', os.path.join(scratch, 'areaPivTbl'))

    # create empty dictionary to hold input table field values
    tblDict = {}
    # add values to dictionary
    with arcpy.da.SearchCursor(areaPivTbl, ['ReachID', 'VeryLow', 'Low', 'Moderate', 'High']) as cursor:
        for row in cursor:
            tblDict[row[0]] = [row[1], row[2], row[3], row[4]]

Specifically, something is causing areaPivTbl to not have the one of the attributes it needs in that last SearchCursor. I'm not familiar with this segment of code, so I don't know how it gets those attributes normally, much less why it's not now, but that's what I could identify. @bangen, do you see anything wrong with this code?

@kbartelt
Copy link
Author

kbartelt commented Jul 5, 2018

I can't get the code to show up in a code block- sorry- but here is a snip of the error

brat_table_error

@bangen
Copy link
Contributor

bangen commented Jul 5, 2018

The issue is that in the case of the Salmon there are only 2 'LUI_Class' categories: 'VeryLow' and 'High'. This is a case we haven't run into yet, which is totally fine, but it broke the code here. So we'll need to update the code.

Braden, the attribute is from the 'LUI_Class'. The pivot table just transposes the table from a long format table to a wide format table. In this case that means turning the single column into 4 columns. But in this case not all 4 columns 'exist' once the table in transposed (only 2). Could you please edit the code so that if one of those 4 categories ('VeryLow', 'Low', 'Moderate', High') doesn't exist it assigns a value of '0' to the 'SUM_propArea' field?

Once you've made the change please put up a new release and let Karen know so she can download it. Thanks!

@banderson1618
Copy link
Contributor

Just a clarification to make sure I'm understanding you right:

After creating areaPivTbl, we want to check that the fields ['VeryLow', 'Low', 'Moderate', 'High'] exist. If a field doesn't exist, we add it to the table and populate it with zeros. Is that what you're wanting, @bangen? I'm not sure that I'm understanding what you mean by assigning a value of '0' to 'SUM_propArea', since I don't think it exists in areaPivTbl? I might be misunderstanding how the Pivot Table function works.

@bangen
Copy link
Contributor

bangen commented Jul 5, 2018

You're correct that the 'SUM_propArea' field doesn't exist in the pivot table. It's one of the fields that the pivot table is created from on this line:

areaPivTbl = arcpy.PivotTable_management(areaTbl, ['ReachID'], 'LUI_CLASS', 'SUM_propArea', os.path.join(scratch, 'areaPivTbl'))

The workflow that your suggesting should work. So, check that the fields ['VeryLow', 'Low', 'Moderate', 'High'] exist. If a field doesn't exist, add it to the table and populate it with zeros. We just need to make sure this happens before adding the values to the tblDict.

@banderson1618
Copy link
Contributor

@kbartelt the latest release should fix the problem. You can download it from the main page.

@banderson1618
Copy link
Contributor

There was a problem with the 3.0.14 release. I fixed it and created a new release. Sorry for the confusion, that was my bad. The tool downloaded from the main page now should be fine.

@banderson1618
Copy link
Contributor

@kbartelt, I'm sorry for versioning problems. 3.0.16 should solve this problem, without any of the code I was using to test it. Download the latest release, hopefully for the last time in a while.

@kbartelt
Copy link
Author

kbartelt commented Jul 9, 2018

@banderson1618 that worked, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Requests from analysts and BRAT users to help solve specific problems or find work-arounds.
Projects
None yet
Development

No branches or pull requests

3 participants