Is it possible to clip (management) a raster using a buffer created via the PointGeometry function?

507
0
07-08-2019 08:50 PM
SeannChia
New Contributor

I need to make clips from a single raster (and save them as individual raster files). The coordinates of each point are in a shapefile. At the moment, I am buffering a radius around (1meter) each point, then clipping it from a raster using the following code in a loop for each point.

point = arcpy.Point()
point.X = x_coord
point.Y = y_coord
ptObject = arcpy.PointGeometry(point, arcpy.SpatialReference("WGS 1984")).projectAs(arcpy.SpatialReference("WGS 1984 UTM Zone 55S"))

arcpy.Buffer_analysis(in_features = ptObject, out_feature_class = buffer_filepath, buffer_distance_or_field = "1 Meters", line_side = "FULL", line_end_type  = "ROUND", dissolve_option  = "NONE", dissolve_field  = "", method  = "PLANAR")

desc = arcpy.Describe(buffer_filepath)
ExtStr = "{0} {1} {2} {3}".format(desc.extent.XMin, desc.extent.YMin, desc.extent.XMax, desc.extent.YMax)

arcpy.Clip_management(in_raster = raster_filepath, out_raster = clip_filepath, rectangle = ExtStr, nodata_value = "-999", in_template_dataset = buffer_filepath, clipping_geometry = "ClippingGeometry", maintain_clipping_extent = "NO_MAINTAIN_EXTENT")

This works, but is a little slow for a couple of hundred clips, and I need to do cleanup on the temporary buffer files afterwards.

I would like to do something like the following:

point = arcpy.Point()
point.X = x_coord
point.Y = y_coord
ptObject = arcpy.PointGeometry(point, arcpy.SpatialReference("WGS 1984")).projectAs(arcpy.SpatialReference("WGS 1984 UTM Zone 55S"))

# Create a buffer property directly on the point geometry object
ptObject_buffer = ptObject.buffer(1.0)
ptObject_extent = ptObject_buffer.extent
arcpy.MakeFeatureLayer_management(in_features = ptObject_buffer, out_layer = "tmp")

# Clip the raster using the point geometry object directly
arcpy.Clip_management(in_raster = raster_filepath, out_raster = clip_filepath, rectangle = ptObject_extent , nodata_value = "-999", in_template_dataset  = "tmp", clipping_geometry = "ClippingGeometry", maintain_clipping_extent = "NO_MAINTAIN_EXTENT")

When I run this code I get error 001143: "Background server threw an exception"

My aim is to avoid creating the temporary buffer files - although I realize this method also requires me to delete the temporary layer file in each loop iteration, which might turn out to be slower in the end...

Am I on the right track, or is there a simpler or faster way to achieve what I need to? Thank you in advance for your help!

0 Kudos
0 Replies