How can I extract x, y and z coordinates from geographical data by Python?

≡放荡痞女 提交于 2019-12-13 05:36:12

问题


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.


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/489901/how-can-i-extract-x-y-and-z-coordinates-from-geographical-data-by-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!