Jump to content

User:Ucucha/mapper.php

From Wikipedia, the free encyclopedia

See User:Ucucha/Mapper.

#!/usr/bin/php
<?
/* mapper.php
 * Ucucha, January 2011
 *
 * Places markers on a map of Madagascar
 */

/*
 * data
 */
$inmap = "Mada_temp.svg";
$sm = 35.38609001;
$si = -388.3345367;
$em = 34.79835974;
$ei = -1402.352063;
// out = $(s|e)m * in + $(s|e)i

/*
 * conversion functions from minutes/seconds to decimal latitude/longitude
 */

function degconvert($in)
{
    $tmp = preg_split("/\s+/", $in);
    $deg = $tmp[0];
    if($tmp[1])
        $min = $tmp[1];
    if($tmp[2])
        $sec = $tmp[2];

    $out = $deg;
    $out += $min / 60;
    $out += $sec / 3600;

    return $out;
}
/*
 * prepare for input
 */

// file streams
$in = fopen("php://stdin","r");

// temporarily store data
class Marker
{
    public $dlat, $dlong, $mlat, $mlong, $color, $name;
   
    public function __construct($newdlat, $newdlong, $newname, $newcolor)
    {
        global $sm, $si, $em, $ei;
        $this->dlat = $newdlat;
        $this->mlat = $sm * $newdlat + $si;
        $this->dlong = $newdlong;
        $this->mlong = $em * $newdlong + $ei;
        $this->name = $newname;
        $this->color = $newcolor;
    }
}
// array of layers containing Markers (defined above)
$markers = array();
// array of strings containing layer names, with index == that of $markers
$layers = array();
$i = 0;

/*
 * get data from user
 */
echo "File name to write to: ";
$out = trim(fgets($in));
$tmp = `cp $inmap $out`;
if($tmp)
    die("Error creating output file");
$map = fopen($out, "r+");

echo "Type in the layers and localities.\nTo add a new layer, type 'l' instead of a locality name.\nTo finish, type 'q' instead of a locality name.\n";
while(true)
{
    echo "Layer name: ";
    $layers[$i] = trim(fgets($in));
    $markers[$i] = array();
    echo "Color (used for all markers in this layer): ";
    $lcolor = trim(fgets($in));
    // make green if not specified
    if(!$lcolor)
        $lcolor = "#40a040";

    $j = 0;
    while(true)
    {
        // marker
        echo "Locality name: "; $tname = trim(fgets($in));
        if($tname == "l")
            break;
        else if($tname == "q")
            break 2;
        echo "Latitude (S): "; $tdlat = degconvert(trim(fgets($in)));
        echo "Longitude (E): "; $tdlong = degconvert(trim(fgets($in)));
        $markers[$i][$j] = new Marker($tdlat, $tdlong, $tname, $lcolor);
        $j++;
    }
    $i++;
}

/*
 * handle data
 */
// go to position right before closing </svg>
fseek($map, -7, SEEK_END);

foreach($markers as $key => $layer)
{
    fwrite($map, "\t<g\n\t\tstyle=\"display:inline\"\n\t\tinkscape:groupmode=\"layer\"\n\t\tinkscape:label=\"" . $layers[$key] . "\">\n");
    foreach($layer as $marker)
    {
        fwrite($map,
            "\t\t<path\n\t\t\tstyle=\"fill:" .
            $marker->color .
            ";fill-opacity:1;fill-rule:evenodd;stroke:none;display:inline;enable-background:new\"\n\t\t\td=\"m " .
            $marker->mlong .
            "," .
            $marker->mlat .
            " a 4.3493844,4.3493844 0 1 1 -8.69876,0 4.3493844,4.3493844 0 1 1 8.69876,0 z\">\n\t\t\t<title>" .
            $marker->name .
            "</title>\n\t\t</path>\n");
    }
    fwrite($map, "\t</g>\n");
}

// close file
fwrite($map, "\t</svg>");
?>