6

I want to intersect two polygons within an OpenLayers layer. I found the JSTS library, which should be able to intersect two polygons, and return the new created polygon of the intersection. The function to use: intersection

polygon1.intersection(polygon2)

Using the OpenLayers Polygons directly, I get errors, resulting of the missing functions, added by JSTS. And if I use JSTS Polygons, there seems still something missing. this.getFactory().createGeometryCollection is not a function

How do I get an intersection of those two polygons?

happly
  • 63
  • 1
  • 4

1 Answers1

8

The intersects method in OpenLayers will only return a boolean value, not the intersection of the features.

if(polygon1.intersects(polygon2)){
   //polygons intersect

Edit: I see you mean using the OpenLayers polygon type rather than the OpenLayers intersect method.

The JSTS library has only been out a few days, and the intersects function you referenced was updated 6 hours ago, so not sure how stable it is at the moment.

geographika
  • 14,320
  • 4
  • 53
  • 77
  • You are right. I just want to use the OpenLayers polygon type. I need the actual coordinates of the new intersection. Thanks pointing out that JSTS is still very fresh. – happly May 25 '11 at 09:02
  • @happly - I downloaded it a couple of days ago and couldn't get the unit tests to run, I'll try again in a week, but I wouldn't rely on it for a production system yet. Probably best to do it server-side for the moment – geographika May 25 '11 at 09:49
  • hmm, I could use it so much now. Thanks. You don't now any mature client-based solutions? – happly May 25 '11 at 15:43
  • @happly - not really.. see http://gis.stackexchange.com/questions/7159/pure-javascript-library-for-geometry-operations/7173#7173 – geographika May 27 '11 at 13:17