I run this simple script, line-by-line, and I get the error
Runtime error SyntaxError: can't assign to literal (, line 1)
What am I doing wrong?
import arcpy
# Set workspace
arcpy.env.workspace = "D:/pro-restaura/ProRestaura-dados/outout-mg/reproject-mg"
# Set local variables
in_features = (arcpy.Raster, "area_pro_restaura_minas.tif")
out_feature_class = "D:/pro-restaura/ProRestaura-dados/outout-mg/reproject-mg/class_UpVeg.tif"
where_clause = "Value" = 2103
# Execute Select
arcpy.Select_analysis(in_features, out_feature_class, where_clause)
3 Answers 3
You need to use Extract by Attributes function if you have Spatial Analyst to extract specific values from raster data:
Here is a code snippet from the help above:
import arcpy
from arcpy import env
# Import Spatial Analyst extension
from arcpy.sa import *
# Specify the workspace
env.workspace = r"F:\Ahmad\Test\ASTGTM2_N22E093"
# Use the extract function. "test_dem" is the name of the raster file
# with esri grid format
# If you want to extract multiple values at once, you have to write
# it like this: "VALUE in (500,1000,1500)"
attExtract = ExtractByAttributes("test_dem", "VALUE in(500,1000,1500)")
# Save the output raster file. No extension means esro grid as input file
attExtract.save(r"F:\Ahmad\Test\ASTGTM2_N22E093\test_dem_2")
You need to adapt your code based on the code above.
This the attribute table of the input raster "test_dem"
:
The attribute table of the output raster "test_dem_2"
:
Here is how it looks in the table of content:
-
Sorry, but I don't understand. I don't want to do spatial analys, I just want to select in the attribute table by value. "where_clause = ' "Value" = ('2103', '2109', '1405', 2103') 'Lilian Guimarães– Lilian Guimarães2019年01月21日 13:24:20 +00:00Commented Jan 21, 2019 at 13:24
-
arcpy.Select_analysis
deals only with vector data. If you need to extract values from raster data, you need to usearcpy.sa.ExtractByAttributes()
. To use the function, you need to have Spatial Analyst extension.ahmadhanb– ahmadhanb2019年01月21日 13:29:18 +00:00Commented Jan 21, 2019 at 13:29 -
Thank you very much to try to help me! I made changes to the methods and the declaration form of the string (Value) and integer values (2103, 2109), but doesnt seems to work Here it is:
import arcpy from arcpy import env env.workspace = "D: \ pro-restore \ pro-data-outout-mg \ reproject-mg" attExtract = ExtractByAttributes ('MG_2015_2017.tif', '"Value" = (2103,2109,1405,2103)' attExtract.save ("D: \ pro-restore \ ProRestore-data \ outout-mg \ reproject-mg \ class_UpVeg.tif")
Lilian Guimarães– Lilian Guimarães2019年01月21日 13:41:19 +00:00Commented Jan 21, 2019 at 13:41 -
I updated my answer. If you don't have Spatial Analyst, it will not work. You need to add this line
from arcpy.sa import *
if you have Spatial Analyst.ahmadhanb– ahmadhanb2019年01月21日 14:01:11 +00:00Commented Jan 21, 2019 at 14:01
You have a problem with the where_clause variable. You should surround the clause with quotations.
where_clause = '"Value" = 2103'
-
Now I understand, mr.@Mohannad ! I'm really appreciate your help! Unfortunately, I'm still stuck with a simple problem setting up my workspace. I use `env.workspace = r 'D: / pro-restore / ProReset-data / ProReset' And I get the message "Parsing error SyntaxError: invalid syntax (line 1)". I tried to invert the bars in writing the path, but still the same error happens.Lilian Guimarães– Lilian Guimarães2019年01月21日 14:43:49 +00:00Commented Jan 21, 2019 at 14:43
-
1@Lilian I don't think that the problem is in this line. To be sure try to run it seperately preceded by "import arcpy" and "from arcpy import env".Mohannad Adham– Mohannad Adham2019年01月21日 16:10:04 +00:00Commented Jan 21, 2019 at 16:10
you can use this clause, for example, attExtract = ExtractByAttributes("test_dem", "VALUE=500 OR value=1000 OR value=1500")
where_clause = '"Value" = 2103'