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)
-
You will need to create a new feature class which you can store in the in_memory geodatabase. pro.arcgis.com/en/pro-app/tool-reference/data-management/… and pro.arcgis.com/en/pro-app/help/analysis/geoprocessing/…Alex Tereshenkov– Alex Tereshenkov2016年04月24日 20:31:51 +00:00Commented Apr 24, 2016 at 20:31
-
Alternatively you can construct an arcpy.Geometry() and then use Copy Features to copy to the in_memory gdb: desktop.arcgis.com/en/arcmap/10.3/analyze/arcpy-classes/… and desktop.arcgis.com/en/arcmap/10.3/tools/data-management-toolbox/…Alex Tereshenkov– Alex Tereshenkov2016年04月24日 20:56:20 +00:00Commented Apr 24, 2016 at 20:56
1 Answer 1
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()