Reading existing KML with PHP

2,007 views
Skip to first unread message

MikeR

unread,
Apr 19, 2008, 7:37:07 PM4/19/08
to KML Developer Support - KML Server Side Scripting
I am currently experiencing some frustration while trying to read some
KML generated by an export module for ArcView with the SimpleKML
library in php. I can open the file and navigate through some of the
elements in the document but hit a wall at certain spots.

Here's a rough outline of how the KML file is set up (my comments
prefaced by #):

<Document>
<name>blah</name>
#a bunch more elements
<Style>
#a bunch of stuff in each style element
</Style>
#a whole bunch more Styles
<Folder>
<name>blah blah</name>
<open>0</open>
<Folder>
<name>name here</name>
<SCHEMA_tus_test2_pl>
<name>some name here</name>
<Snippet maxLines="2"></Snippet>
<description>a big long description here in CDATA - note that
all names are CDATA as well</description>
<styleUrl>name to match a previous style here</styleUrl>
<Polygon>
<extrude>0</extrude>
<altitudeMode>clampedToGround</altitudeMode>
<outerBoundaryIs>
<LinearRing>
<coordinates>#big list of coordinate pairs here</
coordinates>
</LinearRing>
</outerBoundaryIs>
</Polygon>
#a bunch more elements relating to this polygon follow
</SCHEMA_tus_test2_pl>
</Folder>
</Folder>

Then another folder begins for the next polygon.

Here's some of the php code I'm testing with right now:

$file="tus_test_pl.kml";
$xml=simplexml_load_file($file);
echo $xml->Document->Folder->name;
echo $xml->Document->Folder->Folder[0]->name;
echo $xml->Document->Folder->Folder[0]->SCHEMA_tus_test2_pl->name;
foreach($xml->Document->Folder->Folder as $folders){
echo $Folder->name, "<br>";
}

The first echo for Document->Folder[0]->name works fine as does the
second. The third echo doesn't work, and the loop foreach loop works
fine. Essentially I'm having a problem navigating the kml object
model past the <SCHEMA_tus_test2_pl> line (by the way, tus_test2_pl
was the kml file name). I'm really interested in getting at the
<description> as well as the <Polygon> <coordinates> so I can upload
the data from kml to a mysql database.

mwill

unread,
Apr 19, 2008, 10:27:41 PM4/19/08
to KML Developer Support - KML Server Side Scripting

MikeR,

I could not recreate the exact problems you are having. However,
there are a couple of other problems one of which might be your
stumbling block.

First, as written, there is an error in your foreach loop. The
variable $folders that you declare in the loop declaration is not the
variable ($Folder) that you use inside the loop. They need to be the
same variable. Is it possible that the loop is not working, but the
3rd echo is?

Second, there is atleast one problem with your KML. As written,
your KML is not valid, for instance <Document> is not closed, KML
namespace is missing, etc. I figure you just posted a snippet as an
example, but make sure your KML is valid. Just as a test, I deleted
the </Document> element from my test file, and the errors only began
at the <SCHEMA....> line. Which brings me to the second KML
problem.... What is <SCHEMA....> doing in there?

mwill

MikeR

unread,
Apr 20, 2008, 12:26:05 AM4/20/08
to KML Developer Support - KML Server Side Scripting
mwill:

Thanks for taking the time to have a look at this.

You are correct that I am just posting a snippet of the actual KML
file. It does indeed have a </Document>. I also caught that $Folder
$folders error in the intervening time - and it indeed will list all
of the <Folder> <name>'s. I've experimented with commenting out
various parts to ensure which parts are working and which aren't. As
to what the <SCHEMA_tus_test2_pl> is doing there - no idea. It was
auto generated by an export filter from ArcView. I work with a guy
who specializes in GIS work with ArcView/ArcIMS etc. He sent me the
data and my job's to get it into a web site. I'm thinking about
building my own parser right now as the SimpleXML isn't getting me
terribly far so far.

Hmm... I just re-read the KML File and found where the SCHEMA thing
might come from. There's a section near the top before the <Style>
and <Folders> that has a section that starts like this:

<Schema parent="Placemark" name="SCHEMA_tus_test2_pl">
<SimpleField type="wstring" name="Local_Id_Num"></SimpleField>
<SimpleField type="wstring" name="TUS_ID"></SimpleField>
<SimpleField type="wstring" name="Common_Name"></SimpleField>
<SimpleField type="wstring" name="Alt_Name1"></SimpleField>

It's quite long so I just included the first few lines. I'm really
not an expert at xml or kml - just trying to muddle my way along right
now.

mwill

unread,
Apr 20, 2008, 8:34:08 PM4/20/08
to KML Developer Support - KML Server Side Scripting

MikeR,

Ok, I see what <Schema...> is doing in there, to a degree, although
since it isn't using any custom data fields, I don't see the point.
But that is all besides the point, the real problem is the trouble
you're having with SimpleXML. What version of PHP are you using?
Certain functions of SimpleXML are only valid in certain versions of
PHP.
Beyond that, I think we're at the point that we need to see the
whole PHP file and source KML file. If you can post a link to them or
post them in the "Pages" section, we can take a look. I use SimpleXML
all the time with no problems, and would like to help you work through
these difficulties.

mwill

MikeR

unread,
May 1, 2008, 1:03:24 PM5/1/08
to KML Developer Support - KML Server Side Scripting
Sorry for taking so long to get back on to this - some things came up
that both put this project on the back burner and made me completely
forget about this topic for a while.

In any case, I found out the problem after coming back to it fresh
(read: had to look at it from the start to remember where I left off)
and I discovered I had been looking at another version of the exported
kml than I was using. When I looked at the structure of the kml file I
was using I discovered it had a different tag nesting structure than
the other one and a bit of playing around got things back on track.
That's a big relief! Again, thanks for your patience and assistance.

Essentially I found how to get to the coordinates for the first
polygon with:

Document->Folder->Folder[0]->Placemark->outerBoundary->LinearRing-
>coordinates

Wow, that's a lot of typing.

mwill

unread,
May 2, 2008, 1:03:19 AM5/2/08
to KML Developer Support - KML Server Side Scripting
MikeR,
It's great to hear you got a fresh look at your project. I take it
from your post that you got it all figured out. If you have any other
problems we're here.

mwill
> > mwill- Hide quoted text -
>
> - Show quoted text -
Reply all
Reply to author
Forward
0 new messages