【问题标题】:How can I extract x, y and z coordinates from geographical data by Python?如何通过 Python 从地理数据中提取 x、y 和 z 坐标?
【发布时间】:2009-01-28 23:36:31
【问题描述】:

我有包含 14 个变量的地理数据。数据格式如下:

QUADNAME:rockport_colony_SD 分辨率:10 ULLAT:43.625
乌隆:-97.87527466 LRLAT:43.5
LRLON:-97.75027466 HDATUM:27
ZMIN: 361.58401489 ZMAX: 413.38400269 ZMEAN:396.1293335 ZSIGMA:12.36359215 方法:5
QUADDATE:20001001

整个数据在序列中有许多先前的变量。

如何将数据中的坐标ULLAT、ULLON和LRLAT提取到三个列表中,使每一行对应一个位置?

这个问题是由the post中的问题提出的。

【问题讨论】:

  • 多行数据是像上面这样还是格式问题?
  • 您可以使用一个三元组列表,而不是三个列表。
  • 另外,我认为“LRNAT”是指“LRLAT”。
  • Unkwntech:这是格式问题。

标签: python extraction geography


【解决方案1】:

如果数据都在一个大的平面文本文件中,这样的事情可能会起作用:

import re

data = """
QUADNAME: rockport_colony_SD RESOLUTION: 10 ULLAT: 43.625
ULLON: -97.87527466 LRLAT: 43.5
LRLON: -97.75027466 HDATUM: 27
ZMIN: 361.58401489 ZMAX: 413.38400269 ZMEAN: 396.1293335 ZSIGMA: 12.36359215 PMETHOD: 5
QUADDATE: 20001001
"""

regex = re.compile(
    r"""ULLAT:\ (?P<ullat>-?[\d.]+).*?
    ULLON:\ (?P<ullon>-?[\d.]+).*?
    LRLAT:\ (?P<lrlat>-?[\d.]+)""", re.DOTALL|re.VERBOSE)

print regex.findall(data) # Yields: [('43.625', '-97.87527466', '43.5')]

【讨论】:

  • @Nick:抱歉,我不明白你的评论——你是说我可以让一些东西更具可读性吗?这只是一种概念验证演示,展示了如何使用正则表达式解析数据。
  • 谢谢!我现在在 .py 文件中有你的代码。如何使用它来处理 .txt 文件?我猜我们在.py文件中需要一个参数,这样我们就可以使用类似$ py-file file-to-be-processed的语法
  • @Masi:听起来应该是另一个问题的内容!
  • @cdleary:这里有一个新帖子:stackoverflow.com/questions/491085/…
【解决方案2】:

给定一个名为StreamReader 的读者,这应该会给你一个(float,float,float)列表。我建议使用 3 元组列表,因为它可能会更方便和更有效地遍历,除非出于某种原因您只想单独获得所有点。

coords = []
reader
while line=reader.readline():

  index_ullat = line.find("ULLAT")
  if index_ullat >= 0:
    ullat = float(line[ index_ULLAT+7 : ])

    line = reader.readline()

    index_ullon = line.find("ULLON")
    index_lrlat = line.find("LRLAT")
    if index_ullon >= 0 and index_lrlat >= 0:
      ullon = float(line[ index_ullon+7 : index_lrlat-1 ])
      lrlat = float(line[ index_lrlat+7 : ])
    else:
      raise InputError, "ULLON and LRLAT didn't follow ULLAT."

    coords.append(ullat, ullon, lrlat)

它可能有效,但它很难看。我不是字符串解析方面的专家。

【讨论】:

  • 编辑:只是指向新的、更漂亮的文档的链接。 :-)
猜你喜欢
  • 1970-01-01
  • 2014-05-18
  • 2019-04-10
  • 2022-11-13
  • 1970-01-01
  • 1970-01-01
  • 2022-12-10
  • 1970-01-01
  • 2019-10-22
相关资源
最近更新 更多