Purpose:To take an input raster image and clip its shape to a parcel block. In later stages this model will be applied to a loop.
Logic: The Model receives an input raster image and calculates a centroid as a feature class. The centroid is then used in a spatial selection from a grid (PlatMap in this case). The selection of a single plat is then used by the clip tool.
Area of Trouble: the Python script that generates a point feature class that allows the selection of the specific plat I need.
Question: From the script presented below, what needs to change to get this to work? I have some experience coding in Python, but this is my first script using ArcPy. I could use more clarification on are how the input and output variables are called into and out of the script, as well as the write function for the created feature class.
First Attempt:
Currently my parameters (in properties for the script) are set to:
- RasterInput = Raster Layer
- RasterOutpur = Feature Class
Codeblock is largely inspired by Blord-Castillo's response to a question here.
import arcpy, os
# Calculates the center of the raster image.
def RasterCenter(raster):
#raster: string reference to raster
raster = arcpy.Raster(raster)
fcname = "{}_center".format(os.path.basename(str(raster)))
x = raster.extent.XMin + (raster.extent.XMax - raster.extent.XMin)/2
y = raster.extent.YMin + (raster.extent.YMax - raster.extent.YMin)/2
#Combine the two to create a feature class containing the coordinates.
featureclass = arcpy.CreateFeatureclass_management("in_memory", fcname, "POINT",spatial_reference = raster.spatialReference)
with arcpy.da.InsertCursor(featureclass, ['SHAPE@XY']) as cursor:
cursor.insertRow(((x, y),))
RasterOutput = arcpy.setParameter(featureclass)
RasterInput = arcpy.GetParameterAsText(0)
RasterCenter(RasterInput)
Second Attempt:
In response to ahmadhanb's revisions.
I changed some other portions of the text to be clearer to me, but the biggest error I still receive when running the script is as follows. It is the same error I receive when running the revision from ahmadhanb. My suspicion is the the spatial_reference field, which I changed to the Authority code because it should be the same for every time I run this.
My parameters (in properties for the script) are set to:
- rasterFile = Raster
- Output_fc = Feature Class
import arcpy, os
# Calculates the center of the raster image.
def RasterCenter(rasterInput):
#raster: string reference to raster
raster = arcpy.Raster(rasterInput)
item = os.path.split(rasterInput)
name,ext = os.path.splitext(item[-1])
fcname = "{0}_center".format(name)
# Set the template file path.
template = "C:/Users/preed/Desktop/STAGING/1799_Leak_Detection/LeakDetection.gdb/CentroidTemplate"
# Setting Spatial reference to NAD 1983 StatePlane Washington North FIPS 4601 feet
# Authority Code = 32048
spatial_reference = arcpy.SpatialReference(32048)
#Fetches the approximate X and Y coordinates we'll be using.
x = raster.extent.XMin + (raster.extent.XMax - raster.extent.XMin)/2
y = raster.extent.YMin + (raster.extent.YMax - raster.extent.YMin)/2
#Combine the two to create a feature class containing the coordinates.
featureclass = arcpy.CreateFeatureclass_management("in_memory", fcname, "POINT", template ,"DISABLED", "DISABLED", spatial_reference)
with arcpy.da.InsertCursor(featureclass, ['SHAPE@XY']) as cursor:
cursor.insertRow(((x, y),))
return featureclass
rasterFile = arcpy.GetParameterAsText(0)
output_fc = RasterCenter(rasterFile)
save = arcpy.CopyFeatures_management(output_fc,arcpy.GetParameterAsText(1))