0

I generated two lines by calculating the intersection of each of two overlapping Polygon objects with an overlapping LineString object that goes through both. In pictures:

Here is the code:

src = LineString([(-122.440616, 37.750902), (-122.440459, 37.749301)])
a_poly = Polygon(((-122.44084144582368, 37.74966501461027),
   (-122.4407546051888, 37.749669348654436),
   (-122.44074728377709, 37.74966413691908),
   (-122.44072947894435, 37.74965106437307),
   (-122.44071605469057, 37.74964123563146),
   (-122.44071580998164, 37.749638173243724),
   (-122.44044682175999, 37.749655315369054),
   (-122.44041519016142, 37.749341472792594),
   (-122.44082372316697, 37.74931828715529),
   (-122.44082515110463, 37.74933242065194),
   (-122.44094012271799, 37.74932576700128),
   (-122.44093963322123, 37.74932023686068),
   (-122.44112760800229, 37.74930901283527),
   (-122.44114383090036, 37.74948122299267),
   (-122.44110346569798, 37.749483671390834),
   (-122.44112066603057, 37.74965192189693),
   (-122.44084195921394, 37.749670004175755),
   (-122.44084144582368, 37.74966501461027))
)
b_poly = Polygon(((-122.44036668771517, 37.7495078520912),
   (-122.4403364930644, 37.749488729479936),
   (-122.44033309824997, 37.749442807195095),
   (-122.44035460764636, 37.74942062092555),
   (-122.44035494604461, 37.74940952831284),
   (-122.44035994318475, 37.74940920977011),
   (-122.44035326642151, 37.74934005863108),
   (-122.44045007744725, 37.74933406591913),
   (-122.44066689420853, 37.74932409318848),
   (-122.44066673795734, 37.749321420022994),
   (-122.44069487226923, 37.74929327979845),
   (-122.44075497066868, 37.74928946804546),
   (-122.44079256200197, 37.74931065350201),
   (-122.44079271493288, 37.749312664460376),
   (-122.44088625460884, 37.74930719843525),
   (-122.44088655376535, 37.749310404080106),
   (-122.44115133391134, 37.749293728937005),
   (-122.44116853709428, 37.74946620946213),
   (-122.44090259255685, 37.74948298993952),
   (-122.44090310242379, 37.74948893632612),
   (-122.44088110888472, 37.74949026172547),
   (-122.44047257587917, 37.74951344736277),
   (-122.44036703029442, 37.74951757439541),
   (-122.44036668771517, 37.7495078520912)))

a = src.intersection(a_poly)
b = src.intersection(b_poly)

If I plot a and b, I see that they are (almost?) aligned.

But when I ask for the intersection of the two lines, I get a singular point:

from shapely.geometry import LineString
a = LineString([(-122.44049345405486, 37.749652343578546), (-122.44046270447373, 37.749338776193795)])
b = LineString([(-122.44047979320894, 37.74951303775477), (-122.44046218794172, 37.749333508883325)])
type(a.intersection(b))

shapely.geometry.point.Point

I expected that the intersection of these lines would be another line. I do not understand how two independent intersections of a polygon and a line can not themselves intersect in a line. What error am I making here?

  • 1
    Floating point computing is inaccurate en.wikipedia.org/wiki/…. You should round the coordinates a bit and accept some tolerance. – user30184 Nov 4 '18 at 21:56
  • This is correct, if you write this comment as an answer I can accept it and close out the question. – Aleksey Bilogur Nov 5 '18 at 0:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.