phpGPX
用PHP编写的简单库,用于阅读和创建GPX文件。可以使用Jekyll生成的GitHub页面获得文档。
欢迎贡献和反馈!请检查托多的问题。我会很高兴的每个功能或拉请求。
存储库分支:
-
master:最新稳定版本 -
develop:在2.x上工作
特征
- 全面支持官方规范。
- 统计计算。
- 扩展。
- JSON&XML和PHP数组输出。
支持的扩展
- Garmin TrackPointExtension:http://www.garmin.com/xmlschemas/trackpointextension/v1
统计计算
- (平滑)距离(M)
- 平均速度(m/s)
- 平均速度(S/km)
- 最小 /最大高度(M)
- 最小 /最大坐标([[LAT,LNG])
- (平滑)高程增益 /损失(M)
- 开始 /结束(DateTime对象)
- 开始 /结束坐标([[LAT,LNG])
- 持续时间(秒)
安装
您可以轻松地使用Composer安装phpGPX库。目前还没有稳定的发布,因此请使用释放候选者。
composer require sibyx/ phpGPX :1.3.0
例子
打开GPX文件并加载基本统计数据
phpGPX ;
$gpx = new phpGPX ();
$file = $gpx->load(\’example.gpx\’);
foreach ($file->tracks as $track)
{
// Statistics for whole track
$track->stats->toArray();
foreach ($track->segments as $segment)
{
// Statistics for segment of track
$segment->stats->toArray();
}
}\”>
<?php use phpGPX \\ phpGPX ; $ gpx = new phpGPX (); $ file = $ gpx -> load ( \' example.gpx \' ); foreach ( $ file -> tracks as $ track ) { // Statistics for whole track $ track -> stats -> toArray (); foreach ( $ track -> segments as $ segment ) { // Statistics for segment of track $ segment -> stats -> toArray (); } }
写入文件
phpGPX ;
$gpx = new phpGPX ();
$file = $gpx->load(\’example.gpx\’);
// XML
$file->save(\’output.gpx\’, phpGPX ::XML_FORMAT);
//JSON
$file->save(\’output.json\’, phpGPX ::JSON_FORMAT);\”>
<?php use phpGPX \\ phpGPX ; $ gpx = new phpGPX (); $ file = $ gpx -> load ( \' example.gpx \' ); // XML $ file -> save ( \' output.gpx \' , phpGPX :: XML_FORMAT ); //JSON $ file -> save ( \' output.json \' , phpGPX :: JSON_FORMAT );
从头开始创建文件
phpGPX\\Models\\GpxFile;
use phpGPX \\Models\\Link;
use phpGPX \\Models\\Metadata;
use phpGPX \\Models\\Point;
use phpGPX \\Models\\Segment;
use phpGPX \\Models\\Track;
require_once \’/vendor/autoload.php\’;
$sample_data = [
[
\’longitude\’ => 9.860624216140083,
\’latitude\’ => 54.9328621088893,
\’elevation\’ => 0,
\’time\’ => new \\DateTime(\”+ 1 MINUTE\”)
],
[
\’latitude\’ => 54.83293237320851,
\’longitude\’ => 9.76092208681491,
\’elevation\’ => 10.0,
\’time\’ => new \\DateTime(\”+ 2 MINUTE\”)
],
[
\’latitude\’ => 54.73327743521187,
\’longitude\’ => 9.66187816543752,
\’elevation\’ => 42.42,
\’time\’ => new \\DateTime(\”+ 3 MINUTE\”)
],
[
\’latitude\’ => 54.63342326167919,
\’longitude\’ => 9.562439849679859,
\’elevation\’ => 12,
\’time\’ => new \\DateTime(\”+ 4 MINUTE\”)
]
];
// Creating sample link object for metadata
$link = new Link();
$link->href = \”https://sibyx.*g*it*hub.io/phpGPX\”;
$link->text = \’ phpGPX Docs\’;
// GpxFile contains data and handles serialization of objects
$gpx_file = new GpxFile();
// Creating sample Metadata object
$gpx_file->metadata = new Metadata();
// Time attribute is always \\DateTime object!
$gpx_file->metadata->time = new \\DateTime();
// Description of GPX file
$gpx_file->metadata->description = \”My pretty awesome GPX file, created using phpGPX library!\”;
// Adding link created before to links array of metadata
// Metadata of GPX file can contain more than one link
$gpx_file->metadata->links[] = $link;
// Creating track
$track = new Track();
// Name of track
$track->name = \”Some random points in logical order. Input array should be already ordered!\”;
// Type of data stored in track
$track->type = \’RUN\’;
// Source of GPS coordinates
$track->source = \”MySpecificGarminDevice\”;
// Creating Track segment
$segment = new Segment();
foreach ($sample_data as $sample_point)
{
// Creating trackpoint
$point = new Point(Point::TRACKPOINT);
$point->latitude = $sample_point[\’latitude\’];
$point->longitude = $sample_point[\’longitude\’];
$point->elevation = $sample_point[\’elevation\’];
$point->time = $sample_point[\’time\’];
$segment->points[] = $point;
}
// Add segment to segment array of track
$track->segments[] = $segment;
// Recalculate stats based on received data
$track->recalculateStats();
// Add track to file
$gpx_file->tracks[] = $track;
// GPX output
$gpx_file->save(\’CreatingFileFromScratchExample.gpx\’, \\ phpGPX \\ phpGPX ::XML_FORMAT);
// Serialized data as JSON
$gpx_file->save(\’CreatingFileFromScratchExample.json\’, \\ phpGPX \\ phpGPX ::JSON_FORMAT);
// Direct GPX output to browser
header(\”Content-Type: application/gpx+xml\”);
header(\”Content-Disposition: attachment; filename=CreatingFileFromScratchExample.gpx\”);
echo $gpx_file->toXML()->saveXML();
exit();\”>
<?php use phpGPX \\ Models \\ GpxFile ; use phpGPX \\ Models \\ Link ; use phpGPX \\ Models \\ Metadata ; use phpGPX \\ Models \\ Point ; use phpGPX \\ Models \\ Segment ; use phpGPX \\ Models \\ Track ; require_once \' /vendor/autoload.php \' ; $ sample_data = [ [ \' longitude \' => 9.860624216140083 , \' latitude \' => 54.9328621088893 , \' elevation \' => 0 , \' time \' => new \\ DateTime ( \" + 1 MINUTE \" ) ], [ \' latitude \' => 54.83293237320851 , \' longitude \' => 9.76092208681491 , \' elevation \' => 10.0 , \' time \' => new \\ DateTime ( \" + 2 MINUTE \" ) ], [ \' latitude \' => 54.73327743521187 , \' longitude \' => 9.66187816543752 , \' elevation \' => 42.42 , \' time \' => new \\ DateTime ( \" + 3 MINUTE \" ) ], [ \' latitude \' => 54.63342326167919 , \' longitude \' => 9.562439849679859 , \' elevation \' => 12 , \' time \' => new \\ DateTime ( \" + 4 MINUTE \" ) ] ]; // Creating sample link object for metadata $ link = new Link (); $ link -> href = \" https://sibyx.*g*it*hub.io/phpGPX \" ; $ link -> text = \' phpGPX Docs \' ; // GpxFile contains data and handles serialization of objects $ gpx_file = new GpxFile (); // Creating sample Metadata object $ gpx_file -> metadata = new Metadata (); // Time attribute is always \\DateTime object! $ gpx_file -> metadata -> time = new \\ DateTime (); // Description of GPX file $ gpx_file -> metadata -> description = \" My pretty awesome GPX file, created using phpGPX library! \" ; // Adding link created before to links array of metadata // Metadata of GPX file can contain more than one link $ gpx_file -> metadata -> links [] = $ link ; // Creating track $ track = new Track (); // Name of track $ track -> name = \" Some random points in logical order. Input array should be already ordered! \" ; // Type of data stored in track $ track -> type = \' RUN \' ; // Source of GPS coordinates $ track -> source = \" MySpecificGarminDevice \" ; // Creating Track segment $ segment = new Segment (); foreach ( $ sample_data as $ sample_point ) { // Creating trackpoint $ point = new Point (Point:: TRACKPOINT ); $ point -> latitude = $ sample_point [ \' latitude \' ]; $ point -> longitude = $ sample_point [ \' longitude \' ]; $ point -> elevation = $ sample_point [ \' elevation \' ]; $ point -> time = $ sample_point [ \' time \' ]; $ segment -> points [] = $ point ; } // Add segment to segment array of track $ track -> segments [] = $ segment ; // Recalculate stats based on received data $ track -> recalculateStats (); // Add track to file $ gpx_file -> tracks [] = $ track ; // GPX output $ gpx_file -> save ( \' CreatingFileFromScratchExample.gpx \' , \\ phpGPX \\ phpGPX :: XML_FORMAT ); // Serialized data as JSON $ gpx_file -> save ( \' CreatingFileFromScratchExample.json \' , \\ phpGPX \\ phpGPX :: JSON_FORMAT ); // Direct GPX output to browser header ( \" Content-Type: application/gpx+xml \" ); header ( \" Content-Disposition: attachment; filename=CreatingFileFromScratchExample.gpx \" ); echo $ gpx_file -> toXML ()-> saveXML (); exit ();
目前,支持的输出格式:
- XML
- JSON
配置
使用phpGPX中的静态常数来修改行为。
/** * Create Stats object for each track, segment and route */ public static $ CALCULATE_STATS = true ; /** * Additional sort based on timestamp in Routes & Tracks on XML read. * Disabled by default, data should be already sorted. */ public static $ SORT_BY_TIMESTAMP = false ; /** * Default DateTime output format in JSON serialization. */ public static $ DATETIME_FORMAT = \' c \' ; /** * Default timezone for display. * Data are always stored in UTC timezone. */ public static $ DATETIME_TIMEZONE_OUTPUT = \' UTC \' ; /** * Pretty print. */ public static $ PRETTY_PRINT = true ; /** * In stats elevation calculation: ignore points with an elevation of 0 * This can happen with some GPS software adding a point with 0 elevation */ public static $ IGNORE_ELEVATION_0 = true ; /** * Apply elevation gain/loss smoothing? If true, the threshold in * ELEVATION_SMOOTHING_THRESHOLD and ELEVATION_SMOOTHING_SPIKES_THRESHOLD (if not null) applies */ public static $ APPLY_ELEVATION_SMOOTHING = false ; /** * if APPLY_ELEVATION_SMOOTHING is true * the minimum elevation difference between considered points in meters */ public static $ ELEVATION_SMOOTHING_THRESHOLD = 2 ; /** * if APPLY_ELEVATION_SMOOTHING is true * the maximum elevation difference between considered points in meters */ public static $ ELEVATION_SMOOTHING_SPIKES_THRESHOLD = null ; /** * Apply distance calculation smoothing? If true, the threshold in * DISTANCE_SMOOTHING_THRESHOLD applies */ public static $ APPLY_DISTANCE_SMOOTHING = false ; /** * if APPLY_DISTANCE_SMOOTHING is true * the minimum distance between considered points in meters */ public static $ DISTANCE_SMOOTHING_THRESHOLD = 2 ;
我在Backbone SRO中写了这个图书馆。
执照
该项目是根据MIT许可证的条款获得许可的。
