0

I have to reproject a shapefile's coordinate system. To do that I am using Arcpy and I want to get an output to use it as input in other tool (in Model Builder).

Now I get an output, but I cannot use it as input in model builder's tool. I am not programing well when I process the output shapefile.

The code is:

def getParameterInfo(self): """Define parameter definitions"""

 # First parameter

    param0 = arcpy.Parameter(
        displayName="Shape entrada a reproyectar",
        name="in_features",
        datatype="DEShapefile",
            parameterType="Required",
        direction="Input")

# Second parameter

    param1 = arcpy.Parameter(
        displayName="Imagen satelite de la cual se utilizara el CRS",
            name="raster",
        datatype="GPRasterLayer",
            parameterType="Required",
        direction="Input")

# Third parameter
     param2 = arcpy.Parameter(
        displayName="Shape de salida",
        name="out_features",
        datatype="DEShapefile",
            parameterType="Derived",
        direction="Output")

    parameters = [param0, param1, param2]
    return parameters`

def execute(self, parameters, messages): """The source code of the tool."""

shape= parameters[0].valueAsText
raster= parameters[1].valueAsText
output=parameters[2].valueAsText
#-----------------------------

desc = arcpy.Describe(shape)

out_CRS = arcpy.Describe (raster).spatialReference

output=arcpy.Project_management(shape, desc.path + "\\" + desc.baseName + "_Rprj.shp", out_CRS)


return 
2

You are setting output but you are not creating it as a result of Project tool. Check out your execute function. It collects output from the second parameter as text and this variable gets overwritten by the Project tool. By the way, many ArcGIS tools return result object and you need to specify output location by calling its first element to be on the safe side. In your case,

output=arcpy.Project_management(shape, desc.path + "\\" + desc.baseName + "_Rprj.shp", out_CRS)[0]

will assign desc.path + "\\" + desc.baseName + "_Rprj.shp" to output.

Still it does not resolve your issue. I think you should set the parameter after Project tool, i.e.,

output=arcpy.Project_management(shape, desc.path + "\\" + desc.baseName + "_Rprj.shp", out_CRS)[0]

arcpy.SetParameterAsText(2, output)

More clearly, here is a possible execute def:

def execute(self, parameters, messages):
        """The source code of the tool."""
        desc = arcpy.Describe(parameters[0])
        out_CRS = arcpy.Describe(parameters[1]).spatialReference
        output = arcpy.Project_management(parameters[0].valueAsText, desc.path + "\\" + desc.baseName + "_Rprj.shp", out_CRS)
        # That's what @Hornbydd suggested in the comments
        my_output = output.getOutput(0)
        arcpy.SetParameterAsText(2, my_output)
        return

Here is a snip of my toolbox:

enter image description here

Lastly, this is the snip of Reprojector tool in Model Builder after a successful run.

enter image description here

  • 3
    To compliment this answer I would like to add; as you correctly identify output is actually a result object. To avoid confusion I would have called it something like objRes. Then you retrieve the output of the tool as myFeatureClass = objRes.getOutput(0). It is then clear exactly what it is. I often see peoples code passing result objects around their code as if they are datasets which makes it difficult to read. – Hornbydd Aug 30 '18 at 13:30
  • @fatih_dur, I modify the code but I cannot obtain the output. Can you modify the code? Thanks – MikV89 Aug 31 '18 at 8:49
  • 1
    @MikV89, Did you try to add this python tool to a model builder? What do you see? I will replicate your example and add a snapshot! – fatih_dur Aug 31 '18 at 8:51

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.