Select to view content in your preferred language

Error when using insert cursor and search cursor to output point feature

563
3
07-05-2023 01:12 PM
OveringUMD
New Contributor II
 
Traceback (most recent call last):
  File "<string>", line 95, in execute
TypeError: cannot alter multipart geometry type

 I am getting the above error for my .pyt toolbox. The Toolbox takes a Polyline feature, finds the point between the start and end point of the polyline and is supposed to output a point feature class. I am not sure what is going wrong. 

code:

## EDITS MADE BASED OFF COMMENTS, STILL THE SAME ERROR

 

 

import arcpy
import os


class Toolbox(object):
    def __init__(self):
        """Define the toolbox (the name of the toolbox is the name of the
        .pyt file)."""
        self.label = "Create Door Centroids 2"
        self.alias = "Create Door Centroids 2"

        # List of tool classes associated with this toolbox
        self.tools = [Centroid]


class Centroid(object):
    def __init__(self):
        """Define the tool (tool name is the name of the class)."""
        self.label = "Create Door Centroids"
        self.description = "Create Door Centroids"
        self.canRunInBackground = False

    def getParameterInfo(self):
        """Define parameter definitions"""
        door_layer = arcpy.Parameter(
            displayName="Door Feature Layer (LINE)",
            name="door_fc",
            datatype="GPFeatureLayer",
            parameterType="Required",
            direction="Input"
        )
        door_layer.filter.list = ['LINE']

        centroid_layer = arcpy.Parameter(
            displayName="Door Centroid Feature Layer (Output)",
            name="doors_fc",
            datatype="GPFeatureLayer",
            parameterType="Required",
            direction="Output"
        )
        centroid_layer.filter.list = ['POINT']

        params = [door_layer, centroid_layer]
        return params

    def isLicensed(self):
        """Set whether tool is licensed to execute."""
        return True

    def updateParameters(self, parameters):
        """Modify the values and properties of parameters before internal
        validation is performed.  This method is called whenever a parameter
        has been changed."""
        return

    def updateMessages(self, parameters):
        """Modify the messages created by internal validation for each tool
        parameter.  This method is called after internal validation."""
        return

    def execute(self, parameters, messages):
        """The source code of the tool."""
        door_layer = parameters[0].valueAsText
        outFC = parameters[1].valueAsText

        spatial_reference = arcpy.Describe(door_layer).spatialReference

         # create output feature class
        out_name = os.path.basename(outFC)
        out_path = os.path.dirname(outFC)
        arcpy.management.CreateFeatureclass(out_path, out_name, "POLYLINE", None, "DISABLED", "DISABLED", spatial_reference)

        # add field
        arcpy.management.AddField(outFC, "floor_id", "TEXT")
        arcpy.management.AddField(outFC, "room_type", "TEXT")

        with arcpy.da.InsertCursor(outFC, [ "SHAPE@", "floor_id", "room_type"]) as icursor:
                with arcpy.da.SearchCursor(door_layer, [ "OID@", "SHAPE@","FIRST_floor_id", "FIRST_room_type",]) as scursor:
                    for row in scursor:
                        door_line = row[1]
                        point1 = door_line.firstPoint
                        point2 = door_line.lastPoint
                        x1 = point1.X
                        y1 = point2.Y
                        x2 = point2.X
                        y2 = point2.Y
                        newX = (x1+x2)/2
                        newY = (y1+y2)/2
                        pt = arcpy.Point(newX, newY)
                        arcpy.AddMessage(point1)
                        arcpy.AddMessage(newX)
                        arcpy.AddMessage(pt)
                        pt_geometry = arcpy.PointGeometry(pt, spatial_reference)
                        row_pt = (pt_geometry, row[2], row[3])
                        icursor.insertRow(row_pt)

        return 

 

 

3 Replies
ChrisRingo
Occasional Contributor

In your scursor loop you've got "row" as the index, but then you're redefining "row" at the bottom of the scursor loop. I think you want to assign a different variable there - something like:

"row_pt = ( row[0], pt_geometry, row[2], row[3])"

icursor.insertRow(row_pt)

Also, it looks like your row insertion is attempting to assign an OID to the inserted record - it seems to me that since the OID is assigned by Arc you may not be able to do that, though I may be wrong on that.

0 Kudos
OveringUMD
New Contributor II

Hi Chris, thanks for the suggestion. I made the changes (see updated post), but I am still getting the same error

0 Kudos
ChrisRingo
Occasional Contributor

Looks like you've created your outFC as POLYLINE (line 71), but you want to write point geometries to it?

0 Kudos