Python code to convert region mesh code to latitude / longitude

Introduction

I have the opportunity to work with big data whose location is provided in the regional mesh code, and I've created a function that converts it to a latitude / longitude form that is easy to handle in the program, so I'll share it.

Referenced site

This article corresponds to the conversion code up to the 3rd mesh published in this blog up to 1/8 area mesh.

What is a regional mesh code?

According to an article in Wikipedia

The area mesh (chiiki mesh) is a mesh that divides the area into meshes of almost the same size based on latitude and longitude for use in statistics. The code for identifying the mesh is called the regional mesh code.

In the production and provision of maps, paper maps are prepared for each regional mesh, or digital map data files are prepared.

It is standardized by JIS X 0410. It is organized in an easy-to-understand manner on the Esri Japan site.

Execution environment

Function to convert from mesh code to latitude / longitude

def get_latlon(meshCode):

    #Convert to string
    meshCode = str(meshCode)

    #Calculation for primary mesh
    code_first_two = meshCode[0:2]
    code_last_two = meshCode[2:4]
    code_first_two = int(code_first_two)
    code_last_two = int(code_last_two)
    lat  = code_first_two * 2 / 3
    lon = code_last_two + 100

    if len(meshCode) > 4:
        #Calculation for secondary mesh
        if len(meshCode) >= 6:
            code_fifth = meshCode[4:5]
            code_sixth = meshCode[5:6]
            code_fifth = int(code_fifth)
            code_sixth = int(code_sixth)
            lat += code_fifth * 2 / 3 / 8
            lon += code_sixth / 8

        #Calculation for 3rd mesh
        if len(meshCode) >= 8:
            code_seventh = meshCode[6:7]
            code_eighth = meshCode[7:8]
            code_seventh = int(code_seventh)
            code_eighth = int(code_eighth)
            lat += code_seventh * 2 / 3 / 8 / 10
            lon += code_eighth / 8 / 10
            
        # 1/Calculation for 2 mesh
        if len(meshCode) >= 9:
            code_nineth = meshCode[8:9]
            code_nineth = int(code_nineth)
            if code_nineth % 2 == 0:
                lon += 0.00138888
            if code_nineth > 2:
                lat += 0.00416666
                
        # 1/Calculation for 4 mesh
        if len(meshCode) >= 10:
            code_tenth = meshCode[9:10]
            code_tenth = int(code_tenth)
            if code_tenth % 2 == 0:
                lon += 0.00138888 / 2
            if code_tenth > 2:
                lat += 0.00416666 / 2
        
        # 1/Calculation for 8 mesh
        if len(meshCode) >= 11:
            code_eleventh = meshCode[10:11]
            code_eleventh = int(code_eleventh)
            if code_eleventh % 2 == 0:
                lon += 0.00138888 / 2 / 2
            if code_eleventh > 2:
                lat += 0.00416666 / 2 / 2
            
    return lat, lon

How to use the function

If you are familiar with Python, you can see it,

lat,lon =  get_latlon(60413212422)

If you do, the latitude will be assigned to lat and the longitude will be assigned to lon.

Recommended Posts

Python code to convert region mesh code to latitude / longitude
Python script to convert latitude / longitude to mesh code
Convert cubic mesh code to WKT in Python
Convert python 3.x code to python 2.x
Rewrite Python2 code to Python3 (2to3)
Convert STL to Voxel mesh using Python VTK
Convert latitude, longitude, GPS altitude to 3D Cartesian coordinates
[python] Convert date to string
Convert numpy int64 to python int
[Python] Convert list to Pandas [Pandas]
Convert Scratch project to Python
[Python] Convert Shift_JIS to UTF-8
Convert markdown to PDF in Python
Workflow to convert formula (image) to python
Convert list to DataFrame with python
Python> list> Convert double list to single list
[Python] Convert natural numbers to ordinal numbers
Convert decimal numbers to n-ary numbers [python]
Python> tuple> Convert double tuple to single tuple
I wrote a code to convert quaternions to z-y-x Euler angles in Python
Convert memo at once with Python 2to3
Convert Python> two value sequence to dictionary
[Python] How to convert a 2D list to a 1D list
How to convert Python to an exe file
[Python] Convert csv file delimiters to tab delimiters
Try converting latitude / longitude and world coordinates to each other with python
Convert psd file to png in Python
Convert Excel data to JSON with python
Convert Hiragana to Romaji with Python (Beta)
Convert from katakana to vowel kana [python]
Convert FX 1-minute data to 5-minute data with Python
python> Convert tuple to list> aList = list (pi_tuple)
Convert Python date types to RFC822 format
Convert HEIC files to PNG files with Python
Convert Chinese numerals to Arabic numerals with Python
Convert from Markdown to HTML in Python
Convert absolute URLs to relative URLs in Python
Sample to convert image to Wavelet with Python
Convert FBX files to ASCII <-> BINARY in Python
Convert PDF to image (JPEG / PNG) with Python
Convert "number" of excel date to python datetime
Convert PDFs to images in bulk with Python
[Python] Convert from DICOM to PNG or CSV
Python beginners tried to code some energy drinks
Latitude / longitude coordinates ↔ UTM coordinate conversion with python
Convert svg file to png / ico with Python
Personal notes to doc Python code in Sphinx
[Automation] Convert Python code into an exe file
Convert Windows epoch values to date with python
How to convert SVG to PDF and PNG [Python]
List of Python code to move and remember
Convert the character code of the file with Python3
Convert exponential notation float to str in Python
I want to make C ++ code from Python code!
Convert strings to character-by-character list format with python
Updated to Python 2.7.9
Convert to HSV
python character code
[Python] Algorithm-aware code
"Backport" to python 2
Convert timezoned date and time to Unixtime in Python2.7