0

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?

1

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.

  • I do have spatial analyst toolbox. I am going to give it a try and will post the result. – ahoosh Jul 14 '14 at 21:48
  • Your method works great. Though, it is still not accurate enough to the extent I want. Eventually, I want to convert the clipped shapefile into a 1/0 raster with the cells exactly as the raster. When I use feature to raster, I end up with a raster with 1635, 544 columns and rows rather than 1635, 543. I have 1 row less although I am using the exact same cell size when using feature to raster! – ahoosh Jul 14 '14 at 22:27
  • Are you setting your snap raster to the existing raster? – Michael Stimson Jul 14 '14 at 22:29
  • What do you mean by snap raster? I am using the clipped shapefile shp_clp as input to make a raster out of it using this code: arcpy.FeatureToRaster_conversion(shp_clp, Dummy_Int, output_raster, input_raster) where I use cell size of input_raster in the code. – ahoosh Jul 14 '14 at 22:36
  • Snap raster is an environment setting resources.arcgis.com/en/help/main/10.2/index.html#//… that ensures the output cells align perfectly with an existing raster. Also consider output extent environment setting, that may help. – Michael Stimson Jul 14 '14 at 22:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.