2012/08/15

geometry tips: generates identifier of standard grid cell which covers Japan region from latitude and longitude.

In geometric operation with python, sometimes we map point (which is latitude and longitude pair) to corresponding standard grid cell. Here is sample code for mapping point to identifier of the cell.
#!/usr/bin/env python
#coding: utf-8

def get_meshcode_from_latlon(lat, lon):
    lat, lon = lat * 3600, lon * 3600

    l_code = []

    l_code += str(int(lat/2400+100))[1:3]
    l_code += str(int(lon/3600+100))[1:3]
    t_lat, t_lon = int(lat%2400), int(lon%3600)

    l_code += str(t_lat/300)
    l_code += str(t_lon/450)
    t_lat, t_lon = t_lat % 300, t_lon % 450

    l_code += str(t_lat/30)
    l_code += str(t_lon/45)
    t_lat, t_lon = t_lat % 30, t_lon % 45

    if (t_lat < 15):
        if (t_lon < 23):
            l_code += '1'
        else:
            l_code += '2'
    else:
        if (t_lat < 23):
            l_code += '3'
        else:
            l_code += '4'

    return ('').join(l_code)

if __name__ == "__main__":
    lat = 34.12345
    lon = 139.12345
    print get_meshcode_from_latlon(lat, lon)

    lat = 30.12345
    lon = 130.12345
    print get_meshcode_from_latlon(lat, lon)

No comments:

Post a Comment

100