0

I have data in a .csv that looks something like this:

Entry A Xa0,Ya0 Xa1,Ya1 Xa2,Ya2 Entry B Xb0,Yb0 Xb1,Yb1 Xb2,Yb2 Entry C Xc0,Yc0 Xc1,Yc1 Xc2,Yc2

The ultimate goal is to plot the points in each entry and connect them with lines, resulting in shape A, shape B, shape C, etc.

For now I am hung up on how to import these columns using arcpy. In the ArcGIS documentation there is code like this (and a tool that does the same thing) but it assumes lat/long are split up into separate columns:

in_Table = "table.csv"
x_coords = "POINT_X"
y_coords = "POINT_Y"
z_coords = "POINT_Z"
out_Layer = "points_layer"
saved_Layer = r"c:\output\points.lyr"


# Make the XY event layer...
arcpy.MakeXYEventLayer_management(in_Table, x_coords, y_coords, out_Layer, spRef, z_coords)

How do I modify this code to work with my data in the format it is in, that is, where a single column represents an X,Y pair?

1

I don't believe that you can import with only one column. You'll need to do some data manipulation on your .csv in order to split data out into separate columns. Or additional manipulation in your python script to differentiate one value from another.

For string manipulation in python, see these two Stack Overflow posts on Split string into a list in Python or Split a string by a delimiter in python. You can either use string.split or a csv module for python to achieve what you like and define a variable to that result.

Split the string in text on delimiter: ",":

words = text.split(",")

You will likely need to convert the string to float as well.

From ArcGIS Help on Importing X,Y Data:

To add a table of x,y coordinates to your map, globe, or scene, the table must contain two fields, one for the x-coordinate and one for the y-coordinate.

  • Thanks Mary Beth, I would like to make my python script do that manipulation for me. Any guidance on how to do that? – user2192778 Mar 21 '16 at 20:37
  • Updated answer to include some links on splitting the string in python – MaryBeth Mar 21 '16 at 20:46
0

What separator are you using for your csv? Apparently your X and Y are separated by a comma but remain within the same one column. This implies you are using another delimiter than the comma. You can check this by opening the csv file in a notepad app such as sublime text.

to answer your question using Qgis: In QGIS you can menu -> add layer -> delimimated text layer. There you can specify custom delimiters including commas.

to answer your question for ArcPy (as specified): If you are using Arcpy you can use regular expressions ( import re) to split your variable into two.

import re    
A =  "Xa0,Ya0"    
[B,C] = re.split(".,.",A)

You can then use variable B and C as input for your arcPy command

  • The original data was a .txt file seperated by "|". So it looked like: X0,Y0 | X1, Y1 | etc. I made it into a .csv in Excel, separating columns by "|". I don't want to separate by commas and make separate columns for X and Y because I have many points per entry. Thanks for the response, what is your advice? – user2192778 Mar 21 '16 at 20:36
  • I will update my original answer to make it clear – RutgerH Mar 21 '16 at 20:39
  • You need to split up the the XY pair for the tool to work...there is no way around that. What you could do is split up the XY to use the tool, then use the add field tool, then calculate that new field by adding the xy coordinates together separated by a comma – ziggy Mar 21 '16 at 21:08

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.