Select to view content in your preferred language

Working in arctoolbox directly with an image service

1970
16
11-03-2021 04:38 AM
Labels (2)
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

I am not sure without seeing the data.  But here is some information with python snippets: https://pro.arcgis.com/en/pro-app/latest/arcpy/get-started/data-access-using-cursors.htm 

Everything else I find is suggesting the use of iterators in Model Builder here: https://pro.arcgis.com/en/pro-app/latest/tool-reference/modelbuilder-toolbox/iterate-row-selection.h... 

Amanda Bishop, GISP
0 Kudos
OlgaKoblet
New Contributor III

Just to be sure: the attached script works without any problems, but if I am to change the input of the function arcpy.da.SearchCursor from mosaic to image service, it doesn't work anymore since this function takes as input only a feature class, layer, table, or table view. Meaning that I either have to convert the image service prior to this step or use some other function for iterating through the image service.

0 Kudos
ABishop
MVP Regular Contributor

@OlgaKoblet 

In the input feature, try pasting the image service URL.

Amanda Bishop, GISP
0 Kudos
OlgaKoblet
New Contributor III

Mit der URL geht es auch nicht. Vielleicht wäre Python API eine bessere Option? 

OlgaKoblet_0-1636016065554.png

 

0 Kudos
Zikang
by
New Contributor III

olga,

There is no simple arcpy solution for this workflow at the moment.

We will need to take advantages of image service rest apis for some of the operations. (or using equivalent arcgis python apis)

For instance, you may use image service "query" operation to get item info to loop through them; you may use image service "compute statistics and histograms" operation to figure out the statistics of them.

https://developers.arcgis.com/rest/services-reference/enterprise/image-service.htm

https://developers.arcgis.com/python/api-reference/arcgis.raster.toc.html#imagerylayer 

Why not arcpy? - still using the compute stats as an example, I think arcpy will try to write it back to data, you cannot do it with image service for now. theoretically, you can only do it when you are the admin/publisher of a service.

zikang

0 Kudos
OlgaKoblet
New Contributor III

thank you for your message? Do you know if I can just select rasters in the image service (not in the mosaic as I do) based on some attribute, and then export them locally? May be calculating statistics is not a good example. Basically doing in arcpy the manuall steps of selecting the rows in the attribute table of the footprints and then doing "selection -> create layer from selected features".

The limitation (for us) of the ArcGIS API for python is that the services need to be in portal, and not just on the server. 

0 Kudos
OlgaKoblet
New Contributor III

@PeterBecker  may be you have ideas?

0 Kudos