Error in ExtractByMaskScript,

1420
8
06-03-2019 07:45 AM
Yaseen_TahaMustafa
New Contributor III

Hi all, 

I do use ArcGIS often and I also used buit-in Python but very long time ago when I had ArcGIS 9. This time I get back to it again with ArcGIS 10.5. 

I have exported a simple script from Module Builder of ArcGIS and I did modification on some paths. However, when I ran the script, errors appears. 

The error is:

Traceback (most recent call last):
File "E:\Post_Graduate_Affairs\Mohammed_Forest\2ndObjective\AreaforSentinel\PythonOutput\Extractpy.py", line 17, in <module>
arcpy.gp.ExtractByMask_sa(NDVItif, R29, Extract_tif9)
File "C:\Program Files (x86)\ArcGIS\Desktop10.5\ArcPy\arcpy\geoprocessing\_base.py", line 510, in <lambda>
return lambda *args: val(*gp_fixargs(args, True))
ExecuteError: ERROR 999999: Error executing function.
Failed to execute (ExtractByMask).

The script is:

import sys, string, os, arcgisscripting, math , arcpy, arcgisscripting, string
from arcpy import env
from arcpy.sa import *
gp = arcgisscripting.create()
gp.OverWriteOutput = 1
gp.CheckOutExtension("spatial")
arcpy.CheckOutExtension("spatial")
gp.AddToolbox("C:/Program Files (x86)/ArcGIS/Desktop10.5/ArcToolbox/Toolboxes/Spatial Analyst Tools.tbx")


# Local variables:
NDVItif = "E:/test/NDVI.tif"
R29 = "E:/test/R29.shp"
Extract_tif9 = "E:/test/NDVIR29.tif"

# Process: Extract by Mask
arcpy.gp.ExtractByMask_sa(NDVItif, R29, Extract_tif9)
print gp.GetMessages()

Any help would be high;y appreciated.

0 Kudos
8 Replies
DanPatterson_Retired
MVP Emeritus

Yaseen... Your imports are largely unnecessary and/or are repetitive,

Try emulating the version in the help topic to rule out those issues

Extract by Mask—Help | ArcGIS Desktop 

and the E drive is a real physical drive and not a virtual drive or usb drive?

/blogs/dan_patterson/2016/08/14/script-formatting would help provide line numbers for people to reference

Yaseen_TahaMustafa
New Contributor III

Thanks Dan for the reply. 

I tried to use the simple example that is provided in the link that you sent me and even change the folder path. It is as:

import arcpy
from arcpy import env
from arcpy.sa import *
env.workspace = "C:/testPY"
outExtractByMask = ExtractByMask("NDVI.tif", "R29.shp")
outExtractByMask.save("C:/testPY/maskextract")

However, i got more errors:

Traceback (most recent call last):
File "E:\Post_Graduate_Affairs\Mohammed_Forest\2ndObjective\AreaforSentinel\PythonOutput\Extractpy.py", line 47, in <module>
outExtractByMask = ExtractByMask("NDVI", "R29.shp")
File "C:\Program Files (x86)\ArcGIS\Desktop10.5\ArcPy\arcpy\sa\Functions.py", line 7201, in ExtractByMask
in_mask_data)
File "C:\Program Files (x86)\ArcGIS\Desktop10.5\ArcPy\arcpy\sa\Utils.py", line 53, in swapper
result = wrapper(*args, **kwargs)
File "C:\Program Files (x86)\ArcGIS\Desktop10.5\ArcPy\arcpy\sa\Functions.py", line 7197, in Wrapper
out_raster)
File "C:\Program Files (x86)\ArcGIS\Desktop10.5\ArcPy\arcpy\geoprocessing\_base.py", line 510, in <lambda>
return lambda *args: val(*gp_fixargs(args, True))
ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000865: Input raster: NDVI does not exist.
Failed to execute (ExtractByMask).

I cannot figure out all these....

0 Kudos
DanPatterson_Retired
MVP Emeritus

ERROR 000865: Input raster: NDVI does not exist.

Then try the full paths... and of course check that they actually exist as shown

outExtractByMask = ExtractByMask("C:/testPY/NDVI.tif",  "C:/testPY/R29.shp")

0 Kudos
Yaseen_TahaMustafa
New Contributor III

Sorry to bother you again and thanks for the notation. I already did what you have recommend and tried to read the errors lines, but i could not find the problem. I did run the code as you mentioned and got these errors:

Traceback (most recent call last):
File "E:\Post_Graduate_Affairs\Mohammed_Forest\2ndObjective\AreaforSentinel\PythonOutput\Extractpy.py", line 48, in <module>
outExtractByMask = ExtractByMask("C:/testPY/NDVI.tif", "C:/testPY/R29.shp")
File "C:\Program Files (x86)\ArcGIS\Desktop10.5\ArcPy\arcpy\sa\Functions.py", line 7201, in ExtractByMask
in_mask_data)
File "C:\Program Files (x86)\ArcGIS\Desktop10.5\ArcPy\arcpy\sa\Utils.py", line 53, in swapper
result = wrapper(*args, **kwargs)
File "C:\Program Files (x86)\ArcGIS\Desktop10.5\ArcPy\arcpy\sa\Functions.py", line 7197, in Wrapper
out_raster)
File "C:\Program Files (x86)\ArcGIS\Desktop10.5\ArcPy\arcpy\geoprocessing\_base.py", line 510, in <lambda>
return lambda *args: val(*gp_fixargs(args, True))
ExecuteError: ERROR 999999: Error executing function.
Failed to execute (ExtractByMask).

0 Kudos
Yaseen_TahaMustafa
New Contributor III

I also get back to my previous code:

import sys, string, os, arcgisscripting, math , arcpy, arcgisscripting, string
arcpy.env.workspace = "C:/testPY"

arcpy.env.overwriteQutput = True
from arcpy import env
from arcpy.sa import *
gp = arcgisscripting.create()
gp.OverWriteOutput = 1
gp.CheckOutExtension("spatial")
arcpy.CheckOutExtension("spatial")
gp.AddToolbox("C:/Program Files (x86)/ArcGIS/Desktop10.5/ArcToolbox/Toolboxes/Spatial Analyst Tools.tbx")


# Local variables:
NDVItif = "NDVI.tif"
R29 = "R29.shp"
Ext = "NDVIR29.tif"

# Process: Extract by Mask
arcpy.gp.ExtractByMask_sa(NDVItif, R29, Ext)
print gp.GetMessages()

and I did run the script and  got less line errors:

Traceback (most recent call last):
File "C:\testPY\Extractpy.py", line 20, in <module>
arcpy.gp.ExtractByMask_sa(NDVItif, R29, Ext)
File "C:\Program Files (x86)\ArcGIS\Desktop10.5\ArcPy\arcpy\geoprocessing\_base.py", line 510, in <lambda>
return lambda *args: val(*gp_fixargs(args, True))
ExecuteError: ERROR 999999: Error executing function.
Failed to execute (ExtractByMask).

0 Kudos
MichaelVolz
Esteemed Contributor

Can you try using the paths you modified in the script in Model Builder and then export that specific Model to see if it differs from the manual modifications that you made in the original export?

0 Kudos
Yaseen_TahaMustafa
New Contributor III

I did one last test which is using another raster instead on NDVI raster and the could works well. I was search online of the reason of getting such errors and I found this one:

"

ERROR 999999: Error executing function. The value type is incompatible with the field type. Failed to execute (CalculateField).

"

Therefore, I change the ratser thinking that the raster "NDVI" that I used could have some error values. 

Now I have to think how to have NDVI raster with a correct values. 

Thanks Dan and Michael

0 Kudos
DanPatterson_Retired
MVP Emeritus

should have been  

outExtractByMask = arcpy.ExtractByMask_sa("C:/testPY/NDVI.tif", "C:/testPY/R29.shp")

I was writing shorthand... sorry