Hi all,
I need to access a published image service through python and perform some simple operations with it. The example I've written takes a mosaic dataset -> looks for all the images within a specified month -> checks their mean value -> and exports the image with the maximum mean value (attached file). However, I didn't figure out how to do the same starting with the image service. In the data types of the arctoolbox there is a type DEImageServer, but I can't apply the function arcpy.da.SearchCursor to it. So I have to convert somehow the service to a mosaic I guess? Any ideas how? Or should the approach be totally different?
The reason why we do that is that not all of the users have the access to the original data, so we want them to take the image service and then proceed with whatever they are doing.
def execute(self, parameters, messages):
"""The source code of the tool."""
in_mosaic = parameters[0].valueAsText
in_month = parameters[1].valueAsText
output_folder = parameters[2].valueAsText
# find all the rasters taken within the month identified in the input and check their average value of all cells.
my_mosaic = os.path.normpath(in_mosaic)
mean_month = {}
with arcpy.da.SearchCursor(my_mosaic, ['OBJECTID', 'FILE_DATE']) as cursor:
for row in cursor:
month = row[1].date().month
if int(month) == int(in_month):
raster = str(my_mosaic) + '/Raster.OBJECTID =' + str(row[0])
arcpy.CalculateStatistics_management(raster)
mean_value = arcpy.GetRasterProperties_management(in_raster=raster, property_type='MEAN')
arcpy.AddMessage(mean_value)
mean_month[row[0]] = mean_value
del cursor
objectid_max_mean = max(mean_month.iteritems(), key=operator.itemgetter(1))[0]
arcpy.AddMessage(objectid_max_mean)
#Save the raster with the maximum mean value
where_clause = "OBJECTID =" + str(objectid_max_mean)
arcpy.management.ExportMosaicDatasetItems(my_mosaic, output_folder, "", where_clause, 'TIFF', "", "NONE", "", "")
return