简体   繁体   English

如何使用PHP / JavaScript解析/处理我的服务器上的KMZ文件?

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

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). 据我所知,KMZ文件是KML的zip文件(如xml)。

The KMZ that i want to process looks like this (when unzipped locally): 我想要处理的KMZ看起来像这样(在本地解压缩时):

A KML file (doc.kml) with the following information: 包含以下信息的KML文件(doc.kml):

<?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. 还有一个名为“files”的文件夹,里面有图片。

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. 理论上我想我必须在我的服务器中解压缩这个文件,读取doc.kml文件,解析它以获取每个元素的坐标和图片名称,然后处理这些信息。

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. 我正在使用PHP网站,我计划显示每个图像的预览以及上传的KMZ文件的坐标。

Any help would be greatly appreciated. 任何帮助将不胜感激。

Unzipping a zip file with PHP: Unzip a file with php 使用PHP解压缩zip文件: 使用php解压缩文件

As for making sure users don't upload on top of each other, consider using a time stamp, using the tempnam() function ( http://us1.php.net/tempnam ), having a separate folder for each user, or using an auto-increasing database column to keep track of the uploads. 至于确保用户不上传彼此之上,请考虑使用时间戳,使用tempnam()函数( http://us1.php.net/tempnam ),为每个用户设置单独的文件夹,或者使用自动增加数据库列来跟踪上载。 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. 希望这会指出你正确的方向。

Here is some examples of code to uncompress the KMZ to the KML. 以下是将KMZ解压缩到KML的代码示例。 This is quite easily done in php. 这在php中很容易完成。 Here is two php example: 这是两个php示例:

<?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. 来自: http//dtbaker.net/web-development/how-to-convert-kmz-to-kml/这在PHP 5.5.9上运行良好,但我在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. 我从这篇文章得到了这个: 用php解压缩文件从那里你几乎可以像任何其他XML一样处理KML。

Hope that helps! 希望有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM