Simple Mercator Factory Project/Unproject

Skip to first unread message

Myles Boone

Mar 16, 2016, 11:55:10 AM3/16/16
to RGeo-Users

I have a multipolygon which is stored using srid: 3857.

The multipolygon = #<RGeo::Geos::CAPIMultiPolygonImpl:0x2433710 "MULTIPOLYGON (((-13122719.938528342 6071784.245903644, -13122879.247851616 6071785.356007137, -13124676.601218017 6071787.824745262, -13124086.351881979 6071796.573031015, -13123486.595861437 6071791.8178063985, -13122886.850972837 6071787.062584423, -13122719.938528342 6071784.245903644)))">

These points (along with RGeo::Geos::CAPIMultiPolygonImpl:0x2433710) make me think that everything is going as planned, and that these coordinates are in the projected coordinate system.
However, when I go to unproject, it gives me an error:

> RGeo::Geographic.simple_mercator_factory.unproject(multipolygon)
RGeo::Error::InvalidGeometry: You can unproject only features that are in the projected coordinate space.

Can anybody point me in the right direction?

Myles Boone

Mar 21, 2016, 12:12:41 AM3/21/16
to RGeo-Users
Ok I figured this problem out...posting my solution here if anybody has this problem in the future.
Every time I called RGeo::Geographic.simple_mercator_factory, it would instantiate a new factory. This factory, while identical to the RGeo factory set for my model's column, is not the same instantiation of that factory. Because of this, it would give the factory error.

What I ended up doing was putting
FACTORY = RGeo::Geographic.simple_mercator_factory in my initializers folder as a constant for my application.
I then added

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

to the same file.

In this way, I can call FACTORY.unproject(modelRow.spatialColumn) and it would use the same factory to unproject as the one I assigned to the column.
Reply all
Reply to author
0 new messages