3

I'm looking for a way to check if 2 polygons (set of lat/lon coordinates) overlap in ruby. So for example if I have a points for the USA and points for California, I should be able to tell that they overlap.

I looked into rgeo, but apparently it requires some linux-only binaries to get it to work and I'm looking for a cross platform solution.

For example let's say I have 2 polygons that look like:

p1 = [[30, 30], [30, 40], [40, 40], [40, 30], [30, 30]]
p2 = [[35, 35], [35, 45], [45, 45], [45, 35], [35, 35]]

How can I show that they overlap in ruby?

pguardiario
  • 53,827
  • 19
  • 119
  • 159
  • It depends on whether both polygons are covex. (A set is covex if all points on a straight line joining any two points in the set are all in the set.) If either polygon is not convex you have a nasty problem. If they are both convex, you need to determine (as was mentioned) whether any vertex of the first convex polygon is within the second convex polygon. I showed how to do that (for one vertex) [here](http://stackoverflow.com/questions/25497324/creating-polygon-object-for-geofencing-ruby). – Cary Swoveland Jan 17 '16 at 09:25
  • Good point, let's assume that both polys are convex. – pguardiario Jan 17 '16 at 09:29
  • If you insist on using libraries that are "easy" to use with windows, you should put that in your question. – boulder_ruby Jan 25 '16 at 15:54
  • It's all there in the second paragraph. I followed all the steps to get rgeos working on windows and continue to experience problems with that particular library. – pguardiario Jan 25 '16 at 20:08

1 Answers1

2

Using rgeo, it would look something like this...

note: I'm just going to use my local rectilinear projection for this, SRID:3361, should work fine. The units are in feet, as it were. But basically just a cartesian grid CRS (coordinate reference system)

require 'rgeo'
f = RGeo::Geos.factory(:srid => 3361)
p1 = [[30, 30], [30, 40], [40, 40], [40, 30], [30, 30]]
p2 = [[35, 35], [35, 45], [45, 45], [45, 35], [35, 35]]
#first you have to build the polygons, its a 3 step process. array of points --> linear ring --> polygon
polygons = []
[p1, p2].each do |pointset|
  points = []
  pointset.each do |x, y|
    points << f.point(x,y)
  end
  polygons << f.polygon(f.linear_ring(points))
end
#then you'd just use rgeo's methods to do the work
polygons[0].overlaps?(polygons[1])
#=> true

Tested this. it works

boulder_ruby
  • 38,457
  • 9
  • 79
  • 100
  • This looks good but RGeo::Geos requires some linux-only binaries and I'm looking for something that will work on windows. – pguardiario Jan 20 '16 at 22:29
  • Linux or mac...and it looks like there is some support for windows, but you have to jump through some hoops first. The question asker answered his own question which means he got it working. http://stackoverflow.com/questions/22297117/rgeo-on-ruby-under-windows-how-to-enable-geos-support – boulder_ruby Jan 21 '16 at 00:05
  • 1
    I did spend some time trying but I eventually had to give up because I wasn't getting anywhere. – pguardiario Jan 21 '16 at 00:35