TransWikia.com

ArcMap 10.6 "write" a layer file

Geographic Information Systems Asked by user24007 on February 10, 2021

I want to create a group layer (or similar) of many raster files that only display when I tick them. They must not all be displaying when I make the layer, or my standard office entry level machine will crash.

I will need to recreate this "layer" as files are moved around and new ones are found on the network.

So I am after tricks for

  • a quick way to get them all into one "layer"
  • but with each raster being unticked/not showing so my machine does not crash

I have collected the list of 100 odd raster file paths in a text file. Can I easily turn this into some kind of text file that imports into ArcMap and it displays the list of grids without any being ticked to display? A text file with something wrapped around each file address, such as XML?

It does not matter if it converts into the ESRI binary format of a layer file. Making a large layer without dragging and dropping images is more important.

Added 10/9/2020

Follow Michael Stimson suggestion, I tried the following

import arcpy

mxd_path = arcpy.GetParameterAsText(0)
mxd = arcpy.mapping.MapDocument(mxd_path)

text = arcpy.GetParameterAsText(1)

layer_path = arcpy.GetParameterAsText(2)
lyr = arcpy.mapping.Layer(layer_path)

df = arcpy.mapping.ListDataFrames(mxd)

log = open(text + ".log", "w")

f = open(text, "r").readlines()
for filepath in f:
    log.write(filepath)
    newlayer = arcpy.mapping.Layer(filepath)
    arcpy.mapping.AddLayerToGroup(df, lyr, newlayer)

lyr.save()

The only failure is AddLayerToGroup. The UNC network address successfully creates a layer and I even isolated to check the lyr.save line works by itself.

Error:

Traceback (most recent call last):
  File "K:Tim KProgram ResourcesArcGISArcgis10.1GridsToLayer.py", line 22, in <module>
    arcpy.mapping.AddLayerToGroup(df, lyr, newlayer)
  File "c:program files (x86)arcgisdesktop10.6arcpyarcpyutils.py", line 182, in fn_
    return fn(*args, **kw)
  File "c:program files (x86)arcgisdesktop10.6arcpyarcpymapping.py", line 87, in AddLayerToGroup
    assert isinstance(data_frame, DataFrame)
AssertionError

Failed to execute (GridsToLayer).

I suspect the problem is in the AddLayerToGroup help where it states the layer I want to add to the group layer must be "a layer file on disk or a layer file in a map document". That would mean copying the raster files to new disk location and converting each into layer. I was hoping to simply reference where they are now. That location may change a bit each week a few files pass through a QA/QC "process" by others.

It looks like I want an unmanaged Raster Catalog, which references files instead of copying to a gdb. But, can it reference a workspace for every raster?

Turns out yes, but its not displaying the rasters, just polygons where they are located.

See answer below – changed to making an mxd of rasters.

One Answer

After a bit of poking around I found there is no way to create a new group layer in arcpy which comes with a suggested workaround. Neither can ArcMap drawing be paused from arcpy which is a bit of a disappointment.

It is possible to load layers as not visible with python, reading the data paths from a text file:

import os, sys, arcpy

FileOfPaths= r'E:TestingDataPaths.txt'
OpenDoc    = arcpy.mapping.MapDocument('CURRENT') # reference to the current document
FocusFrame = OpenDoc.activeDataFrame              # and the active data frame

with open(FileOfPaths,'r') as InFile:
    for Line in InFile:
        NewLay         = arcpy.mapping.Layer(Line) # data path makes new layer
        NewLay.visible = False
        NewLay.name    = os.path.splitext( os.path.basename(Line) )[0] # the name for this data source
        #arcpy.mapping.AddLayerToGroup(FocusFrame,NewGL,NewLay)# use this if you have a group layer 
        arcpy.mapping.AddLayer(FocusFrame,NewLay)# use this if you don't
        del NewLay

As the layer object exists prior to being added you can toggle the visible off and even change symbology before the layer is added to the map then delete the reference before making a new one otherwise arcpy might think you want to modify the first added layer by each subsequent layer and you'll only have the last layer.

The code expects to be run from within ArcMap, either with the python console or as a script tool but would work equally well if you want to add your layers to an existing map document without opening it, just change OpenDoc = arcpy.mapping.MapDocument('CURRENT') to OpenDoc = arcpy.mapping.MapDocument(r'c:fullpathtoyourMapDocument.mxd') then you can shell ArcMap to open the focus document.. as the existing document isn't open it wont redraw.

Answered by Michael Stimson on February 10, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP