Sorting placemark into an uncertain amount of folders (php, sql, dom)

36 views
Skip to first unread message

Chris Mel

unread,
Oct 19, 2009, 2:08:18 PM10/19/09
to KML Developer Support - KML Server Side Scripting
I'm looking for an easy way to out placemark automatically into
folders. I have written some php scripts that takes a kismet (wifi-
sniffer) logfile and put the data into a mysql-database. Some other
scripts do the kml-outout with php's DOM.

My 'main'-kml file just uses three folder (open, wep, wpa), hence it's
easy to sort placemark into the right folder using some if-clauses.

Now I what to create some more advanced kmls with different sortings
e.g. vendor-lookup based on MAC-Adresse stored in my DB.

Problem now: I won't know how many different 'vendor'-folders there
will be, and I can't handle them all with if-constructs.

As a first try I created the all the folders:

require_once ('dbinfo.php');


function convertstrings($text)
{
return htmlentities(htmlentities($text));
}

// Create header etc...
$Dom = new DOMDocument('1.0', 'UTF-8');
$NameSpace = $Dom->createElementNS('http://www.opengis.net/kml/2.2',
'kml');
$Dom->appendChild($NameSpace);
$DocumentNode = $Dom->createElement('Document');
$NameSpace->appendChild($DocumentNode);
$DocumentName = $Dom->createElement('name','Vendor-Map');
$DocumentNode->appendChild($DocumentName);
$FolderNodeMain = $Dom->createElement('Folder');
$FolderNodeMain->setAttribute('id','Vendor');
$DocumentNode->appendChild($FolderNodeMain);
$FolderNameMain = $Dom->createElement('name','Vendor');
$FolderNodeMain->appendChild($FolderNameMain);

$query = "SELECT DISTINCT vendor_ID, vendor_OUI, vendor_Name FROM
vendor";
$result = mysql_query($query);
while ($row = @mysql_fetch_assoc($result))
{
$row[vendor_Name] = convertstrings($row[vendor_Name]);

$Folder = $Dom->createElement('Folder');
$Folder->setAttribute('id','vendor_'.$row[vendor_ID]);
$FolderNodeMain->appendChild($Folder);
$FolderName = $Dom->createElement('name',$row[vendor_Name]);
$Folder->appendChild($FolderName);

$Dom->formatOutput = true;
$kmlOutput = $Dom->saveXML();
//header('Content-type: application/vnd.google-earth.kml+xml');
echo $kmlOutput;

}

This will output something like this:
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<name>Vendor-Map</name>
<Folder id="Vendor">
<name>Vendor</name>
<Folder id="vendor_1">
<name>Siemens</name>
</Folder>
<Folder id="vendor_2">
<name>SURECOM</name>
</Folder>
</Folder>
</Document>
</kml>

Everything's fine so far, but how do I get the placemark into the
right folder?

My placemark-query looks like this:

$query = "SELECT *
FROM ap
LEFT OUTER JOIN vendor ON left(ap_BSSID,8) = vendor_OUI
$result = mysql_query($query);

So, when doing a while-loop, $row[vendor_Name] contains the name of
the vendor of the current entry. How to put that entry into the right
folder, e.g. 'Siemens'?

greets

Chris

Nymor

unread,
Nov 9, 2009, 10:55:38 PM11/9/09
to KML Developer Support - KML Server Side Scripting
Hi,

While I'm sure there are various ways to do this within the DOM - I'm
not to familiar with that - I have come across a similar need myself
creating nested XML content and the way I do it is below. It's written
in psuedo-code but hopefully gets the idea across.


****************

sql = Select Placemark, VendorName order by VendorName

Set a counter $i=0
Set a variable for vendor $vendor ='initialvalueprerow1' // can be
anything that isn't a possible vendor name

while $row {

if ($row[vendor]!=$vendor) { // This will be the case when Vendor
changes incl 1st row
if ($i!=0} {close vendor folder} // if not the 1st row then close
previous vendor folder
$vendor = $row[vendor]
$i++
open vendor folder
}

output $row[placemark]

} //loop to next row

close vendor folder // this closes the last vendor folder outside the
while loop

*************************


This effectively opens a new folder when the vendor changes and as the
results are in vendor order they will be grouped - and closes the
vendor folder before doing so if one is already open.

Hope it makes sense and the logic is of some use - works for me.

Regards
Nymor
Reply all
Reply to author
Forward
0 new messages