Esempio n. 1
0
function process_geomap($MAPCFG, $map_name, &$map_config)
{
    $params = $MAPCFG->getSourceParams();
    list($image_name, $image_path, $data_path) = geomap_files($params);
    // Load the list of locations
    $locations = geomap_get_locations($params);
    // This source does not directly honor the existing map configs. It saves
    // the existing config to use it later for modifying some object parameters.
    // The existing map config must not create new objects. The truth about the
    // existing objects comes only from this source.
    $saved_config = $map_config;
    $map_config = array();
    $iconset = $params['iconset'];
    list($icon_w, $icon_h) = iconset_size($iconset);
    // Adapt the global section
    $map_config[0] = $saved_config[0];
    $map_config[0]['map_image'] = $image_name . '?' . time() . '.png';
    $map_config[0]['iconset'] = $iconset;
    // Now add the objects to the map
    foreach ($locations as $loc) {
        $object_id = $MAPCFG->genObjId($loc['name']);
        $map_config[$object_id] = array('type' => 'host', 'host_name' => $loc['name'], 'iconset' => $iconset, 'object_id' => $object_id, 'alias' => $loc['alias'], 'lat' => $loc['lat'], 'long' => $loc['long']);
        if (isset($loc['backend_id'])) {
            $map_config[$object_id]['backend_id'] = array($loc['backend_id']);
        }
    }
    unset($locations);
    // Now apply the filters. Though the map can be scaled by the filtered hosts
    process_filter($MAPCFG, $map_name, $map_config, $params);
    // Terminate empty views
    if (count($map_config) <= 1) {
        throw new GeomapError(l('Got empty map after filtering. Terminate rendering geomap.'));
    }
    // Now detect the upper and lower bounds of the locations to display
    // Left/upper and right/bottom
    // north/south
    $min_lat = 90;
    $max_lat = -90;
    // east/west
    $min_long = 180;
    $max_long = -180;
    foreach ($map_config as $obj) {
        if ($obj['type'] == 'global') {
            continue;
        }
        if ($obj['lat'] < $min_lat) {
            $min_lat = $obj['lat'];
        }
        if ($obj['lat'] > $max_lat) {
            $max_lat = $obj['lat'];
        }
        if ($obj['long'] < $min_long) {
            $min_long = $obj['long'];
        }
        if ($obj['long'] > $max_long) {
            $max_long = $obj['long'];
        }
    }
    // Fix equal coordinates (Simply add some space on all sides)
    $min_lat -= $params['geomap_border'];
    $max_lat += $params['geomap_border'];
    $min_long -= $params['geomap_border'];
    $max_long += $params['geomap_border'];
    // FIXME: Too small min/max? What is the minimum bbox size?
    //echo $min_lat . ' - ' . $max_lat. ' - '. $mid_lat.'\n';
    //echo $min_long . ' - ' . $max_long. ' - ' . $mid_long;
    if (!$params['width'] || !$params['height']) {
        throw new GeomapError(l('Missing mandatory "width" and "height" parameters."'));
    }
    // Using this API: http://pafciu17.dev.openstreetmap.org/
    $url = cfg('global', 'geomap_server') . '?module=map' . '&width=' . $params['width'] . '&height=' . $params['height'] . '&type=' . $params['geomap_type'];
    // The geomap zoom seems to be something different than the nagvis zoom. Use
    // the dedicated geomap_zoom parameter
    if (isset($params['geomap_zoom']) && $params['geomap_zoom'] != '') {
        $mid_lat = ($min_lat + $max_lat) / 2;
        $mid_long = ($min_long + $max_long) / 2;
        $url .= '&zoom=' . $params['geomap_zoom'] . '&center=' . $mid_long . ',' . $mid_lat;
    } else {
        $url .= '&bbox=' . $min_long . ',' . $max_lat . ',' . $max_long . ',' . $min_lat;
    }
    //file_put_contents('/tmp/123', $url);
    // Fetch the background image when needed
    if (!file_exists($image_path) || geomap_source_age($params) > filemtime($image_path)) {
        // Allow/enable proxy
        $contents = geomap_get_contents($url);
        file_put_contents($image_path, $contents);
    }
    // Fetch the map bounds when needed
    if (!file_exists($data_path) || geomap_source_age($params) > filemtime($data_path)) {
        // Get the lat/long of the image bounds. The api adds a border area to the
        // generated image. This is good since this makes the outer nodes not touch
        // the border of the image. But this makes calculation of the x/y coords
        // problematic. I found a parameter which tells us the long/lat coordinates
        // of the image bounds.
        // http://pafciu17.dev.openstreetmap.org/?module=map&bbox=6.66748,53.7278,14.5533,51.05&width=1500&height=557&type=osmarender&bboxReturnFormat=csv
        // 2.373046875,54.239550531562,18.8525390625,50.499452103968
        $data_url = $url . '&bboxReturnFormat=csv';
        $contents = geomap_get_contents($data_url);
        if (!$contents || ord($contents[0]) == 137 && ord($contents[1]) == 80 && ord($contents[2]) == 78) {
            // Got an png image as answer - catch this!
            throw new GeomapError(l('Got invalid response from "[U]". This is mostly caused by an unhandled request.', array('U' => $data_url)));
        }
        if (!preg_match('/^-?[0-9]+\\.?[0-9]*,-?[0-9]+\\.?[0-9]*,-?[0-9]+\\.?[0-9]*,-?[0-9]+\\.?[0-9]*$/i', $contents)) {
            throw new GeomapError(l('Got invalid data from "[U]": "[C]"', array('U' => $data_url, 'C' => json_encode($contents))));
        }
        file_put_contents($data_path, $contents);
        $parts = explode(',', $contents);
    } else {
        $parts = explode(',', file_get_contents($data_path));
    }
    $img_left = (double) $parts[0];
    $img_top = (double) $parts[1];
    $img_right = (double) $parts[2];
    $img_down = (double) $parts[3];
    $long_diff = $img_right - $img_left;
    $lat_diff = $img_top - $img_down;
    $long_para = $params['width'] / $long_diff;
    $lat_para = $params['height'] / $lat_diff;
    $lat_mult = $params['height'] / (ProjectF($img_top) - ProjectF($img_down));
    // Now add the coordinates to the map objects
    foreach ($map_config as &$obj) {
        if (!isset($obj['lat'])) {
            continue;
        }
        // Calculate the lat (y) coords
        $obj['y'] = round((ProjectF($img_top) - ProjectF($obj['lat'])) * $lat_mult - $icon_h / 2);
        if ($obj['y'] < 0) {
            $obj['y'] = 0;
        }
        // Calculate the long (x) coords
        $obj['x'] = round($long_para * ($obj['long'] - $img_left) - $icon_w / 2);
        if ($obj['x'] < 0) {
            $obj['x'] = 0;
        }
        unset($obj['lat']);
        unset($obj['long']);
    }
    return true;
    // allow caching
}
Esempio n. 2
0
function automap_obj($MAPCFG, &$params, &$saved_config, $obj_name)
{
    $obj = automap_obj_base($MAPCFG, $params, $saved_config, $obj_name);
    if ($obj_name === '<<<monitoring>>>') {
        $obj['host_name'] = 'Monitoring';
        $obj['type'] = 'shape';
        $obj['icon_size'] = array(22);
        $obj['icon'] = 'std_nagvis.png';
        $obj['.width'] = 22;
        $obj['.height'] = 22;
    } else {
        $obj['type'] = 'host';
        $obj['host_name'] = $obj_name;
        // Default to params iconset
        if (!isset($obj['iconset'])) {
            $obj['iconset'] = $params['iconset'];
        }
        if (!isset($obj['icon_size'])) {
            $obj['icon_size'] = $params['icon_size'];
        }
        if ($obj['icon_size']) {
            if (count($obj['icon_size']) == 1) {
                $w = (int) $obj['icon_size'][0];
                $h = (int) $obj['icon_size'][0];
            } else {
                $w = (int) $obj['icon_size'][0];
                $h = (int) $obj['icon_size'][1];
            }
        } else {
            // Calculate the size of the object for later auto positioning
            $size = iconset_size($obj['iconset']);
            $w = $size[0];
            $h = $size[1];
        }
        $obj['.width'] = $w;
        $obj['.height'] = $h;
    }
    $obj['label_show'] = $MAPCFG->getValue(0, 'label_show');
    $obj['label_border'] = $MAPCFG->getValue(0, 'label_border');
    $obj['label_background'] = $MAPCFG->getValue(0, 'label_background');
    $obj['label_maxlen'] = $MAPCFG->getValue(0, 'label_maxlen');
    // Header menu has z-index 100, this object's label the below+1
    $obj['z'] = 98;
    $obj['.parents'] = array();
    $obj['.childs'] = array();
    return $obj;
}