Skip to content

Instantly share code, notes, and snippets.

@korkey128k
Created May 6, 2014 16:16
Show Gist options
  • Select an option

  • Save korkey128k/16fdfb2f5f04d91e392f to your computer and use it in GitHub Desktop.

Select an option

Save korkey128k/16fdfb2f5f04d91e392f to your computer and use it in GitHub Desktop.

Revisions

  1. korkey128k created this gist May 6, 2014.
    34 changes: 34 additions & 0 deletions Get Average Lat and Long from KML
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    <?php

    $xml = new DOMDocument();
    $xml->validateOnParse = true;

    $file_path = ''; // FilePath. Dur

    if(@$xml->loadXML(file_get_contents($file_path))) :

    $coordinates = $xml->getElementsByTagName('coordinates');
    foreach ($coordinates as $coordinate) {
    $coord_list[] = $coordinate->nodeValue;
    }

    // Clean up the coordinates
    foreach ($coord_list as $coordinates) {
    $coordinates = explode(',0 ', $coordinates);
    $coordinates = array_filter($coordinates);
    $coordinates = array_map('trim', $coordinates);
    }

    $longs = array();
    $lats = array();

    foreach($coordinates as $index => $coordinate_set) {
    $coords = explode(',', $coordinate_set);
    (!empty($coords[0])) ? array_push($longs, $coords[0]) : '';
    (!empty($coords[1])) ? array_push($lats, $coords[1]) : '';
    }

    $longitude = array_sum($longs) / count($longs);
    $latitude = array_sum($lats) / count($lats);

    ?>