Tile::getLibName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
3
namespace Smindel\GIS\Service;
4
5
use SilverStripe\Core\Injector\Injector;
6
use SilverStripe\Core\Config\Configurable;
7
use SilverStripe\Core\Injector\Injectable;
8
use Smindel\GIS\GIS;
9
use Smindel\GIS\Control\WebMapTileService;
10
11
class Tile
12
{
13
    use Configurable;
14
    use Injectable;
15
16
    /**
17
     * Zoom
18
     *
19
     * @var int
20
     */
21
    protected $z;
22
23
    /**
24
     * x'th tile from left
25
     *
26
     * @var int
27
     */
28
    protected $x;
29
30
    /**
31
     * y'th tile from the top
32
     *
33
     * @var int
34
     */
35
    protected $y;
36
37
    /**
38
     * Default tile width in pixel
39
     *
40
     * @var int
41
     */
42
    protected $size;
43
44
    /**
45
     * Wrap around dateline
46
     *
47
     * @var int
48
     */
49
    protected $wrap;
50
51
    /**
52
     * Top left corner of the tile
53
     *
54
     * @var array
55
     */
56
    protected $topLeft;
57
58
    protected $longSpan;
59
60
    protected $resource;
61
62 4
    public function __construct($z, $x, $y, $defaultStyle = [], $wrap = null, $size = null)
63
    {
64 4
        $this->z = $z;
65 4
        $this->x = $x;
66 4
        $this->y = $y;
67 4
        $this->wrap = $wrap !== null ? $wrap : self::config()->get('wrap_date');
68 4
        $this->size = $size ?: WebMapTileService::config()->get('tile_size');
69
70 4
        list($lon, $lat) = Tile::zxy2lonlat($z, $x, $y);
71 4
        $this->longSpan = [$lon, Tile::zxy2lonlat($z, $x + 1, $y)[0]];
72
73 4
        $this->topLeft = [
74 4
            (($lon + 180) / 360) * $this->size * pow(2, $this->z),
75 4
            (0.5 - log((1 + sin($lat * pi() / 180)) / (1 - sin($lat * pi() / 180))) / (4 * pi())) * $this->size * pow(2, $this->z),
76
        ];
77
78 4
        $this->resource = Injector::inst()->get('TileRenderer', false, [$this->size, $this->size, $defaultStyle]);
79 4
    }
80
81
    public function debug($text = null)
82
    {
83
        $this->resource->debug($text ?: "$this->z, $this->x, $this->y");
84
    }
85
86
    public function getZXY()
87
    {
88
        return [$this->z, $this->x, $this->y];
89
    }
90
91 2
    public function getContentType()
92
    {
93 2
        return 'image/png';
94
    }
95
96
    public function getLibName()
97
    {
98
        return get_class($this->resource)::LIB_NAME;
99
    }
100
101
    public function getResource()
102
    {
103
        return $this->resource;
104
    }
105
106 4
    public function getRelativePixelCoordinates($wkt, &$reflection = null)
107
    {
108 4
        $geo = GIS::create($wkt)->reproject(4326);
109
110
        { // determin rendering offset
111 4
            $min = $max = null;
112 4
            GIS::each(
113 4
                $geo,
114 4
                function($lonlat) use (&$min, &$max) {
115 4
                    $min = is_null($min) ? $lonlat[0] : min($min, $lonlat[0]);
116 4
                    $max = is_null($max) ? $lonlat[0] : max($max, $lonlat[0]);
117 4
                }
118
            );
119 4
            $distance = [-360 => null, 0 => null, 360 => null];
120 4
            foreach ($distance as $offset => &$dist) {
121 4
                $dist = min($max, $this->longSpan[1] + $offset) - max($min, $this->longSpan[0] + $offset);
122
            }
123
        }
124
125 4
        foreach ($distance as $offset => &$dist) {
126 4
            $dist = GIS::each(
127 4
                $geo,
128 4
                function($lonlat) use ($offset) {
129
                    return [
130 4
                            (($lonlat[0] + 180 - $offset) / 360) * $this->size * pow(2, $this->z) - $this->topLeft[0],
131 4
                            (0.5 - log((1 + sin($lonlat[1] * pi() / 180)) / (1 - sin($lonlat[1] * pi() / 180))) / (4 * pi())) * $this->size * pow(2, $this->z) - $this->topLeft[1],
132
                        ];
133 4
                }
134
                );
135
        }
136
137 4
        $largest = &$distance[0];
138
139 4
        $reflection = array_filter($distance);
140
141 4
        return $largest;
142
    }
143
144 4
    public function render($list)
145
    {
146 4
        foreach ($list as $item) {
147 4
            if ($item->hasMethod('renderOnWebMapTile')) {
148
                $item->renderOnWebMapTile($this);
149
            } else {
150 4
                $property = GIS::of(get_class($item));
151 4
                $primary = $this->getRelativePixelCoordinates($item->$property, $reflections);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $primary is correct as $this->getRelativePixelC...property, $reflections) targeting Smindel\GIS\Service\Tile...ativePixelCoordinates() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
152
153 4
                if ($this->wrap) {
154 3
                    foreach ($reflections as $reflection) {
155 3
                        $this->resource->{'draw' . GIS::create($item->$property)->type}($reflection);
156
                    }
157
                } else {
158 1
                    $this->resource->{'draw' . GIS::create($item->$property)->type}($primary);
159
                }
160
            }
161
        }
162 4
        return $this->resource->getImageBlob();
163
    }
164
165 5
    public static function zxy2lonlat($z, $x, $y)
166
    {
167 5
        $n = pow(2, $z);
168 5
        $lon_deg = $x / $n * 360.0 - 180.0;
169 5
        $lat_deg = rad2deg(atan(sinh(pi() * (1 - 2 * $y / $n))));
170 5
        return [$lon_deg, $lat_deg];
171
    }
172
}
173