在 Mathematica 中处理 KMZ

发布于 2024-11-11 18:47:20 字数 604 浏览 10 评论 0原文

我被困在转换中。

我有一个包含一些坐标的 KMZ 文件。我像这样阅读该文件:

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

我得到这样的信息:

{{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->{}
}}

我想从每个点中提取 {x,y,z} 以及与这些点关联的地标名称 {1,2,3}。即使我可以从 Geometry->{} 中获取点,那也很好,因为我可以使用 List@@@ 将它们提取到列表中,但我迷失在无法提取的基本部分中几何“规则”。

感谢您的帮助,

罗恩

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

酸甜透明夹心 2024-11-18 18:47:20

虽然 Leonid 的答案是正确的,但您可能会发现它不适用于您的代码。原因是 Import 命令的输出包含字符串(例如 "LayerNames"),而不是符号(例如 LayerNames)。我已将 KML 文件上传到我的网站空间,因此我们可以使用实际的 Import 命令来尝试此操作。尝试如下操作:

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

再次注意,"Geometry" 是一个字符串。事实上,in 的内容看起来像这样(在 InputForm 中):

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

上下文:KML 指的是 Keyhole 标记语言。 Keyhole 是一家开发工具的公司,最终被 Google 收购后成为 Google Earth。 KMZ 是 KML 的压缩版本。

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.

南薇 2024-11-18 18:47:20

我认为可以安全地对 Leonid 和 Mark 的答案进行简化,即删除花哨的 Verbatim 构造。也就是说:

Leonid 的第一个操作可以写成:

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

Leonid 的第二个操作:

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

我在导入 Mark 的数据时遇到了麻烦,但据我猜测,可以写成:

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

我会让这个答案的投票告诉我我是否正确。

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.

皓月长歌 2024-11-18 18:47:20

给定你的表达式

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 -> {}}}

这将提取点:

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}}

这将提取地标:

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

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

这是一个更优雅的方法,利用我们正在寻找的规则,它将提取两者:

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}}

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}}
尐偏执 2024-11-18 18:47:20

Transpose[{"PlacemarkNames", "Geometry"} / 怎么样。 m[[1]]] ?

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文