Example #1
0
<?php

require_once '../lib/lib.everything.php';
enforce_master_on_off_switch($_SERVER['HTTP_ACCEPT_LANGUAGE']);
enforce_api_password($_POST['password']);
$context = default_context(False);
/**** ... ****/
$print_id = $_GET['id'] ? $_GET['id'] : null;
$print = get_print($context->db, $print_id);
if (!$print) {
    die_with_code(400, "I don't know that print");
}
if ($progress = $_POST['progress']) {
    $context->db->query('START TRANSACTION');
    $print['progress'] = $progress;
    set_print($context->db, $print);
    $context->db->query('COMMIT');
}
header('HTTP/1.1 200');
echo "OK\n";
Example #2
0
/**
 * Convert a string of GeoJSON data to an atlas composition and queue it up.
 */
function compose_from_geojson(&$dbh, $data)
{
    $json = json_decode($data, true);
    if (!is_geojson($json)) {
        return null;
    }
    //
    // Move on to the actual business of converting GeoJSON to an atlas.
    // Start with a global paper size and orientation for the full document.
    //
    $p = $json['properties'];
    $paper_size = is_array($p) && isset($p['paper_size']) ? $p['paper_size'] : 'letter';
    $orientation = is_array($p) && isset($p['orientation']) ? $p['orientation'] : 'portrait';
    $layout = is_array($p) && isset($p['layout']) ? $p['layout'] : 'full-page';
    //
    // "orientation" above refers to the *map*, so if we want half-size
    // we'll need to flip the orientation of the overall printed sheet
    // to accommodate it.
    //
    if ($orientation == 'landscape' && $layout == 'half-page') {
        $orientation = 'portrait';
    } elseif ($orientation == 'portrait' && $layout == 'half-page') {
        $orientation = 'landscape';
    }
    list($printed_width, $printed_height) = get_printed_dimensions($paper_size, $orientation, $layout);
    $printed_aspect = $printed_width / $printed_height;
    $paper_type = "{$orientation}, {$paper_size}";
    $message = array('action' => 'compose', 'paper_size' => $paper_size, 'orientation' => $orientation, 'layout' => $layout, 'pages' => array());
    //
    // Iterate over each feature and determine an appropriate extent and zoom.
    // Each feature in the GeoJSON becomes a single page in the atlas.
    //
    foreach ($json['features'] as $f => $feature) {
        $number = $f + 1;
        //
        // Check the properties for a provider and explicit zoom.
        //
        $p = $feature['properties'];
        $provider = is_array($p) && isset($p['provider']) ? new MMaps_Templated_Spherical_Mercator_Provider($p['provider']) : new MMaps_OpenStreetMap_Provider();
        $explicit_zoom = is_array($p) && is_numeric($p['zoom']);
        $zoom = $explicit_zoom ? intval($p['zoom']) : 16;
        $mark = is_array($p['mark']) ? $p['mark'] : null;
        $fuzzy = is_array($p['fuzzy']) ? $p['fuzzy'] : null;
        $text = isset($p['text']) ? $p['text'] : null;
        //
        // Determine extent based on geometry type and zoom level.
        //
        $extent = null;
        if ($feature['geometry']['type'] == 'Point') {
            $extent = geojson_point_extent($feature['geometry'], $zoom);
        } elseif ($feature['geometry']['type'] == 'Polygon') {
            $extent = geojson_polygon_extent($feature['geometry']);
        } else {
            die_with_code(500, "I don't know how to do this yet, sorry.");
        }
        //
        // If we got this far, we know we have a meaningful zoom and extent
        // for this page, now adjust it to the known aspect ratio of the page.
        //
        $_mmap = MMaps_mapByExtentZoom($provider, $extent[0], $extent[1], $zoom);
        $dim = $_mmap->dimensions;
        $_mmap_center = $_mmap->pointLocation(new MMaps_Point($dim->x / 2, $dim->y / 2));
        $_mmap_aspect = $dim->x / $dim->y;
        if ($printed_aspect > $_mmap_aspect) {
            // paper is wider than the map
            $dim->x *= $printed_aspect / $_mmap_aspect;
        } else {
            // paper is taller than the map
            $dim->y *= $_mmap_aspect / $printed_aspect;
        }
        $mmap = MMaps_mapByCenterZoom($provider, $_mmap_center, $zoom, $dim);
        $provider = join(',', $mmap->provider->templates);
        $northwest = $mmap->pointLocation(new MMaps_Point(0, 0));
        $southeast = $mmap->pointLocation($mmap->dimensions);
        $bounds = array($northwest->lat, $northwest->lon, $southeast->lat, $southeast->lon);
        $message['pages'][] = compact('number', 'provider', 'bounds', 'zoom', 'text', 'mark', 'fuzzy');
    }
    //
    // Make room in the database for the new print and all its pages.
    //
    $print = add_print($dbh, 'nobody');
    $print['paper_size'] = $message['paper_size'];
    $print['orientation'] = $message['orientation'];
    $print['layout'] = $message['layout'];
    $print['north'] = $message['pages'][0]['bounds'][0];
    $print['south'] = $message['pages'][0]['bounds'][2];
    $print['west'] = $message['pages'][0]['bounds'][1];
    $print['east'] = $message['pages'][0]['bounds'][3];
    foreach ($message['pages'] as $_page) {
        $page = add_print_page($dbh, $print['id'], $_page['number']);
        $page['text'] = $_page['text'];
        $page['provider'] = $_page['provider'];
        $page['zoom'] = $_page['zoom'];
        $page['north'] = $_page['bounds'][0];
        $page['south'] = $_page['bounds'][2];
        $page['west'] = $_page['bounds'][1];
        $page['east'] = $_page['bounds'][3];
        set_print_page($dbh, $page);
        $print['north'] = max($print['north'], $page['north']);
        $print['south'] = min($print['south'], $page['south']);
        $print['west'] = min($print['west'], $page['west']);
        $print['east'] = max($print['east'], $page['east']);
    }
    $print['progress'] = 0.1;
    // the first 10% is getting it queued
    set_print($dbh, $print);
    $message['print_id'] = $print['id'];
    // queue the task
    queue_task("tasks.composePrint", array("http://" . SERVER_NAME, API_PASSWORD), $message);
    return $print;
}