Is there a good overall tutorial and initial guide?

144 views
Skip to first unread message

Denis Haskin

unread,
Nov 29, 2017, 12:59:11 PM11/29/17
to RGeo-Users
I don't think it's just me... based on reading questions here and on SO, there seems to be a lot of confusion about the various rgeo-related modules, what's current, what's not, how to do pretty basic things.

Is there perhaps a good overall tutorial and guide?  I'd love to put one together but I don't have the time (I freelance and I don't think I can get my client to pay for it :-( ).

Daniel's initial multi-part articles are awesome but (it seems) things have changed and so it can be confusing when example code is no longer valid.

I know this isn't a very specific question... suggestions?

Thanks!

Denis

brbak...@gmail.com

unread,
Jan 12, 2018, 2:14:57 PM1/12/18
to RGeo-Users
Any luck here? I've been digging and haven't found anything yet. 

--Brandon

Barrett Clark

unread,
Jan 13, 2018, 3:19:41 PM1/13/18
to RGeo-Users
I wrote a book a while back on data visualization in Rails, and I have an entire section (3 chapters) on geospatial data. I cover lots of fundamentals and get into some queries and also mapping (using Mapbox). My goal was to teach someone who had no experience with geospatial data how to get up and running and understand what they were doing. I use both rgeo as well as PostGIS directly.


If you do check it out I would love to hear how helpful it was for you.

~Barrett

Greg Scarich

unread,
Mar 3, 2018, 3:08:16 PM3/3/18
to RGeo-Users
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

Barrett Clark

unread,
Mar 4, 2018, 9:32:58 AM3/4/18
to RGeo-Users
Hey Greg. These are some good questions. There are a lot of layers to put together.

I'll start with the query. The query as you have it is actually a query with a subquery. You select from the results of the subquery, and subqueries have to be named. That way in the outer query you can select from a named entity, just like as if you were selecting from a table.

Another way to write the query, which I find easier to puzzle out, is to use Common Table Expression. That query looks like this:

WITH foo AS (
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
)
SELECT ST_Length(the_geog)
FROM foo;

Now you can see a little more clearly that you first generate the subquery results and then select from those results.

However, if you have the data in a spatial column in the database you should not need to enumerate the points. That's a lot of extra work on the application and then the database. ST_Length should be able to use that column directly in the query.

This gets to your first question about extent_json. I think that is related to GeoJSON. I'm not really clear where extent_json comes into play with what you're doing. In general I would try to store the data in the rawest possible format and then transform or decorate it as it goes through the stack for whatever presentation or manipulation is needed. So the column type in the table would be a line_string, and then in the controller (or possibly in the view) as you present the data to Leaflet you would then transform it to GeoJSON. It sounds like maybe your column is one of the JSON flavors?

I would probably lean towards having a JSON builder for the view that does this formatting, but without knowing your specific needs I don't know if that fits your app. If you were to go that direction, then the map loads and makes a call to a separate route to fetch the data asynchronously. That data route is what has the GeoJSON view. So a map would require 2 routes: one for the actual assets (page, JS, CSS) and a second for the data. You could also do it all in a single controller action and have the data available in an ivar as the page loads. I just prefer separate actions in this case for assets and data.

I'll be at RailsConf if you also happen to be going and want to chat more. I'll also send you a note separately off this thread, and if you want more help we can work together.

I hope this helps,

~Barrett
Reply all
Reply to author
Forward
0 new messages