Python script to enter String & Dates in existing fields

775
12
07-19-2013 10:44 AM
JacobDrvar
New Contributor II
All,

I am attempting to write a Python Script that selects a record in the attribute table, zooms to the selected feature, and prompts the user to enter their name, date created, and date modified.  The name, date created, and date modified fields already exist in the attribute table.  The name field is a string type, and the date fields are date types.  I have the first part of the script written, it prompts the user to enter a RENUM value, which selects a record in the Parcels attribute table, and zooms to the selected feature.  How would I get the script to prompt the user to enter their name, date created, and date modified?  Then, have the information they enter stored in the attribute table?  I am going to have the users run the script from ArcToolbox.   Any suggestions would be welcomed.  Thanks.  What I have so far is displayed below:

#Import
import arcpy

#Variables
Parcels = arcpy.GetParameterAsText(0)

mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
lyr = arcpy.mapping.ListLayers(mxd, "Parcels", df)[0]
arcpy.AddMessage(lyr.name)

#Logic
try:
 whereClause = "RENUM = "+Parcels+""
 arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION", whereClause)
 df.extent = lyr.getSelectedExtent()
 df.scale = df.scale*1.1

except:
 print arcpy.GetMessages()
Tags (2)
0 Kudos
12 Replies
JamesCrandall
MVP Frequent Contributor
All,

I am attempting to write a Python Script that selects a record in the attribute table, zooms to the selected feature, and prompts the user to enter their name, date created, and date modified.  The name, date created, and date modified fields already exist in the attribute table.  The name field is a string type, and the date fields are date types.  I have the first part of the script written, it prompts the user to enter a RENUM value, which selects a record in the Parcels attribute table, and zooms to the selected feature.  How would I get the script to prompt the user to enter their name, date created, and date modified?  Then, have the information they enter stored in the attribute table?  I am going to have the users run the script from ArcToolbox.   Any suggestions would be welcomed.  Thanks.  What I have so far is displayed below:

#Import
import arcpy

#Variables
Parcels = arcpy.GetParameterAsText(0)

mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
lyr = arcpy.mapping.ListLayers(mxd, "Parcels", df)[0]
arcpy.AddMessage(lyr.name)

#Logic
try:
 whereClause = "RENUM = "+Parcels+""
 arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION", whereClause)
 df.extent = lyr.getSelectedExtent()
 df.scale = df.scale*1.1

except:
 print arcpy.GetMessages()



One pattern that may work is to implement a Python Add-In that allows for some user interaction, opens another Toolbox dialog that accepts the Name and date parameters and peform the update to the attribute table of ths selected features.

I know that is a very general outline, but this is something that we have done and it works fine.
0 Kudos
JacobDrvar
New Contributor II
Can you provide an example?
0 Kudos
JacobDrvar
New Contributor II
One pattern that may work is to implement a Python Add-In that allows for some user interaction, opens another Toolbox dialog that accepts the Name and date parameters and peform the update to the attribute table of ths selected features.

I know that is a very general outline, but this is something that we have done and it works fine.


Thanks for the suggestion.  I may do that eventually.  But, for now I would prefer to get the script up and running.
0 Kudos
JamesCrandall
MVP Frequent Contributor
First is to build the Add-In:

http://resources.arcgis.com/en/help/main/10.1/index.html#//014p00000025000000

Then, code the click event to open the Toolbox you create that accepts the Name and Date parameters and peforms the updates.  That is, build tat Toolbox and launch it from your add-in like so:


pythonaddins.GPToolDialog(r'C:\Documents\MyToolbox.tbx', 'AttributeEditor')"""

0 Kudos
JacobDrvar
New Contributor II
Thanks.  I will need to read up on Python add-ins.  With the add-in, will I still need to figure out the scripting part?  Or will the add-in allow a GUI?
0 Kudos
JamesCrandall
MVP Frequent Contributor
Thanks.  I will need to read up on Python add-ins.  With the add-in, will I still need to figure out the scripting part?  Or will the add-in allow a GUI?


It depends on the type of Add-In.  If it is a Python Add-In, then your GUI options are quite limited.  I think if you have Visual Studio and build your user interfaces there, then this type of Add-In can extend your options quite a bit.  I've not gone thru the later myself, I have built a ton of COM objects/extension in ArcObjects complete with package installers, but have not had a chance to check out the add-in model.

My experience with the Python Add-in implementations has been with mixed feelings.  You get lots of restrictions placed on you for the GUI-side, but I love the Geoprocessing (scripting) for speeding up the development time compared to building your own ArcObjects.  What I am suggesting is that we have used an extra Geoprocessing tool that simply acts as a GUI to collect user input by launching it from a Python Add-In.  That is, the add-in allows you to create the toolbar, with some ways to 'do stuff', then you can launch another toolbox that is merely a user interface or/and includes the geoprocessing that you need to get done.
0 Kudos
JacobDrvar
New Contributor II
I am attempting to create the toolbar/button and nothing.  The button does not work, but I believe it has something to do with the script.  ESRI suggestions adding the following script to the onClick(self) function. 

# Implementation of OnClick method of Button's class
def onClick(self):
        # Get the current map document and the first data frame.
        mxd = arcpy.mapping.MapDocument('current')
        df = arcpy.mapping.ListDataFrames(mxd)[0]
        # Call the zoomToSelectedFeatures() method of the data frame class
        df.zoomToSelectedFeatures()


Then, my code looks like:

import arcpy
import pythonaddins

class ParcelSearch(object):
 """Implementation for Python_addin.btn1 (Button)"""
 def __init__(self):
  self.enabled = True
  self.checked = False
 def onClick(self):
  #Implementation of OnClick method of Button's class
def onClick(self):
  #Get the current map document and the first data frame.
  mxd = arcpy.mapping.MapDocument('current')
  df = arcpy.mapping.ListDataFrames(mxd)[0]
  #Call the zoomToSelectedFeatures() method of the data frame class
  df.zoomToSelectedFeatures()
pass


What am I missing to get the zoom to selected features part working?
0 Kudos
JacobDrvar
New Contributor II
I am attempting to get the toolbar/button working.  ESRI (http://resources.arcgis.com/en/help/main/10.1/index.html#/button/014p0000001z000000/) suggests adding the following script to the onClick(self) function:

# Implementation of OnClick method of Button's class
def onClick(self):
        # Get the current map document and the first data frame.
        mxd = arcpy.mapping.MapDocument('current')
        df = arcpy.mapping.ListDataFrames(mxd)[0]
        # Call the zoomToSelectedFeatures() method of the data frame class
        df.zoomToSelectedFeatures()


My script then looks like this:

import arcpy
import pythonaddins

class ParcelSearch(object):
#Implementation for Python_addin.btn1 (Button)
 def __init__(self):
  self.enabled = True
  self.checked = False
 def onClick(self):
  #Get the current map document and the first data frame.
  mxd = arcpy.mapping.MapDocument('current')
  df = arcpy.mapping.ListDataFrames(mxd)[0]
  #Call the zoomToSelectedFeatures() method of the data frame class
  df.zoomToSelectedFeatures()

 pass


What am I overlooking to get the zoom to selected features working?  I believe something is wrong with my Python_addin.py.  Nothing happens with I click the button within ArcMap.
0 Kudos
JamesCrandall
MVP Frequent Contributor
I see a "pass" in there at the end --- you should only have that in the def/methods that you do not want to implement.  Not sure if that should be there or if it would have any effect, but I'd go ahead and remove it and see if the tool works then.
0 Kudos