forked from ronnywang/geojson2image
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GeoJSON2Image.php
390 lines (343 loc) · 13.5 KB
/
GeoJSON2Image.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
<?php
/**
* Generate Image from GeoJSON
*
* @copyright 2013-2013 Ronny Wang <ronnywang at gmail.com>
* @license BSD License http://opensource.org/licenses/BSD-3-Clause
*
*/
class GeoJSON2Image
{
/**
* get boundry from 2 boundies
*
* @param array $b1 boundry
* @param array $b2 boundry
* @access protected
* @return array boundry
*/
protected static function computeBoundry($b1, $b2)
{
if (is_null($b1)) {
return $b2;
}
return array(
min($b1[0], $b2[0]),
max($b1[1], $b2[1]),
min($b1[2], $b2[2]),
max($b1[3], $b2[3]),
);
}
/**
* get boundry from geojson
*
* @param object $json
* @access public
* @return array(minx, maxx, miny, maxy)
*/
public static function getBoundry($json)
{
switch ($json->type) {
case 'GeometryCollection':
$return_boundry = null;
foreach ($json->geometries as $geometry) {
$return_boundry = self::computeBoundry($return_boundry, self::getBoundry($geometry));
}
return $return_boundry;
case 'FeatureCollection':
$return_boundry = null;
foreach ($json->features as $feature) {
$return_boundry = self::computeBoundry($return_boundry, self::getBoundry($feature));
}
return $return_boundry;
case 'Feature':
return self::getBoundry($json->geometry);
case 'Point':
return array($json->coordinates[0], $json->coordinates[0], $json->coordinates[1], $json->coordinates[1]);
case 'MultiPoint':
$return_boundry = null;
foreach ($json->coordinates as $point) {
$return_boundry = self::computeBoundry($return_boundry, array($point[0], $point[0], $point[1], $point[1]));
}
return $return_boundry;
case 'LineString':
$return_boundry = null;
foreach ($json->coordinates as $point) {
$return_boundry = self::computeBoundry($return_boundry, array($point[0], $point[0], $point[1], $point[1]));
}
return $return_boundry;
case 'MultiLineString':
$return_boundry = null;
foreach ($json->coordinates as $linestrings) {
foreach ($linestrings as $point) {
$return_boundry = self::computeBoundry($return_boundry, array($point[0], $point[0], $point[1], $point[1]));
}
}
return $return_boundry;
case 'Polygon':
$return_boundry = null;
foreach ($json->coordinates as $linestrings) {
foreach ($linestrings as $point) {
$return_boundry = self::computeBoundry($return_boundry, array($point[0], $point[0], $point[1], $point[1]));
}
}
return $return_boundry;
case 'MultiPolygon':
$return_boundry = null;
foreach ($json->coordinates as $polygons) {
foreach ($polygons as $linestrings) {
foreach ($linestrings as $point) {
$return_boundry = self::computeBoundry($return_boundry, array($point[0], $point[0], $point[1], $point[1]));
}
}
}
return $return_boundry;
default:
throw new Exception("Unsupported GeoJSON type:{$json->type}");
}
}
protected static function pixelX($x)
{
return ($x + 180) / 360;
}
protected static function pixelY($y)
{
$sin_y = sin($y * pi() / 180);
return (0.5 - log((1 + $sin_y) / (1 - $sin_y)) / (4 * pi()));
}
/**
* Tranfrom geojson coordinates to image coordinates
*
* @param array $point
* @param array $boundry
* @param int $width
* @param int $height
* @static
* @access public
* @return void
*/
public static function transformPoint($point, $boundry, $width, $height)
{
if ($point[0] == 180 or $point[0] == -180) {
return false;
}
$x_delta = self::pixelX($boundry[1]) - self::pixelX($boundry[0]);
$y_delta = self::pixelY($boundry[3]) - self::pixelY($boundry[2]);
$new_point = array();
$p = function($a){
$a += 180;
if ($a > 360) {
$a -= 360;
}
return $a;
};
$new_point[0] = floor((self::pixelX($p($point[0]) + $boundry[4]) - self::pixelX($p($boundry[0]) + $boundry[4])) * $width / $x_delta);
$new_point[1] = floor((self::pixelY($boundry[3]) - self::pixelY($point[1])) * $height / $y_delta);
return $new_point;
$x_delta = $boundry[1] - $boundry[0];
$y_delta = $boundry[3] - $boundry[2];
return array(
($point[0] - $boundry[0]) * $width / $x_delta,
($boundry[3] - $point[1]) * $height / $y_delta,
);
}
/**
* draw the GeoJSON on image
*
* @param Image $gd
* @param object $json
* @param array $boundry
* @param int $width
* @param int $height
* @param array $draw_options : background_color : array(r,g,b)
* @static
* @access public
* @return void
*/
public static function drawJSON($gd, $json, $boundry, $width, $height, $draw_options = array())
{
$x_delta = $boundry[1] - $boundry[0];
$y_delta = $boundry[3] - $boundry[2];
$max_delta = max($x_delta, $y_delta);
switch ($json->type) {
case 'GeometryCollection':
foreach ($json->geometries as $geometry) {
self::drawJSON($gd, $geometry, $boundry, $width, $height, $draw_options);
}
break;
case 'FeatureCollection':
foreach ($json->features as $feature) {
self::drawJSON($gd, $feature, $boundry, $width, $height);
}
break;
case 'Feature':
self::drawJSON($gd, $json->geometry, $boundry, $width, $height, (array)($json->properties));
break;
case 'Polygon':
if (array_key_exists('polygon_background_color', $draw_options) and $draw_options['polygon_background_color'] !== false) {
$background_color = imagecolorallocate($gd, $draw_options['polygon_background_color'][0], $draw_options['polygon_background_color'][1], $draw_options['polygon_background_color'][2]);
} else {
// no color if no polygon_background_color
$background_color = null;
}
if (array_key_exists('polygon_border_color', $draw_options)) {
$border_color = imagecolorallocate($gd, $draw_options['polygon_border_color'][0], $draw_options['polygon_border_color'][1], $draw_options['polygon_border_color'][2]);
} else {
$border_color = imagecolorallocate($gd, 0, 0, 0);
}
if (array_key_exists('polygon_border_size', $draw_options)) {
$border_size = $draw_options['polygon_border_size'];
} else {
$border_size = 3;
}
$filled_points = array();
foreach ($json->coordinates as $linestrings) {
$border_points = array();
if ($linestrings[0] != $linestrings[count($linestrings) - 1]) {
$linestrings[] = $linestrings[0];
}
if (count($linestrings) <= 3) {
// skip 2 points
continue 2;
}
foreach ($linestrings as $point) {
if (!$new_point = self::transformPoint($point, $boundry, $width, $height)) {
continue;
}
$border_points[] = floor($new_point[0]);
$filled_points[] = floor($new_point[0]);
$border_points[] = floor($new_point[1]);
$filled_points[] = floor($new_point[1]);
}
if (count($border_points) < 3) {
continue;
}
if ($border_size) {
imagesetthickness($gd, $border_size);
imagepolygon($gd, $border_points, count($border_points) / 2, $border_color);
}
}
if (!is_null($background_color) and count($filled_points) >= 6) {
imagefilledpolygon($gd, $filled_points, count($filled_points) / 2, $background_color);
}
break;
case 'MultiPolygon':
foreach ($json->coordinates as $polygon) {
$j = new StdClass;
$j->type = 'Polygon';
$j->coordinates = $polygon;
self::drawJSON($gd, $j, $boundry, $width, $height, $draw_options);
}
break;
case 'Point':
if (array_key_exists('point_background_color', $draw_options)) {
$background_color = imagecolorallocate($gd, $draw_options['point_background_color'][0], $draw_options['point_background_color'][1], $draw_options['point_background_color'][2]);
} else {
// default point color is red
$background_color = imagecolorallocate($gd, 255, 0, 0);
}
if (array_key_exists('point_border_size', $draw_options)) {
$border_size = $draw_options['point_border_size'];
} else {
$border_size = 1;
}
$point_size = 10;
if (array_key_exists('point_border_color', $draw_options)) {
$border_color = imagecolorallocate($gd, $draw_options['point_border_color'][0], $draw_options['point_border_color'][1], $draw_options['point_border_color'][2]);
} else {
// default point border color is black
$border_color = imagecolorallocate($gd, 0, 0, 0);
}
$point = $json->coordinates;
$new_point = self::transformPoint($point, $boundry, $width, $height);
imagefilledellipse($gd, $new_point[0], $new_point[1], $point_size, $point_size, $background_color);
for ($i = 0; $i < $border_size; $i ++) {
imageellipse($gd, $new_point[0], $new_point[1], $point_size - 1 + $i, $point_size - 1 + $i, $border_color);
}
break;
case 'MultiPoint':
foreach ($json->coordinates as $coordinate) {
$j = new StdClass;
$j->type = 'Point';
$j->coordinates = $coordinate;
self::drawJSON($gd, $j, $boundry, $width, $height, $draw_options);
}
break;
case 'LineString':
$last_point = null;
if (array_key_exists('line_border_color', $draw_options)) {
$border_color = imagecolorallocate($gd, $draw_options['line_border_color'][0], $draw_options['line_border_color'][1], $draw_options['line_border_color'][2]);
} else {
$border_color = imagecolorallocate($gd, 0, 0, 0);
}
if (array_key_exists('line_border_size', $draw_options)) {
$border_size = $draw_options['line_border_size'];
} else {
$border_size = 3;
}
foreach ($json->coordinates as $point) {
$new_point = self::transformPoint($point, $boundry, $width, $height);
if (!is_null($last_point)) {
imagesetthickness($gd, $border_size);
imageline($gd, $last_point[0], $last_point[1], $new_point[0], $new_point[1], $border_color);
}
$last_point = $new_point;
}
break;
case 'MultiLineString':
foreach ($json->coordinates as $coordinate) {
$j = new StdClass;
$j->type = 'LineString';
$j->coordinates = $coordinate;
self::drawJSON($gd, $j, $boundry, $width, $height, $draw_options);
}
break;
default:
throw new Exception("Unsupported GeoJSON type:{$json->type}");
}
}
public function GeoJSON2Image($json)
{
$this->json = $json;
}
protected $_boundry = null;
public function setBoundry($boundry)
{
$this->_boundry = $boundry;
}
protected $_width = 400;
protected $_height = 400;
public function setSize($width, $height)
{
$this->_width = $width;
$this->_height = $height;
}
public function draw($file = null)
{
// 先找到長寬
$boundry = !is_null($this->_boundry) ? $this->_boundry : self::getBoundry($this->json);
$gd = imagecreatetruecolor(
$this->_width,
$this->_height
);
$bg_color = imagecolorallocate($gd, 254, 254, 254);
imagecolortransparent($gd, $bg_color);
imagefill($gd, 0, 0, $bg_color);
$boundry[4] = 0;
if ($boundry[1] > $boundry[0]) {
self::drawJSON($gd, $this->json, $boundry, $this->_width, $this->_height);
} else {
$boundry[1] += 360;
self::drawJSON($gd, $this->json, $boundry, $this->_width, $this->_height);
$boundry[1] -= 360;
$boundry[0] -= 360;
self::drawJSON($gd, $this->json, $boundry, $this->_width, $this->_height);
}
if (is_null($file)) {
header('Content-Type: image/png');
imagepng($gd);
} else {
imagepng($gd, $file);
}
}
}