How to parse/process a KMZ file on my server using PHP/JavaScript?

8.5k views Asked by At

I have been looking around and all that I could find are answers related to using google maps. What I want to do is get the information stored inside to "import" it to my database.

As far as I know, KMZ files are zip files of KML (like xml).

The KMZ that i want to process looks like this (when unzipped locally):

A KML file (doc.kml) with the following information:

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
  <Document>
    <name>KmlFile</name>
    <Placemark>
      <description>
        <![CDATA[<div style='border: 1px solid #aaa; font-size: 20px;'><span style='padding: 1em; display: inline-block;'><img src='files/tags_1470231.jpg' /></span></div>
<div style='background-color: #aaa; color: white; text-align: right; padding: 0.5em 0.5em 0 0;'>Posted: 2013/12/26</div>]]>
        <![CDATA[<div style='border: 1px solid #aaa; font-size: 20px;'><span style='padding: 1em; display: inline-block;'>test
</span></div>
<div style='background-color: #aaa; color: white; text-align: right; padding: 0.5em 0.5em 0 0;'>Posted: 2013/12/26</div>]]>
        <![CDATA[<div style='border: 1px solid #aaa; font-size: 20px;'><span style='padding: 1em; display: inline-block;'>cool pic</span></div>
<div style='background-color: #aaa; color: white; text-align: right; padding: 0.5em 0.5em 0 0;'>Posted: 2013/12/26</div>]]>
      </description>
      <visibility>0</visibility>
      <Point>
        <coordinates>135.753498,35.024914,0</coordinates>
      </Point>
    </Placemark>
    <Placemark>
      <description>
        <![CDATA[<div style='border: 1px solid #aaa; font-size: 20px;'><span style='padding: 1em; display: inline-block;'><img src='files/tags_1470232.jpg' /></span></div>
<div style='background-color: #aaa; color: white; text-align: right; padding: 0.5em 0.5em 0 0;'>Posted: 2013/12/26</div>]]>
        <![CDATA[<div style='border: 1px solid #aaa; font-size: 20px;'><span style='padding: 1em; display: inline-block;'>panda
</span></div>
<div style='background-color: #aaa; color: white; text-align: right; padding: 0.5em 0.5em 0 0;'>Posted: 2013/12/26</div>]]>
      </description>
      <visibility>0</visibility>
      <Point>
        <coordinates>135.753482,35.024868,0</coordinates>
      </Point>
    </Placemark>
  </Document>
</kml>

And a folder called "files" with pictures inside it.

In theory I suppose I have to unzip this file in my server, read the doc.kml file, parse it to get the coordinates and the picture name for each of the elements, and then process this information.

What I am having trouble with is that I am not sure about how to unzip this file and process it in my server for each of the users that use the website.

Am i supposed to use a temporal location in my server? wont the files overwrite if several users do it at the same time? How do i reference each of the images once it has been unziped?

I am using a PHP site, and I plan on showing a preview with each of the images along with its coordinates for the uploaded KMZ file.

Any help would be greatly appreciated.

2

There are 2 answers

1
FredFury On

Here is some examples of code to uncompress the KMZ to the KML. This is quite easily done in php. Here is two php example:

<?php
$data = file_get_contents("http://example.com/some_file.kmz"); // url of the KMZ file
file_put_contents("/tmp/kmz_temp",$data);
ob_start();
passthru('unzip -p /tmp/kmz_temp');
$xml_data = ob_get_clean();
header("Content-type: text/xml");
echo $xml_data;
?> 

From: http://dtbaker.net/web-development/how-to-convert-kmz-to-kml/ This worked fine on PHP 5.5.9, but I've found issues with this on PHP 5.4.35. I've solved this with the following code:

<?php
$zip = new ZipArchive;
$res = $zip->open('some_file.kmz');
if ($res === TRUE) {
  $zip->extractTo('/DestinationFolder/');
  $zip->close();
  echo 'Success!';
} else {
  echo 'errors';
}
?>

I got this from this post: Unzip a file with php From there you can pretty much handle a KML exactly like any other XML.

Hope that helps!

1
John Chrysostom On

Unzipping a zip file with PHP: Unzip a file with php

As for making sure users don't upload on top of each other, consider using a time stamp, using the tempnam() function (https://www.php.net/tempnam), having a separate folder for each user, or using an auto-increasing database column to keep track of the uploads. Any method will work fine to get you a unique file name for each upload so that they don't duplicate.

Hopefully this will point you in the right direction.