4

I'm stuck on a conversion.

I have a KMZ file with some coordinates. I read the file like this:

m=Import["~/Desktop/locations.kmz","Data"]

I get something like this:

{{LayerName->Point Features,
  Geometry->{
    Point[{-120.934,49.3321,372}],
    Point[{-120.935,49.3275,375}],
    Point[{-120.935,49.323,371}]},
  Labels->{},LabeledData->{},ExtendedData->{},
  PlacemarkNames->{1,2,3},
  Overlays->{},NetworkLinks->{}
}}

I want to extract the {x,y,z} from each of the points and also the placemark names {1,2,3} associated with the points. Even if I can just get the points out of Geometry->{} that would be fine because I can extract them into a list with List@@@, but I'm lost at the fundamental part where I can't extract the Geometry "Rule".

Thanks for any help,

Ron

1
  • 1
    Ron, would you please update your answer to show actual data you get from Import? It should apparently contain strings.
    – Mr.Wizard
    Jun 2, 2011 at 20:23

4 Answers 4

5

While Leonid's answer is correct, you will likely find that it does not work with your code. The reason is that the output of your Import command contains strings, such as "LayerNames", rather than symbols, such as LayerNames. I've uploaded a KML file to my webspace so we can try this using an actual Import command. Try something like the following:

in = Import["http://facstaff.unca.edu/mcmcclur/my.kml", "Data"];
pointList = "Geometry" /.  
    Cases[in, Verbatim[Rule]["Geometry", _], Infinity];
pointList /. Point[stuff_] -> stuff

Again, note that "Geometry" is a string. In fact, the contents of in look like so (in InputForm):

{{"LayerName" -> "Waypoints", 
  "Geometry" -> {Point[{-82.5, 32.5, 0}]}, 
  "Labels" -> {}, "LabeledData" -> {}, 
  "ExtendedData" -> {}, "PlacemarkNames" -> {"asheville"}, 
  "Overlays" -> {}, "NetworkLinks" -> {}}}

Context: KML refers to Keyhole Markup Language. Keyhole was a company that developed tools that ultimately became Google Earth, after they were acquired by Google. KMZ is a zipped version of KML.

3
  • Holy cow, folks, thanks for jumping right on this. I really appreciate how responsive this group is, especially to my somewhat basic questions.
    – Ron
    Jun 2, 2011 at 17:33
  • Mark, is my suggested simplification correct? My logic is that "Geometry" /. ... will return the first value found for a rule "Geometry" -> ... and therefore Cases[..., 1] should be equivalent, assuming that such a rule exists somewhere within in.
    – Mr.Wizard
    Jun 2, 2011 at 20:21
  • @Mr.Wizard Your code works fine. I've added the correct version of an imported KML file so you can try it. Jun 3, 2011 at 2:14
4

A simplification to Leonid and Mark's answers that I believe can be made safely is to remove the fancy Verbatim construct. That is:

Leonid's first operation can be written:

Join @@ Cases[expr, (Geometry -> x_) :> (x /. Point -> Sequence), Infinity]

Leonid's second operation:

Join @@ Cases[expr, (PlacemarkNames -> x_) :> x, Infinity]

I had trouble importing Mark's data, but from what I can guess, one could write:

pointList = Cases[in, ("Geometry" -> x_) :> x, Infinity, 1]

I'll let the votes on this answer tell me if I am correct.

11
  • @Gandalf KML import is new in V8. In V7, you could import as XML, as in Import["file.kml","XML"]. Of course, you then need to process the result accordingly. Jun 2, 2011 at 19:06
  • @Mr.Wizard Did you see my alternative method at the bottom of my post? I think it is simple enough (although it doesn't matter since, as @Mark pointed out, it won't work. I based it on the OP's info, did not know these are returned as strings) Jun 2, 2011 at 19:07
  • Thanks Lenoid (and everyone else), I used parts from yours (for the placemarks) and also Mark's. I'm trying the simplified version now to understand how it works.
    – Ron
    Jun 2, 2011 at 20:13
  • @Leonid, yes I did. I just thought it was worth pointing out that (as far as I can tell) Verbatim is not required.
    – Mr.Wizard
    Jun 2, 2011 at 20:15
  • @Mr.Wizard I see your point. You are right. If I would not use this syntax of Cases, but instead a briefer one (with just a pattern, like "Geometry" -> x_), then Verbatim would be needed. Jun 2, 2011 at 20:28
3

Given your expression

expr = {{LayerName -> Point Features, 
       Geometry -> {
         Point[{-120.934, 49.3321, 372}], 
         Point[{-120.935, 49.3275, 375}],
         Point[{-120.935, 49.323, 371}]},
     Labels -> {}, LabeledData -> {}, ExtendedData -> {}, 
     PlacemarkNames -> {1, 2, 3}, Overlays -> {}, NetworkLinks -> {}}}

This will extract the points:

In[121]:= 
   Flatten[Cases[expr, Verbatim[Rule][Geometry, x_] :> (x /. Point -> Sequence),
        Infinity], 1]

Out[121]= {{-120.934, 49.3321, 372}, {-120.935, 49.3275,375}, {-120.935, 49.323, 371}}

And this will extract the placemarks:

In[124]:= Flatten[Cases[expr, Verbatim[Rule][PlacemarkNames, x_] :> x, Infinity], 1]

Out[124]= {1, 2, 3}

Here is a more elegant method exploiting that we are looking for rules, that will extract both:

In[127]:= 
{Geometry, PlacemarkNames} /.Cases[expr, _Rule, Infinity] /. Point -> Sequence

Out[127]= 
{{{-120.934, 49.3321, 372}, {-120.935, 49.3275,375}, {-120.935, 49.323, 371}}, {1, 2, 3}}
0

How about Transpose[{"PlacemarkNames", "Geometry"} /. m[[1]]] ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.