public function GetLayerKml($resId, $format = "kml")
 {
     //Check for unsupported representations
     $fmt = $this->ValidateRepresentation($format, array("kml", "kmz"));
     $width = $this->GetRequestParameter("width", null);
     $height = $this->GetRequestParameter("height", null);
     $drawOrder = $this->GetRequestParameter("draworder", null);
     $dpi = $this->GetRequestParameter("dpi", 96);
     $bbox = $this->GetRequestParameter("bbox", null);
     $extents = null;
     if ($width == null) {
         $this->BadRequest($this->app->localizer->getText("E_MISSING_REQUIRED_PARAMETER", "width"), $this->GetMimeTypeForFormat($format));
     }
     if ($height == null) {
         $this->BadRequest($this->app->localizer->getText("E_MISSING_REQUIRED_PARAMETER", "height"), $this->GetMimeTypeForFormat($format));
     }
     if ($drawOrder == null) {
         $this->BadRequest($this->app->localizer->getText("E_MISSING_REQUIRED_PARAMETER", "draworder"), $this->GetMimeTypeForFormat($format));
     }
     if ($bbox == null) {
         $this->BadRequest($this->app->localizer->getText("E_MISSING_REQUIRED_PARAMETER", "bbox"), $this->GetMimeTypeForFormat($format));
     } else {
         $parts = explode(",", $bbox);
         if (count($parts) == 4) {
             $extents = new MgEnvelope($parts[0], $parts[1], $parts[2], $parts[3]);
         }
     }
     $sessionId = "";
     if ($resId->GetRepositoryType() == MgRepositoryType::Session) {
         $sessionId = $resId->GetRepositoryName();
     } else {
         $sessionId = $this->GetRequestParameter("session", "");
     }
     $this->EnsureAuthenticationForSite($sessionId, true);
     $siteConn = new MgSiteConnection();
     if ($sessionId !== "") {
         $userInfo = new MgUserInformation($sessionId);
         $siteConn->Open($userInfo);
     } else {
         $siteConn->Open($this->userInfo);
         $site = $siteConn->GetSite();
         $sessionId = $site->CreateSession();
         $userInfo = new MgUserInformation($sessionId);
         $siteConn->Open($userInfo);
     }
     $csFactory = new MgCoordinateSystemFactory();
     $csObj = $csFactory->CreateFromCode("LL84");
     $scale = MgUtils::GetScale($extents, $csObj, $width, $height, $dpi);
     $writer = new MgSlimChunkWriter($this->app);
     $doc = new MgKmlDocument($writer);
     $resSvc = $siteConn->CreateService(MgServiceType::ResourceService);
     $featSvc = $siteConn->CreateService(MgServiceType::FeatureService);
     $ldfContent = $resSvc->GetResourceContent($resId);
     $xml = new DOMDocument();
     $xml->loadXML($ldfContent->ToString());
     $destExtent = self::GetLayerExtent($csFactory, $xml, $csObj, $featSvc, $resSvc);
     $doc->StartDocument();
     $doc->WriteString("<visibility>1</visibility>");
     if ($destExtent != null) {
         $widthMeters = $csObj->ConvertCoordinateSystemUnitsToMeters($destExtent->GetWidth());
         $heightMeters = $csObj->ConvertCoordinateSystemUnitsToMeters($destExtent->GetHeight());
         $dimension = sqrt($widthMeters * $heightMeters);
         $vlNodes = $xml->getElementsByTagName("VectorLayerDefinition");
         $glNodes = $xml->getElementsByTagName("GridLayerDefinition");
         $dlNodes = $xml->getElementsByTagName("DrawingLayerDefinition");
         if ($vlNodes->length == 1) {
             $scaleRangeNodes = $vlNodes->item(0)->getElementsByTagName("VectorScaleRange");
             for ($i = 0; $i < $scaleRangeNodes->length; $i++) {
                 $scaleRange = $scaleRangeNodes->item($i);
                 $minElt = $scaleRange->getElementsByTagName('MinScale');
                 $maxElt = $scaleRange->getElementsByTagName('MaxScale');
                 $minScale = "0";
                 $maxScale = 'infinity';
                 // as MDF's VectorScaleRange::MAX_MAP_SCALE
                 if ($minElt->length > 0) {
                     $minScale = $minElt->item(0)->nodeValue;
                 }
                 if ($maxElt->length > 0) {
                     $maxScale = $maxElt->item(0)->nodeValue;
                 }
                 if ($minScale != 'infinity') {
                     $minScale = intval($minScale);
                 }
                 if ($maxScale != 'infinity') {
                     $maxScale = intval($maxScale);
                 } else {
                     $maxScale = 1000000000000.0;
                 }
                 // as MDF's VectorScaleRange::MAX_MAP_SCALE
                 if ($scale > $minScale && $scale <= $maxScale) {
                     $this->AppendScaleRange($resId, $destExtent, $dimension, $minScale, $maxScale, $dpi, $drawOrder, $format, $sessionId, $doc);
                 }
             }
         } else {
             if ($glNodes->length == 1) {
             }
         }
     }
     $doc->EndDocument();
 }