Here's a python function that does something similar. link_value is the id that the points share. lat_fc is the output line featureclass.
def make_laterals( self ):
""" Uses 2 input FCs - SH and BH well locations - and creates a straight
as-the-crow-flies lateral line from the SH to the BH. """
from_sh_fc = args.surf_out_fc
to_bh_fc = args.bott_out_fc
lat_fc = args.laterals_output_dataset
skip_message = 'No corresponding point for link value %s - moving on...'
def add_print_message( msg, severity ):
""" Adds a message based on severity """
if severity == 0: self.gp.AddMessage(msg)
elif severity == 1: self.gp.AddWarning(msg)
elif severity == 2: self.gp.AddError(msg)
def create_point( x, y ):
""" Creates and returns point objects """
pnt = self.gp.CreateObject('Point')
pnt.x = x
pnt.y = y
return pnt
from_desc = self.gp.Describe(from_sh_fc)
to_desc = self.gp.Describe(to_bh_fc)
lb, rb = self.get_field_wrapper()
self.gp.CreateFeatureclass(os.path.split(lat_fc)[0], \
os.path.split(lat_fc)[1], 'POLYLINE', '', 'DISABLED', \
'DISABLED', self.spatial_ref)
out_desc = self.gp.Describe(lat_fc)
out_rows = self.gp.InsertCursor(lat_fc)
from_rows = self.gp.SearchCursor(from_sh_fc)
from_row = from_rows.Next()
point_array = self.gp.CreateObject('Array')
part_array = self.gp.CreateObject('Array')
while from_row:
from_feat = from_row.GetValue(from_desc.Shapefieldname)
from_x, from_y = map(lambda x: float(x), from_feat.centroid.split(' '))
from_point = create_point(from_x, from_y)
# Get value of LATID
link_value = from_row.GetValue(self.lat_id_field)
where_clause = '%s%s%s = %s' % (lb, self.lat_id_field, rb, link_value)
to_rows = self.gp.SearchCursor(to_bh_fc, where_clause)
to_row = to_rows.Next()
match_count = 0
while to_row:
to_feat = to_row.GetValue(to_desc.Shapefieldname)
to_x, to_y = map(lambda x: float(x), to_feat.centroid.split(' '))
to_point = create_point( to_x, to_y )
point_array.Add(from_point)
point_array.Add(to_point)
part_array.Add(point_array)
out_row = out_rows.NewRow()
out_row.SetValue(out_desc.shapefieldname, part_array)
out_rows.InsertRow(out_row)
point_array.RemoveAll()
part_array.RemoveAll()
to_row = to_rows.Next()
match_count+=1
#if match_count == 0:
#add_print_message( skip_message % link_value, 1 )
del to_row
del to_rows
from_row = from_rows.Next()
del out_row
del out_rows
del from_row
del from_rows