GIS on Rails

Siva Gollapalli
Jun 28 · 2 min read

Dealing with Geospatial data is cumbersome and at the same time, it is fun. Developers who do it for the first time might enter some road blockers but as you progress it gives a joy of learning. GIS stands for Geographic information system which deals with gathering, mapping and analyzing geospatial data. But it is not as easy as it sounds. Let me walk through you the challenges that I have faced recently in my project:

Requirement: Import existing polygons information and map that given latitude and longitude with the polygon.

Challenge-1: Understanding the basics and jargon

Since I am doing for the first time, like what SRID should I use, what datatype should I use, what coordinate system should I use? After thorough research I end up using srid 4326, multipolygon datatype and WGS 84 coordinate system.

Many of you might not understand what WGS 84 means? WGS 84 stands for World Geodetic System is kind of survey (standard) which was done on the year 1984. There are different kinds of standards that are exists currently like WGS 72, WGS 66 and WGS 60 etc. These are the surveys(standards) that are conducted in those respective years by making some assumptions about earth. So, for example if we calculate India location using WGS 84 is might be different if we calculate using WGS 72.

SRID stands for Spatial Reference System Identifier which tells us about the coordinate system we were using while gathering spatial data.

Challenge-2: Data importing

Since we are using rails powered with postgresql, importing data into database is quite easy but not in this case as we got information in geojson format which I never seen before. Fortunately, ruby provides a gem called rgeo-json which makes our life easy. Following is the code snippet that I have written to import geojson data.

@geo_factory = RGeo::Geographic.spherical_factory(srid: 4326)f = RGeo::GeoJSON.decode(
File.open(<geojson file path>).read,
geo_factory: @geo_factory)
)
f.each do |polygon|
Polygon.new(geopolygon: polygon.geometry).save
end

where polygon is our model. Here is our migration file looks like:

add_column :polygons, :geopolygon, :multi_polygon, srid: 4326 

Challenge-3: Map point with Polygon using sql query

PostGIS is a postgres database extension which enables postgres to make spatial calculations. To find that given point lies which polygon we need to make use of st_contains function. To use st_contains, first we have to make sure whether two geometries should use same SRID or not ie., in our case it is 4326. Since in my database we are storing latitude and longitude as float columns, first we need to typecast them to as a POINT and set SRID to 4326 as follows:

# Returns geometry of a given point.
ST_SetSRID(ST_POINT(<longitude>, <latitude>), 4326)

Now final query to find appropriate polygon:

Polygon.where("st_contains(geopolygon, 
ST_SetSRID(ST_Point(<long>,<lat>), 4326)
) = true").first

Though it took time to complete requirement but at the end I felt I have done something different than what I usually do. I hope this article would throw some light about GIS.

Don’t forget to clap, share and comment if you like this post. Thanks.