Hi all,
I am fairly new to using ArcGIS Pro, but I am slightly experienced in Python in general. I'm not sure if this is the exact place to post this question, but I am stuck. I have successfully used ArcPy to clip a raster from a shapefile, and I now want to perform the SegmentMeanShift() function on said clipped raster.
This process works in ArcGIS Pro, and I would like to have a script to automate this process for many different rasters. Below is the snippet of my code that gives me issues (most of it was copied from the documentation here)
# script to clip and segment a raster
# import libraries
import arcpy
import matplotlib.pyplot as plt
import numpy as np
import os
from arcpy.sa import *
#%% Clip raster
in_ras = r"data\s20180403.tif"
clip_ras = in_ras[:5] + r"clip_ras\\" + in_ras[5:-4] + r"_clip.tif"
feature_template = r"shapefiles\square_domain_0723.shp"
arcpy.Clip_management(
in_ras, "#", clip_ras, feature_template, "0", "ClippingGeometry")
#%% Segment raster
#check for spatial analyst license
arcpy.CheckOutExtension("Spatial")
# segment mean shift the clipped raster
spec_detail = "10"
spat_detail = "10"
min_seg_size = "20"
band_indexes = "1"
seg_clip_ras = r"data\\seg_clip_ras\\" + "s20180403" + "_clip_seg.tif"
seg_ras = SegmentMeanShift(in_ras, spec_detail,
spat_detail, min_seg_size, band_indexes)
seg_ras.save(seg_clip_ras)
The output I receive from running this chunk of the script is:
Traceback (most recent call last):
File "<ipython-input-33-232592bd359c>", line 11, in <module>
spat_detail, min_seg_size, band_indexes)
File "D:\ArcGISPro\Resources\ArcPy\arcpy\sa\Functions.py", line 7768, in SegmentMeanShift
band_indexes)
File "D:\ArcGISPro\Resources\ArcPy\arcpy\sa\Utils.py", line 53, in swapper
result = wrapper(*args, **kwargs)
File "D:\ArcGISPro\Resources\ArcPy\arcpy\sa\Functions.py", line 7761, in Wrapper
band_indexes)
File "D:\ArcGISPro\Resources\ArcPy\arcpy\geoprocessing\_base.py", line 506, in <lambda>
return lambda *args: val(*gp_fixargs(args, True))
ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000735: Output Raster Dataset: Value is required
Failed to execute (SegmentMeanShift).
I am unsure what is happening here, when I try to add another argument, the error says that SegmentMeanShift() only takes 5 arguments, not 6, which makes sense and is what I thought originally. So why is a value required for Output Raster Dataset?
Basically, when this script is finished, I would like a raster saved in my directory that is clipped and segmented from the first raster. I have looked over the documentation and other questions and I cannot seem to find this specific issue.
Thanks for help in advance, I have been stuck on this for awhile now.
seg_clip_ras = r"data\\seg_clip_ras\\" + "s20180403" + "_clip_seg.tif"
your path... what does 'data' refer to? or are you just anonymizing your actual path? (ie you have no drive specification, if you are working locally)
Hi Dan,
Thanks for the reply. 'data' simple refers to a folder named data. I have created an environment after I imported the libraries in the code above, something along the lines of:
arcpy.env.workspace = "D:\my\project\path"
but I have omitted this line for anonymity. Yes, I am working locally on my computer. I think the path works fine, because the program is able to clip the raster, but not segment it.