Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
EPSG3857
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
3 / 3
3
100.00% covered (success)
100.00%
1 / 1
 latLonToPoint
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 projectToSphericalMercator
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 pointTransformation
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace Kartographer\Special;
4
5/**
6 * @license MIT
7 */
8class EPSG3857 {
9
10    private const MAX_LATITUDE = 85.0511287798;
11    private const A = 0.159154943;
12
13    /**
14     * EPSG3857 (Spherical Mercator) is the most common CRS for web mapping and is used by Leaflet
15     * by default. Converted to PHP from L.CRS.EPSG3857 (leaflet.js)
16     *
17     * @param float[] $latLon Latitude (north–south) and longitude (east-west) in degree.
18     * @return float[] Point (x, y)
19     */
20    public static function latLonToPoint( array $latLon ): array {
21        $projectedPoint = self::projectToSphericalMercator( $latLon );
22        $scale = 256;
23
24        return self::pointTransformation( $projectedPoint, $scale );
25    }
26
27    /**
28     * Spherical Mercator is the most popular map projection, used by EPSG:3857 CRS. Converted to
29     * PHP from L.Projection.SphericalMercator (leaflet.js)
30     *
31     * @param float[] $latLon Latitude (north–south) and longitude (east-west) in degree. Latitude
32     *  is truncated between approx. -85.05° and 85.05°. Longitude should be -180 to 180°, but is
33     *  not limited.
34     * @return float[] Point (x, y)
35     */
36    private static function projectToSphericalMercator( array $latLon ): array {
37        $lat = max( min( self::MAX_LATITUDE, $latLon[0] ), -self::MAX_LATITUDE );
38        $x = deg2rad( $latLon[1] );
39        $y = deg2rad( $lat );
40
41        $y = log( tan( ( M_PI / 4 ) + ( $y / 2 ) ) );
42
43        return [ $x, $y ];
44    }
45
46    /**
47     * Performs a simple point transformation through a 2d-matrix. Converted to PHP from
48     * L.Transformation (leaflet.js)
49     *
50     * @param float[] $point
51     * @param int $scale
52     * @return float[] Point (x, y)
53     */
54    private static function pointTransformation( array $point, int $scale ): array {
55        return [
56            $scale * ( self::A * $point[0] + 0.5 ),
57            $scale * ( -self::A * $point[1] + 0.5 )
58        ];
59    }
60
61}