Zoom to ATS Location and Merge Features

645
7
04-09-2013 04:18 PM
CourtneySorsdahl
New Contributor
Alright. Let me start off by saying I am completely new to python . I am a GIS student and for a project I have (ambitiously) decided to try and make a couple (I thought simple) script tools. The first one I want is to be able to have the user input which meridian, township, range, section, quarter section, and LSD they want to look at (yes just look at) and then have the map zoom to that area. I need the script to still work if any of the fields are left blank (they put in meridian, township, range but nothing else, for example). Simple right? I thought so but apparently my brain is too mushy to figure it out. I have no idea where to start.

The second thing i want to do is a little more tricky. I downloaded multiple NTS base features and need to (in the end) be able to rename them, define the projection, reproject them, merge like-features into one (all hydrology together, all roads together, etc), and then use this final feature in a cost distance analysis. What I've done for this is create a model that iterates through the folder I have all of my folders of base features in and rename them (Hydrology_0, Hydrology_1, etc) and defines the projection and then puts the shapefiles back into the folder of folders (so outside their original ones but inside the folder containing them). I then manually merged them and used them but is there any way to make this simpler using python?

Any help is greatly appreciated! Thanks so much
Tags (2)
0 Kudos
7 Replies
MathewCoyle
Frequent Contributor
For your first problem it is fairly simple to design a script tool to take optional parameters. Then based on which parameters are input create a query to input into a layer selection or layer definition query to then get the selected extent and set that to your data frame extent.

Here's a simple example.
arcpy.SelectLayerByAttribute_management(
    lyr, "NEW_SELECTION", "{0} = '{1}'".format(field, value))

df.extent = lyr.getSelectedExtent(False)


The second problem is a little harder, it will be highly dependant on your particular environment and requirements in regards to specific features and how they are organized.
0 Kudos
CourtneySorsdahl
New Contributor
Thanks so much for the tip. So I kind of have some kind of code but I don't know if i have everything I need.. Every time I run it it gives me different errors, but most have to do with my expression. any advice?? Oh, I'm also trying to make a script tool out of it and I have all of the parameters set as strings..

import arcpy
from arcpy import mapping

mxd = arcpy.mapping.MapDocument(r"F:\CostDistanceEx.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
lyr = arcpy.mapping.ListLayers(mxd, "QTR_83K", df)[0]

Meridian = arcpy.GetParameterAsText(0)
if Meridian == '':
    Meridian = "*" 

Range = arcpy.GetParameterAsText(1)
if Range == '':
    Range = "*" 

Township = arcpy.GetParameterAsText(2)
if Township == '':
    Township = "*" 

Section = arcpy.GetParameterAsText(3)
if Section == '':
    Section = "*" 

Quarter = arcpy.GetParameterAsText(4)
if Quarter == '':
    Quarter = "*" 

Expression = "\"MER\" = " + str(Meridian) + "\"RGE\" = " + str(Range) + "\"TWP\" = " + str(Township) + "\"SEC\" = " + str(Section) + "\"QTR\" = " + str(Quarter)

arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION", Expression)

df.extent = lyr.getSelectedExtent(False)

df.scale = df.scale * 1.1

arcpy.RefreshActiveView()
del mxd
0 Kudos
MathewCoyle
Frequent Contributor
To do things like zooming etc you will need to work with the 'current' mxd.
mxd = arcpy.mapping.MapDocument("current")

* is not a valid query value for arc (unless you are dealing with strings). It is hard to tell, are the values in your table you are querying in string format or numeric?
0 Kudos
CourtneySorsdahl
New Contributor
Sorry it's confusing, I'm not very good at explaining things! the MER, RGE, TWP and SEC fields are all long (numeric) and the QTR field is a string.. like I said I'm very new to python (this is my second month learning it) and I can't really find anything along the same lines. Thanks so much for the help. if anything else is unclear let me know
0 Kudos
MathewCoyle
Frequent Contributor
I came up with something like the following. It will take any parameter input and append the associated field name and the value as a tuple to a list. Then iterate through that list appending the values to the expression for however many values there are. Then at the end it removes the 'AND' tag that is defaulted at the end of each loop. The AddMessage is a good line to add when trouble shooting to ensure your variables are being put together as expected.
list1 = []
Expression = ''

Meridian = arcpy.GetParameterAsText(0)
if Meridian:
    list1.append(('MER', Meridian))

Range = arcpy.GetParameterAsText(1)
if Range:
    list1.append(('RGE', Range))

Township = arcpy.GetParameterAsText(2)
if Township:
    list1.append(('TWP', Township))

Section = arcpy.GetParameterAsText(3)
if Section:
    list1.append(('SEC', Section))

Quarter = arcpy.GetParameterAsText(4)
if Quarter:
    list1.append(('QTR', "'{0}'".format(Quarter)))

for field, val in list1:
    Expression += '{0} = {1} AND '.format(field, val)
Expression = Expression.strip()[:-4]
arcpy.AddMessage(Expression)
0 Kudos
CourtneySorsdahl
New Contributor
Alright yeah that makes sense! now.. I would just put my select by attributes and the rest of it as i had it or does it need to be changed? was the rest of it okay? because I just tried it and that part worked fine but its giviing me a syntax error at this part
arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION", Expression)

if i follow it with whats below, is that the correct way to do it?
df.extent = lyr.getSelectedExtent(False)
df.scale = df.scale * 1.1
arcpy.RefreshActiveView()
del mxd
0 Kudos
CourtneySorsdahl
New Contributor
Scratch that! thanks so so much!!! you're a life saver. Works great
0 Kudos