0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

経度、緯度の配列からメッシュコードを計算する

Last updated at Posted at 2023-11-20

経度、緯度の配列からメッシュコードを計算する

jismeshで予期しない値が返ってきたので、自前で実装し直し。

latlon2mesh があったから、numpyの配列を指定できるように改良してみた。

import numpy as np

def latlon2mesh(lat, lon):
    #1次メッシュ上2けた
    lat60 = np.round(lat * 60, decimals=7)
    quotient_lat, remainder_lat = np.divmod(lat60, 40)
    first2digits = quotient_lat.astype(int).astype(str)
    
    #1次メッシュ下2けた
    last2digits = (lon - 100).astype(int).astype(str)
    remainder_lon = lon - lon.astype(int)
    
    #1次メッシュ
    first_mesh = np.char.add(first2digits, last2digits)
    
    #2次メッシュ上1けた
    first1digits, remainder_lat = np.divmod(remainder_lat, 5)
    
    #2次メッシュ下1けた
    remainder_lon60 = np.round(remainder_lon * 60, decimals=7)
    last1digits, remainder_lon = np.divmod(remainder_lon60, 7.5)
    
    #2次メッシュ
    first1digits = first1digits.astype(int).astype(str)
    last1digits  = last1digits.astype(int).astype(str)
    second_mesh = np.char.add(first_mesh, np.char.add(first1digits, last1digits))

    #3次メッシュ上1けた
    remainder_lat60 = np.round(remainder_lat * 60, decimals=7)
    first1digits, remainder_lat = np.divmod(remainder_lat60, 30)

    #3次メッシュ下1けた
    remainder_lon60 = np.round(remainder_lon * 60, decimals=7)
    last1digits, remainder_lon = np.divmod(remainder_lon60, 45)
    
    #3次メッシュ
    first1digits = first1digits.astype(int).astype(str)
    last1digits  = last1digits.astype(int).astype(str)
    third_mesh = np.char.add(second_mesh, np.char.add(first1digits, last1digits))

    print("1次メッシュ:", first_mesh)
    print("2次メッシュ:", second_mesh)
    print("3次メッシュ:", third_mesh)

    
lat0 = 34.875
lon0 = 135.1875

latlon2mesh(
    np.array([lat0, lat0]),
    np.array([lon0, lon0]),
)

np.char.add がネストしているところがあるけど、ここカッコ悪い。。。

import numpy as np

def latlon2mesh(lat, lon):
    #1次メッシュ上2けた
    lat60 = np.round(lat * 60, decimals=7)
    quotient_lat, remainder_lat = np.divmod(lat60, 40)
    first2digits = quotient_lat.astype(int)
    
    #1次メッシュ下2けた
    last2digits = (lon - 100).astype(int)
    remainder_lon = lon - lon.astype(int)
    
    #1次メッシュ
    first_mesh = first2digits * 100 + last2digits
    
    #2次メッシュ上1けた
    first1digits, remainder_lat = np.divmod(remainder_lat, 5)
    
    #2次メッシュ下1けた
    remainder_lon60 = np.round(remainder_lon * 60, decimals=7)
    last1digits, remainder_lon = np.divmod(remainder_lon60, 7.5)
    
    #2次メッシュ
    first1digits = first1digits.astype(int) * 10
    last1digits  = last1digits.astype(int)
    second_mesh = first_mesh * 100 + first1digits + last1digits

    #3次メッシュ上1けた
    remainder_lat60 = np.round(remainder_lat * 60, decimals=7)
    first1digits, remainder_lat = np.divmod(remainder_lat60, 30)

    #3次メッシュ下1けた
    remainder_lon60 = np.round(remainder_lon * 60, decimals=7)
    last1digits, remainder_lon = np.divmod(remainder_lon60, 45)
    
    #3次メッシュ
    first1digits = first1digits.astype(int) * 10
    last1digits  = last1digits.astype(int)
    third_mesh = second_mesh * 100 + first1digits + last1digits

    print("1次メッシュ:", first_mesh)
    print("2次メッシュ:", second_mesh)
    print("3次メッシュ:", third_mesh)

    
lat0 = 34.875
lon0 = 135.1875

latlon2mesh(
    np.array([lat0, lat0]),
    np.array([lon0, lon0]),
)

文字である必要ないので、intにしてみた。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?