定制 derickr/gpx-utils 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

derickr/gpx-utils

Composer 安装命令:

composer require derickr/gpx-utils

包简介

Set of utils to merge and sort different types of GPX files

README 文档

README

GPX Utils

A set of utilities to operate on GPX files and sets of GPX files.

Reading GPX Files

The GPX reader class DerickR/GPX/Reader can be used to read a GPX file.

It can read GPX routes (rte/rtept) and if none are present, it will read a GPX track (trk/trkseg/trkpt) instead.

You can read a GPX file with:

<?php
require __DIR__ . '/vendor/autoload.php';

use DerickR\GPX\Reader;

$gpxInformation = new Reader( $file );
?>

To access the tracks, you can use the getTrack() method which returns a DerickR/GPX/Track object, which encapsulates a ordered set of coordinate pairs. If the GPX file had multiple tracks, these are merged into one:

$track = $gpxInformation->getTrack();

To get individual tracks out of a GPX file, you can use the getTracks() method to obtain an array of Track objects:

$tracks = $gpxInformation->getTracks();
foreach ( $tracks as $track )
{
        /* Do something with $track */
}

For GPX files that contain routes (rte/rtept) this will still return a merged track.

For GPX files that contain tracks (trk/trkseg) this return an array of tracks, with each Track object representing a track as stored in the GPX file.

To merge tracks stored in multiple GPX files, see Merging GPX Files.

Writing GPX Files

The GPX writer class DerickR/GPX/Writer can be used to write a Track object as a GPX track using trk/trkseg/trkpt elements. It only supports the lat and lon XML elements.

You can write a track to GPX file with:

<?php
require __DIR__ . '/vendor/autoload.php';

use DerickR\GPX\Writer;

$writer = new Writer( $track );
$writer->writeGpx( 'route-test.gpx' );
?>

Instead of writing GPX XML directly to a file, it is also possible to retrieve a string with this information, by using the getGpxString() method:

<?php
require __DIR__ . '/vendor/autoload.php';

use DerickR\GPX\Writer;

$writer = new Writer( $track );
$xmlString = $writer->getGpxString();
?>

Operating on Tracks

The Track class has several methods to operate on GPX tracks. The following methods are supported:

getTrackData
Returns an array with lat/lon coordinate pairs.
getFirstPointAsGeoJson
Returns the first element in the track as a GeoJson formatted array.
getLastPointAsGeoJson
Returns the last element in the track as a GeoJson formatted array.
appendTrack( Track $newTrack )
Adds all the coordinate pairs from $newTrack in sequence to the track.
reverse()
Reverses the array of coordinate pairs that make up the track.
truncatePrecision( int $decimals )
Loops over all the coordinate pairs in the track, and truncates the number of decimals in the coordinate pair to $decimals. In a second loop, it merges two consecutive coordinate pairs if they are the same after truncation.

Merging GPX Files

The Utils::createMergedTrack static method can be used to combine a set of GPX files. The method automatically reverses and selects the order in which the tracks are merged, based on the first and last points in the track.

The second argument to createMergedTrack determines how far (in metres) the different points of different tracks might lay away from each other to be considered as consecutive. By default, this is 25.0 metres.

If the GPX files are not consecutive then an Exception in thrown.

For example to merge all GPX files in a directory, you would use:

<?php
require __DIR__ . '/vendor/autoload.php';

use DerickR\GPX\Reader;
use DerickR\GPX\Utils;
use DerickR\GPX\Writer;

// Find all .gpx files
$files = glob( '*.gpx' );

// Read all tracks into an array
$tracks = [];
foreach ( $files as $file )
{
        $tracks[] = (new Reader( $file ))->getTrack();
}

// Merge all tracks into a single one.
$track = Utils::createMergedTrack( $tracks );

// Write created track to a file
$writer = new Writer( $track );
$writer->writeGpx( 'route-test.gpx' );
?>

Changelog

Release Changes
0.1.0 Initial Release
0.2.0 Added the DerickRGPXWriter::getGpxString() method
0.3.0 Added the DerickRGPXReader::getTracks() method

derickr/gpx-utils 适用场景与选型建议

derickr/gpx-utils 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 47 次下载、GitHub Stars 达 2, 最近一次更新时间为 2022 年 07 月 24 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「gps」 「gpx」 「algorithms」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 derickr/gpx-utils 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 derickr/gpx-utils 我们能提供哪些服务?
定制开发 / 二次开发

基于 derickr/gpx-utils 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 47
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 22
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 2
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-07-24