-2

I have a set of coordinates of lines in CSV file format, how can I create these lines by a Python script if my CSV file is formatted as below

Loop,StaID,Lat,Lon,
342,9188,15.5746355,101.0633011 (Start point)
342,295,15.7204628,101.0525589
342,297,15.735034,101.0975418
342,298,15.6492653,101.0743179
342,299,15.6219378,101.1156235
342,269,15.5810852,101.1130219
342,324,15.5432377,101.0828323
342,325,15.513896,101.0956879 (Stop point)
,,,,
341,9188,15.574626,101.0633163 (Start point)
341,57,15.6896362,101.1338882
341,69,15.6626444,101.143425
341,76,15.6743202,101.1845016
341,118,15.6577511,101.2464371
341,191,15.6297569,101.2159348
341,264,15.6050987,101.2157745
341,265,15.5768576,101.1519699 (Stop point)
,,,,
340,9188,15.5746603,101.0632782 (Start point)
340,1,15.6460323,100.9904327
340,2,15.6539774,100.9547653
340,3,15.6661177,100.9103851
340,4,15.6963463,100.8859406
340,5,15.656641,100.8758774
340,6,15.6098309,100.9330368
340,7,15.5961771,100.9054489
340,20003,15.5841169,100.8449707
340,8,15.5683022,100.8716278 (Stop point)
  • 1
    What have you tried? Please include code snippet and info on where you are stuck and any errors you encounter – Midavalo Sep 18 '16 at 17:59
2

You will need to edit your CSV file first to remove the blank rows with ,,,,. For example:

Loop,StaID,Lat,Lon
342,9188,15.5746355,101.0633011
342,295,15.7204628,101.0525589
342,297,15.735034,101.0975418
342,298,15.6492653,101.0743179
342,299,15.6219378,101.1156235
342,269,15.5810852,101.1130219
342,324,15.5432377,101.0828323
342,325,15.513896,101.0956879
341,9188,15.574626,101.0633163
341,57,15.6896362,101.1338882
341,69,15.6626444,101.143425
341,76,15.6743202,101.1845016
341,118,15.6577511,101.2464371
341,191,15.6297569,101.2159348
341,264,15.6050987,101.2157745
341,265,15.5768576,101.1519699

This can easily be accomplished with any text editor using find and replace. Or you can probably integrate this into your script with str.replace.

For the python script, you will first need to convert the CSV file into a spatial format. This can be done with MakeXYEventLayer_management - here is a sample usage from ESRI.

Once you've done that, you can use PointsToLine_management - here is also a sample usage from ESRI. Use the Loop field as the Line_Field parameter.

ESRI's website is always a great place to start for scripting examples, mind you the MakeXYEventLayer_management is not so obvious. This is all a fairly simple process once you format the CSV file properly. All of this can easily be done using tools in ArcGIS (as opposed to a python script), and when that's the case, ESRI has examples of just about every tool in python script format.

|improve this answer|||||
  • 1
    Can't the Loop field be used for the line ID? – mkennedy Sep 19 '16 at 17:46
  • Yes, good catch! I completely overlooked that and will update my answer. – ge0m3try Sep 20 '16 at 1:20

Not the answer you're looking for? Browse other questions tagged or ask your own question.