phayes / geoPHP

Advanced geometry operations in PHP
https://geophp.net
Other
861 stars 262 forks source link

append point to linstring #88

Open stefanocudini opened 10 years ago

stefanocudini commented 10 years ago

hi

how can I append new point at end of gpx track?

$newPoint = geoPHP::load('POINT(12.23 45.2)','wkt');

$trackFile = 'mytrack.gpx';

$track = geoPHP::load(file_get_contents($trackFile),'gpx');

...???append point???...

file_put_contents($trackFile, $out);
mprins commented 10 years ago

you would probably nee to get the last point of your track, use that and newPoint to build a line and then use [union()] to join the two lines (https://github.com/phayes/geoPHP/blob/master/lib/geometry/Geometry.class.php#L222) (requires geos) and assuming your track exists of only one segment

the other option is to serialize to string, append the point to the string and deserialize from string

stefanocudini commented 10 years ago

Thank you for answer. I'm resolved in this way:

    $trackFile = 'track.gpx';
    $loc = array(42.34,13.23);  //new POINT
    $locGeo = geoPHP::load('POINT('.implode(' ',$loc).')','wkt');

    $track = geoPHP::load(file_get_contents($trackFile),'gpx');

    $trackAr =  $track->asArray();

    if( $locGeo->equals($track->endPoint()) )
        die('this last point just exists');
    else
        $trackAr[]= $loc;

    $trackOut = array();
    foreach($trackAr as $v)
        $trackOut[]= new Point($v[0],$v[1]);

    $trackOut = new LineString($trackOut);

    $out = $trackOut->out('gpx');

    file_put_contents($trackFile, $out);
stefanocudini commented 10 years ago

maybe a new method: addPoint inside LineString class could be useful