Site menu:

Cyclocross For Roadies: How and Why to Get Started - Buy Now!

Site search

Categories

Create Midpoints Along Lines with OGR

Today I found myself trying to create a ‘midpoint’ on a large shapefile line layer. QGIS doesn’t have a tool to do this, and because the layer was large, I really didn’t want to mess around converting file formats to stick it into a database. However, similar to my last post about Creating Points On A Surface Using OGR, I found a quick trick (via this question on gis.stackexchange.com) that also uses the awesome ‘-dialect’ switch in OGR. It works like this:

ogr2ogr OutputPoints.shp InputLines.shp -dialect sqlite -sql “SELECT AsText(Line_Interpolate_Point(InputLines.geometry, 0.5)),OTHER,FIELDS,CAN,GO,HERE from InputLines”

Simply replace “OutputPoints” with the desired output filename, and “InputLines” with the name of the line file you wish to create the midpoints from. You can change the “0.5″ to any number you wish – it represents how far along the line the point will fall, so if you want a midpoint, leave it at 0.5, but if you want the point 1/4 of the way along, change 0.5 to 0.25.  If don’t specify fields where I have “OTHER,FIELDS,CAN,GO,HERE” you will get only a point file with no attributes. If you include other field names from InputLines.shp (comma separated) after the closing bracket, OGR will also return those attributes from InputLines.shp.

Here’s a quick visual:

Lines With Midpoints

Lines With Midpoints

Bonus assignment: rather than making an intermediate point layer, let’s just write the lat/lon of the midpoint directly to the attribute table:

ogr2ogr OutputLines.shp InputLines.shp -dialect sqlite -sql “SELECT *, X(Line_Interpolate_Point(InputLines.geometry, 0.5)) as X, Y(Line_Interpolate_Point(InputLines.geometry, 0.5)) as Y from InputLines”

 

Search and Filter in Golden Cheetah v3

I put together a quick screencast showing the Search and Filter capabilities of Golden Cheetah v3. Check it out!

Golden Cheetah will let you do free text search, which allows you to enter any text and find it in your Activities. You can also perform Filters on columns. This allows you to build complex queries based on any of the fields and metrics that Golden Cheetah offers. You can use standard AND/OR query syntax, and also save searches or filters as “Favorites” to quickly open them at any time.

Creating Points On a Surface using OGR

If you’ve ever had to create a point on a polygon surface, you fairly quickly realize that there’s a lack of easy tools that will perform this task. Almost every desktop GIS package will create a ‘centroid,’ but that doesn’t work when you need to find a point that’s guaranteed to fall on the surface of your polygon. Below is an example of two polygons that will work fine with a ‘centroid’ tool, and two that will cause the resulting point to fall off the surface of the polygon:

Example Polygons

Example Polygons

Running the QGIS “Polygon Centroids” tool on these polygons gives the following results:

Centroids - Notice how the top right and bottom results are incorrect.

Centroids – Notice how the top right and bottom results are incorrect.

Of course, many of the more advanced GIS packages (PostGIS, SpatiaLite, etc.) have a tool for this exact purpose. However, the problem is that often those tools are overkill, or require you to convert your data format in order to use their nifty spatial functions. What a pain!

However, there’s an easy solution, and once again, it involves the heavy-hitter OGR , which never ceases to amaze me with its functionality. One simple command will create points forced onto the polygon surface, using the smarts of SpatiaLite, but not requiring you to convert formats or load anything into a database. Here’s all you need to run, from the command line (I suggest installing OGR via OSGeo4W if you’re on Windows):

ogr2ogr OutputPoints.shp InputPolygons.shp -dialect sqlite -sql “SELECT ST_PointOnSurface(geometry) from InputPolygons”

That’s it! The result is a point for each polygon, with it ‘forced’ onto the surface.

The resulting points, forced onto the polygon surface.

The resulting points, forced onto the polygon surface.

Cool eh? Observant readers will notice that all the OGR command is doing is actually changing it’s ‘dialect’ to sqlite, which allows you to pass in any SpatiaLite query. This of course opens up many of the other awesome tools available to you, right from the command line, with any file type. Perhaps I’ll post some other neat examples soon; stay tuned!

Moving Polygons in QGIS 1.9 While Respecting Snapping Settings

Have you ever tried using the “Move Feature(s)” tool in QGIS, only to get frustrated that you cannot use it and also snap to existing features at the same time? I sure have. With a bit of research, I found a workaround. It’s simple really; select all nodes with the Node Tool, and then drag the feature. It will respect your snapping settings! Check out the video below for a demonstration!

The 40km Project Part XVI

Well, I am back to report on the somewhat dormant 40km Project. While planning out my 2013 ride schedule, I checked out the Almonte Bicycle Club calendar to see when the monthly 40km Calabogie TTs were. I noticed, to my dismay, that there was only one TT in Calabogie, and the rest were on a new course in Franktown. Uh-oh! Only one more chance to meet my goal of going sub-hour on the Calabogie course? Damn! My final shot at the hour mark was Sunday, June 2.

For race prep, I did an easy (very easy!) spin on Friday night due to the ridiculously hot and humid weather. Saturday turned out to be kind of rainy and dreary, so instead of going for a ride, I decided to just rest up completely, taking the entire day off. Krystelle and I ate well (a pasta with bacon/mushroom sauce – fantastic!) and went to bed at an early hour so I could be well rested.

Sunday dawned…wet. Overcast and rainy looked like the outlook for race day. I had a good breakfast of oatmeal, banana and yogurt, and we loaded up the car and headed north to Calabogie.Pre-race pump-up tunes included this one:

“I set my body on fire so I can be free” – Seems fitting in a TT context!

The rain was coming down intermittently, but it looked like it may clear up. When we arrived, the rain had mostly stopped, leaving it hot and humid. However, because I wasn’t sure if the rain was going to hold off for the entire race, and because the road was wet regardless, I made the call to not run the PowerTap rear wheel. I figured it wasn’t worth frying it and wasting a lot of money all for one race. This was a tough decision, and very disappointing, because I had had great success last time I used the PowerTap in a TT, and was hoping to replicate that on the Calabogie course. Since I wasn’t going to be using power, I thought I would try something new; I decided to go ‘blind’ and ignore the computer as much as possible. I set it to display heart rate (no speed, time or distance) and attempted to ignore it as much as possible.

I got registered, and changed. I was given #20 (of 13 riders, starting at 11, so I would be starting 4th from the end.) This gave me 20 minutes to get warmed up before go time. I put the bike on the trainer, and started to spin. Oh man… it was humid! I felt like I was having trouble breathing, and was already sweating a lot! With a few minutes to go, I took the bike off the trainer, and rolled up to the start line.   I started out fairly easy, hoping to pace myself a bit more intelligently than I have in the past; a negative split was the goal. I passed my minute and two-minute men before the 8km mark, and before I knew it was at the dreaded 13 km hill. It passed fairly ‘easily’ and I was feeling really good! I had been consciously standing more on the climbs, and it helped to keep me moving when in the past I have faltered a bit.

I was in the zone! I got to the turnaround and felt like I was only a quarter of the way through, not over half! Feeling good, as at this point I had passed all but two riders who had started in front of me, and had one of them in my sights at the turn. I went into the turn a bit ‘hot’ and due to the wet road actually skidded out the rear wheel a bit on the turn. No harm done, but shows that I still need some work on the turnaround!  I hammered back up to speed, passing the second remaining rider shortly after the turn. I had a gel shoved in the leg of my skinsuit, and ate it within a few minutes of the turn. I noted that I was feeling ‘comfortable’ on the bike, not shifting positions a lot, and not labouring to stay in my tuck. It’s the first time I’ve ever really felt this feeling on the TT bike!

I arrived to the point that I always think is 10km from the end, and was still feeling good. No uncomfortableness on the bike, no extreme pain, no wasting of ‘matches.’ I had never felt so good on the bike! However, since I was running blind, I had no idea how well I was doing. In my mind, I was convinced I was going to be in around the 63 minute mark, because I wasn’t killing myself, and didn’t ‘feel’ fast.

At this point, a car passed me, followed closely by Rod, who went on to win with a great time of 58:32 (congrats Rod!) – as soon as he passed me, we crested a small hill and I realized I was less than 1km from the finish! Oh no! I had a lot of energy left, and didn’t think I was so close! I opened it up, hoping to burn as many matches as I could in the last few seconds. I crossed the line, yelled my number, and hit ‘stop’ on my Garmin. Flipping screens to one that shows time, I saw that it read 1:00. Wait? What?!? I was that fast? Wait a minute.. what if…I started playing scenarios in my head—perhaps it was 59:59, and I just took that extra second to hit stop, or.. or or… there was a glimmer of hope that I had made it. Alas, I realized it was all wishful thinking, and that I had been SO CLOSE yet again, and missed my chance, while feeling GREAT, on the last time race on this course. I may have cursed loudly when the realization hit me. Disappointment.

When final results were announced, I was third (by one second), at 60:45. I didn’t even feel super tired. I have done TTs where I was completely WRECKED, and been 2 minutes slower. I am hoping that means that winter CrossFit and smart training have paid off. Or else the stars aligned and I missed my best opportunity ever. Either way, I am torn—it was a good time, and I am happy with it, BUT… I did not make my goal. What a sport; extreme highs and extreme lows within an hour!

Unfortunately, the lack of PowerTap, and the fact that I don’t yet have a speed/cadence sensor on the TT bike mean I have basically no data. No power, no cadence. Just speed from GPS (which is always suspect) and heart rate. I will say that I went in with a stress balance of -0.9, which I’ve never really had the data to back up before. This is interesting, and deserves some more attention! I’ll certainly be paying more attention to the Stress Balance figures before key events in the future!

Bookshelf 2.0 developed by revood.com