1

It's been years since working with ArcPy and I'm having trouble setting up the parameters in my script tool.

What I'm trying to do:

I have a layer file (.lyr) that is a state wide index grid of available elevation model tiles. The layer's attribute table has a field "URL" that contains the download link. In my map document, I want to be able to make a selection of records in the layer and download the elevation model image tiles in my selected project area.

Here's the layer file. And the data table at the bottom

What works:

I can run the script tool successfully but it's downloading all 15,000 files in the table not just my selected records.

this is the .py behind my script tool:

`

import urllib

import os

import arcpy

#where am I saving the download?
path = (str(arcpy.GetParameterAsText(1)))
os.chdir(path)


#path to my index layer...stil unsure how to get this as a dropdown menu item in my tool
fc= r"C:\Users\Z\Desktop\LiDAR_Index.lyr"
cursor = arcpy.SearchCursor(fc)

for row in cursor:
    field ="URL"
    link =(row.getValue(field))
    head, tail = os.path.split(str(link))
    print "Downloading: "+ tail
    download=urllib.urlretrieve((str(link)), tail)

`

I'm lost as to how to only make this act on the selected records and not the whole feature class or shapefile.

Current UI on left and tool properties on right

2

As you have a map layer in your map document, you need to work not on the .lyr file, but instead on the Layer object. To get the Layer object in your current map document, use the arcpy.ListLayers() function.

mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
map_lyr = arcpy.mapping.ListLayers(mxd, "Lidar Index", df)[0].name

When you select certain features on the map, you run the cursor for the map layer object:

with arcpy.da.SearchCursor(map_lyr,"url_field_name") as cur:
  for row in cur:
    #code

The cursors respect the selection; so you get back only the rows that were selected.

| improve this answer | |

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.