-
Notifications
You must be signed in to change notification settings - Fork 1
/
linestring.php
187 lines (164 loc) · 6.23 KB
/
linestring.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
require 'color.php';
require 'toTile.php';
function output_error($message, $statusCode=400) {
http_response_code($statusCode);
header('Content-Type: text/plain');
$expires = 60*60*24*14;
header("Pragma: public");
header("Cache-Control: maxage=".$expires);
header('Expires: ' . gmdate('D, d M Y H:i:s', time()+$expires) . ' GMT');
print($message . "\n");
exit(0);
}
class Point {
public $x, $y;
public function __construct($x, $y) {
$this->x = $x;
$this->y = $y;
}
public function x_on_map($width, $centerX, $tileSize, $zoom) {
return floor(($width/2) - $tileSize * ($centerX - lonToTile($this->x, $zoom)));
}
public function y_on_map($height, $centerY, $tileSize, $zoom) {
return floor(($height/2) - $tileSize * ($centerY - latToTile($this->y, $zoom)));
}
}
class LineString {
protected $points = array();
protected $current = 0;
public $lineColor;
public $fillColor;
public $width;
public function __construct($lineColor, $width, $fillColor) {
$this->lineColor = $lineColor;
$this->width = $width;
$this->fillColor = $fillColor;
}
public function addPoint($point) {
array_push($this->points, $point);
}
public function length() {
return count($this->points);
}
public function at($index) {
return $this->points[$index];
}
public function isClosed() {
return ($this->length() >= 4) && ($this->points[0]->x == end($this->points)->x) && ($this->points[0]->y == end($this->points)->y);
}
public function gdPointsArray($width, $height, $centerX, $centerY, $zoom, $tileSize) {
$gdPArray = array();
for ($i = 0; $i < count($this->points) - 1; $i++) {
$x1 = $this->at($i)->x_on_map($width, $centerX, $tileSize, $zoom);
$y1 = $this->at($i)->y_on_map($height, $centerY, $tileSize, $zoom);
$gdPArray[] = $x1;
$gdPArray[] = $y1;
}
return array($gdPArray, $this->length() - 1);
}
}
function buildLineString($pointList, $lineColor, $width, $fillColor) {
if ($pointList[0] != '(' || $pointList[strlen($pointList) - 1] != ')') {
output_error('Point list is invalid. Its first character must be an opening round, the last one must be a closing round bracket.');
}
// The use of strlen($pointList) - 2 is inteded.
$points = preg_split('/\)\(/', substr($pointList, 1, strlen($pointList) - 2));
if (count($points) < 2) {
output_error('A line must contain at least two points.');
}
$linestring = new LineString($lineColor, $width, $fillColor);
foreach ($points as $point) {
list($lon, $lat) = explode(' ', $point, 2);
if (is_numeric($lon) && is_numeric($lat)) {
$linestring->addPoint(new Point($lon, $lat));
} else {
output_error('Could not parse point list. It contains a point coordinate which is not a number.');
}
}
return $linestring;
}
class Arc {
public $center;
public $radius;
public $start = 0;
public $end = 360;
public $lineWidth = 3;
public $fillColor;
public $lineColor;
public $straightEdges;
public function __construct($center, $radius, $width, $lineColor, $fillColor, $start=null, $end=null, $straightEdges=FALSE) {
$this->lineWidth = $width;
$this->center = $center;
$this->start = $start;
$this->end = $end;
$this->radius = $radius;
$this->lineColor = $lineColor;
$this->fillColor = $fillColor;
$this->straightEdges = $straightEdges;
}
public static function urlParamToBool($value) {
if ($value === "1" or $value === "true" or $value === "TRUE") {
return TRUE;
}
return FALSE;
}
public function isCircle() {
return $this->start === 0 && $this->end === 360;
}
/**
* Return radius in pixel.
*/
public function radiusInPixel($mapCenterLat, $zoomLevel, $tileSize) {
return $this->radius * pixelPerMeter($mapCenterLat, $zoomLevel, $tileSize);
}
public function getArcPointX($centerX, $angle, $radiusPx) {
return $centerX + cos(deg2rad($angle)) * $radiusPx;
}
public function getArcPointY($centerY, $angle, $radiusPx) {
return $centerY - sin(deg2rad($angle)) * $radiusPx;
}
/**
* Get a line from the center to the start of the arc.
*/
public function getArcStartOrEnd($mapCenterX, $mapCenterY, $mapWidth, $mapHeight, $zoom, $tileSize, $start) {
$ang = 0;
if ($start) {
$ang = 360 - $this->start;
} else {
$ang1 = 360 - $this->start;
$ang = $ang1 - ($this->end - $this->start);
}
$radiusPx = $this->radiusInPixel($this->center->y, $zoom, $tileSize);
$centerPxX = $this->center->x_on_map($mapWidth, $mapCenterX, $tileSize, $zoom);
$centerPxY = $this->center->y_on_map($mapHeight, $mapCenterY, $tileSize, $zoom);
return array(
$this->getArcPointX($centerPxX, $ang, $radiusPx),
$this->getArcPointY($centerPxY, $ang, $radiusPx)
);
}
/**
* Get an approximation of the arc as a polygon.
*
* Returns an array containing the x and y coordinates of the polygons vertices consecutively.
*/
public function getArcPoints($mapCenterX, $mapCenterY, $mapWidth, $mapHeight, $zoom, $tileSize) {
// Radius intentionally 2 pixel smaller to avoid that the fill appears beyond the outline as well.
$radiusPx = $this->radiusInPixel($this->center->y, $zoom, $tileSize) - 2;
$centerPxX = $this->center->x_on_map($mapWidth, $mapCenterX, $tileSize, $zoom);
$centerPxY = $this->center->y_on_map($mapHeight, $mapCenterY, $tileSize, $zoom);
// It is easier to work with counterclockwise angles here.
$ang1 = 360 - $this->start;
$ang2 = $ang1 - ($this->end - $this->start);
$points = array($centerPxX, $centerPxY);
for ($i = $ang2; $i <= $ang1; $i = $i + 0.5) {
array_push(
$points,
$this->getArcPointX($centerPxX, $i, $radiusPx),
$this->getArcPointY($centerPxY, $i, $radiusPx)
);
}
return $points;
}
}
?>