import arcpy outPath = r"E:\Backup Files\TMC Interstates\TMC-Sets" #output path for the feature class featureClass = "roadSegments.shp" #the name of your shapefile spatialRef = arcpy.SpatialReference("WGS 1984") #the spatial reference you want; set it to whatever you want arcpy.CreateFeatureclass_management(outPath, featureClass, "POLYLINE", " ", "DISABLED", "DISABLED", spatialRef) #sets up the shapefile we're using arcpy.AddField_management(outPath + "\\" + featureClass, "segmentID", "SHORT", 15) #I assume that each segment has some sort of ID number that you might want to import into roadArray = arcpy.Array() #this will hold the coordinates for each line segment until we write it to the shapefile. roadFile = open(r"E:\Backup Files\TMC Interstates\TMC-Sets\TMC_Identification.csv") #the path to your road lines file headerLine = roadFile.readLine() indexList = headerLine.split(",") #now we need the index locations for the columns that contain the latitude and longitude points for the starting and ending points for each line segment. I'm also assuming that each segment has some sort of ID value in your segment. Change the value inside the parenthesis to reflect whatever you named your column for those values. idValueIndex = indexList.index('tmc') fromLatValueIndex = indexList.index('start_latitude') fromLongValueIndex = indexList.index('start_Longitude') toLatValueIndex = indexList.index('end_Latitude') toLongValueIndex = indexList.index('end_Longitude') roadCursor = arcpy.InsertCursor(outPath + "\\" + featureClass) for fileLine in roadFile.readlines(): readLine = fileLine.split(",") idNumber = int(readLine[idValueIndex]) fromLat = float(readLine[fromLatValueIndex]) fromLong = float(readLine[fromLongValueIndex]) toLat = float(readLine[toLatValueIndex]) toLong = float(readLine[toLongValueIndex]) #now let's add the points and draw the lines fromVertex = arcpy.Point(fromLong, fromLat) roadArray.add(fromVertex) toVertex = arcpy.Point(toLong, toLat) roadArray.add(toVertex) roadCursor = arcpy.InsertCursor(outPath + "\\" + featureClass) feature = roadCursor.newRow() polyline = arcpy.Polyline(roadArray, spatialRef) feature.shape = polyline feature.setValue("segmentID", idNumber) #adds the segmentID in the attribute table roadCursor.insertRow(feature) roadArray.removeAll() del roadCursor print "Finished!" print arcpy.GetMessages()
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question