Draw Line.

2860
5
10-05-2015 02:58 AM
RajP
by
New Contributor

Hello,

In the excel file I have column name as LineId and Long_Lat. In the LineId, there is LineID value & In the Long_Lat, there is Line Feature Vertices. I need to draw line for each lineId using Long_Lat using Python Script. Can any one suggest me.

  

LineIDLat_Long
S13665522701321.12173590787274,79.1472733529529,21.1218167959283,79.14727069126126,21.12189369747202,79.14727603991854,21.1219516019768,79.14727817566171
N06247332701221.12173590787274,79.1472733529529,21.1218167959283,79.14727069126126,21.12189369747202,79.14727603991854,21.1219516019768,79.14727817566171,21.12200625594409,79.14728308379594
S69845552701221.020929394087638,79.12738378329531,21.02091591890449,79.12744257372505

Regards,

Raj P

0 Kudos
5 Replies
IanMurray
Frequent Contributor

You can work with arcpy Geometry objects to create polylines from points.

ArcGIS Help (10.2, 10.2.1, and 10.2.2) Working with geometries

ArcGIS Help (10.2, 10.2.1, and 10.2.2) Arcpy.Polyline

You will need to be able to parse each set of lat longs, convert to point geometry make arrays of points for each line, then insert the arrays into the Polyline Object.

LukeSturtevant
Occasional Contributor III

There are a couple ways to go about doing this, but you should first export your excel table to an info table using the Excel to Table tool

Toolboxes\System Toolboxes\Conversion Tools.tbx\Excel\Excel To Table:

ArcGIS Help (10.2, 10.2.1, and 10.2.2)

if your excel table is setup the same as your example with a long list of coordinates for each entry in a single column then you could try a code similar to the following to parse the coordinate pairs:

polyline = arcpy.CreateFeatureclass_management("C:\Generate_Lines","Lines.shp","POLYLINE","","","",4326) # This generates an empty line shapefile with WGS 1984 spatial reference WKID: 4326

arcpy.AddField_management(polyline,"LineID","TEXT") # This adds a LineID field to the empty line layer

table = "C:\Generate_Lines\generatelines" # This references the table exported from excel

with arcpy.da.SearchCursor(table,["LINEID","LAT_LONG"]) as search:

     for row in search:

         array = arcpy.Array()

         for i in range (len(row[1].split(","))/2):

                 array.add(arcpy.Point(row[1].split(",")[i*2+1],row[1].split(",")[i*2]))

         with arcpy.da.InsertCursor(polyline,["SHAPE@","LineID"]) as insert:

             insert.insertRow((arcpy.Polyline(array),row[0]))

The above search and insert cursor section loops through the 'table' which would reference the exported excel table that has the field names "LINEID" and "LAT_LONG". It will cycle through the lat / long entries and create an array that can be used to generate a polyline. This is all contingent on valid coordinate entries for polyline generation of course.

However, sometimes the above method may not produce desired results in which case you can generate a point layer first and then just use the standard Points to Line ArcGIS Help (10.2, 10.2.1, and 10.2.2) tool to generate lines based on LineID:

points = arcpy.CreateFeatureclass_management("C:\ArcGIS_LPS\EGIS_Help\Generate_Lines","Vertices.shp","POINT","","","",3857)

arcpy.AddField_management(points ,"LineID","TEXT")

table = "C:\Generate_Lines\generatelines"

with arcpy.da.SearchCursor(table,["LINEID","LAT_LONG"]) as search:

      for row in search:

          array = arcpy.Array()

          for i in range (len(row[1].split(","))/2):

                  with arcpy.da.InsertCursor(points ,["SHAPE@","LineID"]) as insert:

                      insert.insertRow((arcpy.Point(row[1].split(",")[i*2+1],row[1].split(",")[i*2]),row[0]))

  

arcpy.PointsToLine_management(points,"C:\Generate_Lines\\generatelines.shp","LineID")

RajP
by
New Contributor

Hi,

Thanks for replying. I am using ArcGIS 10.1. Is it possible can I use "ExcelToTable_conversion"?

Regards,

Rengaraj P

0 Kudos
DanPatterson_Retired
MVP Emeritus

There are a couple ways to go about doing this, but you should first export your excel table to an info table using the Excel to Table tool

Toolboxes\System Toolboxes\Conversion Tools.tbx\Excel\Excel To Table:

ArcGIS Help (10.2, 10.2.1, and 10.2.2)

as indicated in his post​, does it exist in 10.1?  check the help

0 Kudos
DanPatterson_Retired
MVP Emeritus

Before you start drawing lines, perhaps you might be interested in closing your other open threads.