GIS on Rails
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 geojsonformat 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,
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:
) = 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.
Original Source: https://medium.com/@learnitnow/gis-on-rails-23513c15de7c
- How to make open source success less of a crapshootCommentary: It's hard to know which open source projects will take off, but it's easy to guess which ones won't, and why.
- Front-end frameworks are about to get even cooler, says Google leadCommentary: Front-end frameworks keep getting better, but progressive delivery just might be the best thing to hit in 2020.
- How to prevent Zoom bombing: 5 simple tipsInternet trolls are crashing Zoom video conferences and flooding them with inappropriate content. Here are easy ways to protect your meetings from Zoom bombers.
We are happy to announce that Aaron Gette is on board as the Chief Technology Officer (CTO) and Partner with West A… https://t.co/wEbanPI5PKFollow
Nowadays is all about online business. But one needs a trusted platform to hire and buy your services that’s why we… https://t.co/t1RxaNz1RtFollow