0

I have a sample raster of my area which is unclassified, and I have a MS-Excel file with the first column for the day of the year and the second column showing the corresponding daily value in %. This % value will vary from day to day, hence for 365 days, I will have 365 rows for each day & its respective % value. Normally, I would use the Reclassify tool in ArcGIS to manually define the breaks (by referring to the MS-Excel %value column) for instance-Please note that the old values column in the screen-shot were taken from the MS-Excel file % value column for that particular day: The old values column in the screen-shot was taken from the MS-Excel file % value column

Thus, for each day in a year, I will have to do this manually in ArcGIS, so is there a way to automate this in R (maybe the raster package!) or in Python? Please note that the Excel file is not gridded!

UPDATE The % values of each day will vary from each other, so in the script (R or Python), I will have to manually enter the ranges using a loop and If-else for each day. Basically, New values 1 i.e.Old values 0-39 remain constant for each day, so this break value of 39 will stay the same for each day. It is the New values 2 that change in accordance with the % value in the Excel file (96% for the day seen in the screenshot) of that day. And, New value 3 is the remainder %. But, If I could read the MS-Excel file into the script that basically would automate the break values for New values 2. I hope this further clarifies the question!

2

1 Answer 1

1

I don’t think this will be too bad to run as a python script. You will want to use openpyxl to open your excel workbook and iterate through the rows. Then for each row you will grab that percent value and stick it in a remap range object, it looks like a list of lists that contain a start, end and the value you want to assign to it.

https://pro.arcgis.com/en/pro-app/tool-reference/spatial-analyst/reclassify.htm

then you simply pass that remap and the other parameters you want to the reclassify tool, which is the same tool that you would use in ArcMap.

https://pro.arcgis.com/en/pro-app/tool-reference/spatial-analyst/reclassify.htm

And voila, make sure you save it when you are done. This is untested, but it should give you a decent start.

import arcpy
import openpyxl

# get the active worksheet
excel_file_path = r"C:\your file path here.xls"
wb = openpyxl.load_workbook(excel_file_path)
ws = wb.active
raster_to_reclass = r'C:\path_here'

# # May need to check out extension, I'm not sure.
# try:
#     if arcpy.CheckExtension("Spatial") == "Available":
#         arcpy.CheckOutExtension("Spatial")
#     else:
#         print("No Spatial Analyst Extension available.")


# iterate through the rows of your excel sheet.
for row in ws.iter_rows():
    percent_number = row[3]  # use this to get the variable you are interested in for each row

    # you will need to do some processing to set your remap range here
    remap_range = arcpy.sa.RemapRange([
        [0, 39, 1],
        [39, percent_number, 2],
        [percent_number, 100, 3]
    ])

    # Perform the Reclassify
    out_ras = arcpy.sa.Reclassify(in_raster=raster_to_reclass, reclass_field="Value", remap_range, missing_values="NODATA")
    out_ras.save(r"C:\filepath_here")  # save raster


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.