of class "nztopo", see http://www.topomap.co.nz/. */ function NZTopo_Parse($p1, $p2, $p3, $p4, $p5, $p6, $p7, $p8, $p9) { # global $NZTopoZoom, $NZTopoNewwin; global $debugon; # for debug // Initialise variables $retval = ''; # return value $opt = ParseArgs($p1 . ' ' . $p2 . ' ' . $p3 . ' ' . $p4 . ' ' . $p5 . ' ' . $p6 . ' ' . $p7 . ' ' . $p8 . ' ' . $p9); $version = 2; # fixed version of nztopomap interface $newwin = $NZTopoNewwin; # open map in new window $ppcm = 80; # 80 pixels per centrimetre to provide base scale for maps $width = $opt['width']; # iframe parameter $height = $opt['height']; # iframe parameter $float = $opt['float'] ? 'float:' . $opt['float'] . ';' : ""; # div parameter $clear = $opt['clear'] ? 'clear:' . $opt['clear'] . ';' : ""; # div parameter $pdiv = $clear . $float ? ' style="' . $clear . $float . '"' : ""; # div positioning $debugval = $debugon ? '
' : ''; // query string parameters $bllbs = (boolean) $opt['llbs']; if( $bllbs ) { # get size of map in km list ($widthkm, $heightkm, $dbgval) = llbskm($opt['llbs']); $debugval .= $dbgval; $width = max ($widthkm * $ppcm, $width); # override height $height = max ($heightkm * $ppcm, $width); # override height } $bnzbs = (boolean) $opt['nzbs']; if( $bnzbs ) { # get size of map in km list ($widthkm, $heightkm, $dbgval) = nzbskm($opt['nzbs']); $debugval .= $dbgval; $width = max ($widthkm * $ppcm, $width); # override height $height = max ($heightkm * $ppcm, $width); # override height } $query = ''; $query .= '&new=' . $newwin; $query .= $opt['ll'] ? '&ll=' . $opt['ll'] : ""; $query .= $opt['llbs'] ? '&llbs=' . $opt['llbs'] : ""; $query .= $opt['topo50'] ? '&topo50=' . $opt['topo50'] : ""; $query .= $opt['nzbs'] ? '&nzbs=' . $opt['nzbs'] : ""; $query .= $opt['nzne'] ? '&nzne=' . $opt['nzne'] : ""; $query .= $opt['kml'] ? '&kml=' . rawurlencode($opt['kml']) : ""; $query .= $opt['gpx'] ? '&gpx=' . rawurlencode($opt['gpx']) : ""; $query .= $opt['mapref'] ? '&mapref=' . $opt['mapref'] : ""; $query .= $opt['pin'] ? '&pin=' . $opt['pin'] : ""; $query .= $opt['label'] ? '&lbl=' . rawurlencode(htmlentities($opt['label'])) : ""; # double url encode unicode chars for them to work with the topo map $query .= $opt['zoom'] ? '&z=' . $opt['zoom'] : '&z=' . $NZTopoZoom; $awidth = $width ? ' width="' . $width . '"' : ""; # iframe parameter $aheight = $height ? ' height="' . $height . '"' : ""; # iframe parameter $src = 'http://www.topomap.co.nz/NZTopoMapEmbedded?&v=' . $version . $query; $retval .= '
' . "\n"; $retval .= ''; $retval .= '
' . "\n"; /* example HTML for nztopomap */ if( $debugon ) { $debugval .= 'p1=' . $p1 . ' p2=' . $p2 . ' p3=' . $p3 . ' p4=' . $p4 . ' p5=' . $p5 . ' p6=' . $p6 . ' p7=' . $p7 . ' p8=' . $p8 . ' p9=' . $p9 . '
' . "\n"; $debugval .= 'src="' . $version . $query . '"
' . "\n"; } return $retval . $debugval; } # NZTopo_Parse function llbskm($llbs) { global $debugon; # for debug $dbgval = ''; $latlngs = preg_split ('/[,;]/', $llbs); $lats = array(); # eastings $lngs = array(); # northings foreach ($latlngs as $k => $v) { $k % 2 == 0 ? $lats [] = $v : $lngs [] = $v; } $maxlat = max ($lats); $maxlng = max ($lngs); $minlat = min ($lats); $minlng = min ($lngs); /* rectangle coordinates are (lat/long) left: right: top: max/min max/max bot: min/min min/max */ $width1 = distance ($maxlat, $minlng, $maxlat, $maxlng); $width2 = distance ($minlat, $minlng, $minlat, $maxlng); $height1 = distance ($maxlat, $minlng, $minlat, $minlng); $height2 = distance ($maxlat, $maxlng, $minlat, $maxlng); $widthkm = ceil (max ($width1, $width2) * 10) / 10; # take largest width and round up to km $heightkm = ceil (max ($height1, $height2) * 10) / 10; # take largest height and round up to km if( $debugon ) { $dbgval .= 'lats="' . var_export ($lats, TRUE) . '"
' . "\n"; $dbgval .= 'lngs="' . implode ('", "', $lngs) . '"
' . "\n"; $dbgval .= 'minlat=' . $minlat . ', maxlat=' . $maxlat . ', minlng=' . $minlng . ', maxlng=' . $maxlng . '
' . "\n"; $dbgval .= 'w1=' . $width1 . ', w2=' . $width2 . ', h1=' . $height1 . ', h2=' . $height2 . ', w=' . $widthkm . 'km, h=' . $heightkm . 'km
' . "\n"; } return array ($widthkm, $heightkm, $dbgval); } function nzbskm($nzbs) { global $debugon; # for debug $dbgval = ''; $nthsests = preg_split ('/[,;]/', $nzbs); # http://www.linz.govt.nz/topography/topo-maps/topo50/prototype/map-reading-references $nths = array(); # northings $ests = array(); # eastings foreach ($nthsests as $k => $v) { $k % 2 == 0 ? $nths [] = $v : $ests [] = $v; } $maxnth = max ($nths); $maxest = max ($ests); $minnth = min ($nths); $minest = min ($ests); /* rectangle coordinates are (nth/est) left: right: top: max/min max/max bot: min/min min/max */ $width1 = ($maxest - $minest) / 1000; # metres to km $height1 = ($maxnth - $minnth) / 1000; # metres to km $widthkm = ceil ($width1 * 10) / 10; # round up to 0.1 km $heightkm = ceil ($height1 * 10) / 10; # round up to 0.1 km if( $debugon ) { $dbgval .= 'nthsests="' . implode ('", "', $nthsests) . '"
' . "\n"; $dbgval .= 'ests="' . var_export ($ests, TRUE) . '"
' . "\n"; $dbgval .= 'nths="' . implode ('", "', $nths) . '"
' . "\n"; $dbgval .= 'minest=' . $minest . ', maxest=' . $maxest . ', minnth=' . $minnth . ', maxnth=' . $maxnth . '
' . "\n"; $dbgval .= 'w1=' . $width1 . ', h1=' . $height1 . ', w=' . $widthkm . 'km, h=' . $heightkm . 'km
' . "\n"; } return array ($widthkm, $heightkm, $dbgval); } # http://snipplr.com/view/2531/ function distance($lat1, $lng1, $lat2, $lng2) { $pi80 = M_PI / 180; $lat1 *= $pi80; $lng1 *= $pi80; $lat2 *= $pi80; $lng2 *= $pi80; $r = 6372.797; // mean radius of Earth in km $dlat = $lat2 - $lat1; $dlng = $lng2 - $lng1; $a = sin($dlat / 2) * sin($dlat / 2) + cos($lat1) * cos($lat2) * sin($dlng / 2) * sin($dlng / 2); $c = 2 * atan2(sqrt($a), sqrt(1 - $a)); $km = $r * $c; return $km; } # distance