[GIS] Clip a shapefile based on raster extent with high accuracy

accuracyarcpycliprastershapefile

I am trying to clip a shapefile based on the extents of a smaller raster. I find the extent of the raster using this code:

Extents = arcpy.sa.Raster(MyRaster).extent`

Then, I use these lines to make a polygon out of these extents:

pnt_array = arcpy.Array()
pnt_array.add(Extents2.lowerLeft)
pnt_array.add(Extents.lowerRight)
pnt_array.add(Extents.upperRight)
pnt_array.add(Extents.upperLeft)
poly = arcpy.Polygon(pnt_array)

Finally, I cut clip my shapefile using this code

arcpy.Clip_analysis(shp, poly, Shp_clip)

The problem is that I am using accuracy in two places. Please take a look:

Extents.XMax =                          -86.22649016383502
poly.extent.XMax =                      -86.22650146484375
arcpy.Describe(Shp_clip).extent.XMax=   -86.22650146517043

Is there anyway that I can get a clipped shapefile with the extent exactly as Extents?

Best Answer

Yes, the extent isn't accurate. If you have spatial analyst extension then use IsNull to create a binary raster, raster to polygon (no simplify) and then dissolve with no fields to create a clipping polygon.

Once you've got that then just clip as normal.

If you haven't got Spatial Analyst then it's a bit more tricky.