How to remove point altitude in a polygon?

2

Below is a representation of a Polygon in a KML. See:

<coordinates> 
   -112.2550785337791,36.07954952145647,20 
   -112.2549277039738,36.08117083492122,20 
   -112.2552505069063,36.08260761307279,20 
   -112.2564540158376,36.08395660588506,20 
   -112.2580238976449,36.08511401044813,20 
   -112.2595218489022,36.08584355239394,20 
   -112.2608216347552,36.08612634548589,20 
   -112.262073428656,36.08626019085147,20 
   -112.2633204928495,36.08621519860091,20
   -112.2644963846444,36.08627897945274,20 
   -112.2656969554589,36.08649599090644,20
</coordinates>

The last coordinate value, or 20 , represents the altitude, according to the documentation , which I do not need. So at first I made a foreach like this below:

foreach($names as $coordinate) {
    $coordinates[] = explode( ',20', $coordinate );
 }

But if altitude changes in any situation, this rule in explode will not work. What would be a viable way to solve this problem and catch only those convicted ignoring altitude? Can a regular expression solve this?

    
asked by anonymous 03.05.2017 / 18:53

1 answer

3

If the polygon point is always at the end of the line and the number 20 may be variant, you can use before saving to the vector:

<?php

$kml = '
   -112.2550785337791,36.07954952145647,20 
   -112.2549277039738,36.08117083492122,20 
   -112.2552505069063,36.08260761307279,20 
   -112.2564540158376,36.08395660588506,20 
   -112.2580238976449,36.08511401044813,20 
   -112.2595218489022,36.08584355239394,20 
   -112.2608216347552,36.08612634548589,20 
   -112.262073428656,36.08626019085147,20 
   -112.2633204928495,36.08621519860091,20
   -112.2644963846444,36.08627897945274,20 
   -112.2656969554589,36.08649599090644,20';

$kml = preg_replace('#,\d+(\s|$)#', '$1', $kml);

echo $kml;

Explaining the regex:

 ,\d+(\s|$)
   ^  ^
   |  |------ Pega qualquer espaçamento, como quebra de linha
   |          e espaços, se não tiver então será o '$' que considera
   |          como o final da string
   |
   |---- Pega qualquer numero após a virgula
    
03.05.2017 / 19:12