Click here to Skip to main content
15,881,248 members
Articles / Database Development / MySQL

Semantic Maps

Rate me:
Please Sign up or sign in to vote.
4.55/5 (5 votes)
1 Aug 2009GPL313 min read 36.8K   1.4K   23  
A MediaWiki extension that allows you to insert, edit, view and aggregate coordinate data
<?php

/**
 * Abstract class that provides the common functionallity for all map query printers
 *
 * @file SM_MapPrinter.php
 * @ingroup SemanticMaps
 *
 * @author Jeroen De Dauw
 * @author Robert Buzink
 * @author Yaron Koren
 */

if( !defined( 'MEDIAWIKI' ) ) {
	die( 'Not an entry point.' );
}

abstract class SMMapPrinter extends SMWResultPrinter {
	// TODO: make class and child's more OOP, in a way similair to class MapsBaseMap in Maps

	protected $m_locations = array();
	
	public function getResult($results, $params, $outputmode) {
		// Skip checks, results with 0 entries are normal
		$this->readParameters($params, $outputmode);
		return $this->getResultText($results, SMW_OUTPUT_HTML);
	}
	
	protected function getResultText($res, $outputmode) {
		while ( ($row = $res->getNext()) !== false ) {
			$this->addResultRow($outputmode, $row);
		}
		
		$this->m_params = MapsMapper::setDefaultParValues($this->m_params, true);
	}
	
	/**
	 * This function will loop through all properties (fields) of one record (row),
	 * and add the location data, title, label and icon to the m_locations array.
	 *
	 * @param unknown_type $outputmode
	 * @param unknown_type $row The record you want to add data from
	 */
	private function addResultRow($outputmode, $row) {
		global $wgUser;
		$skin = $wgUser->getSkin();		
		
		$title = '';
		$text = '';
		$lat = '';
		$lon = '';		
		
		// Loop throught all fields of the record
		foreach ($row as $i => $field) {
			$pr = $field->getPrintRequest();
			
			// Loop throught all the parts of the field value
			while ( ($object = $field->getNextObject()) !== false ) {
				if ($object->getTypeID() == '_wpg' && $i == 0) {
					$title = $object->getLongText($outputmode, $skin);
				}
				
				if ($object->getTypeID() != '_geo' && $i != 0) {
					$text .= $pr->getHTMLText($skin) . ": " . $object->getLongText($outputmode, $skin) . "<br />";
				}
		
				if ($pr->getMode() == SMWPrintRequest::PRINT_PROP && $pr->getTypeID() == '_geo') {
					list($lat,$lon) = explode(',', $object->getXSDValue());
				}
			}
		}
		
		if (strlen($lat) > 0 && strlen($lon)  > 0) {
			$icon = $this->getLocationIcon($row);
			$this->m_locations[] = array($lat, $lon, $title, $text, $icon);
		}
	}
	
	/**
	 * Get the icon for a row
	 *
	 * @param unknown_type $row
	 * @return unknown
	 */
	private function getLocationIcon($row) {
		$icon = '';
		$legend_labels = array();
		
		// Look for display_options field, which can be set by Semantic Compound Queries
		if (property_exists($row[0], 'display_options')) {
			if (array_key_exists('icon', $row[0]->display_options)) {
				$icon = $row[0]->display_options['icon'];

				// This is somewhat of a hack - if a legend label has been set, we're getting it for every point, instead of just once per icon	
				if (array_key_exists('legend label', $row[0]->display_options)) {
									
					$legend_label = $row[0]->display_options['legend label'];
					
					if (! array_key_exists($icon, $legend_labels)) {
						$legend_labels[$icon] = $legend_label;
					}
				}
			}
		// Icon can be set even for regular, non-compound queries If it is, though, we have to translate the name into a URL here	
		} elseif (array_key_exists('icon', $this->m_params)) {
	
			$icon_title = Title::newFromText($this->m_params['icon']);
			$icon_image_page = new ImagePage($icon_title);
			$icon = $icon_image_page->getDisplayedFile()->getURL();
		}	

		return $icon;
	}
	
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Software Developer
Belgium Belgium
I am a free and open source software enthusiast and freelance software developer with multiple years of experience in both web and desktop development. Currently my primarily focus is on MediaWiki and Semantic MediaWiki work. I'm in the all time top 10 MediaWiki comitters and am one of the WikiWorks consultants. You can contact me at jeroendedauw at gmail for development jobs and questions related to my work.

More info can be found on my website [0] and my blog [1]. You can also follow me on twitter [2] and identi.ca [3].

[0] http://www.jeroendedauw.com/
[1] http://blog.bn2vs.com/
[2] https://twitter.com/#!/JeroenDeDauw
[3] http://identi.ca/jeroendedauw

Comments and Discussions