Quantcast
Channel: Forums - Python
Viewing all articles
Browse latest Browse all 2485

Python Add-In Edit a Feature Class

$
0
0
I am working on my first Python Add-In and am having a couple issues. What I am trying to do is create a tool that will allow the user to click somewhere on the map and take the map coordinates to create a circle with a predefined radius, fill out some default values, insert it into a SDE feature class, then select the feature to display it's attributes in the attribute editor. This all works pretty well.

The problem is related to editing. I would like for the tool to be disabled if the feature class is not editable or if the layer is not in the current map document. I am also struggling with the edit state. If I start editing and run my tool I get this message: The requested operation is invalid on a closed state. If I make at least one edit, I can run the tool just fine.

Code:

def onMouseDownMap(self, x, y, button, shift):
        mxd = arcpy.mapping.MapDocument('current')
        df = arcpy.mapping.ListDataFrames(mxd, 'Layers')[0]
        fc = ''


        for lyr in arcpy.mapping.ListLayers(mxd,'*', df):
            if lyr.supports("dataSource"):
                lyrSource = lyr.dataSource.split("\\")
                if lyrSource[3] == 'zodiac.GIS.IPCoROW':
                    fc = lyr.dataSource
                    rowLayer = lyr
                    break
        # set the radius of the circle
        radius = 20
        # set the number of sides/vertices the circle will have
        side_count = 40
        # create the degrees for the circle
        degrees = 360 / side_count
        # create a list to hold each vertice point for the circle
        cir_poly = []
        # loop through the number of sides
        for i in range(side_count):
            # set the angle for the current vertice
            angle = (degrees * i) * (math.pi / 180)
            # do some math to set the xFactor and the yFactor
            xFactor = math.sin(angle)
            yFactor = math.cos(angle)
            # set the x and y vertices
            xVal = radius * xFactor + x
            yVal = radius * yFactor + y
            # create a arcpy point
            pnt = arcpy.Point(xVal,yVal)
            # add the point to the list
            cir_poly.append(pnt)
        # create an array and add the list to it
        array = arcpy.Array([cir_poly])
        # create a polygon from the array of points
        polygon = arcpy.Polygon(array)
       
        # set the path to the IPCoROW layer
        #fc = r"Database Connections\Connection to landbaseProd.sde\zodiac.GIS.Land\zodiac.GIS.IPCoROW"
        if len(fc) > 0:
           
            try:
                # create the insert cursor to be used to add our new circle
                print 'Create Insert Cursor'
                with arcpy.da.InsertCursor(fc, ["SHAPE@","OwnerType","ConvType","SOURCE","Comments"]) as c:
                    # insert the circle with attributes into the IPCoROW layer
                    print 'Insert Row'
                    i = c.insertRow([polygon, 'ROW','Easement','DIGITIZED','INSUFFICIENT DATA'])
                    print 'OID =',i
                   
                # refresh the active view to show the new feature
                arcpy.RefreshActiveView()
                print 'Select Features'
                arcpy.SelectLayerByAttribute_management (rowLayer, "NEW_SELECTION", "OBJECTID = " + str(i))


            except Exception, e:
                # if there was an error, print it here
                # common errors include:
                # attempting to edit outside an edit session
                # attempting to edit prior to making any other edits
                print 'Error -', e
            finally:
                pass
        else:
            print 'Could not find zodiac.GIS.IPCoROW'


Viewing all articles
Browse latest Browse all 2485

Trending Articles