Синтаксис стиля для полигона в KML с использованием объекта PHP DOM - PullRequest
1 голос
/ 24 марта 2012

Я работаю над приложением, которое создаст файл KML с PHP.Как правило, я делал это с отдельными точками, и у меня не было никаких проблем, но для полигонов мне было очень трудно заставить их стилизоваться.

Я пытаюсь раскрасить форму многоугольника.основан на динамическом стиле из PHP;однако, я не могу даже заставить жестко запрограммированный стиль работать первым.Структура KML показана после примера кода, с которым у меня возникли проблемы.

Как должен выглядеть правильный синтаксис стиля dom, если все, что я хочу сделать, - это установить стиль цвета многоугольника и определенную ширину.

Спасибо за вашу помощь

Вот начальный код:

// Creates the Document.
$dom        = new DOMDocument('1.0', 'UTF-8');
$kmlfile    = $dom->createElementNS('http://earth.google.com/kml/2.1', 'kml');
$parentnode = $dom->appendChild($kmlfile);
$dfnode     = $dom->createElement('Document');
$documentnode = $parentnode->appendChild($dfnode);

$gpsStyleNode = $dom->createElement('Style');
$gpsStyleNode->setAttribute('id', 'style_gps');
$gpsIconstyleNode = $dom->createElement('IconStyle');
$gpsIconstyleNode->setAttribute('id', 'icon_gps');
$gpsIconstyleNode->setAttribute('scale', '0.6');
$gpsIconstyleNode->setAttribute('color', 'ff0000ff');
$gpsIconNode = $dom->createElement('Icon');
$gpsHref = $dom->createElement('href', 'http://maps.google.com/mapfiles/kml/shapes/cross-hairs.png');
$gpsIconNode->appendChild($gpsHref);

$gpsIconstyleNode->appendChild($gpsIconNode);
$gpsStyleNode->appendChild($gpsIconstyleNode);

$gpslinetyleNode = $dom->createElement('PolyStyle');
$gpsStyleNode->appendChild($gpslinetyleNode);   
$gpslinetyleNode->setAttribute('id', 'Icon_gps2');          
$gpslinetyleNode->setAttribute('color', 'ff0000ff');
$gpslinetyleNode->setAttribute('width', '20');

$documentnode->appendChild($gpsStyleNode);  

Вывод KML

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
<Placemark id="12">
<name>PolygonName</name>
<description>test</description>
<styleUrl>Icon_gps2</styleUrl>
<Polygon>
<extrude>1</extrude>
<altitudeMode>relativeToGround</altitudeMode>
<outerBoundaryIs>
<LinearRing>
<coordinates>
(Dynamic Coords pulled from DB in proper format)
</coordinates>
</LinearRing>
</outerBoundaryIs>
</Polygon>
</Placemark>
</kml>

РЕДАКТИРОВАТЬ, чтобы добавить код многоугольника

$placeobject = $dom->createElement('Placemark');
$placeNode = $nDoc->appendChild($placeobject);          
$placeobject->setAttribute('id',$tmpid);

$placename = $dom->createElement('name','PolygonName');
$placeNode->appendChild($placename);                

$placedesc = $dom->createElement('description', 'test');
$placeNode->appendChild($placedesc);

$stylenode =$dom->createElement('styleUrl','line_gps');
$placeNode->appendChild($stylenode);

$linenode = $dom->createElement('Polygon');
$placeNode->appendChild($linenode);

$lineextrude = $dom->createElement('extrude', '1');
$linenode->appendChild($lineextrude);

$linealtitude = $dom->createElement('altitudeMode', 'relativeToGround');
$linenode->appendChild($linealtitude);                  

$outerboundnode = $dom->createElement('outerBoundaryIs');
$linenode = $linenode->appendChild($outerboundnode);

$ringtype =$dom->createElement('LinearRing');
$linenode = $linenode->appendChild($ringtype);

$coordnode = $dom->createElement('coordinates',$locationstring);
$ringtype->appendChild($coordnode); 

1 Ответ

0 голосов
/ 25 марта 2012

Вы устанавливаете атрибуты (цвет, масштаб, ширина), которые ожидаются как элементы.

Более того, ширина применяется к LineStyle, а не к PolyStyle

$dom        = new DOMDocument('1.0', 'UTF-8');
$nKml        = $dom->appendChild($dom->createElementNS('http://earth.google.com/kml/2.1', 'kml'));
$nDoc        = $nKml->appendChild($dom->createElement('Document'));

$idSuffix='_gps';

//Style
$nStyle      = $nDoc->appendChild($dom->createElement('Style'));
$nStyle->setAttribute('id', 'style'.$idSuffix);

  //IconStyle
  $nIconStyle      = $nStyle->appendChild($dom->createElement('IconStyle'));
  $nIconStyle->setAttribute('id', 'icon'.$idSuffix);
    //color
    $nIconStyleColor=$nIconStyle->appendChild($dom->createElement('color'));
    $nIconStyleColor->appendChild($dom->createTextNode('ff0000ff'));
    //Icon
    $nIconStyleIcon=$nIconStyle->appendChild($dom->createElement('Icon'));
      //href
      $nIconStyleHref=$nIconStyleIcon->appendChild($dom->createElement('href'));
      $nIconStyleHref->appendChild($dom->createTextNode('http://maps.google.com/../cross-hairs.png'));

  //PolyStyle
  $nPolyStyle      = $nStyle->appendChild($dom->createElement('PolyStyle'));
  $nPolyStyle->setAttribute('id', 'poly'.$idSuffix);
    //color
    $nPolyStyleColor=$nPolyStyle->appendChild($dom->createElement('color'));
    $nPolyStyleColor->appendChild($dom->createTextNode('ff0000ff'));

  //LineStyle
  $nLineStyle      = $nStyle->appendChild($dom->createElement('LineStyle'));
  $nLineStyle->setAttribute('id', 'line'.$idSuffix);
    //width
    $nLineStyleWidth=$nLineStyle->appendChild($dom->createElement('width'));
    $nLineStyleWidth->appendChild($dom->createTextNode('20'));
    $nLineStyleColor=$nLineStyle->appendChild($dom->createElement('color'));
    $nLineStyleColor->appendChild($dom->createTextNode('ff0000ff'));

результат:

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.1">
  <Document>
    <Style id="style_gps">
      <IconStyle id="icon_gps">
        <color>ff0000ff</color>
        <Icon>
          <href>http://maps.google.com/../cross-hairs.png</href>
        </Icon>
      </IconStyle>
      <PolyStyle id="poly_gps">
        <color>ff0000ff</color>
      </PolyStyle>
      <LineStyle id="line_gps">
        <width>20</width>
        <color>ff0000ff</color>
      </LineStyle>
    </Style>
  </Document>
</kml>
...