Skip to main content
edited tags; edited title
Link
PolyGeo
  • 65.4k
  • 29
  • 114
  • 345

How do I have my Adding layers add to TOC at the end of theArcPy process?

Post Reopened by Mapperz
Clearified question
Source Link

How do I have my layers add to TOC at the end of the process

I would like each function to be added into the opened mxd. This was originally designed to run in Catalog, but the users will all be using ArcMap.

This is my Script:

How do I have my layers add to TOC

This is my Script:

How do I have my layers add to TOC at the end of the process

I would like each function to be added into the opened mxd. This was originally designed to run in Catalog, but the users will all be using ArcMap.

This is my Script:

Post Closed as "Needs details or clarity" by julien, Dan C, Mapperz
added 320 characters in body
Source Link
Nathan W
  • 35k
  • 5
  • 99
  • 148

nrcs_script_main.py

Description: This Script will serve to create a geodatabase, create Area of Interest Raster Datasets, and process them with external functions

Parameters (type): folder_for_gdb (folder), file_gdb_name (string), dem (raster dataset), input_mask_polygon (shapefile), contour_interval (double), smoothing_distance (linear unit)

---------------------------------------------------------------------------

Set the necessary product code

import arceditor

Import arcpy module

import arcpy

Check out any necessary licenses

arcpy.CheckOutExtension("spatial")

Import tool modules

import sys import os import shutil import aoigen import useraoigen import contourgen import hydrology

Script arguments

project_folder = arcpy.GetParameterAsText(0) folder_for_gdb = project_folder + "\" + arcpy.GetParameterAsText(1) file_gdb_name = arcpy.GetParameterAsText(1) dem = arcpy.GetParameterAsText(2) dem_sr = arcpy.GetParameterAsText(3) extraction_checkbox = arcpy.GetParameterAsText(4) input_mask_polygon = arcpy.GetParameterAsText(5) mask_polygon_sr = arcpy.GetParameterAsText(6) aoi_buffer_dist = arcpy.GetParameterAsText(7) contour_checkbox = arcpy.GetParameterAsText(8) contour_interval = arcpy.GetParameterAsText(9) if contour_interval == '#' or not contour_interval: contour_interval = "1" # provide a default value if unspecified smoothing_distance = arcpy.GetParameterAsText(10) contour_thinning = arcpy.GetParameterAsText(11) import_rule_layer = arcpy.GetParameterAsText(12) contour_unit_conversion_factor = arcpy.GetParameterAsText(13) hydrology_checkbox = arcpy.GetParameterAsText(14)

Copy and Paste job folder

arcpy.AddMessage("Copy and Paste Folder") src = os.path.join("C:\",'ArcPy_NRCS','Template') dst = folder_for_gdb shutil.copytree(src,dst,symlinks=False,ignore=None)

Rename Map Document

arcpy.AddMessage("Rename directories and Documents") os.rename(dst + '\template.mxd', dst + '\' + file_gdb_name + '.mxd')

Execute Selected Functions

if extraction_checkbox == "true": arcpy.AddMessage("Aoigen.aoi") aoigen.aoi(folder_for_gdb, file_gdb_name, dem, input_mask_polygon,aoi_buffer_dist) if contour_checkbox == "true": arcpy.AddMessage("Running Contouring Functions") contourgen.contour(folder_for_gdb, file_gdb_name, contour_interval, smoothing_distance, contour_thinning, import_rule_layer, contour_unit_conversion_factor) if hydrology_checkbox == "true": arcpy.AddMessage("Running Hydrology Functions") aoi = folder_for_gdb +"\" + "Project_Datasets.gdb" + "\" + "AOI_dem" hydrology.drainvis(folder_for_gdb,file_gdb_name,aoi)
else: arcpy.AddMessage("useraoigen.useraoi") useraoigen.useraoi(folder_for_gdb, file_gdb_name, dem) if contour_checkbox == "true": arcpy.AddMessage("Running Contouring Functions") contourgen.contour(folder_for_gdb, file_gdb_name, contour_interval, smoothing_distance, contour_thinning, import_rule_layer,contour_unit_conversion_factor) if hydrology_checkbox == "true": arcpy.AddMessage("Running Hydrology Functions") aoi = folder_for_gdb +"\" + "Project_Datasets.gdb" + "\" + "AOI_dem" hydrology.drainvis(folder_for_gdb,file_gdb_name,aoi)

Add Map in ArcGIS

mapdoc = os.path.join(folder_for_gdb,file_gdb_name+".mxd")

arcpy.AddMessage("Lidar Toolset Finished! Your Map document will open when this window is closed.")

# nrcs_script_main.py

# Description: This Script will serve to create a geodatabase, create Area of Interest Raster Datasets, and process them with external functions
# Parameters (type): folder_for_gdb (folder), file_gdb_name (string), dem (raster dataset), input_mask_polygon (shapefile), contour_interval (double), smoothing_distance (linear unit)
# ---------------------------------------------------------------------------

# Set the necessary product code
import arceditor

# Import arcpy module
import arcpy

# Check out any necessary licenses
arcpy.CheckOutExtension("spatial")

# Import tool modules
import sys
import os
import shutil
import aoigen
import useraoigen
import contourgen
import hydrology
# Script arguments
project_folder = arcpy.GetParameterAsText(0)
folder_for_gdb = project_folder + "\\" + arcpy.GetParameterAsText(1)
file_gdb_name = arcpy.GetParameterAsText(1)
dem = arcpy.GetParameterAsText(2)
dem_sr = arcpy.GetParameterAsText(3)
extraction_checkbox = arcpy.GetParameterAsText(4)
input_mask_polygon = arcpy.GetParameterAsText(5)
mask_polygon_sr = arcpy.GetParameterAsText(6)
aoi_buffer_dist = arcpy.GetParameterAsText(7)
contour_checkbox = arcpy.GetParameterAsText(8)
contour_interval = arcpy.GetParameterAsText(9)
if contour_interval == '#' or not contour_interval:
    contour_interval = "1" # provide a default value if unspecified
smoothing_distance = arcpy.GetParameterAsText(10)
contour_thinning = arcpy.GetParameterAsText(11)
import_rule_layer = arcpy.GetParameterAsText(12)
contour_unit_conversion_factor = arcpy.GetParameterAsText(13)
hydrology_checkbox = arcpy.GetParameterAsText(14)

# Copy and Paste job folder
arcpy.AddMessage("Copy and Paste Folder")
src = os.path.join("C:\\",'ArcPy_NRCS','Template')
dst = folder_for_gdb
shutil.copytree(src,dst,symlinks=False,ignore=None)

# Rename Map Document
arcpy.AddMessage("Rename directories and Documents")
os.rename(dst + '\\template.mxd', dst + '\\' + file_gdb_name + '.mxd')

# Execute Selected Functions
if extraction_checkbox == "true":
    arcpy.AddMessage("Aoigen.aoi")
    aoigen.aoi(folder_for_gdb, file_gdb_name, dem, input_mask_polygon,aoi_buffer_dist)
    if contour_checkbox == "true":
        arcpy.AddMessage("Running Contouring Functions")
        contourgen.contour(folder_for_gdb, file_gdb_name, contour_interval, smoothing_distance, contour_thinning, import_rule_layer, contour_unit_conversion_factor)
    if hydrology_checkbox == "true":
        arcpy.AddMessage("Running Hydrology Functions")
        aoi =  folder_for_gdb +"\\" + "Project_Datasets.gdb" + "\\" + "AOI_dem"
        hydrology.drainvis(folder_for_gdb,file_gdb_name,aoi)       
else:
    arcpy.AddMessage("useraoigen.useraoi")
    useraoigen.useraoi(folder_for_gdb, file_gdb_name, dem)
    if contour_checkbox == "true":
        arcpy.AddMessage("Running Contouring Functions")
        contourgen.contour(folder_for_gdb, file_gdb_name, contour_interval, smoothing_distance, contour_thinning, import_rule_layer,contour_unit_conversion_factor)
    if hydrology_checkbox == "true":
        arcpy.AddMessage("Running Hydrology Functions")
        aoi =  folder_for_gdb +"\\" + "Project_Datasets.gdb" + "\\" + "AOI_dem"
        hydrology.drainvis(folder_for_gdb,file_gdb_name,aoi)       

# Add Map in ArcGIS
mapdoc = os.path.join(folder_for_gdb,file_gdb_name+".mxd")


arcpy.AddMessage("Lidar Toolset Finished! Your Map document will open when this window is closed.")  

nrcs_script_main.py

Description: This Script will serve to create a geodatabase, create Area of Interest Raster Datasets, and process them with external functions

Parameters (type): folder_for_gdb (folder), file_gdb_name (string), dem (raster dataset), input_mask_polygon (shapefile), contour_interval (double), smoothing_distance (linear unit)

---------------------------------------------------------------------------

Set the necessary product code

import arceditor

Import arcpy module

import arcpy

Check out any necessary licenses

arcpy.CheckOutExtension("spatial")

Import tool modules

import sys import os import shutil import aoigen import useraoigen import contourgen import hydrology

Script arguments

project_folder = arcpy.GetParameterAsText(0) folder_for_gdb = project_folder + "\" + arcpy.GetParameterAsText(1) file_gdb_name = arcpy.GetParameterAsText(1) dem = arcpy.GetParameterAsText(2) dem_sr = arcpy.GetParameterAsText(3) extraction_checkbox = arcpy.GetParameterAsText(4) input_mask_polygon = arcpy.GetParameterAsText(5) mask_polygon_sr = arcpy.GetParameterAsText(6) aoi_buffer_dist = arcpy.GetParameterAsText(7) contour_checkbox = arcpy.GetParameterAsText(8) contour_interval = arcpy.GetParameterAsText(9) if contour_interval == '#' or not contour_interval: contour_interval = "1" # provide a default value if unspecified smoothing_distance = arcpy.GetParameterAsText(10) contour_thinning = arcpy.GetParameterAsText(11) import_rule_layer = arcpy.GetParameterAsText(12) contour_unit_conversion_factor = arcpy.GetParameterAsText(13) hydrology_checkbox = arcpy.GetParameterAsText(14)

Copy and Paste job folder

arcpy.AddMessage("Copy and Paste Folder") src = os.path.join("C:\",'ArcPy_NRCS','Template') dst = folder_for_gdb shutil.copytree(src,dst,symlinks=False,ignore=None)

Rename Map Document

arcpy.AddMessage("Rename directories and Documents") os.rename(dst + '\template.mxd', dst + '\' + file_gdb_name + '.mxd')

Execute Selected Functions

if extraction_checkbox == "true": arcpy.AddMessage("Aoigen.aoi") aoigen.aoi(folder_for_gdb, file_gdb_name, dem, input_mask_polygon,aoi_buffer_dist) if contour_checkbox == "true": arcpy.AddMessage("Running Contouring Functions") contourgen.contour(folder_for_gdb, file_gdb_name, contour_interval, smoothing_distance, contour_thinning, import_rule_layer, contour_unit_conversion_factor) if hydrology_checkbox == "true": arcpy.AddMessage("Running Hydrology Functions") aoi = folder_for_gdb +"\" + "Project_Datasets.gdb" + "\" + "AOI_dem" hydrology.drainvis(folder_for_gdb,file_gdb_name,aoi)
else: arcpy.AddMessage("useraoigen.useraoi") useraoigen.useraoi(folder_for_gdb, file_gdb_name, dem) if contour_checkbox == "true": arcpy.AddMessage("Running Contouring Functions") contourgen.contour(folder_for_gdb, file_gdb_name, contour_interval, smoothing_distance, contour_thinning, import_rule_layer,contour_unit_conversion_factor) if hydrology_checkbox == "true": arcpy.AddMessage("Running Hydrology Functions") aoi = folder_for_gdb +"\" + "Project_Datasets.gdb" + "\" + "AOI_dem" hydrology.drainvis(folder_for_gdb,file_gdb_name,aoi)

Add Map in ArcGIS

mapdoc = os.path.join(folder_for_gdb,file_gdb_name+".mxd")

arcpy.AddMessage("Lidar Toolset Finished! Your Map document will open when this window is closed.")

# nrcs_script_main.py

# Description: This Script will serve to create a geodatabase, create Area of Interest Raster Datasets, and process them with external functions
# Parameters (type): folder_for_gdb (folder), file_gdb_name (string), dem (raster dataset), input_mask_polygon (shapefile), contour_interval (double), smoothing_distance (linear unit)
# ---------------------------------------------------------------------------

# Set the necessary product code
import arceditor

# Import arcpy module
import arcpy

# Check out any necessary licenses
arcpy.CheckOutExtension("spatial")

# Import tool modules
import sys
import os
import shutil
import aoigen
import useraoigen
import contourgen
import hydrology
# Script arguments
project_folder = arcpy.GetParameterAsText(0)
folder_for_gdb = project_folder + "\\" + arcpy.GetParameterAsText(1)
file_gdb_name = arcpy.GetParameterAsText(1)
dem = arcpy.GetParameterAsText(2)
dem_sr = arcpy.GetParameterAsText(3)
extraction_checkbox = arcpy.GetParameterAsText(4)
input_mask_polygon = arcpy.GetParameterAsText(5)
mask_polygon_sr = arcpy.GetParameterAsText(6)
aoi_buffer_dist = arcpy.GetParameterAsText(7)
contour_checkbox = arcpy.GetParameterAsText(8)
contour_interval = arcpy.GetParameterAsText(9)
if contour_interval == '#' or not contour_interval:
    contour_interval = "1" # provide a default value if unspecified
smoothing_distance = arcpy.GetParameterAsText(10)
contour_thinning = arcpy.GetParameterAsText(11)
import_rule_layer = arcpy.GetParameterAsText(12)
contour_unit_conversion_factor = arcpy.GetParameterAsText(13)
hydrology_checkbox = arcpy.GetParameterAsText(14)

# Copy and Paste job folder
arcpy.AddMessage("Copy and Paste Folder")
src = os.path.join("C:\\",'ArcPy_NRCS','Template')
dst = folder_for_gdb
shutil.copytree(src,dst,symlinks=False,ignore=None)

# Rename Map Document
arcpy.AddMessage("Rename directories and Documents")
os.rename(dst + '\\template.mxd', dst + '\\' + file_gdb_name + '.mxd')

# Execute Selected Functions
if extraction_checkbox == "true":
    arcpy.AddMessage("Aoigen.aoi")
    aoigen.aoi(folder_for_gdb, file_gdb_name, dem, input_mask_polygon,aoi_buffer_dist)
    if contour_checkbox == "true":
        arcpy.AddMessage("Running Contouring Functions")
        contourgen.contour(folder_for_gdb, file_gdb_name, contour_interval, smoothing_distance, contour_thinning, import_rule_layer, contour_unit_conversion_factor)
    if hydrology_checkbox == "true":
        arcpy.AddMessage("Running Hydrology Functions")
        aoi =  folder_for_gdb +"\\" + "Project_Datasets.gdb" + "\\" + "AOI_dem"
        hydrology.drainvis(folder_for_gdb,file_gdb_name,aoi)       
else:
    arcpy.AddMessage("useraoigen.useraoi")
    useraoigen.useraoi(folder_for_gdb, file_gdb_name, dem)
    if contour_checkbox == "true":
        arcpy.AddMessage("Running Contouring Functions")
        contourgen.contour(folder_for_gdb, file_gdb_name, contour_interval, smoothing_distance, contour_thinning, import_rule_layer,contour_unit_conversion_factor)
    if hydrology_checkbox == "true":
        arcpy.AddMessage("Running Hydrology Functions")
        aoi =  folder_for_gdb +"\\" + "Project_Datasets.gdb" + "\\" + "AOI_dem"
        hydrology.drainvis(folder_for_gdb,file_gdb_name,aoi)       

# Add Map in ArcGIS
mapdoc = os.path.join(folder_for_gdb,file_gdb_name+".mxd")


arcpy.AddMessage("Lidar Toolset Finished! Your Map document will open when this window is closed.")  
Source Link
Loading