I am looking for a way to use ArcPy to add all unique values to a layer.

My tool converts ASC files to TIF files and then adds the layers to my mxd.

All works well: I have created layer files to store color attributes for each layer.

The problem is that some layers do not always have the same amount of unique values.

To explain: if I convert a project A, it could be that layer BEST_SERV_MO99.TIF has only 3 unique values stored. If I convert an other project B, that layer BEST_SERV_MO99.TIF has now 12 unique values.

Is there a possibility to add for this layer the unique values by ArcPy? If I use the layer file (.lyr) it will show for Project B only 3 colors, even if that layer would have 8 colors...

Here is a small snippet out of my converter:

filename_asc = i
tif_file = filename_asc.replace(".ASC", ".TIF")
tif2_file = filename_asc.replace(".ASC", "")
lyr_file = filename_asc.replace(".ASC", ".lyr")

outRaster = os.path.join(path_now, tif_file)
symbologyLayer = os.path.join(self.lyr_dir, lyr_file)

addLayer = arcpy.mapping.Layer(outRaster)
arcpy.mapping.AddLayerToGroup(self.df, targetGroupLayer, addLayer, "BOTTOM")
layers = arcpy.mapping.ListLayers(self.mxd)

self.pyramid(os.path.join(path_now, tif_file))
for layer in layers:
    if layer.name == tif_file:
        if os.path.isfile(symbologyLayer) == True:
            self.err("Farbschema konnte nicht angewendet werden: " + symbologyLayer.encode('utf-8'))

        layer.name = tif2_file

edit: if I try to see the symbologyType, it tells me always: OTHER

self.log("layer.symbologyType: " + layer.symbologyType) # output --> layer.symbologyType: OTHER
  • I can see you're somewhat adept at python so have a read of RasterToNumpyArray resources.arcgis.com/en/help/main/10.2/index.html#//… then start with an empty list and iterate the array adding values not in list and done! all the unique values. It's a bit more difficult to modify a renderer to match the count of unique values in arcpy though, you might need to have a selection of suitable layers to match symbology to based on the contents of your value list. – Michael Stimson Sep 14 '16 at 0:42
  • Thank you for your help! I will try this, but first I have to fix the problem, that my layer.symbologyType is always OTHER, do you have any tips for that? if layer.symbologyType is OTHER I can not manipulate the layer anyway... it should be UNIQUE_VALUES – schurtertom Sep 14 '16 at 11:45
  • Have a read of desktop.arcgis.com/en/arcmap/10.3/analyze/arcpy-mapping/… - The only way to change the symbology type is by publishing the desired result to a layer file and using the UpdateLayer function. though I don't see how UpdateLayer is much different to ApplySymbologyFromLayer, it should overwrite the symbology, including the symbologyType, from the source layer - provided the source layer is also a raster with the same pixel type. – Michael Stimson Sep 14 '16 at 22:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.