RGeo / Rails で Google マップの地理データを処理するDaniel Azuma のソリューションの実装に問題があります。
設定
ロケーション テーブル:
create_table "locations", force: :cascade do |t|
t.string "name",
t.geometry "polygon", limit: {:srid=>3857, :type=>"polygon"}
end
ロケーション クラス:
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
初期化子/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
問題
それでは、Google マップの座標を含む geojson シェイプから新しい Location オブジェクトを作成します。
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)
この時点で、すべての地理プロパティが適切に機能しています。
# 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))">
しかし、データベースを保存して再読み込みすると、何か問題が発生します。
# 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'
両方のオブジェクトの投影されたジオメトリが同等であることを考えると、後者のunproject
操作が失敗する理由がわかりません。