Pregunta

I am working with RGeo (PostGIS) and Ruby on Rails, total newbie to both. I've got a model that contains a coordinates attribute (latitude & longitude). I am using a text input field when binding a form that that attribute, but I wanted to format the value into a human readable form. Currently it is being 'rendered' as the raw DB value, i.e., '0101000020E61000009468C9E3698C5EC065FED13769D64740'. I'd like to format that as something like '-122.193963, 47.675086' for the user. Conversely, if the user updates the value, I will need to parse it into a format that PostGIS can understand.

I've been searching and reading what I can find about binding data to forms in RoR, but can't find anything useful.

¿Fue útil?

Solución

You are seeing a hex-encoded representation of well-known binary (WKB). There are many geometry accessors to get coordinate information in different formats or representations. For example:

SELECT ST_AsText(geom) AS WKT, ST_Y(geom) AS latitude, ST_X(geom) AS longitude
FROM (
  SELECT '0101000020E61000009468C9E3698C5EC065FED13769D64740'::geometry AS geom
) AS f;

             wkt              | latitude  |  longitude
------------------------------+-----------+-------------
 POINT(-122.193963 47.675086) | 47.675086 | -122.193963

On the Ruby RGeo side, similar information can be obtained using WKBParser. For example, in tc_wkb_parser.rb you could do something similar:

parser_ = ::RGeo::WKRep::WKBParser.new
obj_ = parser_.parse('0101000020E61000009468C9E3698C5EC065FED13769D64740')
print [obj_.y, obj_.x]  # [-122.193963, 47.675086]
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top