TransWikia.com

Layer no longer renders on map after running custom Python Tool

Geographic Information Systems Asked by Sharpie171 on April 18, 2021

Please note: I believe this is a display issue, since the data isn’t disappearing from anywhere but the lines on the map.

When I run a custom Python Toolbox in ArcGIS Pro 2.7.0, the layer that was input disappears from the map. If I close the Map in the project and open it again, the layer re-appears. I cannot find any other way to make the lines display again.

I have tried:

  • Closing and re-opening the project
  • Closing and re-opening the map.
  • Re-creating the tool in a new project.

Each of these ‘fixes’ temporarily bring the linework back, but the lines will disappear again after running the tool.

This is my first crack at making a Python Toolbox. The tool does exactly what I need it to do which is placing a point at the end of each line based upon the two boolean parameters and creating a text reference to the lines GUID. I know that the point layer doesn’t get added to the map because I don’t want the points generated by this tool on the map since this is the first step in a much bigger process. Why does the line layer not draw after running this tool?

Screenshots of the lines before and after running the tool. The orange squares are other data that is separate from this issue, but is included to illustrate the lack of lines post-tool:

Lines Rendering Correctly Lines are gone, but the data is still there.

Code:

class MakeEndPointsFromLine(object):
    def __init__(self):
        """Define the tool (tool name is the name of the class)."""
        self.label = "Create Line Endpoints"
        self.description = "Puts points at the ends of input line features."
        self.canRunInBackground = False

    def getParameterInfo(self):
        """Define parameter definitions"""
        # Allow the user to select a line layer as input and customize output
        lines = arcpy.Parameter(
            displayName="Line Layer",
            name="Lines",
            datatype="GPFeatureLayer",
            parameterType="Required",
            direction="Input"
        )
        lines.filter.list = ["Polyline"]
        start = arcpy.Parameter(
            displayName="Return Start Points",
            name='Return Start',
            parameterType="Optional",
            datatype='GPBoolean',
            direction="Input"
        )
        end = arcpy.Parameter(
            displayName="Return End Points",
            name='Return End',
            parameterType='Optional',
            datatype='GPBoolean',
            direction="Input"
        )
        out = arcpy.Parameter(
            displayName="Output Points",
            name='Points',
            parameterType='Required',
            datatype='GPFeatureLayer',
            direction="Output"
        )
        out.filter.list = ["Point"]
        # Gather all parameters and return them
        params = [lines, start, end, out]
        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."""
        # Inform the script to preserve Global IDs rather than recalculating Global IDs
        arcpy.env.preserveGlobalIDs = True
        # Parse the parameters pulled in from the getParameterInfo() function
        lines = parameters[0].valueAsText
        return_start = parameters[1].value
        return_end = parameters[2].value
        output = parameters[3].valueAsText
        # Determine the geodatabase directory of the selected output.
        index = output.find('.gdb') + len('.gdb')
        out_dir = output[0:index]
        out_name = output[index:len(output)]
        # Add x and y point information to the lines
        arcpy.management.AddGeometryAttributes(lines, "LINE_START_MID_END")
        arcpy.management.CalculateField(lines, "Line_GUID", "str(!GLOBALID!)", 'PYTHON3', field_type='TEXT')
        line_spatial_ref = arcpy.Describe(lines).spatialReference
        # Create a point layer for output
        points = arcpy.management.CreateFeatureclass(out_dir, out_name, 'POINT', spatial_reference=line_spatial_ref)
        # Add a text field representation of the line's GUID
        arcpy.management.AddField(points, "Line_GUID", "TEXT", field_alias="Line GLOBALID")
        with arcpy.da.SearchCursor(lines, ['GLOBALID', 'START_X', 'START_Y', 'END_X', 'END_Y']) as line_cursor:
            for line in line_cursor:
                line_id, start_x, start_y, end_x, end_y = line[0], line[1], line[2], line[3], line[4]
                # Add the start and/or end points of the current line to the point table
                with arcpy.da.InsertCursor(points, ["SHAPE@", "Line_GUID"]) as point_cursor:
                    if return_start:
                        point_cursor.insertRow([arcpy.Point(start_x, start_y), str(line_id)])
                    if return_end:
                        point_cursor.insertRow([arcpy.Point(end_x, end_y), str(line_id)])
        return points

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP