Select to view content in your preferred language

Create polylines from sets of coordinates and convert to feature class

2035
3
Jump to solution
06-13-2023 04:53 PM
wavey
by
New Contributor

I have a csv with data that contains 4 sets of coordinates per line. start_x, start_y, end_x, and end_y which correspond to the start and end of individual line segments. The goal is to take these coordinates, load them into a dataframe, create polylines, and then save them as a feature class in the database.

 

I'm able to get the points loaded into a pandas dataframe, use the Polyline function from arcgis.geometry to create polyline objects, and store them in the pandas dataframe. Before storing them back in the dataframe I check to make sure that the resulting polyline objects are valid with .is_valid and they all return true. However when I try to convert to a spatially enabled dataframe I get an error saying: Invalid geometry type for method.

 

I'm not entirely sure where to go from here. Any suggestions about a better way to accomplish this or how to deal with converting to SEDF and then feature class would be greatly appreciated.

 

 

# Import the csv file as a df
df = pd.read_csv('sample_vals.csv')

# Define the function which takes x, y, and wkid inputs and converts them into an arcgis geometry polyline object
def coordToPolyline(coord_list, input_wkid):

    line_dict = {"paths": coord_list, "spatialReference": {"wkid": input_wkid}}
    
    output_polyline = Polyline(line_dict)
    
    return output_polyline


# Create the SHAPE column in the pandas dataframe
df['SHAPE'] = ''
print(df)

# Run through the pandas dataframe and calculate the lines
for i in range(0, len(df)):

    # Grab the start_x, start_y, end_x, and end_y and put into a list to use for Polyline
    initial_x = df.iloc[i]['start_x']
    initial_y = df.iloc[i]['start_y']
    end_x = df.iloc[i]['end_x']
    end_y = df.iloc[i]['end_y']
    
    # Populate the list
    iter_coord_list = []
    iter_coord_list.append([initial_x, initial_y])
    iter_coord_list.append([end_x, end_y])
    
    # Call the function and pass the coord list and wkid=2276
    iter_polyline_obj = coordToPolyline(iter_coord_list, 2276)
    
    # Verify that the polyline object created is valid
    if iter_polyline_obj.is_valid():
        # If Polyline object is valid then add it to the pandas dataframe in the SHAPE column at index i
        df.at[i, 'SHAPE'] = iter_polyline_obj
        print(iter_polyline_obj.is_valid())
        
    else:
        print('One or more of the calculated Polyline objects is not valid.')


# Try to go to an sdf
sdf = GeoAccessor.from_df(df, geometry_column='SHAPE')

 

0 Kudos
1 Solution

Accepted Solutions
FrédéricPRALLY
Esri Contributor

Hi wavey,

Why not simply use tool XY to lines

import arcgis
input_table = r'C:\temp\sample_vals.csv'
out_lines = r'C:\temp\Default.gdb\sample_vals'
# XY To Line
arcpy.XYToLine_management(input_table, out_lines, "start_x", "start_y", "end_x", "end_y", "PLANAR","","NAD_1983_StatePlane_Texas_North_Central_FIPS_4202_Feet" )

Best regards,

Fred

View solution in original post

3 Replies
FrédéricPRALLY
Esri Contributor

Hi wavey,

Why not simply use tool XY to lines

import arcgis
input_table = r'C:\temp\sample_vals.csv'
out_lines = r'C:\temp\Default.gdb\sample_vals'
# XY To Line
arcpy.XYToLine_management(input_table, out_lines, "start_x", "start_y", "end_x", "end_y", "PLANAR","","NAD_1983_StatePlane_Texas_North_Central_FIPS_4202_Feet" )

Best regards,

Fred

wavey
by
New Contributor

Thank you so much! I had no idea the XY to lines tool existed but it is exactly what I was looking for.

0 Kudos
sofoo
by
Occasional Contributor

Sorry but that solution still doesn't solve the question.  How is one suppose to do this with spatial dataframes (pandas or esri)??

0 Kudos