-
Notifications
You must be signed in to change notification settings - Fork 5
/
ModestMaps.php
227 lines (171 loc) · 8.89 KB
/
ModestMaps.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
<?php
require_once 'Geo.php';
require_once 'Core.php';
require_once 'Providers.php';
class Modest_Map
{
var $provider;
var $dimensions;
var $coordinate;
var $offset;
function Modest_Map($provider, $dimensions, $coordinate, $offset)
{
$this->provider = $provider;
$this->dimensions = $dimensions;
$this->coordinate = $coordinate;
$this->offset = $offset;
}
function locationPoint($location)
{
$point = $this->offset->copy();
$coord = $this->provider->locationCoordinate($location)->zoomTo($this->coordinate->zoom);
// distance from the known coordinate offset
$point->x += $this->provider->tile_width * ($coord->column - $this->coordinate->column);
$point->y += $this->provider->tile_height * ($coord->row - $this->coordinate->row);
// because of the center/corner business
$point->x += $this->dimensions->x / 2;
$point->y += $this->dimensions->y / 2;
return $point;
}
function pointLocation($point)
{
$hizoomCoord = $this->coordinate->zoomTo(MMaps_Coordinate_Max_Zoom);
// because of the center/corner business
$point = new MMaps_Point($point->x - $this->dimensions->x/2,
$point->y - $this->dimensions->y/2);
// distance in tile widths from reference tile to point
$xTiles = ($point->x - $this->offset->x) / $this->provider->tile_width;
$yTiles = ($point->y - $this->offset->y) / $this->provider->tile_height;
// distance in rows & columns at maximum zoom
$xDistance = $xTiles * pow(2, (MMaps_Coordinate_Max_Zoom - $this->coordinate->zoom));
$yDistance = $yTiles * pow(2, (MMaps_Coordinate_Max_Zoom - $this->coordinate->zoom));
// new point coordinate reflecting that distance
$coord = new MMaps_Coordinate(round($hizoomCoord->row + $yDistance),
round($hizoomCoord->column + $xDistance),
$hizoomCoord->zoom);
$coord = $coord->zoomTo($this->coordinate->zoom);
$location = $this->provider->coordinateLocation($coord);
return $location;
}
function draw()
{
$coord = $this->coordinate->copy();
$corner = new MMaps_Point(floor($this->offset->x + $this->dimensions->x/2), floor($this->offset->y + $this->dimensions->y/2));
while($corner->x > 0)
{
$corner->x -= $this->provider->tile_width;
$coord = $coord->left();
}
while($corner->y > 0)
{
$corner->y -= $this->provider->tile_height;
$coord = $coord->up();
}
$tiles = array();
$rowCoord = $coord->copy();
for($y = $corner->y; $y < $this->dimensions->y; $y += $this->provider->tile_height)
{
$tileCoord = $rowCoord->copy();
for($x = $corner->x; $x < $this->dimensions->x; $x += $this->provider->tile_width)
{
$tiles[] = array($this->provider->getTileURLs($tileCoord), new MMaps_Point($x, $y));
$tileCoord = $tileCoord->right();
}
$rowCoord = $rowCoord->down();
}
return MMaps_renderTiles($this->provider, $tiles, $this->dimensions->x, $this->dimensions->y);
}
}
function MMaps_renderTiles($provider, $tiles, $width, $height)
{
$img = imagecreatetruecolor($width, $height);
foreach($tiles as $tile)
{
list($urls, $position) = $tile;
foreach($urls as $url)
{
$tile = @imagecreatefromstring(@file_get_contents($url));
error_log("MMaps_renderTiles: {$url}");
if($tile !== false)
@imagecopy($img, $tile, $position->x, $position->y, 0, 0, imagesx($tile), imagesy($tile));
}
}
return $img;
}
function MMaps_calculateMapCenter($provider, $centerCoord)
{
// initial tile coordinate
$initTileCoord = $centerCoord->container();
// initial tile position, assuming centered tile well in grid
$initX = ($initTileCoord->column - $centerCoord->column) * $provider->tile_width;
$initY = ($initTileCoord->row - $centerCoord->row) * $provider->tile_height;
$initPoint = new MMaps_Point(round($initX), round($initY));
return array($initTileCoord, $initPoint);
}
function MMaps_calculateMapExtent($provider, $width, $height, $locations)
{
list($min_row, $min_column, $min_zoom) = array(INF, INF, INF);
list($max_row, $max_column, $max_zoom) = array(-INF, -INF, -INF);
foreach($locations as $location)
{
$coordinate = $provider->locationCoordinate($location);
$min_row = min($min_row, $coordinate->row);
$min_column = min($min_column, $coordinate->column);
$min_zoom = min($min_zoom, $coordinate->zoom);
$max_row = max($max_row, $coordinate->row);
$max_column = max($max_column, $coordinate->column);
$max_zoom = max($min_zoom, $coordinate->zoom);
}
$TL = new MMaps_Coordinate($min_row, $min_column, $min_zoom);
$BR = new MMaps_Coordinate($max_row, $max_column, $max_zoom);
// multiplication factor between horizontal span and map width
$hFactor = ($BR->column - $TL->column) / ($width / $provider->tile_width);
// multiplication factor expressed as base-2 logarithm, for zoom difference
$hZoomDiff = log($hFactor) / log(2);
// possible horizontal zoom to fit geographical extent in map width
$hPossibleZoom = $TL->zoom - ceil($hZoomDiff);
// multiplication factor between vertical span and map height
$vFactor = ($BR->row - $TL->row) / ($height / $provider->tile_height);
// multiplication factor expressed as base-2 logarithm, for zoom difference
$vZoomDiff = log($vFactor) / log(2);
// possible vertical zoom to fit geographical extent in map height
$vPossibleZoom = $TL->zoom - ceil($vZoomDiff);
// initial zoom to fit extent vertically and horizontally
$initZoom = min($hPossibleZoom, $vPossibleZoom);
# coordinate of extent center
$centerRow = ($TL->row + $BR->row) / 2;
$centerColumn = ($TL->column + $BR->column) / 2;
$centerZoom = ($TL->zoom + $BR->zoom) / 2;
$centerCoord = new MMaps_Coordinate($centerRow, $centerColumn, $centerZoom);
$centerCoord = $centerCoord->zoomTo($initZoom);
return MMaps_calculateMapCenter($provider, $centerCoord);
}
function MMaps_mapByCenterZoom($provider, $center, $zoom, $dimensions)
{
$centerCoord = $provider->locationCoordinate($center)->zoomTo($zoom);
list($mapCoord, $mapOffset) = MMaps_calculateMapCenter($provider, $centerCoord);
return new Modest_Map($provider, $dimensions, $mapCoord, $mapOffset);
}
function MMaps_mapByExtent($provider, $locationA, $locationB, $dimensions)
{
list($mapCoord, $mapOffset) = MMaps_calculateMapExtent($provider, $dimensions->x, $dimensions->y, array($locationA, $locationB));
return new Modest_Map($provider, $dimensions, $mapCoord, $mapOffset);
}
function MMaps_mapByExtentZoom($provider, $locationA, $locationB, $zoom)
{
// a coordinate per corner
$coordA = $provider->locationCoordinate($locationA)->zoomTo($zoom);
$coordB = $provider->locationCoordinate($locationB)->zoomTo($zoom);
// precise width and height in pixels
$width = abs($coordA->column - $coordB->column) * $provider->tile_width;
$height = abs($coordA->row - $coordB->row) * $provider->tile_height;
// nearest pixel actually
$dimensions = new MMaps_Point(floor($width), floor($height));
// projected center of the map
$centerCoord = new MMaps_Coordinate(($coordA->row + $coordB->row) / 2,
($coordA->column + $coordB->column) / 2,
$zoom);
list($mapCoord, $mapOffset) = MMaps_calculateMapCenter($provider, $centerCoord);
return new Modest_Map($provider, $dimensions, $mapCoord, $mapOffset);
}
?>