of class "nztopo", see http://www.topomap.co.nz/.
*/
function NZTopo_Parse($p1, $p2, $p3, $p4, $p5, $p6, $p7, $p8, $p9) {
#
global $NZTopoZoom, $NZTopoNewwin; # configuration variables from configuration file, e.g. config.php
global $debugon; # for debug
// Initialise variables
$retval = ''; # return value
$opt = ParseArgs($p1 . ' ' . $p2 . ' ' . $p3 . ' ' . $p4 . ' ' . $p5 . ' ' . $p6 . ' ' . $p7 . ' ' . $p8 . ' ' . $p9); # see pmWiki documentation
$src_url = 'http://www.topomap.co.nz/NZTopoMap'; # URL for browser service
$src_urlemb = 'http://www.topomap.co.nz/NZTopoMapEmbedded'; # URL for embedded service
$src_proxy = 'http://www.topomap.co.nz/proxy.ashx?'; # URL for gpx proxy
$version = 'v=2'; # fixed version of nztopomap interface
$newwin = $NZTopoNewwin; # open map in new window
//
## need dpi to calculate size of map to ask for, have not been told what topomap generates
$ppcm = 80; # 80 pixels per centimetre to provide base scale for maps (guess)
//
$width = $opt['width']; # iframe parameter
$height = $opt['height']; # iframe parameter
$float = (bool) $opt['float'] ? 'float:' . $opt['float'] . ';' : ''; # div parameter
$clear = (bool) $opt['clear'] ? 'clear:' . $opt['clear'] . ';' : ''; # div parameter
$pdiv = (bool) $clear . $float ? ' style="' . $clear . $float . '"' : ''; # div positioning
$debugval = $debugon ? '
' : ''; # initialise
$dbgval = ''; # initialise
## 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
}
## build query string (i.e. topomap API)
$query = ''; # initialise
$query .= (bool) $opt['ll'] ? '&ll=' . $opt['ll'] : '';
$query .= (bool) $opt['llbs'] ? '&llbs=' . $opt['llbs'] : '';
$query .= (bool) $opt['topo50'] ? '&topo50=' . $opt['topo50'] : '';
$query .= (bool) $opt['nzbs'] ? '&nzbs=' . $opt['nzbs'] : '';
$query .= (bool) $opt['nzne'] ? '&nzne=' . $opt['nzne'] : '';
//
# trim URLs for spaces and quotes; rawurldecode URLs before rawurlencode them for nztopomap
# rawurldecode resets any pasted url to a 'known' state
//$query .= (bool) $opt['kml'] ? '&kml=' . rawurlencode(htmlentities(rawurldecode(trim($opt['kml'], ' \'\"')))) : ''; # does not comply with topomap API
$query .= (bool) $opt['kml'] ? '&kml=' . encode_path($opt['kml'], $dbgval) : ''; # call function to comply with topomap API
$debugval .= (bool) $opt['kml'] ? $dbgval : ''; # debug output
if( $debugon ) { # apply different combinations of encoding for match with topomap API
# kml filename double rawurlencode (for debug and testing only)
$query .= (bool) $opt['kml2'] ? '&kml=' . rawurlencode(rawurlencode(htmlentities(rawurldecode(trim($opt['kml2'], ' \'\"'))))) : '';
# kml filename single rawurlencode with no rawurldecode (for debug and testing only)
$query .= (bool) $opt['kml3'] ? '&kml=' . rawurlencode(htmlentities(trim($opt['kml3'], ' \'\"'))) : '';
# kml filename double rawurlencode with no rawurldecode (for debug and testing only)
$query .= (bool) $opt['kml4'] ? '&kml=' . rawurlencode(rawurlencode(htmlentities(trim($opt['kml4'], ' \'\"')))) : '';
} # debugon
// note gpx works fine
$query .= (bool) $opt['gpx'] ? '&gpx=' . rawurlencode($src_proxy . htmlentities(rawurldecode(trim($opt['gpx'], ' \'\"')))) : '';
//
$query .= (bool) $opt['mapref'] ? '&mapref=' . $opt['mapref'] : '';
$query .= (bool) $opt['pin'] ? '&pin=' . $opt['pin'] : '';
$query .= (bool) $opt['label'] ? '&lbl=' . htmlentities(trim($opt['label'])) : ''; # trin spaces, encode special chars for them to work with the topo map
$query .= (bool) $opt['zoom'] ? '&z=' . $opt['zoom'] : '&z=' . $NZTopoZoom;
$query .= '&new=' . $newwin; # set open in new window option
$awidth = (bool) $width ? ' width="' . $width . '"' : ''; # iframe parameter
$aheight = (bool) $height ? ' height="' . $height . '"' : ''; # iframe parameter
$src = $src_urlemb . '?' . $version . $query;
$retval .= '
' . "\n";
$retval .= '';
$retval .= '
' . "\n";
/* example query string for nztopomap http://www.topomap.co.nz/NZTopoMap or http://www.topomap.co.nz/NZTopoMapEmbedded
?v=2&ll=-41.293722,174.871482&z=15&pin=1
?v=2&kml=http%3A%2F%2Fkiwiwiki.co.nz%2Fpmwiki%2Fuploads%2FTest%2FNZTopo-Attach%2F29%2520May%25202015%252020_01_35.kml
?v=2&gpx=http%3A%2F%2Fwww.topomap.co.nz%2Fproxy.ashx%3Fhttp%3A%2F%2Fkiwiwiki.co.nz%2Fpmwiki%2Fuploads%2FTest%2FNZTopo-Attach%2F20150531.gpx
*/
if( $debugon ) { # display inputs and outputs to wiki page
$debugval .= '
input: p1=' . $p1 . ' p2=' . $p2 . ' p3=' . $p3 . ' p4=' . $p4 . ' p5=' . $p5 . ' p6=' . $p6 . ' p7=' . $p7 . ' p8=' . $p8 . ' p9=' . $p9 . '' . "\n";
$debugval .= '
output: src="' . '
' . $version . $query . '';
$debugval .= '"
' . "\n";
} # debugon
return $retval . $debugval;
} # NZTopo_Parse
function encode_path($kml_url, &$dbgval) {
global $debugon; # for debug
# for the kml parameter the topomap API is inconsistent. The kml path is urlencoded separately to the full url string
// specifically rawurlencode($src_proxy . rawurldecode(trim(filename.gpx, ' \'\"'))) works,
// but rawurlencode( rawurldecode(trim(filename.kml, ' \'\"'))) does not
$temp_url = # decode for parse_url and to start with known state (user may for example provide spaces in the URL string)
htmlentities( # protect against injection attacks
rawurldecode(trim($kml_url, ' \'\"'))); # trim single and double quotes and spaces
$parsed_url = parse_url($temp_url); # extract url components
$dbgval .= $debugon ? ' path="' . $parsed_url['path'] . '" ' . "\n" : ''; # debug output
$parsed_url['path'] = rawurlencode(substr($parsed_url['path'], 1)); # encode path separately again, dropping leading '/'
$dbgval .= $debugon ? ' enc_path="' . $parsed_url['path'] . '" ' . "\n" : ''; # debug output
$enc_url = ''; # reconstruct URL
$enc_url .= (bool) $parsed_url['scheme'] ? $parsed_url['scheme'] . '://' : ''; # aka http or https
$enc_url .= (bool) $parsed_url['host'] ? $parsed_url['host'] : ''; # domain name
$enc_url .= (bool) $parsed_url['port'] ? ':' . $parsed_url['port'] : ''; # port
$enc_url .= (bool) $parsed_url['path'] ? '/' . $parsed_url['path'] : ''; # add '/' dropped before as topomap considers it part of the host
# query and fragment omitted
return rawurlencode ($enc_url); # finally encode url and return it
} # encode_path
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";
} # debugon
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";
} # debugon
return array ($widthkm, $heightkm, $dbgval);
}
# http://snipplr.com/view/2531/
function distance($lat1, $lng1, $lat2, $lng2)
# convert distance in km between two lat longs
{
$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