Shapefiles in Ruby on Rails

Shapefiles in Ruby on Rails

Continuing with the last post, we’ll now see how to open a shapefile using Ruby on Rails.

I found basically two ways to accomplish this. The first one is by using the RGeo gem while the second one is by converting the shapefile into a GeoJSON file.

Alternative 1: RGeo gem

RGeo is a Ruby Library that provides a implementation of the OGC Simple Features Specification, used for representing geometric objects. To open shapefiles you need an extra module called RGeo::Shapefile.

So to start with, install the gems as follows:

gem install rgeo
gem install rgeo-shapefile

Next there’s an example of how to open a shapefile using this method:

require 'rgeo/shapefile'

def load_opendata_rgeo
  factory = RGeo::Geographic.projected_factory(:projection_proj4 => "+proj=utm +zone=21 +south +datum=WGS84 +units=m +no_defs")"#{Rails.root}/script/ep_residuos_decaux.shp", :factory => factory.projection_factory) do |file|
    puts "Number of records: #{file.num_records} "
    file.each do |record|

      puts "Record #{record.index}"
      geo = record.geometry
      latlon_geometry = factory.unproject(geo)
      puts "Coordinates: (#{latlon_geometry.latitude},#{latlon_geometry.longitude})"
      puts "Attribute OBJECTID value: #{record.attributes['OBJECTID']}"


If you want to display your information in a map (Google maps, OpenStreetMap) you might need to change your coordinate system. This is what “factory.unproject” does. To do so you need to know the coordinate system in which your shapefile is. In my case the coordinate system is “+proj=utm +zone=21 +south +datum=WGS84 +units=m +no_defs”. You can get this information from QGIS (Right click on the layer > Properties > General > Coordinate reference system).


Warning: Now, the reason why I explored a second alternative is because I ran into problems deploying the application to Heroku while using the rgeo gem. The thing is that you need two libraries in the server to make rgeo to work: Geos and Proj. The only way to do this in Heroku is by installing a buildpack. If you need more information check the following link: .


Alternative 2: Converting the shapefile into a GeoJSON file

GeoJSON is a format for encoding geographic information. The difference with the shapefile format is that GeoJSON is an open standard and also as it’s a JSON you can easily parse it using the json gem.

So, the first thing you need to do is to convert your shapefile into a GeoJSON.

If you have GDAL installed you can use the ogr2ogr utility.

To convert the file run the following command:

ogr2ogr -f GeoJSON -t_srs EPSG:4269 [filename].geojson [filename].shp

The -t_srs parameter is used to reproject/transform the points to a different coordinate system. Two popular systems are crs:84 and EPSG:4269. The difference is that crs:84 has a (longitude, latitude) coordinate order while EPSG:4269 a (latitude, longitude).

The resultant file will be something like this:

"type": "FeatureCollection",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{ "type": "Feature", "properties": { "GID": 22894498.0, "OBJECTID": 1.0, "IDLI": 128.0, "N__ORDINAL": 1.0, "CCZ": 18.0, "COD_IMM": "CP hm.18.01", "CODIGO__DE": 631.0, "TIPO_RESID": "PILAS", "CONTENIDO": "Pilas", "UCREA": null, "FCREA": null, "UACT": "IM1726246", "FACT": "2012-12-13 14:28:23", "IDAUDITORI": "B8C12E7F22806823D3C7AD55FB5520CF" }, "geometry": { "type": "Point", "coordinates": [ -56.352119473107706, -34.790626820565095 ] } },


Now that you have your GeoJSON file is very easy to parse it in Ruby.

First, be sure to have the JSON gem installed. If not, run the following command:

gem install json

Finally, you can access the GeoJson file as follows:

def load_opendata_geojson
  file = open("#{Rails.root}/script/ep_residuos_decaux.geojson")
  json =
  parsed = JSON.parse(json)
  parsed["features"].each do |record|

    puts record["properties"]["OBJECTID"]
    puts record["geometry"]["coordinates"][1] #latitude
    puts record["geometry"]["coordinates"][0] #longitude




One thought on “Shapefiles in Ruby on Rails

Leave a Reply

Your email address will not be published. Required fields are marked *