Barrett
I'm a relative newbie to Rails and it's too easy to get lost. For example, I'm trying to calculate the length of a line_string stored as a json. Parsing the json is confusing added to the layer of PostGIS. Plus SQL. Plus Leaflet. Many have made it through all these hoops, but some of us are slower.
I've stored this as an example in extent_json:
{"type":"LineString","coordinates":[[-118.25573325622828,34.045980983930036],[-118.2428586529568,34.03517030952861],[-118.20938468445094,34.049394594819795],[-118.21710944641383,34.03517030952861]]}
(Why not just the coordinates, I can't remember, but finally got it working this way. I use extent_json to draw the segment on a map.)
Now I want to calculate the length of the segment. From SO I worked this out in psql:
SELECT ST_Length(the_geog)
FROM (SELECT ST_GeographyFromText(
'SRID=4326;LINESTRING(-118.30919122632704 34.09488749149773,-118.30684360562236 34.09499314844378,-118.30362838596157 34.09511993660491,-118.30048971914982 34.095204461940156,-118.29740208757083 34.09535238107372,-118.2950034316334 34.09541577490897,-118.29173717673993 34.095563693673256,-118.28872609800996 34.095627087350245,-118.28671020631786 34.09573274337304,-118.28408189183325 34.095838399263954)') As the_geog)
As foo;
And it works :
st_length
-----------------
2319.6973311388
But why do I need the `as foo;` for the result to show?
Now to extract the line string from the data base in rails console:
Street.find(186).extent_json which give me the json I showed above.
Now I need to combine these. And then get it into my app. I'm lost in sql, rails. I'd rather stay in Rails, but not required. Currently I'm storing the length in the database. I cobbled togethr L.Control.Draw and Turf.js to calculate and store the length. But I already created some of the segments before I knew how to store the length, so I'd like just to calculate the length dynamically.
I think this shows the challenge of writing books and tutorials.
Thanks for listening. Writing this out has helped some. I have your book and it helps. I went through it a year or so ago. Maybe I need to do it again.
Not the OP.
Greg