EDIT: Git Repo for sample files https://github.com/tpubben/lineIntersect
I am trying to calculate the line intersection points in x,y coordinates based on a set of intersecting lines crossing one continuous line made up of multiple segments.
The continuous line is represented by a list of tuples as follows where each segment starts with the x/y coordinate of the endpoint of the previous segment:
lineA = [((x1, y1),(x2,y2)), ((x2,y2),(x3,y3))....]
The crossing lines are represented in the same manner, however each is a discrete line (no shared points):
lineB = [((x1, y1),(x2,y2))...]
I am trying to iterate through the continuous line (lineA) and check to see which crossing lines intersect with which segments of lineA.
An example image of what the line intersections would look like is here:
so far I have tried the following:
from __future__ import print_function
def newSurveys(nintyin, injectorin):
# pull data out of pre-prepared CSV files
fh = open(nintyin)
fho = open(injectorin)
rlines = fho.readlines()
rlines90 = fh.readlines()
segA = []
segB = []
segA90 = []
segB90 = []
for item in rlines:
if not item.startswith('M'):
item = item.split(',')
segA.append((float(item[4]),float(item[5])))#easting northing
segB.append((float(item[4]),float(item[5])))#easting northing
segB.pop(0)
z = len(segA)-1
segA.pop(z)
for item in rlines90:
if not item.startswith('N'):
item = item.split(',')
segA90.append((float(item[1]),float(item[0])))#easting northing
segB90.append((float(item[3]),float(item[2])))#easting northing
activeWellSegs = []
injector90Segs = []
for a, b in zip(segA, segB):
activeWellSegs.append((a,b))
for c, d in zip(segA90, segB90):
injector90Segs.append((c,d))
if len(activeWellSegs) >= len(injector90Segs):
lineA = activeWellSegs
lineB = injector90Segs
else:
lineA = injector90Segs
lineB = activeWellSegs
for l1 in lineA:
for l2 in lineB:
##### Use differential equation to calculate line intersections,
##### taken from another user's post
def line_intersection(line1, line2):
xdiff = (line1[0][0] - line1[1][0], line2[0][0] - line2[1][0])
ydiff = (line1[0][1] - line1[1][1], line2[0][1] - line2[1][1])
def det(a, b):
return a[0] * b[1] - a[1] * b[0]
div = det(xdiff, ydiff)
if div == 0:
raise Exception('lines do not intersect')
d = (det(*line1), det(*line2))
x = det(d, xdiff) / div
y = det(d, ydiff) / div
return x, y
print (line_intersection(l1, l2), file=lprint)
newSurveys('producer90.csv', 'injector.csv')