-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
145 changed files
with
6,850 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<chapter id ="GPX"> | ||
<title>Managing GPX Data</title> | ||
|
||
<sect1 id ="loading_gpx"> | ||
<title>Loading GPX Data</title> | ||
|
||
<para> | ||
GPX, or GPS Exchange Format, is an XML data format for GPS data. Location data (and optionally elevation, time, and other information) is stored in tags and can be interchanged between GPS devices and software. Conceptually, a GPX file contains tracks, which are a record of where a moving object has been, and routes, which are suggestions about where it might go in the future. Furthermore, both tracks and routes and composed by points. The following is a truncated (for brevity) example GPX file. | ||
<programlisting> | ||
<?xml version='1.0' encoding='UTF-8' standalone='yes' ?> | ||
<gpx version="1.1" | ||
xmlns="http://www.topografix.com/GPX/1/1" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://www.topografix.com/GPX/1/1 | ||
http://www.topografix.com/GPX/1/1/gpx.xsd" | ||
creator="Example creator"> | ||
<metadata> | ||
<name>Dec 14, 2014 4:32:04 PM</name> | ||
<author>Example creator</author> | ||
<link href="https://..." /> | ||
<time>2014-12-14T14:32:04.650Z</time> | ||
</metadata> | ||
<trk> | ||
<name>Dec 14, 2014 4:32:04 PM</name> | ||
<trkseg> | ||
<trkpt lat="30.16398" lon="31.467701"> | ||
<ele>76</ele> | ||
<time>2014-12-14T14:32:10.339Z</time> | ||
</trkpt> | ||
<trkpt lat="30.16394" lon="31.467333"> | ||
<ele>73</ele> | ||
<time>2014-12-14T14:32:16.00Z</time> | ||
</trkpt> | ||
<trkpt lat="30.16408" lon="31.467218"> | ||
<ele>74</ele> | ||
<time>2014-12-14T14:32:19.00Z</time> | ||
</trkpt> | ||
[...] | ||
</trkseg> | ||
<trkseg> | ||
[...] | ||
</trkseg> | ||
[...] | ||
</trk> | ||
<trk> | ||
[...] | ||
</trk> | ||
[...] | ||
<gpx> | ||
</programlisting> | ||
</para> | ||
|
||
<para> | ||
The following Python program called <varname>gpx_to_csv.py</varname> uses <varname>expat</varname>, a stream-oriented XML parser library, to convert the above GPX file in CSV format. | ||
<programlisting language="python"> | ||
import sys | ||
import xml.parsers.expat | ||
|
||
stack = [] | ||
def start_element(name, attrs): | ||
stack.append(name) | ||
if name == 'gpx' : | ||
print("lon,lat,time") | ||
if name == 'trkpt' : | ||
print("{},{},".format(attrs['lon'], attrs['lat']), end="") | ||
|
||
def end_element(name): | ||
stack.pop() | ||
|
||
def char_data(data): | ||
if stack[-1] == "time" and stack[-2] == "trkpt" : | ||
print(data) | ||
|
||
p = xml.parsers.expat.ParserCreate() | ||
|
||
p.StartElementHandler = start_element | ||
p.EndElementHandler = end_element | ||
p.CharacterDataHandler = char_data | ||
|
||
p.ParseFile(sys.stdin.buffer) | ||
</programlisting> | ||
</para> | ||
|
||
<para> | ||
This Python program can be executed as follows. | ||
<programlisting language="bash"> | ||
python3 gpx_to_csv.py < example.gpx > example.csv | ||
</programlisting> | ||
The resulting CSV file is given next. | ||
<programlisting> | ||
lon,lat,time | ||
31.46032,30.037502,2015-02-09T08:10:16.00Z | ||
31.460901,30.039026,2015-02-09T08:10:31.00Z | ||
31.461981,30.039816,2015-02-09T08:10:57.00Z | ||
31.461996,30.039801,2015-02-09T08:10:58.00Z | ||
... | ||
</programlisting> | ||
The above CSV file can be loaded into MobilityDB as follows. If the command <varname>COPY</varname> throws a permission error, you can instead use the <varname>\copy</varname> command of <varname>psql</varname> to import the CSV file. | ||
<programlisting language="sql"> | ||
DROP TABLE IF EXISTS trips_input; | ||
CREATE TABLE trips_input ( | ||
date date, | ||
lon float, | ||
lat float, | ||
time timestamptz | ||
); | ||
|
||
COPY trips_input(lon, lat, time) FROM | ||
'/home/gpx_data/example.csv' DELIMITER ',' CSV HEADER; | ||
|
||
UPDATE trips_input | ||
SET date = date(time); | ||
|
||
DROP TABLE IF EXISTS trips_mdb; | ||
CREATE TABLE trips_mdb ( | ||
date date NOT NULL, | ||
trip tgeompoint, | ||
trajectory geometry, | ||
PRIMARY KEY (date) | ||
); | ||
|
||
INSERT INTO trips_mdb(date, trip) | ||
SELECT date, tgeompoint_seq(array_agg(tgeompoint_inst( | ||
ST_SetSRID(ST_Point(lon, lat), 4326), time) ORDER BY time)) | ||
FROM trips_input | ||
GROUP BY date; | ||
|
||
UPDATE trips_mdb | ||
SET trajectory = trajectory(trip); | ||
</programlisting> | ||
</para> | ||
|
||
</sect1> | ||
</chapter> | ||
|
Oops, something went wrong.