zackb11

Python script read coordinate system, drag and drop

Discussion created by zackb11 on Jun 18, 2012
Latest reply on Jan 2, 2013 by AdamCox
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

Outcomes