Exemple #1
0
    public function getKML($url, $download = false, $file = "")
    {
        // Create KMZ archieve
        if ($file == "") {
            $file = $this->dirtmp . "/tme" . $this->nomeTemp . ".kmz";
        }
        $this->nomeArquivo = $file;
        if (!file_exists($file)) {
            include dirname(__FILE__) . "/../kmlmapserver/classes/zip.class.php";
            $zip = new zipfile();
            // Add balloon logo to archieve (300 x 30 px)
            $zip->addFile($this->logoline, 'files/balloonlogo.png');
            // KML header
            $kml = "<?xml version='1.0' encoding='UTF-8'?>" . PHP_EOL . "<kml xmlns='http://www.opengis.net/kml/2.2' xmlns:atom='http://www.w3.org/2005/Atom'>" . PHP_EOL . "  <Document>" . PHP_EOL . "    <atom:author>" . PHP_EOL . "      <atom:name>Thematic Mapping Engine</atom:name>" . PHP_EOL . "    </atom:author>" . PHP_EOL . "    <atom:link href='http://thematicmapping.org' rel='related' />" . PHP_EOL . "    <name>{$this->mapTitle}</name>" . PHP_EOL . "    <open>1</open>" . PHP_EOL . "    <Snippet maxLines='1'>{$this->mapSource}</Snippet>" . PHP_EOL . "    <description><![CDATA[ {$this->mapDescription} <p>{$this->mapSource}</p>{$this->engine} ]]></description>" . PHP_EOL;
            // Add style for indicator balloon
            $kmlStyles = "    <Style id='balloonStyle'>" . PHP_EOL . "      <BalloonStyle>" . PHP_EOL . "        <text><![CDATA[" . PHP_EOL . "          <a href='http://thematicmapping.org'><img src='http://thematicmapping.org/engine/files/balloonlogo.png'></a>" . PHP_EOL . "          <p><b><font size='+2'>\$[name]</font></b></p>" . PHP_EOL . "          <p>\$[description]</p>" . PHP_EOL . "        ]]></text>" . PHP_EOL . "      </BalloonStyle>" . PHP_EOL . "    </Style>" . PHP_EOL . "    <styleUrl>#balloonStyle</styleUrl>" . PHP_EOL;
            if ($this->colourType == 'scale') {
                // Need to run before getColourValue / getColourLegend / makeClasses
                self::makeColourScale();
                if ($this->classification != 'unclassed') {
                    self::makeClasses($this->classification, $this->numClasses);
                }
                // Add colour legend to KMZ archieve
                if ($this->showLegend) {
                    $imgLegenda = self::getColourLegend();
                    $zip->addFile($imgLegenda, 'files/legend.png');
                }
                $kmlSingleColour = '';
                // Colours needs to be defined for each feature
                //$kmlColour = self::rgb2bgr($this->noDataColour); // Not in use, only so the variable has a value
            } else {
                $kmlSingleColour = '<color>' . self::rgb2bgr($this->colour) . '</color>';
                //$kmlColour = self::rgb2bgr($this->colour);
            }
            // Add style for value placemarks
            if ($this->showLabel) {
                $kmlStyles .= "    <Style id='labelPlacemark'>" . PHP_EOL . "      <IconStyle>" . PHP_EOL . "        <scale>0.0</scale>" . PHP_EOL . "      </IconStyle>" . PHP_EOL . "      <LabelStyle>" . PHP_EOL . "        <scale>1</scale>" . PHP_EOL . "      </LabelStyle>" . PHP_EOL . "    </Style>" . PHP_EOL;
            }
            // Define shared styles and legend
            $kmlStyles .= "    <Style id='sharedStyle'>" . PHP_EOL;
            switch ($this->mapType) {
                case "choropleth":
                case "prism":
                    $kmlStyles .= "      <PolyStyle>" . PHP_EOL . "        <fill>1</fill>" . PHP_EOL . "        <outline>1</outline>" . PHP_EOL . "        {$kmlSingleColour}" . PHP_EOL . "      </PolyStyle>" . PHP_EOL . "      <LineStyle>" . PHP_EOL . "        <color>cc000000</color>" . PHP_EOL . "      </LineStyle>" . PHP_EOL;
                    break;
                case "bar":
                    if ($this->outlinecolor == "") {
                        $outline = 0;
                    } else {
                        $outline = 1;
                    }
                    $kmlStyles .= "      <PolyStyle>" . PHP_EOL . "        <fill>1</fill>" . PHP_EOL . "        <outline>" . $outline . "</outline>" . PHP_EOL . "        {$kmlSingleColour}" . PHP_EOL . "      </PolyStyle>" . PHP_EOL;
                    if ($this->outlinecolor != "") {
                        $kmlStyles .= "<LineStyle>" . PHP_EOL . "        <color>" . $this->outlinecolor . "</color>" . PHP_EOL . "      </LineStyle>" . PHP_EOL;
                    }
                    break;
                    // Proportional symbol
                // Proportional symbol
                case "symbol":
                    switch ($this->symbolType) {
                        case 'image':
                            $zip->addFile("files/{$this->symbolShape}.png", 'files/symbol.png');
                            $kmlStyles .= "      <IconStyle>" . PHP_EOL . "        {$kmlSingleColour}" . PHP_EOL . "        <Icon>" . PHP_EOL . "          <href>" . $url . "/symbol.png</href>" . PHP_EOL . "        </Icon>" . PHP_EOL . "      </IconStyle>" . PHP_EOL;
                            break;
                        case 'polygon':
                            if ($this->symbolShape == 'square') {
                                $this->symbolVertices = 4;
                            }
                            $kmlStyles .= "      <PolyStyle>" . PHP_EOL . "        {$kmlSingleColour}" . PHP_EOL . "        <fill>1</fill>" . PHP_EOL . "        <outline>1</outline>" . PHP_EOL . "      </PolyStyle>" . PHP_EOL . "      <LineStyle>" . PHP_EOL . "        <color>cc000000</color>" . PHP_EOL . "      </LineStyle>" . PHP_EOL;
                            break;
                        case 'collada':
                            if ($this->colourType == 'scale') {
                                // Limit number of collada objects (one for each colour)
                                if ($this->classification == 'unclassed') {
                                    self::makeClasses('equal', 12);
                                }
                                foreach ($this->classColours as $class => $classColour) {
                                    $colladaColour = self::rgb2collada($classColour);
                                    // Read collada model
                                    $filename = "files/{$this->symbolShape}.dae";
                                    $handle = fopen($filename, "r");
                                    $collada = fread($handle, filesize($filename));
                                    fclose($handle);
                                    // Search and replace colour
                                    $pos = strpos($collada, '<effect id="material0-effect" name="material0-effect">');
                                    $pos = strpos($collada, "<diffuse>", $pos);
                                    $pos = strpos($collada, "<color>", $pos);
                                    $collada = substr_replace($collada, $colladaColour, $pos + 7, 28);
                                    // Add collada object to kmz archieve
                                    $zip->addFromString("files/object{$class}.dae", $collada);
                                }
                            } else {
                                $colladaColour = self::rgb2collada($this->colour);
                                // Read collada model
                                $filename = "files/{$this->symbolShape}.dae";
                                $handle = fopen($filename, "r");
                                $collada = fread($handle, filesize($filename));
                                fclose($handle);
                                // Search and replace colour
                                $pos = strpos($collada, '<effect id="material0-effect" name="material0-effect">');
                                $pos = strpos($collada, "<diffuse>", $pos);
                                $pos = strpos($collada, "<color>", $pos);
                                $collada = substr_replace($collada, $colladaColour, $pos + 7, 28);
                                // Add collada object to kmz archieve
                                $zip->addFromString("files/object.dae", $collada);
                            }
                            $kmlstyle = '';
                            // Not possible to define style for collada objects
                    }
                    // switch symbol
            }
            // switch type
            $kmlStyles .= "      <BalloonStyle>" . PHP_EOL . "        <text><![CDATA[" . PHP_EOL . "          <a href='http://thematicmapping.org'><img src='http://thematicmapping.org/engine/files/balloonlogo.png'></a>" . PHP_EOL . "          <p><b><font size='+2'>\$[name]</font></b></p>" . PHP_EOL . "          <p>{$this->mapTitle}: \$[Snippet]</p>" . PHP_EOL . "          <p>{$this->mapDescription}</p>" . PHP_EOL . "          <p>{$this->mapSource}</p>" . PHP_EOL . "          <p>{$this->engine}</p>" . PHP_EOL . "        ]]></text>" . PHP_EOL . "      </BalloonStyle>" . PHP_EOL . "    </Style>" . PHP_EOL;
            // End of shared style
            $kmlFolder = "    <Folder>" . PHP_EOL . "      <name>Colunas</name>" . PHP_EOL . "      <open>1</open>" . PHP_EOL;
            if ($this->timeType == 'series') {
                $kmlFolder .= "      <Style>" . PHP_EOL . "        <ListStyle>" . PHP_EOL . "          <listItemType>radioFolder</listItemType>" . PHP_EOL . "        </ListStyle>" . PHP_EOL . "      </Style>" . PHP_EOL;
            }
            // Loop thorough all years
            foreach ($this->yearArray as $key => $year) {
                $kmlFeatures = '';
                if ($this->timeType == 'slider' or $year == $this->year) {
                    $visibility = 1;
                } else {
                    $visibility = 0;
                }
                $kmlFolder .= "      <Folder>" . PHP_EOL . "        <name>{$year}</name>" . PHP_EOL . "        <visibility>{$visibility}</visibility>" . PHP_EOL;
                if ($this->showLabel) {
                    $kmlLabels = "        <Folder>" . PHP_EOL . "          <name>Show/hide labels</name>" . PHP_EOL . "          <visibility>{$visibility}</visibility>" . PHP_EOL . "          <Style>" . PHP_EOL . "            <ListStyle>" . PHP_EOL . "              <listItemType>checkHideChildren</listItemType>" . PHP_EOL . "            </ListStyle>" . PHP_EOL . "          </Style>" . PHP_EOL;
                }
                // Add timespan if time animation
                if ($this->timeType == 'slider') {
                    $end = '';
                    // Check if there is more years
                    if (array_key_exists($key + 1, $this->yearArray)) {
                        $end = '<end>' . intval($this->yearArray[$key + 1] - 1) . '-12-31</end>';
                    }
                    $kmlFolder .= "        <TimeSpan>" . PHP_EOL . "          <begin>{$year}-01-01</begin>{$end}" . PHP_EOL . "        </TimeSpan>" . PHP_EOL;
                }
                // Loop thorough all features (values without features will not be shown)
                foreach ($this->dataStore['features'] as $featureID => $feature) {
                    $name = $feature['name'];
                    //if (!mb_detect_encoding($name,"UTF-8",true))
                    //{$name = mb_convert_encoding($name,"UTF-8","ISO-8859-1");}
                    $name = "<![CDATA[ " . $name . " ]]>";
                    $value = '';
                    // use null?
                    $valueText = 'no data';
                    $valueLabel = '';
                    $kmlFeature = '';
                    $altitude = 0;
                    $symbolSize = 0;
                    $colladaCount = 0;
                    //$kmlColour = self::rgb2bgr($this->noDataColour);
                    $kmlColour = '';
                    $longitude = $feature['lon'];
                    $latitude = $feature['lat'];
                    // Check if value exists for this feature
                    if (isset($this->indicator['values'][$year][$featureID])) {
                        // Extract value from dataStore
                        $value = $this->indicator['values'][$year][$featureID];
                        $valueText = $valueLabel = number_format($value, 2, ',', '.');
                        // Colour scale
                        if ($this->colourType == 'scale') {
                            if ($this->classification != 'unclassed') {
                                $class = self::getClass($value);
                                $kmlColour = self::rgb2bgr($this->classColours[$class]);
                            } else {
                                $kmlColour = self::getColourValue($value, 'kml');
                            }
                        }
                        // Single colour
                        //else {
                        //    $kmlColour = self::rgb2bgr($this->colour);
                        //}
                    } else {
                        $kmlColour = self::rgb2bgr($this->noDataColour);
                    }
                    if ($this->outlinecolor == "") {
                        $outline = 0;
                    } else {
                        $outline = 1;
                    }
                    switch ($this->mapType) {
                        case "choropleth":
                            $kmlFeature = "          <Style>" . PHP_EOL . "            <PolyStyle>" . PHP_EOL . "              <color>{$kmlColour}</color>" . PHP_EOL . "            </PolyStyle>" . PHP_EOL . "          </Style>" . PHP_EOL;
                            $kmlFeature .= self::wkt2kml($feature['wkt'], 0);
                            break;
                        case "prism":
                            $altitude = intval($value * ($this->maxHeight / $this->maxValue));
                            if ($this->colourType == 'scale') {
                                $kmlFeature = "          <Style>" . PHP_EOL . "            <PolyStyle>" . PHP_EOL . "              <color>{$kmlColour}</color>" . PHP_EOL . "            </PolyStyle>" . PHP_EOL . "          </Style>" . PHP_EOL;
                            }
                            $kmlFeature .= self::wkt2kml($feature['wkt'], $altitude) . PHP_EOL;
                            break;
                        case "bar":
                            if ($value != null) {
                                $altitude = intval($value * ($this->maxHeight / $this->maxValue));
                                if ($this->colourType == 'scale') {
                                    $kmlFeature = "          <Style>" . PHP_EOL . "            <PolyStyle>" . PHP_EOL . "              <color>{$kmlColour}</color>" . PHP_EOL . "              <outline>" . $outline . "</outline>" . PHP_EOL . "            </PolyStyle>" . PHP_EOL;
                                    if ($this->outlinecolor != "") {
                                        $kmlFeature .= "<LineStyle>" . PHP_EOL . "        <color>" . $this->outlinecolor . "</color>" . PHP_EOL . "      </LineStyle>" . PHP_EOL;
                                    }
                                    $kmlFeature .= "          </Style>" . PHP_EOL;
                                }
                                $kmlFeature .= self::kmlSymbolCalculator($longitude, $latitude, $this->barSize, $this->numvertices, $altitude);
                            }
                            break;
                        case "symbol":
                            if ($value != null) {
                                switch ($this->symbolType) {
                                    case 'im$z = new ZipArchive();
									$z->open("test.zip", ZIPARCHIVE::CREATE);
									folderToZip("storeThisFolder", $z);
									$z->close();age':
                                        //$symbolSize = round(self::getSymbolSize($value, $this->symbolShape),2);
                                        $symbolSize = round($this->symbolMaxSize * sqrt($value / $this->maxValue), 2);
                                        $kmlFeature = "          <Style>" . PHP_EOL . "            <IconStyle>" . PHP_EOL . "              <scale>{$symbolSize}</scale>" . PHP_EOL . "              <color>{$kmlColour}</color>" . PHP_EOL . "            </IconStyle>" . PHP_EOL . "          </Style>" . PHP_EOL;
                                        $kmlFeature .= "          <LookAt>" . PHP_EOL . "            <longitude>{$longitude}</longitude>" . PHP_EOL . "            <latitude>{$latitude}</latitude>" . PHP_EOL . "            <altitude>0</altitude>" . PHP_EOL . "            <range>3200000</range>" . PHP_EOL . "            <altitudeMode>clampToGround</altitudeMode>" . PHP_EOL . "          </LookAt>" . PHP_EOL . "          <Point>" . PHP_EOL . "            <coordinates>{$longitude},{$latitude},0</coordinates>" . PHP_EOL . "          </Point>" . PHP_EOL;
                                        break;
                                    case 'polygon':
                                        //$symbolSize = intval(self::getSymbolSize($value, $this->symbolShape));
                                        $symbolSize = intval($this->symbolMaxSize * sqrt($value / $this->maxValue) * 70000);
                                        if ($this->colourType == 'scale') {
                                            $kmlFeature = "          <Style>" . PHP_EOL . "            <PolyStyle>" . PHP_EOL . "              <color>{$kmlColour}</color>" . PHP_EOL . "            </PolyStyle>" . PHP_EOL . "          </Style>" . PHP_EOL;
                                        }
                                        $kmlFeature .= self::kmlSymbolCalculator($longitude, $latitude, $symbolSize, $this->symbolVertices, 0);
                                        break;
                                    case 'collada':
                                        //$symbolSize = intval(self::getSymbolSize($value, $this->symbolShape));
                                        $symbolSize = intval($this->symbolMaxSize * pow($value / $this->maxValue, 1 / 3) * 20000);
                                        $class = '';
                                        // Single colour
                                        if ($this->colourType == 'scale') {
                                            $class = self::getClass($value);
                                        }
                                        $altitude = $symbolSize;
                                        // Used for label placement
                                        $kmlFeature = "          <Model>" . PHP_EOL . "            <altitudeMode>absolute</altitudeMode>" . PHP_EOL . "            <Location>" . PHP_EOL . "              <longitude>{$longitude}</longitude>" . PHP_EOL . "              <latitude>{$latitude}</latitude>" . PHP_EOL . "              <altitude>0</altitude>" . PHP_EOL . "            </Location>" . PHP_EOL . "            <Scale>" . PHP_EOL . "              <x>{$symbolSize}</x>" . PHP_EOL . "              <y>{$symbolSize}</y>" . PHP_EOL . "              <z>{$symbolSize}</z>" . PHP_EOL . "            </Scale>" . PHP_EOL . "            <Link>" . PHP_EOL . "              <href>files/object{$class}.dae</href>" . PHP_EOL . "            </Link>" . PHP_EOL . "          </Model>" . PHP_EOL;
                                }
                                // switch
                            }
                            // if
                    }
                    // switch
                    $kmlFeatures .= "        <Placemark>" . PHP_EOL . "          <name>{$name}</name>" . PHP_EOL . "          <visibility>{$visibility}</visibility>" . PHP_EOL . "          <Snippet>{$valueText} ({$year})</Snippet>" . PHP_EOL . "          <styleUrl>#sharedStyle</styleUrl>" . PHP_EOL . $kmlFeature . "        </Placemark>" . PHP_EOL;
                    if ($this->showLabel) {
                        $label = '';
                        if ($this->showNames) {
                            $label = $name;
                        }
                        if ($this->showValues) {
                            $label .= ' ' . $valueLabel;
                        }
                        $kmlLabels .= "          <Placemark>" . PHP_EOL . "            <name>{$label}</name>" . PHP_EOL . "            <visibility>{$visibility}</visibility>" . PHP_EOL . "            <styleUrl>#labelPlacemark</styleUrl>" . PHP_EOL . "            <Point>" . PHP_EOL . "              <altitudeMode>absolute</altitudeMode>" . PHP_EOL . "              <coordinates>{$longitude}, {$latitude}, {$altitude}</coordinates>" . PHP_EOL . "            </Point>" . PHP_EOL . "          </Placemark>" . PHP_EOL;
                    }
                }
                // foreach features
                if ($this->showLabel) {
                    $kmlLabels .= "        </Folder>";
                    $kmlFolder .= $kmlLabels;
                }
                $kmlFolder .= $kmlFeatures;
                $kmlFolder .= "      </Folder>" . PHP_EOL;
            }
            // foreach years
            // Close Years folder
            $kmlFolder .= "    </Folder>" . PHP_EOL;
            // Create logo with title and source and add to archieve
            if ($this->showTitle) {
                $imgBrand = self::mapTitleImage();
                $zip->addFile($imgBrand, 'files/brand.png');
            } else {
                $zip->addFile($this->logo, 'files/brand.png');
            }
            // Add title
            $kml .= "    <ScreenOverlay>" . PHP_EOL . "      <name>Titulo</name>" . PHP_EOL . "      <Icon>" . PHP_EOL . "        <href>" . $url . "/" . basename($imgBrand) . "</href>" . PHP_EOL . "      </Icon>" . PHP_EOL . "      <overlayXY x='0.01' y='0.99' xunits='fraction' yunits='fraction'/>" . PHP_EOL . "      <screenXY x='0.01' y='0.99' xunits='fraction' yunits='fraction'/>" . PHP_EOL . "      <size x='-1' y='-1' xunits='pixels' yunits='pixels'/>" . PHP_EOL . "    </ScreenOverlay>" . PHP_EOL;
            // Add legend
            if ($this->showLegend && $this->colourType == 'scale') {
                $kml .= "    <ScreenOverlay>" . PHP_EOL . "      <name>Legenda</name>" . PHP_EOL . "      <Icon>" . PHP_EOL . "        <href>" . $url . "/" . basename($imgLegenda) . "</href>" . PHP_EOL . "      </Icon>" . PHP_EOL . "      <overlayXY x='0.01' y='0.14' xunits='fraction' yunits='fraction'/>" . PHP_EOL . "      <screenXY x='0.01' y='0.14' xunits='fraction' yunits='fraction'/>" . PHP_EOL . "      <size x='-1' y='-1' xunits='pixels' yunits='pixels'/>" . PHP_EOL . "    </ScreenOverlay>" . PHP_EOL;
            }
            $kml .= $kmlStyles . $kmlFolder;
            $kml .= "  </Document>" . PHP_EOL . "</kml>" . PHP_EOL;
            // Add kml to archieve
            //$zip->addFromString("doc.kml", $kml);
            $zip->addFile($kml, 'doc.kml');
            //edmar
            $zip->output($file);
            //edmar
        }
        if ($download) {
            ob_end_clean();
            //header('Content-Type: application/vnd.google-earth.kml+xml');
            header('Content-Disposition: attachment; filename=' . basename($file));
            print $file;
            exit;
        } else {
            return $url . basename($file);
        }
    }