User:SignpostMarv Martin/PHP/SLOpenID SL Maps API

From Second Life Wiki
Jump to navigation Jump to search

This API wrapper requires PHP 5.2.0 upwards, as it uses the json_decode() functions. It's statically based, however you do need to call new slmapapi(); in order initiate the CURL object for staticy goodness.

The only documentation available currently is over at http://blogs.slopenid.net/blog/2007/04/12/slopenid-sl-maps-api/

In case you're wondering what the API wrapper is capable of, take a peak at http://dev.slopenid.net/sl-map-fu/webmap/

SignpostMarv Martin

<?php
# The SLOpenID SL Maps API uses Sean Huber's CURL library
# Don't forget to set CURLOPT_HEADER to FALSE
# http://uk.php.net/manual/en/ref.curl.php#65700
class slmapapi
{
	protected static $slmapapi_root = 'http://services.slopenid.net/grid-map-json/';
	protected static $curl;
	function slmapapi()
	{
		slmapapi::$curl = new CURL();
	}
	public static function regionexists($region)
	{
		return json_decode(self::$curl->doRequest('GET',self::$slmapapi_root . 'regionexists/' . rawurlencode($region) . '/',NULL));
	}
	public static function region2offset($region)
	{
		return json_decode(self::$curl->doRequest('GET',self::$slmapapi_root . 'region2offset/' . rawurlencode($region) . '/',NULL));
	}
	public static function region2coord($region)
	{
		return json_decode(self::$curl->doRequest('GET',self::$slmapapi_root . 'region2coord/' . rawurlencode($region) . '/',NULL));
	}
	public static function offset2region($x,$y)
	{
		return json_decode(self::$curl->doRequest('GET',self::$slmapapi_root . 'offset2region/' . $x . '/' . $y . '/',NULL)); 
	}
	public static function coord2region($x,$y)
	{
		return json_decode(self::$curl->doRequest('GET',self::$slmapapi_root . 'coord2region/' . $x . '/' . $y . '/',NULL)); 
	}
	public static function mapsegment($focus_x,$focus_y,$range_x,$range_y,$scale)
	{
		return json_decode(self::$curl->doRequest('GET',self::$slmapapi_root . 'mapsegment/' . $focus_x . ',' . $focus_y . '/' . $range_x . '/' . $range_y . '/' . $scale . '/',NULL));
	}
}
class slmapapi_fu
{
	protected static $composite_url_prefix_length = 48;	#	Length of http://services.slopenid.net/grid-map-composite/
	public static function calc_segment_layout($segment)
	{
		$layout = array();
		foreach($segment as $k => $file)
		{
			list($y_range,$x_range) = explode(' ',substr($file,strpos($file,'/',self::$composite_url_prefix_length)+1,-5));	# -5 due to length of '.jpeg
			$layout[$y_range][$x_range] = $file;
		}
		foreach($layout as $y => $x)
		{
			natsort($layout[$y]);
		}
		$layout = array_reverse($layout);
		return $layout;
	}
	public static function calc_segment_size(&$segment)
	{
		$layout = self::calc_segment_layout($segment);
		return array(count(current($layout))*256,count($layout)*256);
	}
	public static function calc_prime_beacon_loc(&$region,&$scale,&$segment,&$x_dest,&$y_dest)
	{
		list($x,$y) = explode(',',slmapapi::region2offset($region));
		list($table_width,$table_height) = self::calc_segment_size($segment);
		$layout = self::calc_segment_layout($segment);
		$y_range_top = key($layout);
		$x_range_top = key(current($layout));
		unset($layout);
		list($foo,$top_y) = explode('-',$y_range_top);
		list($bottom_x,$foo) = explode('-',$x_range_top);

		$chunk = 256 / $scale;
		$y_pos = ((($top_y - $y) * $chunk) - 8) + ($chunk - ($y_dest/$scale));
		$x_pos = ((($x - $bottom_x) * $chunk) - 8) + ($x_dest/$scale);
		return array($x_pos,$y_pos);
	}
	public static function gen_xhtml_table_from_segment(&$segment)
	{
		$layout = self::calc_segment_layout($segment);
		$table = <<<EOT
<table cellpadding="0" cellspacing="0">

EOT;
		$y_loop = count($layout);
		foreach($layout as $y => $col)
		{
			$table .= <<<EOT
	<tr height="256px">

EOT;
			foreach($col as $x => $file)
			{
				$table .= <<<EOT
		<td><img src="$file" /></td>

EOT;
			}
			$table .= <<<EOT
	</tr>

EOT;
		}
		$table .= <<<EOT
</table>
EOT;
		return $table;
	}
	public static function gen_xhtml_div_from_segment(&$segment)
	{
		$layout = self::calc_segment_layout($segment);
		list($foo,$y_end) = explode('-',key($layout));end($layout);
		$scale = ($y_end - $foo)+1;
		list($y_start,$foo) = explode('-',key($layout)); reset($layout);
		list($x_start,$foo) = explode('-',key(current($layout))); end(current($layout));
		list($foo,$x_end) = explode('-',key(current($layout))); reset(current($layout));
		$divs = '';
		$y = 0;
		foreach($layout as $y_range => $row)
		{
			$x = 0;
			foreach($row as $x_range => $file)
			{
				$id = "x$x_range";
				$id .= "_y$y_range";
				$top = ($y * 256) . 'px';
				$left = ($x * 256) . 'px';
				$divs .= <<<EOT
<div class="region-tile" id="$id" style="position:absolute;top:$top;left:$left;width:256px;height:256px;"><img src="$file" alt="SL Regions ranging from x$x_range, y$y_range" /></div>

EOT;
				++$x;
			}
			++$y;
		}
		return $divs;
	}
}
?>