GeoGraf

Build Status

Overview

This gem computes the relation between multiple polygons (do they overlap and by how much?), such as Wimdu's GeoLocations.

GeoGraf uses RGeo to perform the calculations. It uses the Mercator projection, which projects the surface of earth onto a flat surface. This is inaccurate, as areas appear bigger when they are further away from the equator (have a look at Greenland and the DR Congo on Google Maps, they both are roughly 2 million square kilometers big). However, the assumption is that the error of areas that relate to each other is rather small, as the overlapping area is at the same latitude. And we're not interested in the correct area calculation, we just need to know if they overlap, which is the bigger one and roughly how much they overlap.

The input is expected to be an array of polygon descriptions. Each polygon description has to be a hash with an :id key and :polygon_coords, which itself is an array of coordinates describing the polygon. Each coordinate is an array of it's latitude and longitude (in this order).

Example:

require 'geo_graf'

GeoGraf.intersections_for(
  [
    {
      id: 1,
      polygon_coords: [[-2, -2], [2, -2], [2, 2], [-2, 2], [-2, -2]]
    },
    {
      id: 2,
      polygon_coords: [[-3, -3], [-1, -3], [-1, -1], [-3, -1], [-3, -3]]
    }
  ]
)

The output is an array of relation descriptions. Each description is a hash in the following format:

  • :id: The ID of the polygon. It's always the ID of the smaller polygon (by area).
  • :contained_area_percentage: How much of the area of the smaller polygon is contained in the larger one.
  • :container_id: The ID of the larger polygon.

If two polygons don't overlap, no relation exists and there will be no description returned for these two.

For the example above, the following result will be returned:

[
  {
    id: 2,
    contained_area_percentage: 25,
    container_id: 1
  }
]

This means that 25% of the area of polygon 2 overlap with the area of polygon 1.

Installation

GeoGraf depends on RGeo with GEOS >= 3.3.3. You need to install it before installing RGeo.

MacOs with homebrew

brew install geos

Ubuntu/Debian

sudo apt-get install libgeos++-dev

License

Copyright (c) 2013 Wimdu GmbH (MIT License). See LICENSE.txt for details.