如何使用Python从地理数据中提取x、y和z坐标?

发布于 2024-07-13 01:35:20 字数 561 浏览 5 评论 0原文

我有包含 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 PM方法:5
四次日期:20001001

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

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

这个问题是由 帖子

I have geographical data which has 14 variables. The data is in the following format:

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

The whole data has many previous variables in the sequence.

How can I extract the coordinates ULLAT, ULLON and LRLAT from the data into three lists, so that the each row corresponds to one location?

This question was raised by the problem in the post.

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

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

发布评论

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

评论(2

画中仙 2024-07-20 01:35:21

给定一个 StreamReader 命名阅读器,这应该给你一个 (浮动,浮动,浮动)。 我建议使用三元组列表,因为它可能会更方便、更高效地遍历,除非由于某种原因您只想单独获取所有点。

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)

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

Given a StreamReader named reader, this should give you a list of (float, float, float). I suggest a list of 3-tuples because it'll probably be more convenient and more efficient to walk through, unless for some reason you only want to get all the points individually.

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)

It may work, but it's ugly. I'm no expert at string parsing.

ゝ杯具 2024-07-20 01:35:20

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

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')]

Something like this might work if the data is all in a big flat text file:

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