Drawing polygon and adding buffer to it

1487
7
08-25-2017 02:19 AM
PaulinaR
New Contributor

Hi!

I would like to draw polygon on my map and afterwards add 1,5 m buffer to it. I managed to draw a polygon however I have no idea how to create a buffer. The idea is that a user of ArcGIS will draw a polygon and receive directly the buffer. Is that possible?

def onLine(self, line_geometry):
        array = arcpy.Array()  
        part = line_geometry.getPart(0)  
        for pt in part:  
            print pt.X, pt.Y  
            array.add(pt)  
        array.add(line_geometry.firstPoint)  
        polygon = arcpy.Polygon(array)  
          
        # Local Variables  
        Input = polygon  
        NewPolygons = "\\\\abc\\home\\xxx\\Documents\\moje dokumenty\\xyz\\arcgis\\new polygons" 
  
        # Process: Append Polygon to existing file or Create new file if one is not created.    
  
        if arcpy.Exists("NewPolygons"):    
            arcpy.Append_management(Input, NewPolygons, "Test", "", "")  
  
        else:  
            arcpy.CopyFeatures_management(Input,NewPolygons, "", "0","0","0")
            
        print " Excavation Polygon located" 
0 Kudos
7 Replies
PaulinaR
New Contributor

I have managed to create a buffer (see below). Does somebody know how to add transparency to the polygon and to the buffer? EDIT:/ I added to the polygon (
        ExcavationPolygon.transparency= 60 ) but unfortunately for buffer it shows error.

def onLine(self, line_geometry):
        array = arcpy.Array()  
        part = line_geometry.getPart(0)  
        for pt in part:  
            print pt.X, pt.Y  
            array.add(pt)  
        array.add(line_geometry.firstPoint)  
        polygon = arcpy.Polygon(array)  
          
        # Local Variables  
        Input = polygon  
        NewPolygons = "\\\\abc\\home\\xxx\\Documents\\moje dokumenty\\xyz\\arcgis\\new polygons"  
  
        # Process: Append Polygon to existing file or Create new file if one is not created.    
  
        if arcpy.Exists("NewPolygons"):    
            arcpy.Append_management(Input, NewPolygons, "Test", "", "")  
  
        else:  
            arcpy.CopyFeatures_management(Input,NewPolygons, "", "0","0","0")
            
        print " Excavation Polygon located" 
        # Buffer areas of impact around major roads
        ExcavationPolygon = arcpy.CopyFeatures_management(Input,NewPolygons, "", "0","0","0")
        PolygonBuffer = "\\\\abc\\home\\xxx\\Documents\\moje dokumenty\\xyz\\arcgis\\PolygonBuffer"
        distanceField = "1.5"
        sideType = "OUTSIDE_ONLY"
        endType = "ROUND"
        arcpy.Buffer_analysis(ExcavationPolygon, PolygonBuffer, distanceField, sideType, endType)
        print "Buffer of 1.5 m completed" ‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Like most geoprocessing tools, Copy Features returns an ArcPy Result object, and there is no transparency property for a Result object.  Even if there was a transparency property, it would be the transparency of the result object itself and not the feature class the tool created.  In short, trying to set the transparency of ExcacationPolygon won't work for multiple reasons.

As you have already found out, or at least read about, setting the transparency of a layer is fairly simple since the ArcPy Layer class has a transparency property.  The trick is getting a reference to the layer object so you can set the transparency property.  Before I give specific suggestions, how exactly are you executing your code?  I assume it isn't in the interactive Python window since you have other users executing the code.  A script tool?  If so, what are the settings for in-process, etc...?  Are you just running default settings?

0 Kudos
curtvprice
MVP Esteemed Contributor

If running for ArcMap, Pro, or even publishing to a geoprocessing service, this is easily done without futzing with geometry objects by setting up the input to be a Feature set,

Working with feature sets and record sets—Help | ArcGIS Desktop 

and setting the symbology property of the output to a layer file.

Setting script tool parameters—Help | ArcGIS for Desktop 

0 Kudos
PaulinaR
New Contributor

Thank you for your help Joshua and Curtis! I am using Notepad++. I created an Add-in for ArcGIS using add-in wizard.

PATH_TO_XYZ_FOLDER = "\\\\abc\\home\\xxx\\Documents\\moje dokumenty\\xyz\\arcgis"
MY_WORKSPACE="\\\\abc\\home\\xxx\\Documents\\ArcGIS\\Default.gdb"
0 Kudos
PaulinaR
New Contributor

Hi,

I have one more question. In the next step  I want to create a grid on buffer. Nevertheless, I need to do it in different class and I would like to set buffer as global variable. Do I make it correctly? because It shows me error (global name 'ExcavationZone' is not defined)when I push the grid button . I added global variable at the end of above mentioned code. Thanks a lot for any help!

global ExcavationZone #Setting excavation zone as global variable in order to use it later
        ExcavationZone= arcpy.Buffer_analysis(ExcavationPolygon, PolygonBuffer, distanceField, sideType, endType)
        print "Excavation Zone established"
0 Kudos
curtvprice
MVP Esteemed Contributor

All variables set at the top level in a .py file are global. It's generally not good practice to use global if you don't have to.

"ExcavationZone =" is setting a result object , which when str()'d or provided as a parameter as a tool (again str()'d) will resolve to the pathname (full path for the python variable PolygonBuffer, this string should be set before running the tool).

0 Kudos
PaulinaR
New Contributor

thank you for help!

0 Kudos