-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgeojson_port.php
213 lines (158 loc) · 5.71 KB
/
geojson_port.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
<?php
//
require_once(dirname(__FILE__) . '/port.php');
//--------------------------------------------------------------------------------------------------
// Based on http://stackoverflow.com/a/2706847/9684
class GoogleMapsAPIProjection
{
var $PixelTileSize = 256.0;
var $DegreesToRadiansRatio ;
var $RadiansToDegreesRatio;
var $PixelGlobeCenter = array();
var $XPixelsToDegreesRatio;
var $YPixelsToRadiansRatio;
function __construct($zoomLevel = 0)
{
$this->DegreesToRadiansRatio = 180.0 / M_PI;
$this->RadiansToDegreesRatio = M_PI / 180.0;
$this->pixelGlobeSize = $this->PixelTileSize * pow(2, $zoomLevel);
$this->XPixelsToDegreesRatio = $this->pixelGlobeSize / 360.0;
$this->YPixelsToRadiansRatio = $this->pixelGlobeSize / (2.0 * M_PI);
$halfPixelGlobeSize = $this->pixelGlobeSize / 2.0;
$this->PixelGlobeCenter = array($halfPixelGlobeSize,$halfPixelGlobeSize);
}
// convert (long, lat) pair to pixels
function FromCoordinatesToPixel($coordinates)
{
$x = $this->PixelGlobeCenter[0] + ($coordinates[0] * $this->XPixelsToDegreesRatio);
$f = min(max(sin($coordinates[1] * $this->RadiansToDegreesRatio), -0.9999), 0.9999);
$y = $this->PixelGlobeCenter[1] + 0.5 * (log( (1.0 + $f) / (1.0 - $f)) * -$this->YPixelsToRadiansRatio);
$pixel = array($x, $y);
return $pixel;
}
// convert (x,y) to (long, lat)
function FromPixelToCoordinates($pixel)
{
$longitude = ($pixel[0] - $this->PixelGlobeCenter[0]) / $this->XPixelsToDegreesRatio;
$latitude = (2 * atan( exp(
($pixel[1] - $this->PixelGlobeCenter[1]) / -$this->YPixelsToRadiansRatio))
- M_PI / 2.0) * $this->DegreesToRadiansRatio;
$longlat = array($longitude, $latitude);
return $longlat;
}
}
//--------------------------------------------------------------------------------------------------
// Bounding box
class BoundingBox
{
var $min_xy;
var $max_xy;
function __construct()
{
$this->min_xy = array(180.0, 90.0);
$this->max_xy = array(-180.0, -90.0);
}
function extend($latlong)
{
$this->min_xy[0] = min($this->min_xy[0], $latlong[1]);
$this->min_xy[1] = min($this->min_xy[1], $latlong[0]);
$this->max_xy[0] = max($this->max_xy[0], $latlong[1]);
$this->max_xy[1] = max($this->max_xy[1], $latlong[0]);
}
function toArray()
{
$a = array(
$this->min_xy[0],
$this->min_xy[1],
$this->max_xy[0],
$this->max_xy[1]
);
return $a;
}
}
//-------------------------------------------------------------------------------------------------
class GeoJsonPort extends Port
{
var $bounds = null;
//----------------------------------------------------------------------------------------------
function Circle($pt, $r, $action = '')
{
}
//----------------------------------------------------------------------------------------------
function DrawCircleArc($p0, $p1, $radius, $large_arc_flag = false)
{
}
//----------------------------------------------------------------------------------------------
function DrawLine($p0, $p1, $style = '{ "weight":2, "color":"yellow","opacity":1}')
{
$g = new GoogleMapsAPIProjection();
// 1. Convert pixels to lat/long
$pixels = array($p0['x'], $p0['y']);
$long_lat0 = $g->FromPixelToCoordinates($pixels);
$pixels = array($p1['x'], $p1['y']);
$long_lat1 = $g->FromPixelToCoordinates($pixels);
$feature = new stdclass;
$feature->type = "Feature";
$feature->geometry = new stdclass;
$feature->geometry->type = "LineString";
$feature->geometry->coordinates = array();
$feature->geometry->coordinates[] =array($long_lat0[0], $long_lat0[1]);
$feature->geometry->coordinates[] =array($long_lat1[0], $long_lat1[1]);
$feature->properties = new stdclass;
$feature->properties->style = json_decode($style);
$this->geojson->features[] = $feature;
$this->bounds->extend(array($long_lat0[1], $long_lat0[0]));
$this->bounds->extend(array($long_lat1[1], $long_lat1[0]));
}
//----------------------------------------------------------------------------------------------
function DrawText ($pt, $text, $action = '')
{
$g = new GoogleMapsAPIProjection();
// 1. Convert pixels to lat/long
$pixels = array($pt['x'], $pt['y']);
$long_lat = $g->FromPixelToCoordinates($pixels);
$feature = new stdclass;
$feature->type = "Feature";
$feature->geometry = new stdclass;
$feature->geometry->type = "Point";
$feature->geometry->coordinates = array($long_lat[0], $long_lat[1]);
$feature->properties = new stdclass;
$feature->properties->name = $text;
$feature->properties->popupContent = $text;
$this->geojson->features[] = $feature;
$this->bounds->extend(array($long_lat[1], $long_lat[0]));
}
//----------------------------------------------------------------------------------------------
function DrawRotatedText ($pt, $text, $action = '', $align = 'left', $angle = 0)
{
}
//----------------------------------------------------------------------------------------------
function StartPicture($centre = false)
{
$this->bounds = new BoundingBox();
$this->geojson = new stdclass;
$this->geojson->type = "FeatureCollection";
$this->geojson->bbox = array();
$this->geojson->features = array();
}
//----------------------------------------------------------------------------------------------
function EndPicture ()
{
//print_r($this->bounds);exit();
$this->geojson->bbox = $this->bounds->toArray();
//$this->output = json_encode($this->geojson, JSON_PRETTY_PRINT);
$this->output = json_encode($this->geojson, JSON_PRETTY_PRINT);
}
}
// test
if (0)
{
$g = new GeoJsonPort(0,0,0,10);
// Coordinates in pixels
$pt1 = array('x'=> 128, 'y' => 128);
$pt2 = array('x'=> 128, 'y' => 130);
$g->DrawLine($pt1, $pt2);
$js = $g->EndPicture();
echo $js;
}
?>