User:Ucucha/mapper.php

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>");
?>

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.