1

I would like to create new shapefile layer or memory layer with one point with certain coordinates in ArcGIS 10.2 with Python Toolbox. Which function should I use? I wrote this code:

import arcpy
class Toolbox(object):
 def __init__(self):
 self.label = "Python Toolbox"
 self.alias = ""
 self.tools = [XY]
class XY(object):
 def __init__(self):
 self.label = "XY"
 self.description = "Coordinates to point"
 self.canRunInBackground = False
 def getParameterInfo(self):
 parameters = []
 x = arcpy.Parameter(
 displayName="X",
 name="x",
 datatype="String",
 parameterType="Required",
 direction="Input")
 parameters.append(x)
 y = arcpy.Parameter(
 displayName="Y",
 name="y",
 datatype="String",
 parameterType="Required",
 direction="Input")
 parameters.append(y)
 return parameters
 def isLicensed(self):
 return True
 def updateParameters(self, parameters):
 return
 def updateMessages(self, parameters):
 return
 def execute(self, parameters, messages):
 x = float(parameters[0].valueAsText)
 y = float(parameters[1].valueAsText)
PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Apr 24, 2016 at 16:19
2

1 Answer 1

0

The Python Toolbox portion of your code looks fine, it would be better to just focus on your feature class and point creation portion (even if you separate that out from the Python Toolbox to test).

def execute(self, parameters, messages):
 x = float(parameters[0].valueAsText)
 y = float(parameters[1].valueAsText)
 # This could be added as another parameter for output file name and path
 outputFile = "n:/gisse/output/pointoutput.shp" 
 # OR
 # outputFile = "in_memory/pointoutput"
 newPoint = arcpy.Point()
 pntGeometry = []
 newPoint.X = x # Note the upper case X in newPoint.X
 newPoint.Y = y # Note the upper case Y in newPoint.Y
 pntGeometry.append(arcpy.PointGeometry(newPoint))
 arcpy.CopyFeatures_management(pntGeometry, outputFile)

If this is designed to be an in_memory dataset you'll need to also add it to your map

layer_name = "newPoint"
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd, "*")[0]
arcpy.MakeFeatureLayer_management(outputFile, layer_name)
add_layer = arcpy.mapping.Layer(layer_name)
arcpy.mapping.AddLayer(df, add_layer)
arcpy.RefreshTOC()
answered Apr 24, 2016 at 21:10

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.