How to remove point altitude in a Poligono?

Asked

Viewed 45 times

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 value of the coordinate, ie the 20, represents the altitude, according to the documentation, which I don’t need. So at first I made a foreach thus below:

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

But if altitude changes in any situation, this rule explode won’t work. What would be a viable way to solve this problem and get only the condemned ignoring the altitude? A regular expression can solve this?

1 answer

3


If the Poligono point is always at the end of the line and the number 20 can be variant, you can use before saving to 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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.