arcpy开发实例之add-in代码
import arcpy
import pythonaddins
class BtnEditFeature(object):
"""Implementation for pydemoaddin_addin.button_edit (Button)"""
def __init__(self):
self.enabled = True
self.checked = False
def onClick(self):
with arcpy.da.UpdateCursor("points",["name","OID@"]) as cursor:
for row in cursor:
row[0]="point_"+str(row[1]);
cursor.updateRow(row);
class BtnNewFeatureClass(object):
"""Implementation for pydemoaddin_addin.button_newfeatureclass (Button)"""
def __init__(self):
self.enabled = True
self.checked = False
def onClick(self):
if arcpy.Exists("d:/temp/points.shp"):
arcpy.management.Delete("d:/temp/points.shp");
arcpy.management.CreateFeatureclass("d:/temp","points.shp","POINT");
arcpy.management.AddField("d:/temp/points.shp","name","TEXT",20);
pythonaddins.MessageBox("Create point.shp successfully!","TIP");
class ToolDrawPoint(object):
"""Implementation for pydemoaddin_addin.tool_drawpoint (Tool)"""
def __init__(self):
self.enabled = True
self.cursor = 3;
self.shape = "NONE" # Can set to "Line", "Circle" or "Rectangle" for interactive shape drawing and to activate the onLine/Polygon/Circle event sinks.
def onMouseDownMap(self, x, y, button, shift):
with arcpy.da.InsertCursor("d:/temp/points.shp",['SHAPE@XY']) as dc:
dc.insertRow([(x,y)]);
print ("One point at the mouse position({0},{1}) has been added.".format(x,y);
mxd = arcpy.mapping.MapDocument("current");
df = mxd.activeDataFrame;
ext = df.extent;
extent = ext
ext.XMin = ext.XMin-0.1
df.extent = ext;
class ToolIdentify(object):
"""Implementation for pydemoaddin_addin.tool_identify (Tool)"""
def __init__(self):
self.enabled = True
self.shape = "NONE" # Can set to "Line", "Circle" or "Rectangle" for interactive shape drawing and to activate the onLine/Polygon/Circle event sinks.
def onMouseDownMap(self, x, y, button, shift):
mxd = arcpy.mapping.MapDocument("current");
df = mxd.activeDataFrame;
lyr = arcpy.mapping.ListLayers(mxd,"points",df)[0];
pt = arcpy.Point(x,y);
geom = arcpy.PointGeometry(pt)
arcpy.SelectLayerByLocation_management( "points","WITHIN_A_DISTANCE" ,geom,0.1);
selectedCount = arcpy.GetCount_management("points");
if selectedCount>0:
for f in lyr.getSelectionSet():
print f;
class ToolRemove(object):
"""Implementation for pydemoaddin_addin.tool_remove (Tool)"""
def __init__(self):
self.enabled = True
self.shape = "NONE" # Can set to "Line", "Circle" or "Rectangle" for interactive shape drawing and to activate the onLine/Polygon/Circle event sinks.
def onRectangle(self, rectangle_geometry):
mxd = arcpy.mapping.MapDocument("current");
df = mxd.activeDataFrame;
lyr = arcpy.mapping.ListLayers(mxd,"points",df)[0];
rect = rectangle_geometry;
arcpy.SelectLayerByLocation_management( "points","WITHIN" ,rect);
selectedCount = arcpy.GetCount_management("points");
if selectedCount>0:
arcpy.da.UpdateCursor("points",)
with arcpy.da.UpdateCursor("points","OID@") as cursor:
for f in lyr.getSelectionSet():
for row in cursor:
if row[0]==f:
cursor.deleteRow();