Python> I made a test code for my own external file

Operating environment


Xeon E5-2620 v4 (8 cores) x 2
32GB RAM
CentOS 6.8 (64bit)
openmpi-1.8.x86_64 and its-devel
mpich.x86_64 3.1-5.el6 and its-devel
gcc version 4.4.7 (And gfortran)
NCAR Command Language Version 6.3.0
WRF v3.7.Use 1.
Python 3.6.0 on virtualenv

Related geometry> Distance when deviated by 1 degree (latitude direction or longitude direction) at 2 points [km] Related geometry> Find the distance from two latitudes and longitudes> Use Haversine formula

I wanted to make a test code in the process of converting the calculation process to an external file.

I did the following.

File to be tested v0.2

UtilGeolocation.py


from math import sin, cos, radians, sqrt, asin

'''
v0.2 Mar 31, 2017
    - add calc_distance_center_shift()
v0.1 Mar 31, 2017
    - add get_shifted_position()
    - add calc_distance_2places()
'''
# codingrule:PEP8


def calc_distance_2places(pos0, pos1, radius=None):
    '''
    distance based on Haversine formula
    Ref: https://en.wikipedia.org/wiki/Haversine_formula

    (pos0, pos1[, radius])
    where
      pos0 is (lat, lon) in [km]
      pos1 is (lat, lon) in [km]
      radius is in [km]
    '''
    if radius is None:
        radius = 6378.137  # km (Earth's radius)
    latang1, lonang1 = pos0
    latang2, lonang2 = pos1
    phi1, phi2 = radians(latang1), radians(latang2)
    lam1, lam2 = radians(lonang1), radians(lonang2)
    term1 = sin((phi2 - phi1) / 2.0) ** 2
    term2 = sin((lam2 - lam1) / 2.0) ** 2
    term2 = cos(phi1) * cos(phi2) * term2
    wrk = sqrt(term1 + term2)
    wrk = 2.0 * radius * asin(wrk)
    return wrk


def get_shifted_position(pos0, shift_lat_deg=0.0, shift_lon_deg=0.0):
    lat, lon = pos0
    return (lat + shift_lat_deg, lon + shift_lon_deg)


def calc_distance_center_shift(center, shift):
    '''
    distance with shifted (latitude, longitude)
    '''
    clat, clon = center
    slat, slon = shift

    lat_shifted = (clat + slat, clon + 0.0)
    dist_lat_km = calc_distance_2places(center, lat_shifted)

    lon_shifted = (clat + 0.0, clon + slon)
    dist_lon_km = calc_distance_2places(center, lon_shifted)
    return (dist_lat_km, dist_lon_km)


#-------------------------------------------------

if __name__ == '__main__':
    print("Help on calc_distance_2places:")
    print(calc_distance_2places.__doc__)
    print("Help on calc_distance_center_shift:")
    print(calc_distance_center_shift.__doc__)

Test file v0.1

The import method doesn't look very good.

testGeolocation.py


from UtilGeolocation import calc_distance_2places
from UtilGeolocation import calc_distance_center_shift
from UtilGeolocation import get_shifted_position

'''
v0.1 Mar. 31, 2017
    - add Test_calc_distance_center_shift()
    - add Test_calc_distance_2places()
'''
# codingrule:PEP8


def Test_calc_distance_2places():
    # at Location1
    Osaka1 = 34.702113, 135.494807
    print("Osaka:")
    Osaka2 = get_shifted_position(Osaka1, shift_lat_deg=1.0)
    print("  %.2f km for 1 deg latitude"
          % calc_distance_2places(Osaka1, Osaka2))
    Osaka2 = get_shifted_position(Osaka1, shift_lon_deg=1.0)
    print("  %.2f km for 1 deg longitude"
          % calc_distance_2places(Osaka1, Osaka2))

    # at Location2
    print("London:")
    London1 = 51.476853, 0.0
    London2 = get_shifted_position(London1, shift_lat_deg=1.0)
    print("  %.2f km for 1 deg latitude"
          % calc_distance_2places(London1, London2))
    London2 = get_shifted_position(London1, shift_lon_deg=1.0)
    print("  %.2f km for 1 deg longitude"
          % calc_distance_2places(London1, London2))


def Test_calc_distance_center_shift():
    Osaka1 = 34.702113, 135.494807
    print("Osaka:")
    dist = calc_distance_center_shift(Osaka1, shift=(1.0, 1.0))
    print("  %.2f km, %.2f km" % dist)


if __name__ == '__main__':
    Test_calc_distance_2places()
    Test_calc_distance_center_shift()

Example of use

Run


$ python UtilGeolocation.py 
Help on calc_distance_2places:

    distance based on Haversine formula
    Ref: https://en.wikipedia.org/wiki/Haversine_formula

    (pos0, pos1[, radius])
    where
      pos0 is (lat, lon) in [km]
      pos1 is (lat, lon) in [km]
      radius is in [km]
    
Help on calc_distance_center_shift:

    distance with shifted (latitude, longitude)
    

Run


$ python testGeolocation.py 
Osaka:
  111.32 km for 1 deg latitude
  91.52 km for 1 deg longitude
London:
  111.32 km for 1 deg latitude
  69.33 km for 1 deg longitude
Osaka:
  111.32 km, 91.52 km

Recommended Posts

Python> I made a test code for my own external file
I made a python dictionary file for Neocomplete
I made my own Python library
I made a configuration file with Python
I made a VM that runs OpenCV for Python
I made my own language. (1)
[Python] I made a classifier for irises [Machine learning]
I made a python text
I made my own language (2)
I made my own AML
[VSCode] I made a user snippet for Python print f-string
I made a Line-bot using Python!
I made a fortune with Python.
I made a daemon with Python
I made a script in Python to convert a text file for JSON (for vscode user snippet)
I made a lot of files for RDP connection with Python
I made a scaffolding tool for the Python web framework Bottle
I made my own filter plugin for text parsing of Ansible
I made a library that adds docstring to a Python stub file.
[python] I made a class that can write a file tree quickly
I made a Python wrapper library for docomo image recognition API.
I made a dash docset for Holoviews
I made a character counter with Python
A tool for easily entering Python code
I made a Hex map with Python
After studying Python3, I made a Slackbot
I made a roguelike game with Python
I made a simple blackjack with Python
I made a library for actuarial science
A textbook for beginners made by Python beginners
I made a neuron simulator with Python
I made a Docker container to use JUMAN ++, KNP, python (for pyKNP).
I made a program to check the size of a file in Python
I made a competitive programming glossary with Python
I made a spare2 cheaper algorithm for uWSGI
I made a useful tool for Digital Ocean
I made a GUI application with Python + PyQt5
I made a Twitter fujoshi blocker with Python ①
Procedure for creating a LineBot made with Python
[Python] I made a Youtube Downloader with Tkinter.
I made a downloader for word distributed expression
I made my own primitive static site generator
I tried reading a CSV file using Python
I want to write to a file with Python
I made a peeping prevention product for telework.
I made a Caesar cryptographic program in Python.
I made a bin picking game with Python
I made a Mattermost bot with Python (+ Flask)
I made a Python Qiita API wrapper "qiipy"
I made a QR code image with CuteR
I want to create a nice Python development environment for my new Mac
I made a package to create an executable file from Hy source code
I made a tool in Python that right-clicks an Excel file and divides it into files for each sheet.
I made my own parallel link robot (software version)
I made a Twitter BOT with GAE (python) (with a reference)
I made a prime number generation program in Python
〇✕ I made a game
I made a login / logout process using Python Bottle.
I made a Christmas tree lighting game with Python
I made a window for Log output with Tkinter
I want to write in Python! (2) Let's write a test