Python script read coordinate system, drag and drop

3249
17
06-18-2012 12:02 PM
ZackBartlett
New Contributor III
I have a python script that accepts a project shapefile, asks for a few parameters, and does the following:
1. Projects to a new coordinate system (as defined by the user)
2. Calculates new X and Y values
3. Saves as a layer file
4. Saves as a KMZ file.

The script, fortunately, works as intended, but I'm trying to make it run nicer and be more user-friendly. I want to do the following things:

1. When the original shapefile is loaded, I would like the tool to read it's coordinate system and enter that information in a box in the tool (similar to what happens in the "Project" tool when a shapefile is added). It would just be there for reference and would not be editable. I understand this may need to be done through the Tool Validator class, but I do not have much experience with this.

2. The tool does not currently allow me to drag-and-drop a shapefile into the tools input from the TOC. How do I enable this functionality?

Here is the code I have so far, for reference:

# Author: Zachary Bartlett
# Date: June 14, 2012
# 1. Input .shp
# 2. Read coordinate system
# 3. Input: New coordinate system
# 4. Project: to new MTM coordinate system
# 5. Output: new shapefile projected to MTM
# 5. Input: New X and Y field names
# 6. Add X and Y coordinates
# 7. Output: .lyr with custom name
# 8. Output: .kmz with custom name

import arcpy, os, glob

# INPUT SHAPEFILE

input_shp = arcpy.GetParameterAsText(0)
arcpy.AddMessage("Input shapefile: " + str(input_shp))

work_folder_pos = input_shp.rfind("\\")
work_folder = input_shp[0:int(work_folder_pos)]
arcpy.AddMessage("Workspace: " + str(work_folder))

# READ COORDINATE SYSTEM

coordinatesystem_ORIG = arcpy.Describe(input_shp).spatialReference
arcpy.AddMessage("Input Coordinate System: " + str(coordinatesystem_ORIG.name))

# PROJECT input_shp TO NEW COORDINATE SYSTEM AND SAVE AS NEW SHP

MTM_shp = arcpy.GetParameterAsText(1)
coordinatesystem_NEW = arcpy.GetParameterAsText(2)
arcpy.AddMessage("Output shapefile: " + str(MTM_shp))
arcpy.AddMessage("Output Coordinate System: " + str(coordinatesystem_NEW))
arcpy.Project_management(input_shp, MTM_shp, coordinatesystem_NEW) 

# ADD X AND Y COORDINATES TO MTM_shp AS POINT_X AND POINT_Y

arcpy.AddXY_management(MTM_shp)
#arcpy.AddMessage("Input shapefile coordinate system: ") + coordinatesystem_ORIG

# INPUT X AND Y FIELD NAMES

X_MTM_fieldname = arcpy.GetParameterAsText(3)
Y_MTM_fieldname = arcpy.GetParameterAsText(4)
arcpy.AddField_management(MTM_shp, X_MTM_fieldname, "double", "15", "3", "", "", "NON_NULLABLE", "REQUIRED")
arcpy.AddField_management(MTM_shp, Y_MTM_fieldname, "double", "15", "3", "", "", "NON_NULLABLE", "REQUIRED")

# COPY POINT_X AND POINT_Y to X_MTM_fieldname and Y_MTM_fieldname

arcpy.CalculateField_management(MTM_shp, X_MTM_fieldname, '!POINT_X!', "PYTHON")
arcpy.CalculateField_management(MTM_shp, Y_MTM_fieldname, '!POINT_Y!', "PYTHON")

#DELETE POINT_X AND POINT_Y COLUMNS

arcpy.DeleteField_management(MTM_shp, ["POINT_X", "POINT_Y"])

# CREATE TEMPORARY FEATURE LAYER

fl_temp = work_folder + "\MTM_lf_temp"
arcpy.MakeFeatureLayer_management(MTM_shp, fl_temp)

#OUTPUT LYR WITH CUSTOM NAME

output_lyr = arcpy.GetParameterAsText(5)
#output_lyr = arcpy.AddMessage("Output Layer File: ")
arcpy.SaveToLayerFile_management(fl_temp, output_lyr)

# OUTPUT DBF WITH CUSTOM NAME

##output_dbf = arcpy.GetParameterAsText(6)
##arcpy.TableToDBASE_conversion(MTM_shp, output_dbf)

#OUTPUT KMZ WITH CUTOM NAME

output_kmz = arcpy.GetParameterAsText(6)
#output_kmz = arcpy.AddMessage("Output KMZ File: ")
arcpy.LayerToKML_conversion(output_lyr, output_kmz, 10000)



Any help would be greatly appreciated.

Thanks!

-Zack
Tags (2)
0 Kudos
17 Replies
JakeSkinner
Esri Esteemed Contributor
Hi Zack,

1.  You will need to update the updateParameters function within the ToolValidator class with the following:

def updateParameters(self):
  if self.params[0].value:
    sr = arcpy.Describe(self.params[0].value).spatialReference
    self.params[7].value = sr.name
  self.params[7].enabled = 0
  return


The index value for self.params[7] may be different depending on where you want the input coordinate system to be shown within the tool.


2.  Change the parameter's Data Type to 'Feature Layer'.  This will allow you to drag and drop, and also provide a dropdown listing the feature classes within ArcMap.
0 Kudos
ZackBartlett
New Contributor III
What sort of modifications do I need to make to my script code?

Thanks

-Zack
0 Kudos
JakeSkinner
Esri Esteemed Contributor
You won't have to make any to your python script.
0 Kudos
ZackBartlett
New Contributor III
When I add your text to the tool validator, my "Output .shp" (GetParameterAsText[1]) field is greyed out. And the "Output .kmz" (GetParameterAsText[6]) is now missing.

I tried adding another Parameter to house the Coordinate System, but the field doesn't populate when I add a shapefile, and I get the following error my "Input .shp" (GetParameterAsText[0]):

ERROR
updateParameters Execution Error: Runtime error : ParameterObject: Invalid input value for Value property
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Can you send a screen shot of your Properties tab within the script properties?  Depending on where you have the parameter set up for the coordinate system you will have to change index values for the 'arcpy.GetParameterAsText' function, and the self.params within the ToolValidator.
0 Kudos
ZackBartlett
New Contributor III
Here's a screenshot of the Parameters tab:

[ATTACH=CONFIG]15389[/ATTACH]
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Hi Zack,

It looks like you set the Input Coordinate system as the second parameter, so this will have an index of 1.  Therefore, you will need to increase all of your index values for arcpy.GetParameterAsText (excluding arcpy.GetParameterAsText(0)) by 1 within your code. 

The ToolValidator should appear as below:

def updateParameters(self):
    if self.params[0].value:
      sr = arcpy.Describe(self.params[0].value).spatialReference
      self.params[1].value = sr.name
      self.params[1].enabled = 0
    return
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Also, I noticed in your original code, you have the output dbf and kmz file as the same index for the arcpy.GetParameterAsText:

# OUTPUT DBF WITH CUSTOM NAME  ##output_dbf = arcpy.GetParameterAsText(6) ##arcpy.TableToDBASE_conversion(MTM_shp, output_dbf)  #OUTPUT KMZ WITH CUTOM NAME  output_kmz = arcpy.GetParameterAsText(6) #output_kmz = arcpy.AddMessage("Output KMZ File: ") arcpy.LayerToKML_conversion(output_lyr, output_kmz, 10000)


Change the OUTPUT KMZ to:

output_kmz = arcpy.GetParameterAsText(7)
0 Kudos
AdamCox1
Occasional Contributor II
Hello, this thread was hugely helpful to me, as I was trying to do the exact same things as Zack.  I was having the same problem after following this advice and found that the issue was that I still had the spatial reference parameter input type set to Spatial Reference, when I needed to have it set to String because that's what I was trying to put into it...

However, I have a question about the other suggestion for dragging and dropping.  Changing the parameter input type to "Feature Layer" does allow me to drag and drop from the TOC, which is great, but if I try to drag and drop from an INACTIVE dataframe, ArcMap encounters a serious error and crashes.  Is this a known problem?  Am I missing a piece of the puzzle?

Thanks for your help,
Adam
0 Kudos