Select to view content in your preferred language

Working in arctoolbox directly with an image service

1881
16
11-03-2021 04:38 AM
OlgaKoblet
New Contributor III

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

 

0 Kudos
16 Replies
ABishop
MVP Regular Contributor

Hello @OlgaKoblet 

I would start with analyzing the image service properties and fields.  Your search cursor for the mosaic dataset uses text but it could be different with the published image service.  After you determine what the field type is, you may be able to use the same script with different field type parameters.

Amanda Bishop, GISP
0 Kudos
OlgaKoblet
New Contributor III

Thank you for your message! You mean that I should look for another arcpy function that accesses image service fields?

0 Kudos
ABishop
MVP Regular Contributor

First you need to bring the image service into your ArcMap or Pro and view the attributes and fields.  Make sure the field types are the same as the mosaic dataset.  Then you may have to alter the python script to update that field type.

Amanda Bishop, GISP
0 Kudos
OlgaKoblet
New Contributor III

The image service is already published, and the fields are the same as in the mosaic dataset that was published, but how to access them programmatically? Basically how to iterate through an image service?

0 Kudos
OlgaKoblet
New Contributor III

forgot to tag you 🙂 @ABishop 

0 Kudos
ABishop
MVP Regular Contributor

@OlgaKoblet 

What platform?  ArcMap or ArcGIS Pro?  Also what version?  This will greatly affect the mode and type of script.

Amanda Bishop, GISP
0 Kudos
OlgaKoblet
New Contributor III

ArcGIS Pro, 2.8

0 Kudos
ABishop
MVP Regular Contributor

@OlgaKoblet 

Would you be able to share your service with me for testing?

Amanda Bishop, GISP
0 Kudos
OlgaKoblet
New Contributor III

Wouldn't it be the same for every image service? Just going through the table and collecting the OBJECTIDs in a list, for example?

0 Kudos