draw a polygon with a python add-in and clip it

7583
6
Jump to solution
08-19-2014 11:18 AM
EstelleWuilliez1
New Contributor II

Hello everybody, I really need some help for my project in ArcMap 10.2.

I want to create a tool with a python add-in, then click on it and draw a polygon on a map. Then I want to use this polygon to clip my map and to keep what I've selected with the polygon.

To do that, I've made this code in my Add-in :Untitled.jpg

Everything seems to work except one thing! The clip tool creates a empty output, I've got the error "Empty output generated", and I really don't understand why. My "Route_shapes.shp" and my "polygon.shp" have the same coordinate system.

Any suggestion?

Thank you!

1 Solution

Accepted Solutions
XanderBakker
Esri Esteemed Contributor

Interesting, but not what I was hoping for...

I suppose that you do have access to an Advanced license, right? I noticed that the "Feature To Polygon" tool requires that type of license. I do not have one available at the moment. I did rewrite the code to work as stand alone, so I can test it easier and to solve the "Feature To Polygon", in the code I create an empty featureclass (lines 17 - 25) and write the polygon to it (lines 28 - 29). The line geometry is created on lines 34 - 39.

import arcpy

arcpy.env.overwriteOutput = True

def testing(line_geometry):

    fc_routes = r"D:\Xander\GeoNet\ClipTool\ROUTE_SHAPE.shp"

    fc_graphics = r"D:\Xander\GeoNet\ClipTool\GRAPHICS.shp"

    fc_result = r"D:\Xander\GeoNet\ClipTool\POLYGON.shp"

    array = arcpy.Array()

    part = line_geometry.getPart(0)

    for pt in part:

        array.add(pt)

    array.add(line_geometry.firstPoint)

    polygon = arcpy.Polygon(array, fc_routes)

    # create empty shapefile

    geometry_type = "POLYGON"

    template = ""

    has_m = "DISABLED"

    has_z = "DISABLED"

    sr = arcpy.Describe(fc_routes).spatialReference

    ws_path, fc_out_name = os.path.split(fc_graphics)

    arcpy.CreateFeatureclass_management(ws_path, fc_out_name,

        geometry_type, template, has_m, has_z, sr)

    # FeatureToPolygon_management requires Advanced

    with arcpy.da.InsertCursor(fc_graphics, ("SHAPE@")) as curs:

        curs.insertRow((polygon,))

    arcpy.Clip_analysis(fc_routes, fc_graphics, fc_result)

# create a line geometry

array = arcpy.Array([arcpy.Point(467075, 211310),

                     arcpy.Point(467550, 214930),

                     arcpy.Point(470900, 212561)])

polyline = arcpy.Polyline(array)

line_geometry = arcpy.Polyline(array)

# call the clip function

testing(line_geometry)

This did work without problems:

ClipWithPolygon.png

Can you check your graphics shapefile to see if it has the desired content?

Kind regards, Xander

View solution in original post

6 Replies
XanderBakker
Esri Esteemed Contributor

Maybe it's because the clip features "Graphics.shp" is not assigned a coordinate system? You can do this by changing the line where you create the polygon to this:

polygon = arcpy.Polygon(array, "X:/Users/ROUTE_SHAPES.shp")

0 Kudos
EstelleWuilliez1
New Contributor II

With your line, ArcMap crashes

0 Kudos
XanderBakker
Esri Esteemed Contributor

Interesting, but not what I was hoping for...

I suppose that you do have access to an Advanced license, right? I noticed that the "Feature To Polygon" tool requires that type of license. I do not have one available at the moment. I did rewrite the code to work as stand alone, so I can test it easier and to solve the "Feature To Polygon", in the code I create an empty featureclass (lines 17 - 25) and write the polygon to it (lines 28 - 29). The line geometry is created on lines 34 - 39.

import arcpy

arcpy.env.overwriteOutput = True

def testing(line_geometry):

    fc_routes = r"D:\Xander\GeoNet\ClipTool\ROUTE_SHAPE.shp"

    fc_graphics = r"D:\Xander\GeoNet\ClipTool\GRAPHICS.shp"

    fc_result = r"D:\Xander\GeoNet\ClipTool\POLYGON.shp"

    array = arcpy.Array()

    part = line_geometry.getPart(0)

    for pt in part:

        array.add(pt)

    array.add(line_geometry.firstPoint)

    polygon = arcpy.Polygon(array, fc_routes)

    # create empty shapefile

    geometry_type = "POLYGON"

    template = ""

    has_m = "DISABLED"

    has_z = "DISABLED"

    sr = arcpy.Describe(fc_routes).spatialReference

    ws_path, fc_out_name = os.path.split(fc_graphics)

    arcpy.CreateFeatureclass_management(ws_path, fc_out_name,

        geometry_type, template, has_m, has_z, sr)

    # FeatureToPolygon_management requires Advanced

    with arcpy.da.InsertCursor(fc_graphics, ("SHAPE@")) as curs:

        curs.insertRow((polygon,))

    arcpy.Clip_analysis(fc_routes, fc_graphics, fc_result)

# create a line geometry

array = arcpy.Array([arcpy.Point(467075, 211310),

                     arcpy.Point(467550, 214930),

                     arcpy.Point(470900, 212561)])

polyline = arcpy.Polyline(array)

line_geometry = arcpy.Polyline(array)

# call the clip function

testing(line_geometry)

This did work without problems:

ClipWithPolygon.png

Can you check your graphics shapefile to see if it has the desired content?

Kind regards, Xander

EstelleWuilliez1
New Contributor II

Wow ok your code works on my project!

And there is something strange, my old code works when yours is also launched.. So I think that my problem is linked to coordinates and that kind of stuff, because your polygon is really bigger than my "Route_shape" shapefile.

However, thank you very much for your help and for your time, I really appreciated it.

Kind regards, Estelle

0 Kudos
XanderBakker
Esri Esteemed Contributor

Since I don't know what your data looks like and I don't know where it is located, I just created an arbitrary polygon (triangle) over a set of test data pulled from a different thread. When you run your original code this should create the shapefile "X:/Users/Graphics.shp". Is this shapefile created correctly? Does it overlay with your data in the shapefile "X:/Users/ROUTE_SHAPES.shp"? Is your data frame, where you are drawing your line geometry, in the same projection?

Those would be the first places to check. If the code is now working satisfactory then there may have been something with perhaps a previous shapefile that may have been conflicting...

Do you remove the "Graphics.shp" or have you configured the option to overwrite the output in case it exists? I included this in my code to avoid problems. Since you are creating a tool (which I suppose you want to use more than once) and the intermediate and output results are hard coded (those will be the same each time you run the tool) you will need to include some handling of overwriting existing files.

Kind regards, Xander

0 Kudos
EstelleWuilliez1
New Contributor II

My original code creates all the shapefile I want but they are all empty, my polygon doesn't take the attributes of Route_Shape. But now, when I launch your code and then execute mine, it works. But if I execute mine before yours, it doesn't work.

About the overwrite option, it is a good idea that I'm going to do now, because I removed the "Graphics.shp" all the time before.

0 Kudos