Storing Google Maps geo data with Rails / PostGIS / RGeo

1.2k views Asked by At

Having trouble implementing Daniel Azuma's solution for handling Google Maps geo data with RGeo / Rails.

SETUP

locations table:

create_table "locations", force: :cascade do |t|
  t.string   "name",        
  t.geometry "polygon", limit: {:srid=>3857, :type=>"polygon"}
end

Location class:

class Location < 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 "polygon" 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

initializers/rgeo.rb:

RGeo::ActiveRecord::SpatialFactoryStore.instance.tap do |config|
  # By default, use the GEOS implementation for spatial columns.
  config.default = RGeo::Geos.factory_generator
end

(Solution derived from here and here)

ISSUE

So here we go- I create a new Location object from a geojson shape containing coordinates from a Google Map:

manhattan_polygon_data = '{"type":"Polygon","coordinates":[[[-73.9784998975546,40.7367992185915],[-73.9808911983494,40.7334453322506],[-73.9899687850649,40.7350399153528],[-73.9894998975546,40.7395992185915]]]}'
location = Location.from_geojson(manhattan_polygon_data)

At this point, all our geo properties are working properly:

# Test geo properties:
location.polygon_projected
=> #<RGeo::Geos::CAPIPolygonImpl:0x3fc428c0c7dc "POLYGON ((-8235248.938246019 4973596.780357394, -8235515.136632829 4973104.057720993, -8236525.648963631 4973338.316345756, -8236473.452644745 4974008.150049943, -8235248.938246019 4973596.780357394))">
location.polygon_geographic
=> #<RGeo::Geographic::ProjectedPolygonImpl:0x3fc425d137a0 "POLYGON ((-73.9784998975546 40.73679921859151, -73.9808911983494 40.73344533225058, -73.9899687850649 40.73503991535281, -73.9894998975546 40.73959921859151, -73.9784998975546 40.73679921859151))">

But when we save and re-read from the database, something goes wrong:

# Save and retrieve from DB:
location.save!
location_from_db = Location.find(location.id)

# Test geo properties again:
location_from_db.polygon_projected
=> #<RGeo::Geos::CAPIPolygonImpl:0x3fc425cfb664 "POLYGON ((-8235248.938246019 4973596.780357394, -8235515.136632829 4973104.057720993, -8236525.648963631 4973338.316345756, -8236473.452644745 4974008.150049943, -8235248.938246019 4973596.780357394))">
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'

Given that the projected geometries of both objects are equivalent, I'm not sure why the latter unproject operation is failing.

1

There are 1 answers

0
Yarin On BEST ANSWER

Found my answer here: Simple Mercator Factory Project/Unproject - Google Groups

The problem was I was using a different factory instance for the properties than what was being used by the ActiveRecord adapter. The solution is to create a single instance of the simple mercator factory in the initializer.

Location class:

class Location < ActiveRecord::Base

  # To interact in projected coordinates,
  # just use the "polygon" 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

initializers/rgeo.rb:

# Create a single instance of 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

RGeo::ActiveRecord::SpatialFactoryStore.instance.tap do |config|
  config.default = FACTORY.projection_factory
end