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.
LineID | Lat_Long |
S136655227013 | 21.12173590787274,79.1472733529529,21.1218167959283,79.14727069126126,21.12189369747202,79.14727603991854,21.1219516019768,79.14727817566171 |
N062473327012 | 21.12173590787274,79.1472733529529,21.1218167959283,79.14727069126126,21.12189369747202,79.14727603991854,21.1219516019768,79.14727817566171,21.12200625594409,79.14728308379594 |
S698455527012 | 21.020929394087638,79.12738378329531,21.02091591890449,79.12744257372505 |
Regards,
Raj P
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.
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")
Hi,
Thanks for replying. I am using ArcGIS 10.1. Is it possible can I use "ExcelToTable_conversion"?
Regards,
Rengaraj P
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
Before you start drawing lines, perhaps you might be interested in closing your other open threads.