Storing Google Maps geo data with Rails / PostGIS / RGeo
<p>Having trouble implementing <a href="http://daniel-azuma.com/articles/georails/part-7" rel="nofollow noreferrer">Daniel Azuma's solution</a> for handling Google Maps geo data with RGeo / Rails.</p>
<h1>SETUP</h1>
<p><strong>locations table:</strong></p>
<pre><code>create_table &quot;locations&quot;, force: :cascade do |t|
  t.string   &quot;name&quot;,        
  t.geometry &quot;polygon&quot;, limit: {:srid=&gt;3857, :type=&gt;&quot;polygon&quot;}
end
</code></pre>
<p><strong>Location class:</strong></p>
<pre><code>class Location &lt; ActiveRecord::Base

  # Create a simple mercator factory. This factory itself is
  # geographic (latitude-longitude) but it also contains a
  # companion projection factory that uses EPSG 3857.
  FACTORY = RGeo::Geographic.simple_mercator_factory

  # To interact in projected coordinates,
  # just use the &quot;polygon&quot; attributes directly.
  def polygon_projected
    self.polygon
  end
  def polygon_projected=(value)
    self.polygon = value
  end

  # To use geographic (lat/lon) coordinates,
  # convert them using the wrapper factory.
  def polygon_geographic
    FACTORY.unproject(self.polygon)
  end
  def polygon_geographic=(value)
    self.polygon = FACTORY.project(value)
  end

  def self.from_geojson(geojson)

    location = Location.new
    decoded_polygon = RGeo::GeoJSON.decode(geojson, json_parser: :json, geo_factory: RGeo::Geographic.simple_mercator_factory)
    location.polygon_geographic = decoded_polygon
    return location

  end

end
</code></pre>
<p><strong>initializers/rgeo.rb:</strong></p>
<pre><code>RGeo::ActiveRecord::SpatialFactoryStore.instance.tap do |config|
  # By default, use the GEOS implementation for spatial columns.
  config.default = RGeo::Geos.factory_generator
end
</code></pre>
<p>(Solution derived from <a href="http://daniel-azuma.com/articles/georails/part-7" rel="nofollow noreferrer">here</a> and <a href="https://github.com/rgeo/activerecord-postgis-adapter/blob/master/README.md#creating-a-spatial-rails-app" rel="nofollow noreferrer">here</a>)</p>
<h1>ISSUE</h1>
<p>So here we go- I create a new Location object from a geojson shape containing coordinates from a Google Map:</p>
<pre><code>manhattan_polygon_data = '{&quot;type&quot;:&quot;Polygon&quot;,&quot;coordinates&quot;:[[[-73.9784998975546,40.7367992185915],[-73.9808911983494,40.7334453322506],[-73.9899687850649,40.7350399153528],[-73.9894998975546,40.7395992185915]]]}'
location = Location.from_geojson(manhattan_polygon_data)
</code></pre>
<p>At this point, all our geo properties are working properly:</p>
<pre><code># Test geo properties:
location.polygon_projected
=&gt; #&lt;RGeo::Geos::CAPIPolygonImpl:0x3fc428c0c7dc &quot;POLYGON ((-8235248.938246019 4973596.780357394, -8235515.136632829 4973104.057720993, -8236525.648963631 4973338.316345756, -8236473.452644745 4974008.150049943, -8235248.938246019 4973596.780357394))&quot;&gt;
location.polygon_geographic
=&gt; #&lt;RGeo::Geographic::ProjectedPolygonImpl:0x3fc425d137a0 &quot;POLYGON ((-73.9784998975546 40.73679921859151, -73.9808911983494 40.73344533225058, -73.9899687850649 40.73503991535281, -73.9894998975546 40.73959921859151, -73.9784998975546 40.73679921859151))&quot;&gt;
</code></pre>
<p>But when we save and re-read from the database, something goes wrong:</p>
<pre><code># Save and retrieve from DB:
location.save!
location_from_db = Location.find(location.id)

# Test geo properties again:
location_from_db.polygon_projected
=&gt; #&lt;RGeo::Geos::CAPIPolygonImpl:0x3fc425cfb664 &quot;POLYGON ((-8235248.938246019 4973596.780357394, -8235515.136632829 4973104.057720993, -8236525.648963631 4973338.316345756, -8236473.452644745 4974008.150049943, -8235248.938246019 4973596.780357394))&quot;&gt;
location_from_db.polygon_geographic
RGeo::Error::InvalidGeometry: You can unproject only features that are in the projected coordinate space.
    from /usr/local/lib/ruby/gems/2.3.0/gems/rgeo-0.5.3/lib/rgeo/geographic/factory.rb:270:in `unproject'
</code></pre>
<p>Given that the projected geometries of both objects are equivalent, I'm not sure why the latter <code>unproject</code> operation is failing.</p>