Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I'm attempting to automate this in ModelBuilder in ArcMap. I have one feature class with one point (Start) and one feature class with multiple points (Destinations). I want to iterate through the Destinations so I create multiple lines from the Start point to each Destination point.

I was looking at possibly extracting the XY coordinates and then using "XY To Line", but that wants the xy to be in one table (or feature), not two. Is there a simpler way of connecting two points?

share|improve this question
    
It sounds like you are thinking along the lines of Origin-Destination matrices so if you have access to the Network Analyst extension and have not investigated its capabilities then a good entry point is here. –  PolyGeo May 17 '13 at 23:54
    
Erica, how did you tackle the issue with shapefieldname = desc.ShapeFieldName. I am having the same problem you are having. Can you share your code please. –  Abe Jun 25 at 19:40
    
My code included the content from eseglem's Answer below; other than that, I do not have the original anymore, unfortunately (this was a minor script from over a year ago) and so you may want to ask a new Question. Sorry I could not be of more help. –  Erica Jun 26 at 11:11

2 Answers 2

up vote 3 down vote accepted

I don't have Arc on this machine but this should work. It would actually work with multiple start points, but will work on a single start point as well.

import arcpy

start_feature = arcpy.GetParameterAsText(0)
end_feature = arcpy.GetParameterAsText(1)
out_feature = arcpy.GetParameterAsText(2)

start_cursor = arcpy.SearchCursor(start_feature)
end_cursor = arcpy.SearchCursor(end_feature)

desc = arcpy.Describe(start_feature)
shapefieldname = desc.ShapeFieldName

point = arcpy.Point()
array = arcpy.Array()
featureList = []

cursor = arcpy.InsertCursor(out_feature)
feat = cursor.newRow()

for start in start_cursor:
    startFeature = start.getValue(shapefieldname)
    pnt1 = startFeature.getPart()
    for end in end_cursor:
        endFeature = end.getValue(shapefieldname)
        pnt2 = endFeature.getPart()
        point.X = pnt1.X
        point.Y = pnt1.Y
        array.add(point)
        point.X = pnt2.X
        point.Y = pnt2.Y
        array.add(point)
        polyline = arcpy.Polyline(array)
        array.removeAll()
        featureList.append(polyline)
        feat.shape = polyline
        cursor.insertRow(feat)

del feat, cursor, end_cursor, start_cursor

You may actually want to change the inputs so it creates the output file during the script instead of having to point at one that's already created, but that depends on what you want to do.

share|improve this answer
    
This is breaking at line 10, with error: File "C:\Users\Owner\Dropbox\arcpy02.py", line 10, in <module> shapefieldname = start_feature.ShapeFieldName AttributeError: 'unicode' object has no attribute 'ShapeFieldName' –  Erica May 19 '13 at 1:49
    
I fixed that with a minor tweak, but now it's complaining about point.X = pnt1.X : AttributeError: 'PointGeometry' object has no attribute 'X' –  Erica May 19 '13 at 2:11
    
AWESOME, I debugged a couple of things and now it does what it needs to -- I have edited your code and that's a great step forward in my process! –  Erica May 19 '13 at 2:21
    
Sorry for the messy code, like I said I don't have Arc at home or else I would have tested it myself. I actually wrote it all in notepad. Glad you were able to fix them though, and thanks for the edit/accept. –  eseglem May 19 '13 at 2:29
    
No problem -- it's good for me to have to fix it anyway, that way I have an idea of what it's supposed to be doing! :D –  Erica May 19 '13 at 2:41

While this is not model builder, it should give you what you are looking for (python). If you change the origin_x, origin_y, "MYFILE.csv", "layer_with_destinations","field_with_x_coordinate", and "field_with_y_coordinate" to the appropriate names this should give you a single table that has origin x, origin y, destination x and destination y fields which you can use for the input for "XY To Line"

import arcpy
import csv
cur = arcpy.UpdateCursor("layer_with_destinations")
destinations=[]
origin_x = #put your start point x coordinate here
origin_y = #put your start point y coordinate here
for i in cur:
    x,y=i.getValue("field_with_x_coordinate"),i.getValue("field_with_y_coordinate")
    destinations.append([x,y])

output_file = open("MYFILE.csv", "wb")#put your desired output csv file here including path
c = csv.writer(output_file)
c.writerow(["org_x", "org_y", "dest_x", "dest_y"])
for dest in destinations:
    c.writerow([origin_x, origin_y, dest[0], dest[1]])
output_file.close()
share|improve this answer
    
I suppose I could just embed this in a ModelBuilder model, or go all arcpy :-) –  Erica May 17 '13 at 20:08
    
or you could use the design from this and the iterators in model builder to recreate this in the model builder. –  Hotpepper May 17 '13 at 20:20

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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