Search code examples
pythonextractgeography

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


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.


Solution

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