# Clip a shapefile, given a set of coodinates

I need to clip a shapefile, the same way one would use Extract by Polygon on a Raster to achieve a smaller subset of the shapefile. I have previously exployed Extract by Polygon on a Raster and used that raster subsequently in an Extract by Mask operation on the desired shapefile of interest to obtain the final shapefile.

Is there any option to clip a shapefile, given a set of coordinates? I am open to solutions in either ArcMap or R.

In R you can use the extent function in the raster package to easily create a polygon representing the extent that you want to clip to.

``````library(sp)
library(rgeos)
library(raster)

data(meuse)
coordinates(meuse) <- ~x+y
``````

Create extent polygon and clip data using the intersect function from the raster package. You would add the subset coordinates in the extent function (as shown). The coordinate input order is: xmin, xmax, ymin, ymax.

``````e <- as(extent(178627.9, 180250.7, 329779.7, 331045.7), "SpatialPolygons")
meuse.sub <- intersect(meuse, e)
``````

Display results

``````plot(meuse, pch=20)
``````

You can create a polygon feature in memory in arcpy and clip the input polygon to the new bounds.

From the example on http://pro.arcgis.com/en/pro-app/arcpy/classes/polygon.htm, I made a few modifications for the sake of example:

``````import arcpy

# A list of features and coordinate pairs
feature_info = [[[-74.032188, 40.705067], [-74.117732, 40.795640],
[-73.032188, 40.795640], [-73.902191, 40.705067]]]
epsg = 4326 #WGS84

# A list that will hold each of the Polygon objects
features = []

for feature in feature_info:
# Create a Polygon object based on the array of points
# Append to the list of Polygon objects
features.append(
arcpy.Polygon(
arcpy.Array([arcpy.Point(*coords) for coords in feature]),
arcpy.SpatialReference(epsg)))

arcpy.analysis.Clip(inPolygon, features, outPolygon)
``````
• Going wrong somewhere, as an empty shapefile is being generated. inPolygon is the input polygon right? and outPolygon can be left as is, with just the path? – Vijay Ramesh Jun 2 '17 at 16:22
• import arcpy # A list of features and coordinate pairs feature_info = [[[73.34, 13.53], [79.076, 13.53], [79.076, 7.194], [73.593, 7.194]]] epsg = 4326 #WGS84 # A list that will hold each of the Polygon objects features = [] inPolygon = "E:\\WG.shp" outPolygon = "E:\\\\Bar.shp" – Vijay Ramesh Jun 2 '17 at 16:23
• `inPolygon` is the polygon that will be clipped, `features` is the clipping boundary, and `outPolygon` is the path where the extracted polygon will be saved to. – njoosse Jun 2 '17 at 16:30
• for your example in the comment, `WG.shp` will be clipped to the input points and saved to `Bar.shp` – njoosse Jun 2 '17 at 16:31
• Thanks @njoosse. The R solution was much quicker and it actually worked for me. Thanks for the effort! – Vijay Ramesh Jun 2 '17 at 18:49

If you need to clip a square, the `extent` function is very straightforward. If you want to use a more flexible set of coordinates, `rgeos` can read WKT strings and create spatial objects.

``````library(rgeos)
p1 <- as(readWKT("POLYGON ((-1 -1, -1 2, 2 2, 2 -1, -1 -1))"), "SpatialPolygonsDataFrame")
p1\$area <- gArea(p1) # Calculate the area (9 units)
plot(p1)
`````` To clip,make a non-square shape, and then intersect with the first:

``````s1 <- readWKT("POLYGON ((0 0, 1 0.5, 0 1, 0 0))")
plot(s1, add = T, col = 'gray')
`````` ``````s1p1 <- intersect(p1, s1)
s1p1\$area <- gArea(s1p1) # Area = 0.5
plot(s1p1, add = T, col  'red')
`````` The following workflow could easily be created in Modelbuilder or Python: Use your coordinates to create a CSV file. Convert the CSV file to points. Use Points to Line to make a line file. Use Convert Lines to Polygons to create the polygon features. Use Clip with your original data and the newly created polygon. You would need to have an ArcGIS Advance license to use some of these tools.

• This is a similar solution I have been trying to employ, but I don't have the advanced license to use the Feature to Polygon tool. – Vijay Ramesh Jun 2 '17 at 16:02
• Too bad. You could do that step in QGIS but better still is to create the polygon from scratch like @njoosse shows. – GBG Jun 2 '17 at 18:33
• Thanks @GBG. The R solution from Jeff Evans proved to be the quickest and easiest. – Vijay Ramesh Jun 2 '17 at 18:50