Help refining a script

2972
2
Jump to solution
07-09-2015 01:23 PM
BradJones
New Contributor III

I'm building a tool where a coworker can enter an ID# (called PWS ID) and then go through choose from a list of features (about 15) they want added from to the map based on the ID#. The tool will select the features based on the ID number, add them as a layer to the map and add the symbology. It wont create any new data, just what is stored in the map memory while it is open. I'm just working on the code at this point. I have a script with A LOT of variables and iterations of arcpy.MakefeatureLayer_management and arcpy.ApplySymbologyFromLayer_managment functions.  I know I can hardcode the path to the .lyr files used for symbology, but  I'm drawing a blank from there.  I'm very much a Python novice and I cant figure out if it can be done by a for loop with arcpy.mapping.ListLayers (for symbology) or some other way.

Also, I didn't add the whole script.  I left out the code for labels and applying the extent of a layer.

Thanks.

# Import module(s)

import arcpy

from arcpy import env

# Set overwrite option

env.overwriteOutput = True

# Local variables. Reduce number of variables. Hardcode path for ApplySymbologyFromLayer function or create a list or dictionary?

pws = "PWS_ID='465'" # SQL query.

ass_pws = "PWS_ID1 = '465'" # SQL query. Assessment table has different firle dname for PWS ID.

mtr_recpws = "REC_ID = '465'" # SQL query. 'REC_ID" is PWS ID of PWS receiving water through master meter, I assume.

sources = "V:\\Source Water Protection\\SWAP Model\\SWAP.code_newrasters\\GIS\\SWAP.gdb\\sources" # Will need to change if used outside           of SWPP staff because of directory permissions.

assessment = "V:\\Source Water Protection\\SWAP Model\\SWAP.code_newrasters\\GIS\\SWAP.gdb\\assessment_areas" # Will need to change           if used outside of SWPP staff because of directory permissions. Assessments not in WATER_FACILITY.gdb.

srvc_area = "V:\\WATER_FACILITY\\WATER_FACILITY.gdb\\SERVICE_AREA_COMBO"

bacti = "V:\\WATER_FACILITY\\WATER_FACILITY.gdb\\BACTI"

wtp = "V:\\WATER_FACILITY\\WATER_FACILITY.gdb\\WTP"

dbp2 = "V:\\WATER_FACILITY\\WATER_FACILITY.gdb\\DBP_STAGE2"

dbp = "V:\\WATER_FACILITY\\WATER_FACILITY.gdb\\DBP"

tank = "V:\\WATER_FACILITY\\WATER_FACILITY.gdb\\TANK"

valve = "V:\\WATER_FACILITY\\WATER_FACILITY.gdb\\VALVE_MISCELLANEOUS"

mstr_mtr = "V:\\WATER_FACILITY\\WATER_FACILITY.gdb\\MASTER_METER"

waterline = "V:\\WATER_FACILITY\\WATERLINES.gdb"

pumpstn = "V:\\WATER_FACILITY\\WATER_FACILITY.gdb\\PUMP_STATION"

prv = "V:\\WATER_FACILITY\\WATER_FACILITY.gdb\\PRV"

press_plane = "V:\\WATER_FACILITY\\WATER_FACILITY.gdb\\PRESSURE_PLANES"

office = "V:\\WATER_FACILITY\\WATER_FACILITY.gdb\\OFFICE"

src_layer = "Sources"

ass_layer = "Assessment Area"

srvc_layer = "Service Area"

bacti_layer = "Bacti"

wtp_layer = "WTP"

dbp_layer = "DBP"

dbp2_layer = "DBP Stage 2"

tank_layer = "Tank"

valve_layer = "Valve"

mtr_layer = "Master Meter"

line_layer = "Waterline"

pump_layer = "Pump Station"

prv_layer = "PRV"

press_layer = "Pressure Plane"

ofc_layer = "Office"

src_symb = "V:\\Source Water Protection\\PWS_ID_Tool\\SOURCE.lyr"

ass_symb = "V:\\Source Water Protection\\PWS_ID_Tool\\ASSESSMENT.lyr"

wtp_symb = "V:\\Source Water Protection\\PWS_ID_Tool\\WTP.lyr"

valve_symb = "V:\\Source Water Protection\\PWS_ID_Tool\\VALVE.lyr"

tank_symb = "V:\\Source Water Protection\\PWS_ID_Tool\\TANK.lyr"

dbp_symb = "V:\\Source Water Protection\\PWS_ID_Tool\\DBP.lyr"

dbp2_symb = "V:\\Source Water Protection\\PWS_ID_Tool\\DBP2.lyr"

bacti_symb = "V:\\Source Water Protection\\PWS_ID_Tool\\BACTI.lyr"

mtr_symb = "V:\\Source Water Protection\\PWS_ID_Tool\\MASTER_METER.lyr"

ofc_symb = "V:\\Source Water Protection\\PWS_ID_Tool\\OFFICE.lyr"

prv_symb = "V:\\Source Water Protection\\PWS_ID_Tool\\PRV.lyr"

pump_symb = "V:\\Source Water Protection\\PWS_ID_Tool\\PUMP.lyr"

# Make feature layers

arcpy.MakeFeatureLayer_management(office, ofc_layer, pws)

arcpy.MakeFeatureLayer_management(press_plane, press_layer, pws)

arcpy.MakeFeatureLayer_management(srvc_area, srvc_layer, pws)

arcpy.MakeFeatureLayer_management(mstr_mtr, mtr_layer, mtr_recpws)

arcpy.MakeFeatureLayer_management(pumpstn, pump_layer, pws)

arcpy.MakeFeatureLayer_management(prv, prv_layer, pws)

arcpy.MakeFeatureLayer_management(tank, tank_layer, pws)

arcpy.MakeFeatureLayer_management(valve, valve_layer, pws)

arcpy.MakeFeatureLayer_management(wtp, wtp_layer, pws)

arcpy.MakeFeatureLayer_management(dbp, dbp_layer, pws)

arcpy.MakeFeatureLayer_management(dbp2, dbp2_layer, pws)

arcpy.MakeFeatureLayer_management(bacti, bacti_layer, pws)

arcpy.MakeFeatureLayer_management(sources, src_layer, pws)

arcpy.MakeFeatureLayer_management(assessment, ass_layer, ass_pws)

# Apply the symbology from the symbology layer to the input layers

arcpy.ApplySymbologyFromLayer_management(src_layer, src_symb)

arcpy.ApplySymbologyFromLayer_management(ass_layer, ass_symb)

arcpy.ApplySymbologyFromLayer_management(bacti_layer, bacti_symb)

arcpy.ApplySymbologyFromLayer_management(wtp_layer, wtp_symb)

arcpy.ApplySymbologyFromLayer_management(valve_layer, valve_symb)

arcpy.ApplySymbologyFromLayer_management(dbp_layer, dbp_symb)

arcpy.ApplySymbologyFromLayer_management(dbp2_layer, dbp2_symb)

arcpy.ApplySymbologyFromLayer_management(tank_layer, tank_symb)

arcpy.ApplySymbologyFromLayer_management(valve_layer, valve_symb)

arcpy.ApplySymbologyFromLayer_management(mtr_layer, mtr_symb)

arcpy.ApplySymbologyFromLayer_management(ofc_layer, ofc_symb)

arcpy.ApplySymbologyFromLayer_management(prv_layer, prv_symb)

arcpy.ApplySymbologyFromLayer_management(pump_layer, pump_symb)

0 Kudos
1 Solution

Accepted Solutions
DarrenWiens2
MVP Honored Contributor

Something like this may be useful, although it occurs to me now that you may want to use a dictionary instead of lists:

import os, arcpy

pws = "PWS_ID='465'"
files = ['DBP_STAGE2', 'BACTI', 'WTP', ..., 'OFFICE'] # list all file names
layers = ['DBP Stage 2', 'Bacti', 'WTP', ..., 'Office']
symNames = ['DBP2', 'BACTI', 'WTP', ..., 'OFFICE']

root = 'V:\\WATER_FACILITY\\WATER_FACILITY.gdb'
paths = []
for file in files:
    path = os.path.join(root, file)
    paths.append(path) # now have list of paths

symRoot = 'V:\\Source Water Protection\\PWS_ID_Tool'
symPaths = []
for symName in symNames:
    symPath = os.path.join(symRoot, symName) + '.lyr'
    symPaths.append(symPath) # now have list of symbol paths

for i in range(len(files)):
    arcpy.MakeFeatureLayer_management(paths, layers, pws) # make all the layers
    arcpy.ApplySymbologyFromLayer_management(layers, symPaths) # apply the symbologies

Ideally, your files, layers, etc. would have a consistent naming convention (e.g. all DBP2 files had the name 'DBP2', not "DBP_STAGE2', etc.) so you'd only need one list, but you've got to work with what you've got.

View solution in original post

2 Replies
DarrenWiens2
MVP Honored Contributor

Something like this may be useful, although it occurs to me now that you may want to use a dictionary instead of lists:

import os, arcpy

pws = "PWS_ID='465'"
files = ['DBP_STAGE2', 'BACTI', 'WTP', ..., 'OFFICE'] # list all file names
layers = ['DBP Stage 2', 'Bacti', 'WTP', ..., 'Office']
symNames = ['DBP2', 'BACTI', 'WTP', ..., 'OFFICE']

root = 'V:\\WATER_FACILITY\\WATER_FACILITY.gdb'
paths = []
for file in files:
    path = os.path.join(root, file)
    paths.append(path) # now have list of paths

symRoot = 'V:\\Source Water Protection\\PWS_ID_Tool'
symPaths = []
for symName in symNames:
    symPath = os.path.join(symRoot, symName) + '.lyr'
    symPaths.append(symPath) # now have list of symbol paths

for i in range(len(files)):
    arcpy.MakeFeatureLayer_management(paths, layers, pws) # make all the layers
    arcpy.ApplySymbologyFromLayer_management(layers, symPaths) # apply the symbologies

Ideally, your files, layers, etc. would have a consistent naming convention (e.g. all DBP2 files had the name 'DBP2', not "DBP_STAGE2', etc.) so you'd only need one list, but you've got to work with what you've got.

BradJones
New Contributor III

THANKS!  I knew there was a way to do it.  I haven't ever used the os module but I will explore this.  The fc names I can't change but I can change the .lyr names to match so I will only have one list.  you have no idea how giddy this makes me. Ha!

0 Kudos