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)
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()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.