RGeo is a gem for writing location-based applications in Ruby programming language.

It is used to:

• Represent spatial and geolocation data objects such as points, lines, and polygons.
• Perform standard spatial analysis operations such as finding intersections, creating buffers, and computing lengths and areas.
• Correctly handle spherical geometry, and compute geographic projections for map display and data analysis.
• Read and write location data in the WKT and WKB representations used by spatial databases.

#### Prerequisites:

• Rails app.
• PostgreSQL database with PostGIS extension enabled.
• rgeo gem.

#### Installing the rgeo gem:

Before we install rgeo gem, we need to install Geos. For installing geos on Mac, execute brew install geos.

Add rgeo in the Gemfile and execute bundle install.

To verify that rgeo is installed correctly, we need to execute RGeo::Geos.supported? in the Rails console (rails c in the terminal).

If the response is true, it is installed successfully.

#### RGeo Calculations:

To store spatial data, we must create column with spatial type. PostGIS provides various spatial datatypes like point, linestring, polygon, etc.

The activerecord-postgis-adapter gem supports the above spatial type in ActiveRecord’s migration.

We can create spatial datatype columns in migration as below

We will create a simple model Locality with columns name and latlong as below.

The migration file looks as below

##### Distance between two points

We create two Locality entries as below.

We can get the distance between the above two coordinates by using the distance method as below

The distance method returns the shortest distance in meters between two points. To the POINT function, we pass two numeric values longitude and then latitude.

##### Querying by location

The PostGIS database supports querying on spatial datatype.

We can figure out which of the above two localities created are closer to a particular coordinate.

For eg., Boston Public Garden is around 36.3 miles (58.42 km) from Saeloun locality and around 215 miles (346 km) from Statue of Liberty. Using the coordinates of Boston Public Garden we can identify which localities are within 62 miles (100 km) and which are greater than 62 miles.

Boston Public Garden coordinates are 42.3541675, -71.0726166. Where 42.3541675 is latitude and -71.0726166 is longitude.

Here we have used ST_Distance function which calculates the distance between two points.

##### Distance between points using Rgeo factory

We can calculate the distance between two points by using their coordinates and calling the distance function.

The point function expects longitude and latitude as the first and second parameter respectively.

##### Point Inside a polygon or not

PostGIS database also supports storing polygon data in a table.

We will add the polygon datatype column to Locality model via migration as shown below:

Let’s say we create New York locality in Locality table which has polygon endpoints at

• Saeloun Office
• Statue of Liberty
• Syracuse

Syracuse coordinates are (43.0352286, -76.1742994)

We create the New York locality in Locality table as below

Kingston lies between the polygon created above. And Virginia lies outside this polygon.

So we can check whether a point lies inside a polygon or not
using contains? method as below

We can also check whether a coordinate lies within any Locality, using the below query.

Note we can index these spatial datatype colum to improve query performance.