<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule"
>

<channel>
	<title>Scratching Surfaces &#187; GPS</title>
	<atom:link href="http://www.surfaces.co.il/category/gps/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.surfaces.co.il</link>
	<description>Open Source - GIS - Thoughts</description>
	<lastBuildDate>Wed, 22 May 2013 08:26:38 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
<creativeCommons:license>http://creativecommons.org/licenses/by-nc-sa/3.0/</creativeCommons:license>
		<item>
		<title>Get your phone GPS data into a GIS format</title>
		<link>http://www.surfaces.co.il/get-your-phone-gps-data-into-a-gis-format/</link>
		<comments>http://www.surfaces.co.il/get-your-phone-gps-data-into-a-gis-format/#comments</comments>
		<pubDate>Thu, 16 May 2013 19:45:57 +0000</pubDate>
		<dc:creator>Micha Silver</dc:creator>
				<category><![CDATA[GIS]]></category>
		<category><![CDATA[GPS]]></category>
		<category><![CDATA[Spatialite]]></category>

		<guid isPermaLink="false">http://www.surfaces.co.il/?p=1428</guid>
		<description><![CDATA[Nearly every new phone or tablet these days comes GPS enabled. And you can choose any of a slew of apps to capture GPS waypoints and tracks. But how do you get these data into a GIS system? Several apps save the GPS data into an sqlite database, so using Spatialite to convert the locations [...]]]></description>
				<content:encoded><![CDATA[<p>Nearly every new phone or tablet these days comes GPS enabled. And you can choose any of a slew of apps to capture GPS waypoints and tracks. But how do you get these data into a GIS system? Several apps save the GPS data into an sqlite database, so using <a href="https://www.gaia-gis.it/fossil/libspatialite/index" title="Spatialite website">Spatialite</a> to convert the locations to spatial layers is a piece of cake.<br />
<span id="more-1428"></span><br />
I tried using, for example, GPSEssentials, an Android app with all the bells and whistles. I&#8217;m not endorsing any one location app but this one (like some others) stores all its data into sqlite files. So I plugged my phone into a USB socket and copied the &#8220;waypoints&#8221; file from the gpsessentials folder over to my computer. Then I renamed the file to add the popular *.sqlite extension, and opened it in Spatialite_gui. For our purposes there are three tables of interest: Waypoint, Track, and TrackElement. The Waypoint table contains, of course, the waypoints with Longitude and Latitude coordinates, along with altitude, description, etc. The Track table is a list of tracks, and the TrackElement table is a crumb trail of Longitude/latitude values for each location along each of the tracks.<br />
To transform these tables into true spatial layers, for use in GIS, you must first make the sqlite DB spatial. So:</p>
<p><code>SELECT InitSpatialMetaData();</code></p>
<p>Now we can use the Longitude/Latitude values in the waypoints table to create actual geometries. First call the AddGeometryColumn function:</p>
<p><code>SELECT AddGeometryColumn('Waypoint','geometry',4326,'POINT','XY');<br />
UPDATE Waypoints SET geometry = MakePoint(longitude, latitude, 4326);</code></p>
<p>and Waypoint becomes a point layer ready to be opened in QGIS, for example, or exported to a shapefile.<br />
But what about the tracks? Here we need an additional step. We make the Track <strong>and </strong>TrackElement tables spatial, then use the TrackElements to create LINESTRING features in Track geometry column:</p>
<p><code>SELECT AddGeometryColumn('Track','geometry',4326,'LINESTRING','XY');<br />
SELECT AddGeometryColumn('TrackElement','geometry',4326,'POINT','XY');<br />
UPDATE TrackElement SET geometry = MakePoint(longitude, latitude, 4326);</code></p>
<p>and now do the update on the Track table:</p>
<p><code>UPDATE Track SET geometry = (SELECT MakeLine(te.geometry)<br />
FROM TrackElement as te<br />
WHERE te.trackID = Track._id<br />
GROUP BY te.trackID)<br />
</code></p>
<p>and you&#8217;re done. The Track table is now a true line feature, and can be exported as a shapefile, etc. The column names above are as they appear in the sqlite tables created by this particular app, so you might have to replace some of the ingredients. But the recipe should be similar. So get out your spatial mixing bowl, and your phone becomes a handy GIS tool.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.surfaces.co.il/get-your-phone-gps-data-into-a-gis-format/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by-nc-sa/3.0/</creativeCommons:license>
	</item>
		<item>
		<title>Manipulating GPS tracks in Spatialite</title>
		<link>http://www.surfaces.co.il/manipulating-gps-tracks-in-spatialite/</link>
		<comments>http://www.surfaces.co.il/manipulating-gps-tracks-in-spatialite/#comments</comments>
		<pubDate>Tue, 17 Jul 2012 20:13:41 +0000</pubDate>
		<dc:creator>Micha Silver</dc:creator>
				<category><![CDATA[GIS]]></category>
		<category><![CDATA[GPS]]></category>
		<category><![CDATA[Mountain Biking]]></category>
		<category><![CDATA[Spatialite]]></category>

		<guid isPermaLink="false">http://www.surfaces.co.il/?p=1250</guid>
		<description><![CDATA[I returned from a short bike outing with my ride captured as a GPS track. Along the way, I also grabbed the rest stops as waypoints. Both of these were downloaded from the GPS as *.gpx files. So I have tracks.gpx and waypoints.gpx. Now I want to push these layers straight into Spatialite, and do some [...]]]></description>
				<content:encoded><![CDATA[<p>I returned from a short bike outing with my ride captured as a GPS track. Along the way, I also grabbed the rest stops as waypoints. Both of these were downloaded from the GPS as <a title="gpx" href="http://www.topografix.com/gpx.asp">*.gpx</a> files. So I have tracks.gpx and waypoints.gpx. Now I want to push these layers straight into Spatialite, and do some calculations.<br />
<span id="more-1250"></span></p>
<p>I pulled the data from my GPS using <a title="GPSBabel" href="http://www.gpsbabel.org/">gpsbabel</a> but you can also choose the Quantum GIS GPSTools plugin to download GPS data in GPX format. Now to move these layers straight into a Spatialite database I use the ogr2ogr utility. This is part of the GDAL toolset, the &#8220;Swiss Army Knife&#8221; of spatial data formats. It allows me to move vector data from one format to another, with several options to filter or alter the original data along the way.<br />
Here&#8217;s how I start:</p>
<p><code>$ ogr2ogr -f SQLite -nln ride -dsco "SPATIALITE=YES" -t_srs "EPSG:4326" bike_rides.sqlite bike_rides.gpx tracks</code></p>
<p>The first option sets the output format &#8216;<code>-f</code>&#8216; as SQLite. Next I use &#8216;<code>-nln</code>&#8216; to choose a &#8220;New Layer Name&#8221; for the table to be created. The &#8216;<code>-dsco</code>&#8216; options means &#8220;DataSet Create Option&#8221; and here we dictate Spatialite. The next flag &#8216;<code>-t_srs</code>&#8216; sets the spatial reference system for the data layer. While not strictly necessary (GPS data is always Lon/Lat WGS84) it&#8217;s a good policy to always set the SRS explicitly. Now pay attention to the order of the next parameters: the target database is first, and the source (gpx file) is second. This is backwards from many other command line tools. Normally we expect first the &#8220;from&#8221; followed by the &#8220;to&#8221;. For ogr2ogr it&#8217;s: &#8220;destination&#8221; first and &#8220;source&#8221; following. And finally at the end of the above command we choose to bring only GPS <code>tracks</code> into the database.</p>
<p>If our GPX file contains several tracks (perhaps from previous bike rides) we can filter those out with a simple -sql option such as:</p>
<p><code>$ ogr2ogr -f SQLite bike_rides.sqlite bike_rides.gpx -nln amazia_ride -dsco "SPATIALITE=YES" -sql "SELECT name, number FROM tracks WHERE name='Amazia-2' " tracks<br />
</code></p>
<p>Next, I need to get the stops along the way, which were captured as waypoints. Similar to above:</p>
<p><code>$ ogr2ogr -f SQLite bike_rides.sqlite waypoints.gpx -nln stops -append waypoints</code></p>
<p>Now, I don&#8217;t user the &#8216;-dsco&#8217; parameter since the database already exists. What&#8217;s more, I must specify &#8216;<code>-append</code>&#8216; to instruct ogr2ogr to <strong>add</strong> this layer as a new table in the existing sqlite database.</p>
<p>OK, how long was the whole ride? The built in Spatialite function GeodesicLength() calculates lengths of Lon/Lat features <strong>in meters</strong>, along the WGS84 ellipsoid.</p>
<p>spatialite&gt; SELECT GeodesicLength(r.Geometry) AS &#8220;Total Length&#8221; FROM ride AS r;<br />
Total Length<br />
&#8212;&#8212;&#8212;&#8212;&#8212;<br />
9696.2803124924</p>
<p>But I want to know how much each section of the ride was. In other words, I want to see the accumulated distance at each of the stops. We have in spatialite a function ST_Line_Locate_Point() which returns the distance along a line feature for points near the line. (It actually finds the nearest point on the line for each of the input points). So we do:</p>
<p><code>spatialite&gt; SELECT s.name AS "Stop name",<br />
...&gt; ST_Line_Locate_Point(r.Geometry, s.Geometry) AS "Stop Locations"<br />
...&gt; FROM ride AS r, stops AS s<br />
...&gt; GROUP BY s.name ORDER BY s.OGC_FID;<br />
Stop name   Stop Locations<br />
----------  --------------<br />
start       0.0<br />
rest 1      0.124783450768<br />
water stop  0.361703783084<br />
rest 2      0.562004941906<br />
rest 3      0.712163267248<br />
overlook    0.850818111308<br />
end         0.996608235043<br />
</code></p>
<p>The <code>OGC_FID</code> field is automatically created by ogr2ogr as a <em>primary key</em>. So doing &#8220;<code>ORDER BY</code>&#8221; on that field insures that the stop points are displayed in the correct order. But the numbers above are not distances, but rather the <strong>fraction</strong> of the total line length where each point falls. Now multiplying these fractions by the total length from the first query above, I get:</p>
<p><code>spatialite&gt; SELECT s.name AS "Stop name",<br />
...&gt; ST_Line_Locate_Point(r.Geometry, s.Geometry)*GeodesicLength(r.Geometry) AS "Accumlulated Distances"<br />
...&gt; FROM ride AS r, stops AS s<br />
...&gt; GROUP BY s.name ORDER BY s.OGC_FID;<br />
Stop name   Accumlulated Distances<br />
----------  ----------------------<br />
start       0.0<br />
rest 1      1209.93531700926<br />
water stop  3507.18127087581<br />
rest 2      5449.35745373272<br />
rest 3      6905.3346675029<br />
overlook    8249.77090219153<br />
end         9663.3928087156</code></p>
<p><strong>Nice</strong>. One last step, let&#8217;s put these distances permanently into the &#8220;stops&#8221; table.</p>
<p><code>spatialite&gt; ALTER TABLE stops ADD COLUMN accum_dist float;<br />
spatialite&gt; UPDATE stops SET accum_dist=(SELECT<br />
...&gt; ST_Line_Locate_Point(r.Geometry, s.Geometry)*GeodesicLength(r.Geometry)<br />
...&gt; FROM tracks AS r, stops AS s<br />
...&gt; WHERE s.OGC_FID=stops.OGC_FID);</code></p>
<p>We use a subquery to UPDATE the stops table. Take note of the WHERE condition: the OGC_FID values from the stops table <strong>outside of the subquery</strong> must match the values from the same column inside the query.</p>
<p><code>spatialite&gt; SELECT name AS "Stop name",accum_dist AS "Accumulated Distance" FROM stops;<br />
Stop name Accumulated Distance<br />
---------- --------------------<br />
start       0.0<br />
rest 1      1209.93531700926<br />
water stop  3507.18127087581<br />
rest 2      5449.35745373272<br />
rest 3      6905.3346675029<br />
overlook    8249.77090219153<br />
end         9663.3928087156<br />
</code><br />
There we are: accumulated distances for waypoints along a GPS track.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.surfaces.co.il/manipulating-gps-tracks-in-spatialite/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by-nc-sa/3.0/</creativeCommons:license>
	</item>
		<item>
		<title>Get your GPS locations into Spatialite</title>
		<link>http://www.surfaces.co.il/get-your-gps-locations-into-spatialite/</link>
		<comments>http://www.surfaces.co.il/get-your-gps-locations-into-spatialite/#comments</comments>
		<pubDate>Wed, 31 Aug 2011 19:37:51 +0000</pubDate>
		<dc:creator>Micha Silver</dc:creator>
				<category><![CDATA[GIS]]></category>
		<category><![CDATA[GPS]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[Spatialite]]></category>

		<guid isPermaLink="false">http://www.surfaces.co.il/?p=1053</guid>
		<description><![CDATA[Spatialite, built on the shoulders of the popular sqlite single-file database, offers a broad feature set of GIS analysis tools. Getting data into a spatialite database is a snap when you&#8217;re starting from a shapefile. But what about GPS data. Here&#8217;s a few tips on how to upload data from the standard &#8220;GPX&#8221; format into [...]]]></description>
				<content:encoded><![CDATA[<p>Spatialite, built on the shoulders of the popular sqlite single-file database, offers a broad feature set of GIS <a href="http://www.gaia-gis.it/spatialite-2.4.0-4/spatialite-cookbook/index.html">analysis tools</a>. Getting data into a spatialite database is a snap when you&#8217;re starting <a href="http://www.gaia-gis.it/spatialite-2.1/SpatiaLite-manual.html#t6a">from a shapefile</a>. But what about GPS data. Here&#8217;s a few tips on how to upload data from the standard &#8220;GPX&#8221; format into a spatialite DB.</p>
<p><span id="more-1053"></span>I&#8217;ll present three methods and point out the advantages and drawbacks of each. The first and second for those with &#8220;CLI phobia&#8221;, and the third &#8211; easier in my opinion &#8211; a command line option.</p>
<h4>The QGIS method</h4>
<p>We begin with a straightforward &#8220;copy &#8211; paste&#8221; process right in <a href="http://www.qgis.org/en/download/current-software.html">Quantum GIS</a>. You DO have QGIS installed already, I hope&#8230; First activate the GPS plugin in QGIS (this plugin should get installed automatically with QGIS), and click on the GPS icon to open the GPS Tools window. Go over to the &#8220;Load GPX File&#8221; tab, and browse to find your GPX file. Click &#8220;Open&#8221; and you&#8217;ll see the waypoints or tracks from your GPS on the QGIS canvas.</p>
<p>Now in the QGIS menu, choose &#8220;Layer&#8221;-&gt;&#8221;New&#8221;-&gt;&#8221;New Spatialite Layer&#8221;.  You&#8217;ll need to:</p>
<ul>
<li>Select the spatialite *.db file</li>
<li>Enter a name for the new layer</li>
<li>Be sure the SRID is set to 4326 (The Lon/Lat WGS84 based spatial reference system used by all GPS instruments)</li>
<li>Click to add an autoincrementing primary key</li>
<li>Add any other attribute columns you&#8217;ll need</li>
</ul>
<p>Then click OK to add the new layer into into the Spatialite DB, and into QGIS. Now comes the copy/paste dance: choose the GPX layer in the Table of Contents, and using the Select tool, select all of the features (so that they all appear yellow) and click the &#8220;Copy Features&#8221; button on the edit toolbar. Now Choose from the TOC the new empty spatialite layer, and click the &#8220;Toggle Editing&#8221; button to begin editing this new layer. And finally click the &#8220;Paste Features&#8221; button, again from the editing toolbar, to paste all the selected GPX features into the new spatialite table. Simple and elegant, but&#8230;</p>
<p>&#8230;if you open up the new layer&#8217;s attribute table you&#8217;ll see it&#8217;s emtpy.  &#8221;So what&#8221;, you might ask. Well every GPS captures, along with each point location, at least a waypoint name, and elevation. In the original GPX file these appear as XML elements. If this additional information is not important then the above method should be fine. But if you want to<em> keep the waypoint numbers and elevation</em>, you&#8217;ll need a better method.</p>
<h4>The intermediate CSV method</h4>
<p>For our second try, we need two GUI tools; the <a href="http://www.gaia-gis.it/spatialite-2.4.0-4/binaries.html">spatialite-gui</a> binary and <a href="http://www.gpsbabel.org/download.html">GPSbabel GUI</a>. GPSBabel is traditionally a command line program, but there&#8217;s a &#8220;Front End&#8221; for Windows users. This program reads and writes practically every GPS file format in use on the planet. The steps to importing a GPX file<strong> with attributes</strong> are as follows. Using GPSBabel we convert the original GPX to the &#8220;Universal CSV&#8221; format. Then in Spatialite we create a VirtualTable based on this file. And then, using an SQL insert within spatialite we transfer all the GPS locations to a permanent table, and finally create the requisite Geometry column for this new table. Here are the details:</p>
<p>The GPSBabel command to convert to Universal CSV format reads (assuming the original GPX file is waypoints.gpx):<br />
<code><br />
gpsbabel -w -i gpx -f waypoints.gpx  -o unicsv -F waypoints.csv<br />
</code><br />
Alternatively, Using the GPSBabel GUI should be intuitive, choose the input format as GPX (or whatever format you downloaded from your  GPS), and output format Universal CSV. Select the files and check waypoints or tracks, whichever is appropriate. Then run the conversion.</p>
<p>Now fire up spatialite-gui. On the button bar, locate and click the button &#8220;Virtual CSV/TXT&#8221;. Browse to your waypoints.csv from the previous step and click Open to select the file. In the next &#8220;Creating VIrtual CSV/TXT&#8221; window change the table name to something like  &#8221;waypoints_tmp&#8221;, be sure to <span style="text-decoration: underline;">select &#8220;Comma&#8221; as the Column Separator.</span> and click OK. You now have access to the CSV file from within spatialite. You can examine the table structure by right-clicking on it&#8217;s name and choosing &#8220;Show Columns&#8221;. The data is viewable by right-click and &#8220;Query table&#8221;.</p>
<p>It&#8217;s now time to create a new, permanent table for the waypoints. In the query window type the following:<br />
<code><br />
CREATE TABLE waypoints (</code></p>
<p style="padding-left: 30px;">long DOUBLE,<br />
lat DOUBLE,<br />
elev DOUBLE,<br />
wpt TEXT );</p>
<p>&nbsp;</p>
<p>At this point we want to insert all the rows from the GPX table &#8211; waypoints_tmp &#8211; into the new waypoints table. Here&#8217;s the required SQL.<br />
<code><br />
INSERT INTO waypoints (long, lat,elev, wpt)<br />
SELECT Longitude, Latitude, Altitude, Name FROM waypoints_tmp;<br />
</code><br />
That should leave us with a fully populated, permanent waypoints table. At this point a word of explanation is in order. Why was it necessary to leap-frog over that Virtual CSV/TXT? Why not just import the CSV table directly into a permanent Spatialite table? The reason lies with how numeric columns are recognized by the CSV/TXT importer. If numbers appear as negative values, i.e. preceded by a minus sign (latitudes south of the equator, longitudes west of Greenwich, or below sea level elevations like at the Dead Sea) then these numbers get <span style="text-decoration: underline;">imported as text</span>, and they are then useless for creating the Geometry column. When we hand crafted our permanent waypoints table as above, we set the long,lat,elev types to DOUBLE. Then, when inserting the values from the virtual table, the &#8220;text&#8221; with its minus sign got <strong>coerced</strong> into true numeric values.</p>
<p>We have yet one more step to complete. So far this new table is <strong>NOT</strong> a spatial table. The Longitude and Latitude values appear as attribute columns but we haven&#8217;t yet <a href="http://www.gaia-gis.it/spatialite-2.4.0-4/spatialite-cookbook/html/new-geom.html">added the necessary Geometry column</a>. Two more SQL statements complete the job:<br />
<code><br />
SELECT AddGeometryColumn('waypoints','Geometry',4326,'POINT',2);<br />
UPDATE waypoints SET Geometry=MakePoints(long.lat,4326);<br />
</code><br />
The first Spatialite function adds the additional Geometry column, the metadata and constraints, and the second command populates that column based on the longitude/latitude values. Now we have a spatial table with attribute values, imported from a GPS file.</p>
<p>Yet one additional method is available. For those tired of reading such a long post, don&#8217;t worry, this final one is short.</p>
<h4>The OGR command method</h4>
<p>The ubiquitous GDAL library includes a sometimes overlooked, but powerful tool for handling vector format conversions. It&#8217;s called <a href="http://www.gdal.org/ogr2ogr.html">ogr2ogr</a>. Since OGR can read and write both GPX formatted files and Spatialite database layers, we can concoct a one liner ogr2ogr command to push GPS locations from a GPX file directly into a Spatialite database layer, and <strong>including their attributes</strong>. SO, without further ado here it is:</p>
<p><code> ogr2ogr -append -f "SQLite" \</code></p>
<p style="padding-left: 30px;">-dsco SPATIALITE=yes -dsco INIT_WITH_EPSG=yes -t_srs epsg:4326 \<br />
spatial_data.db waypoints.gpx \<br />
-nln waypoints</p>
<p>Just a few words of explanation: -append allows you to add a table to an existing Spatialite database. Based on <a href="http://groups.google.com/group/spatialite-users/browse_thread/thread/e0b2bedc24f10947/df0acb9bd5968137?hl=en&amp;lnk=gst&amp;q=GDAL%2Fogr#df0acb9bd5968137">this</a> maillist thread, it seems that adding the -dsco (dataset creation options) and the target spatial reference system is a good idea. The -nln parameter is the new layer name &#8211; that is the new Spatialite table. Pay attention to the unusual order of the files on the command line: First is the destination database, and after the source (in this case) GPX file.</p>
<p>That&#8217;s it, the whole procedure distilled into one command. Now who still thinks the CLI is old fashioned or clunky?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.surfaces.co.il/get-your-gps-locations-into-spatialite/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by-nc-sa/3.0/</creativeCommons:license>
	</item>
	</channel>
</rss>
