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.
- Rails app.
- PostgreSQL database with PostGIS extension enabled.
- rgeo gem.
- activerecord-postgis-adapter gem.
Installing the rgeo gem:
Before we install rgeo gem, we need to install Geos.
geos on Mac, execute
brew install geos.
rgeo in the Gemfile and execute
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.
To store spatial data, we must create column with spatial type. PostGIS provides various spatial datatypes like point, linestring, polygon, etc.
activerecord-postgis-adapter gem supports the above spatial type in
We can create spatial datatype columns in migration as below
We will create a simple model
Locality with columns
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
distance method returns the shortest distance in meters between two points.
POINT function, we pass two numeric values
longitude and then
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
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
point function expects
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 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
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.