4

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))

enter image description here
Snapshot of the workflow in ModelBuilder

1
  • Please always include errors as text rather than as a picture of an error. That way it is available to future searches.
    – PolyGeo
    Jul 13, 2016 at 11:01

2 Answers 2

10

I modified the original code a little bit to avoid some confusion when defining the RasterCenter function, since the argument named raster used in def RasterCenter(raster) and the variable named raster used in raster = arcpy.Raster(raster) within the function can cause confusion and make things not working properly. I modified parsing the path when reading the path to raster file so you can get the layer name correctly.

The following code works for a single file at a time. You need to define the path to the raster file and then copy the code and paste it inside python window in ArcGIS.

import arcpy, os

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)
    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),))
    return featureclass


rasterFile = r"Path\To\Image.img" # change this line to the correct path

output_fc = RasterCenter(rasterFile)

The output file is stored in memory, you need to save it if you want to keep it.

If you want the user to define the input file, you need also to define the output file in this case, which should be saved in user's hard disk. But this will work with toolbox version not as a script. You need to import this script into the toolbox that you will create. To accept user's input and output, the last two lines in the code should be replaced with the following three lines:

rasterFile = arcpy.GetParameterAsText(0)

output_fc = RasterCenter(rasterFile)

save = arcpy.CopyFeatures_management(output_fc,arcpy.GetParameterAsText(1))
4
  • Edited the original question to account for your response.
    – Fenree
    Jun 29, 2016 at 9:51
  • Does your raster image start with a number?
    – ahmadhanb
    Jun 29, 2016 at 11:52
  • Yes. Variation on names such as 216070_12162015.png
    – Fenree
    Jun 29, 2016 at 12:27
  • 1
    Vector files cannot start with numbers. For testing change the raster file name to start with alphabets.
    – ahmadhanb
    Jun 29, 2016 at 15:36
1

The end code that worked for creating the selection. I ended up having to define the projection using a constant, so I used the Authority Code for the projection.

import arcpy, os
arcpy.env.overwriteOutput =True

# 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 = 2285 
    spatial_reference = arcpy.SpatialReference(2285)

  #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. Changed 'SHAPE@TRUECENTROID'
    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)
PointFile = arcpy.GetParameterAsText(1)

output_fc = RasterCenter(rasterFile)

save = arcpy.CopyFeatures_management(output_fc,PointFile)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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