Skip navigation
All Places >

python snippets

Log in to follow, share, and participate in this socialgroup.

Recent Activity

rastrauch
Have you been meaning to try out the ArcMap Python window, but just aren’t sure how to start? Well, the five code snippets below are a great tool to familiarize yourself with the Python window and the arcpy.mapping module. Make a copy of an MXD file and move it somewhere to test out these snippets. You’ll soon see how useful a few Python tricks… (Show more)
hwigell_extenetsystems
Currently, I am attempting to write a script in python 2.7.11 using arcpy.da.update.cursor  Essentially, I have an original script that works, which I need to rewrite and shorten. The original script is posted below.    The script is for a tool in ArcGIS used to calculate a desired number of Circuit ID's for the appropriate carrier.  Based on the… (Show more)
in python snippets
hwigell_extenetsystems
Currently, I am attempting to write a script using arcpy.update.cursor  Essentially, I have an original script that works, which I need to rewrite and shorten.  I have two ways to do this and would like help improving the scripts.  Any help with the rewrite would be much appreciated.  I am posting the orginal script below. The script is for a tool in ArcGIS used to build CircuitID Numbers based on users input and data referenced in various fields.  Based on the input, the IDs are written back on to a table.  The two scripts I've worked on include:
1)with arcpy.da.UpdateCursor(nodeFeatures, fields) as cursor:
    for row in cursor:
if numIDs == range[1,10]: 
def tablulate (function, start)
return imap(count(start), function)
def count(10)
start = numIDs(1)
 count = [next+2]
 n = 1
def function (["(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (%02i)" %(i+1)])
n = 65
function(i) = (["(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (%02i)" %(i+1)])
2)with arcpy.da.UpdateCursor(nodeFeatures, fields) as cursor:
            for row in cursor:
    if numIDs = >=1
    and numIDs = <=10:
        def combinations_with_replacement(iterable, r):
             # combination_with_replacement(10,(1,'02')) --> 3,'01' 4,'02' 5,'03' 6,'04' 7,'05' 8,'06' 9,'07' 10,'08'
                 pool = tuple(iterable)
                 n = 65
                 r = 10
                 indicies[i:] = [row[i+3]] ["(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (%02i)" %(i+1)]

    elif numIDs =="0":
            row[3] = None
            row[4] = None...
            row[12] = None
            cursor.updateRow(row)
        else:
            pass
3)The original code(My work only fills in for the section of "Update the appropriate number of Circuit ID fields"):
# ###################################################################################################
# Import system modules
# ###################################################################################################

import arcpy


# ###################################################################################################
# Set universal variables
# ###################################################################################################

nodeFeatures = r'OSP Mapping Layers\Node Locations'
numIDs = arcpy.GetParameterAsText(0)
fields = ['ExteNetNodeID', 'ProjectHub', 'ProjectCarrier', 'CircuitID1', 'CircuitID2', 'CircuitID3', 'CircuitID4', 'CircuitID5', 'CircuitID6', 'CircuitID7', 'CircuitID8', 'CircuitID9', 'CircuitID10']


# #########################
# Update the appropriate number of Circuit ID fields
# #########################

with arcpy.da.UpdateCursor(nodeFeatures, fields) as cursor:
    for row in cursor:
        if numIDs == "1":
            row[3] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (01)"
            cursor.updateRow(row)
        elif numIDs == "2":
            row[3] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (01)"
            row[4] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (02)"
            cursor.updateRow(row)
        elif numIDs == "3":
            row[3] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (01)"
            row[4] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (02)"
            row[5] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (03)"
            cursor.updateRow(row)
        elif numIDs == "4":
            row[3] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (01)"
            row[4] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (02)"
            row[5] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (03)"
            row[6] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (04)"
            cursor.updateRow(row)
        elif numIDs == "5":
            row[3] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (01)"
            row[4] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (02)"
            row[5] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (03)"
            row[6] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (04)"
            row[7] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (05)"
            cursor.updateRow(row)
        elif numIDs == "6":
            row[3] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (01)"
            row[4] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (02)"
            row[5] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (03)"
            row[6] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (04)"
            row[7] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (05)"
            row[8] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (06)"
            cursor.updateRow(row)
        elif numIDs == "7":
            row[3] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (01)"
            row[4] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (02)"
            row[5] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (03)"
            row[6] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (04)"
            row[7] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (05)"
            row[8] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (06)"
            row[9] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (07)"
            cursor.updateRow(row)
        elif numIDs == "8":
            row[3] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (01)"
            row[4] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (02)"
            row[5] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (03)"
            row[6] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (04)"
            row[7] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (05)"
            row[8] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (06)"
            row[9] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (07)"
            row[10] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (08)"
            cursor.updateRow(row)
        elif numIDs == "9":
            row[3] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (01)"
            row[4] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (02)"
            row[5] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (03)"
            row[6] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (04)"
            row[7] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (05)"
            row[8] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (06)"
            row[9] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (07)"
            row[10] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (08)"
            row[11] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (09)"
            cursor.updateRow(row)
        elif numIDs == "10":
            row[3] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (01)"
            row[4] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (02)"
            row[5] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (03)"
            row[6] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (04)"
            row[7] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (05)"
            row[8] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (06)"
            row[9] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (07)"
            row[10] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (08)"
            row[11] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (09)"
            row[12] = "(" + row[0] + ") - (" + row[1] + ") - (" + row[2] + ") - (10)"
            cursor.updateRow(row)
        elif numIDs =="0":
            row[3] = None
            row[4] = None
            row[5] = None
            row[6] = None
            row[7] = None
            row[8] = None
            row[9] = None
            row[10] = None
            row[11] = None
            row[12] = None
            cursor.updateRow(row)
        else:
            pass

tinkergnome
I try to reproject a coverage class using arcpy.Project_arc. The synthax states   arcpy.Project_arc(in_cover, out_cover, projection_file)     but I cannot really get the last argument - that is, the projection file -  to work. The esri webpage describing the function (Project—Help | ArcGIS for Desktop ) inputs some kind of transformation file… (Show more)
in python snippets
Dan_Patterson
A snippet, not big enough for a blog.. """ Script:     kmz_kml.py Author:     Dan.Patterson@carleton.ca References: many Purpose: convert kmz to kml base script """ #import sys import zipfile #import glob from xml.dom import minidom def kmz_to_kml(fname):     """save kmz to kml"""     zf = zipfile.ZipFile(fname,'r')     for fn in zf.namelist():…
in python snippets
Dan_Patterson
You are not authorized to view this content.
Load more items