0

I'm trying to reclassify a raster using a Python script. I know I can just use the tool in ArcMap, but I want to start understanding the code behind it.

Can anyone tell me why this doesn't work and how to fix it? I get the following error message: EOL while scanning string literal (reclass_script.py, line 17).

#Import Statements:
import arcpy
import arcpy.gp
import os
#Reclassify
arcpy.gp.Reclassify_sa("S:/MSproject/swregap/swregap_prj","VALUE","0 0;5 5;9 9;11 11;12 12;14 14;15 15;17
17;18 18;19 19;20 20;21 21;22 22;24 24;26 26;28 28;30 30;32 32;33 33;34 34;35 35;36 36;41 41;45 45;48 48;51
51;52 52;55 55;56 56;57 57;58 58;59 59;60 60;61 61;64 64;65 65;67 67;68 68;71 71;76 76;77 113;79 113;80 113;81
113;82 82;83 113;84 113;85 85;86 86;91 91;92 92;93 93;95 95;96 96;105 105;108 108;110
110;111 111;112 112;113 113;114 114;116 116;117 117;118 113;119 119;122
122","S:/MSproject/swregap/swgap_RECLASS","DATA")
PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Nov 5, 2014 at 1:48
3
  • 1
    Which version of arcgis? Consider using Reclass by Table (resources.arcgis.com/en/help/main/10.2/index.html#//…) instead of hard coding each value. Raster objects have changed between 9.3 and 10.x; your example is probably not working the same way. Have a look at the example of Reclassify resources.arcgis.com/en/help/main/10.2/index.html#/Reclassify/…, each pair is specified as a list on a Remap object : [[1,9],[2,8],[3,1],[4,6],[5,3],[6,3],[7,1]] Commented Nov 5, 2014 at 1:54
  • I just changed your title (and upvoted) - I suspect "What's wrong with my code?" is the kind of title that gets an almost automatic downvote, and is best avoided. Commented Nov 5, 2014 at 2:29
  • :) Thanks for the help! First post here; I'm pretty (very) new to all this. Commented Nov 5, 2014 at 2:35

2 Answers 2

1

Thank you, Michael and Polygeo! The Reclassify help page's sample code was actually immensely helpful -- I will remember to look there in the future. I was able to get it to work using the following script, which I lifted almost exactly from that help page.

What this does, if anyone has a similar question, is reclassify those values that are listed in the remap variable, and leave all other values alone (the purpose of including the word "DATA" when executing the reclassify. Still not totally clear on how to know exactly what import statements to use...

#Import statements
import arcpy
from arcpy import env
from arcpy.sa import *
#Set environments
env.workspace = "S:/MSproject/swregap"
# Set local variables
inRaster = "swregap_clip"
reclassField = "VALUE"
remap = RemapValue([[77, 113], [79, 113],[80, 113],[81, 113],[83, 113],[84,113],[118,113]])
# Check out the ArcGIS Spatial Analyst extension license
arcpy.CheckOutExtension("Spatial")
# Execute Reclassify
outReclassify = Reclassify(inRaster, reclassField, remap, "DATA")
# Save the output
outReclassify.save("S:/MSproject/swregap/Recls_NoRipar")
PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
answered Nov 5, 2014 at 2:40
2
  • As a new member today, I strongly recommend taking the 2-minute Tour to learn more about how the site works. Commented Nov 5, 2014 at 2:44
  • import arcpy is the only one there that's absolutely necessary, the others are there so you don't need to write them out longhand: from arcpy import env means env is a shortcut to arcpy.env which is fine, either way works. Same with from arcpy.sa import ** just means you don't have to write arcpy.sa.Reclassify( you can just write Reclassify( - saves a lot of typing the same thing. Beware of duplicated functions, if a function exists in two libs and you *import ** from both the interpreter gets confused when you call it. Commented Nov 5, 2014 at 3:52
0

I think you are getting that error because the line below has an opening " (before 0 0) that has not been matched by another one to close it:

arcpy.gp.Reclassify_sa("S:/MSproject/swregap/swregap_prj","VALUE","0 0;5 5;9 9;11 11;12 12;14 14;15 

As a test make that into a single long line and the error should go away:

arcpy.gp.Reclassify_sa("S:/MSproject/swregap/swregap_prj","VALUE","0 0;5 5;9 9;11 11;12 12;14 14;15 15;17 17;18 18;19 19;20 20;21 21;22 22;24 24;26 26;28 28;30 30;32 32;33 33;34 34;35 35;36 36;41 41;45 45;48 48;51 51;52 52;55 55;56 56;57 57;58 58;59 59;60 60;61 61;64 64;65 65;67 67;68 68;71 71;76 76;77 113;79 113;80 113;81 113;82 82;83 113;84 113;85 85;86 86;91 91;92 92;93 93;95 95;96 96;105 105;108 108;110 110;111 111;112 112;113 113;114 114;116 116;117 117;118 113;119 119;122 122","S:/MSproject/swregap/swgap_RECLASS","DATA")

Also, I think your use of arcpy.gp is antiquated (pre-10.0) so perhaps review the Reclassify (Spatial Analyst) help to see the syntax used nowadays.

answered Nov 5, 2014 at 2:21

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.