Hello,

I am new to Python with only a beginners class completed. I am trying to change code that I have already used to compare line segments. I now want to compare two different polyline shapefiles to eachother and export the intersections as a point shapefile. I'm having trouble understanding how to compare the files. This is what I have so far.

Although not pasted here, I have codes to read in the two shapefiles, and create layers that display in tkinter. I am not using ArcGIS for this, so calling in arc geoprocessing to use the intersect tool is not an option. I need to figure this out all in tkinter. Any help, even with pseudo-logic would help me.

Thanks

Andy

```
import math
from Tkinter import *
class Point:
def __init__(self, x, y):
self.x = float(x)
self.y = float(y)
def dis(self,p1):
return sqrt((self.x-p1.x)**2+(self.y-p1.y)**2)
class Linesegment:
def __init__(self,p1,p2):
self.p1=p1 # end coordinate 1
self.p2=p2 # end coordinate 2
dataFile=open("Segments.txt","r+")
data=dataFile.read()
i=0
lines = []
p1 = Point(50,200)
p2 = Point(400,200)
l1 = Linesegment(p1,p2)
lines.append(l1)
p1 = Point(60,450)
p2 = Point(400,450)
l2 = Linesegment(p1,p2)
lines.append(l2)
p1 = Point(100,600)
p2 = Point(350,250)
l3 = Linesegment(p1,p2)
lines.append(l3)
p1 = Point(300,100)
p2 = Point(300,400)
l4 = Linesegment(p1,p2)
lines.append(l4)
#Determines Overlapping for two same-line line segments:
def checkboundingbox(l1, l2):
smally1=min(l1.p1.y,l1.p2.y)
bigy1=max(l1.p1.y,l1.p2.y)
smally2=min(l2.p1.y,l2.p2.y)
bigy2=max(l2.p1.y,l2.p2.y)
if smally2>bigy1 or bigy2 < smally1:
return False
smallx1=min(l1.p1.x,l1.p2.x)
bigx1=max(l1.p1.x,l1.p2.x)
smallx2=min(l2.p1.x,l2.p2.x)
bigx2=max(l2.p1.x,l2.p2.x)
if smallx2>bigx1 or bigx2 < smallx1:
return False
return True
def intersect(l1, l2):
if l1.p1.x==l1.p2.x:
if l2.p1.x==l2.p2.x:
print 'Both lines vertical'
return False
else:
b2=(l2.p2.y-l2.p1.y)/(l2.p2.x-l2.p1.x)
a2=l2.p1.y-b2*l2.p1.x
x0=l1.p1.x
y0=a2+b2*x0
else:
if l2.p1.x==l2.p2.x:
b1=(l1.p2.y-l1.p1.y)/(l1.p2.x - l1.p1.x)
a1=l1.p1.y-b1*l1.p1.x
x0=l2.p1.x
y0=a1+b1*x0
else:
b1=(l1.p2.y-l1.p1.y)/(l1.p2.x-l1.p1.x)
b2=(l2.p2.y-l2.p1.y)/(l2.p2.x-l2.p1.x)
a1=l1.p1.y-b1*l1.p1.x
a2=l2.p1.y-b2*l2.p1.x
if b1==b2:
print 'Lines parallel'
return False
else:
x0=-(a1-a2)/(b1-b2)
y0=a1+b1*x0
# tests whether intersection point falls on both lines
if (l1.p1.x - x0)*(x0-l1.p2.x)>=0 and (l2.p1.x - x0)*(x0-l2.p2.x)>=0 and (l1.p1.y-y0)*(y0-l1.p2.y)>=0 and (l2.p1.y-y0)*(y0-l2.p2.y)>=0:
print 'Lines cross at ('+str(x0)+','+str(y0)+')'
return [x0,y0]
else:
print 'Lines do not cross'
root = Tk()
# define window size
windowWidth, windowHeight = 800, 600
can = Canvas(root, width = 800, height = 600)
for line in lines:
can.create_line(line.p1.x,line.p1.y,line.p2.x, line.p2.y)
i = 0
for i in range(0, len(lines)-1):
for j in range(i+1, len(lines)):
if checkboundingbox(lines[i],lines[j]):
p = intersect(lines[i],lines[j])
if p==1:
print "Line segments overlap"
elif p: #create point
can.create_oval(p[0]-3,p[1]-3,p[0]+3,p[1]+3,width=1,fill='red')
##How do I export point shapefile here?
else:
print 'Lines do not cross'
can.pack()
root.mainloop()
```