/** * Decode string and generate a full SVG document. * * @uses DOMDocument * @param string $encoded - Encoded polyline * @return string - SVG document */ public static function DecodeToSVG($encoded) { // Create list of points $points = parent::Decode($encoded); // Grab first pair list($x, $y) = self::shiftPoint($points); // Path will need to start by moving to first coordinate. $path = sprintf('M %f %f L ', $x, $y); // Init bounding box's min & max. $minX = $maxX = $x; $minY = $maxY = $y; while ($points) { // This can be simplified with php-5.5's generators. list($x, $y) = self::shiftPoint($points); $path .= sprintf('%f %f, ', $x, $y); // Grow MBR if ($x < $minX) { $minX = $x; } if ($y < $minY) { $minY = $y; } if ($x > $maxX) { $maxX = $x; } if ($y > $maxY) { $maxY = $y; } } // Close poylgon $path = rtrim($path, ', ') . ' Z'; // Create viewBox from MBR points $mbr = sprintf("%f %f %f %f", $minX, $minY, abs($maxX - $minX), abs($maxY - $minY)); return self::generateSVG($path, $mbr); }
public function testRounding() { $originalPoints = array(48.000006, 2.000004, 48.00001, 2.0); $encoded = Polyline::Encode($originalPoints); $this->assertEquals('a_~cH_seK??', $encoded); $decodedPoints = Polyline::Decode($encoded); $this->assertTrue($decodedPoints[0] === $decodedPoints[2]); $this->assertTrue($decodedPoints[1] === $decodedPoints[3]); }
/** * @covers Polyline::Decode */ public function testDecode() { $this->assertCount(count($this->points) * 2, Polyline::Decode($this->encoded)); }