Select to view content in your preferred language

Python script read coordinate system, drag and drop

4948
17
06-18-2012 12:02 PM
ZackBartlett
Regular Contributor
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
ChrisFox3
Frequent Contributor
Adam, which version and sp are you at? I can't reproduce the crash at 10.1 sp1, but I will say drag/drop or referencing a layer from an inactive data frame has never been supported. You can see this if you click the drop-down in the parameter it will only show you the layers from the active data frame. You should get an error on the parameter saying the input dataset doesn't exist. However, it still shouldn't crash and if we can reproduce it I would like to log a bug.
0 Kudos
AdamCox1
Deactivated User
Hello Chris, I'm running ArcGIS 10.0 without any service pack.

I've been retrying this a number of times, and it crashes pretty reliably, but not every single time.  One time gave me the error message you mentioned after I correctly input a layer and then tried to drag one from an inactive dataframe, but now that doesn't work and it crashes everytime...  I'm using a script that I created, and it's one of my first attempts at any meaningful programming, so I could have something wrong that I'm unaware of.  I can give you more details on the script if you need.

Thanks!!
0 Kudos
ChrisFox3
Frequent Contributor
To narrow it down, could you try drag/drop into a system tool like copy features? Does it still crash. This would tell us if it is specific to your tool or not.
0 Kudos
AdamCox1
Deactivated User
Yes, I tried that right away and it doesn't crash, I just get the "does not exist/not supported" message. 

Ok, it only happens when I drag/drop into the parameter that has been modified in the ToolValidator with the code described above.  Here is the code that I have in ToolValidator:

([1] is the input fc parameter set to feature layer, and [2] is the string parameter that just shows the coordinate system of [1]):

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


When I comment out this code, the parameter drag/drop operation functions as it should.
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Hi Adam,

Can you send a screen shot of your Parameters tab within the Script properties?
0 Kudos
AdamCox1
Deactivated User
Hi Jake, Happy New Year!

Here's a screen shot.  Let me know if you wanted a different parameter highlighted.

Thanks!
Adam
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Try upgrading to Service Pack 5 and see if you are still able to reproduce the crash.
0 Kudos
AdamCox1
Deactivated User
Service Pack 5 seems to have done the trick, no crashing, just the expected error message.  Thanks!
0 Kudos