Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Semantic Maps

, 1 Aug 2009 GPL3
A MediaWiki extension that allows you to insert, edit, view and aggregate coordinate data
MapsAndSemanticMaps0.2.zip
SemanticMaps
COPYING
GoogleMaps
INSTALL
OpenLayers
README
RELEASE-NOTES
YahooMaps
Maps
COPYING
Geocoders
GoogleMaps
INSTALL
OpenLayers
OpenLayers
img
blank.gif
cloud-popup-relative.png
drag-rectangle-off.png
drag-rectangle-on.png
east-mini.png
layer-switcher-maximize.png
layer-switcher-minimize.png
marker-blue.png
marker-gold.png
marker-green.png
marker.png
measuring-stick-off.png
measuring-stick-on.png
north-mini.png
panning-hand-off.png
panning-hand-on.png
slider.png
south-mini.png
west-mini.png
zoom-minus-mini.png
zoom-plus-mini.png
zoom-world-mini.png
zoombar.png
theme
default
img
add_point_off.png
add_point_on.png
blank.gif
close.gif
drag-rectangle-off.png
drag-rectangle-on.png
draw_line_off.png
draw_line_on.png
draw_point_off.png
draw_point_on.png
draw_polygon_off.png
draw_polygon_on.png
editing_tool_bar.png
move_feature_off.png
move_feature_on.png
navigation_history.png
overview_replacement.gif
pan-panel-NOALPHA.png
pan-panel.png
panning-hand-off.png
panning-hand-on.png
pan_off.png
pan_on.png
remove_point_off.png
remove_point_on.png
ruler.png
save_features_off.png
save_features_on.png
view_next_off.png
view_next_on.png
view_previous_off.png
view_previous_on.png
zoom-panel-NOALPHA.png
zoom-panel.png
OSM
README
RELEASE-NOTES
YahooMaps
<?php

/**
 * A class that holds handlers for the mapping parser functions
 *
 * @file Maps_Mapper.php
 * @ingroup Maps
 *
 * @author Jeroen De Dauw
 */

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

final class MapsMapper {

	/**
	 * Sets the default map properties and returns the new array.
	 *
	 * @param array $params Array containing the current set of pareters.
	 * @param boolean $strict If set to false, values which a key that does not
	 * exist in the $map array will be retained.
	 * @return unknown
	 */
	public static function setDefaultParValues(array $params, $strict) {
		global $egMapsMapCoordinates, $egMapsMapWidth, $egMapsMapHeight, $egMapsEnableAutozoom, $egMapsEnableEarth;
		
		$map = array(
			'service' => '',
			'coordinates' => $egMapsMapCoordinates,
			'zoom' => '',
			'centre' => '',	
			'type' => '',
			'width' => "$egMapsMapWidth",
			'height' => "$egMapsMapHeight",
			'controls' => array(),
			'autozoom' => $egMapsEnableAutozoom ? 'on' : 'off',
			'earth' => $egMapsEnableEarth ? 'on' : 'off',
			'class' => 'pmap',
			'layers' => array(),	
			'baselayer' => '',
			'title' => '',
			'label' => '',
			'style' => ''	
			);
		
		foreach($params as $paramName => $paramValue) {
			if(array_key_exists($paramName, $map) || !$strict) $map[$paramName] = $paramValue;
		}
		
		// Alias for centre
		if(array_key_exists('center', $params)) $map['centre'] = $params['center'];
		
		self::enforceArrayValues($map['controls']);
		self::enforceArrayValues($map['layers']);
		
		return $map;
	}
	
	public static function enforceArrayValues(&$values, $delimeter = ',') {
		if (!is_array($values)) $values = split($delimeter, $values); // If not an array yet, split the values
		for ($i = 0; $i < count($values); $i++) $values[$i] = trim($values[$i]); // Trim all values
	}
	
	public static function createControlsString(array $controls, array $defaultControls) {
		if (count($controls) < 1) $controls = $defaultControls;
		$controlItems = '';
		foreach ($controls as $control) $controlItems .= "'$control'" . ',';
		
		return strtolower(rtrim($controlItems, ','));
	}			
	
	/**
	 * Sets the default map properties, gets the map HTML depending 
	 * on the provided service, and then returns it.
	 *
	 * @param unknown_type $parser
	 * @return unknown
	 */
	public static function displayPointRender(&$parser) {
		$params = func_get_args();
		array_shift( $params ); // We already know the $parser ...
		
		if (is_array($params[0])) $params = $params[0];
				
		$map = array();
		
		foreach($params as $param) {
			$split = split('=', $param);
			if (count($split) == 2) {
				$paramName = strtolower(trim($split[0]));
				$paramValue = trim($split[1]);
				$map[$paramName] = $paramValue;
			}
			if (count($split) == 1) { // Default parameter (without name)
				$map['coordinates'] = trim($split[0]);
			}
		}
		
		$map = self::setDefaultParValues($map, true);

		$map['service'] = self::getValidService($map['service']);

		
		switch($map['service']) {
			case 'openlayers' :
				$mapClass = new MapsOpenLayers();
				break;
			case 'yahoomaps' : 
				$mapClass = new MapsYahooMaps();
				break;	
			default:
				$mapClass = new MapsGoogleMaps();
				break;
		}

		// Call the function according to the map service to get the HTML output
		$output = $mapClass->displayMap($parser, $map);
		
		// Return the result
		return array( $output, 'noparse' => true, 'isHTML' => true );
	}

	/**
	 * Turns the address parameter into coordinates, then lets 
	 * @see MapsMapper::displayPointRender() do the work and returns it.
	 *
	 * @param unknown_type $parser
	 * @return unknown
	 */
	public static function displayAddressRender(&$parser) {		
		// TODO: refactor to reduce redundancy and improve performance
		global $egMapsDefaultService;
		
		$params = func_get_args();
		array_shift( $params ); // we already know the $parser ...
		
		for ($i = 0; $i < count($params); $i++) {
			$split = split('=', $params[$i]);
			if (strtolower(trim($split[0])) == 'service' && count($split) > 1) {
				$service = trim($split[1]);
				//echo "<!-- ||| $service -->";
			}
		}

		
		$service = isset($service) ? MapsMapper::getValidService($service) : $egMapsDefaultService;
		
		$geoservice = '';
		
		for ($i = 0; $i < count($params); $i++) {
			$split = split('=', $params[$i]);
			if (strtolower(trim($split[0])) == 'geoservice' && count($split) > 1) {
				$geoservice = trim($split[1]);
			}
		}	
		
		for ($i = 0; $i < count($params); $i++) {
			$split = split('=', $params[$i]);
			if (strtolower(trim($split[0])) == 'address' && count($split) > 1) {
				$params[$i] = 'coordinates=' . MapsGeocoder::renderGeocoder($parser, trim($split[1]), $geoservice, $service);
			}
			if (count($split) == 1) { // Default parameter (without name)
				$params[$i] = 'coordinates=' . MapsGeocoder::renderGeocoder($parser, trim($split[0]), $geoservice, $service);
			}
		}
		
		return self::displayPointRender($parser, $params);
	}
	
	/**
	 * Returns a valid service. When an invalid service is provided, the default one will be returned.
	 * Aliases are also chancged into the main service names @see MapsMapper::getMainServiceName().
	 *
	 * @param unknown_type $service
	 * @return unknown
	 */
	public static function getValidService($service) {
		global $egMapsAvailableServices, $egMapsDefaultService;
		
		$service = self::getMainServiceName($service);
		if(!in_array($service, $egMapsAvailableServices)) $service = $egMapsDefaultService;
		
		return $service;
	}
	
	/**
	 * Checks if the service name is an allias for an actual service,
	 * and changes it into the main service name if this is the case.
	 *
	 * @param unknown_type $service
	 * @return unknown
	 */
	public static function getMainServiceName($service) {
		global $egMapsServices;
		
		if (!array_key_exists($service, $egMapsServices)) {
			foreach ($egMapsServices as $serviceName => $aliasList) {
				if (in_array($service, $aliasList)) {
					 $service = $serviceName;
				}
			}
		}
		
		return $service;
	}
}

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)

Share

About the Author

Jeroen De Dauw
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
Follow on   Twitter

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141220.1 | Last Updated 1 Aug 2009
Article Copyright 2009 by Jeroen De Dauw
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid