Conversor latlon <--> local reference EPSG. By default it will be used the reference system EPSG:Tokyo 54N
+proj=utm --> UTM projection +zone=54 --> zone Japan +ellps=bessel --> ellipsoid Bessel (1841, used for Europe and Japan) +towgs84=-146.414,507.337,680.507,0,0,0,0 +units=m --> meters
# ### PARAMETERS
## default reference definition for Japan
sproj4 = "+proj=utm +zone=54 +ellps=bessel +towgs84=-146.414,507.337,680.507,0,0,0,0 +units=m +no_defs"
# ### CONVERSOR: LATLONG --> NEW REFERENCE
def latlon2ref(lat,lon,sreference):
"""
in: lat,lon = numpy array
out: x, y = numpy array / p = pyproj object
"""
from pyproj import Proj
# create object projection
p = Proj(sreference)
# conversion to the defined projection
x,y = p(lon, lat)
# return
return (x,y,p)
# ### CONVERSOR: REFERENCE --> LATLONG
def ref2latlon(y,x,p):
"""
in: x, y = numpy array / p = pyproj object
out: lat,lon = numpy array
"""
# conversion
lon, lat = p(x, y, inverse=True)
return (lat,lon)
# ### MAIN
if __name__ == "__main__":
""" CALCULATE DISTANCE: Osaka, Tokyo """
from math import sqrt
# input parameters
sproj4 = "+proj=utm +zone=54 +ellps=GRS80 +towgs84=-146.414,507.337,680.507,0,0,0,0 +units=m +no_defs"
lat1 = 34.6937378; lon1 = 135.50216509999996 # Osaka
lat2 = 35.6894875; lon2 = 139.69170639999993 # Tokyo
dresult = 396. # km
## CONVERSION: latlon --> new ref
x1,y1,p1 = latlon2ref(lat1,lon1,sproj4)
x2,y2,p2 = latlon2ref(lat2,lon2,sproj4)
# distance calculation
dx = x2 - x1
dy = y2 - y1
d = sqrt(dx*dx+dy*dy)/1000.
print('Distance Osaka-Tokyo: Real = %.3f km Calculated = %.3f km'%(dresult,d))
## CONVERSION: new ref --> latlon
print('\nOsaka:',ref2latlon(y1,x1,p1),'Original:',(lat1,lon1))
print('Tokyo:',ref2latlon(y2,x2,p2),'Original:',(lat2,lon2))
""" CALCULATE DISTANCE: Madrid, Barcelona """
from math import sqrt
# input parameters
sproj4 = "+proj=lcc +lat_1=40 +lat_0=40 +lon_0=0 +k_0=0.9988085293 +x_0=600000 +y_0=600000 +a=6378298.3 +b=6356657.142669561 +pm=madrid +units=m +no_defs "
lat1 = 40.4167754; lon1 = -3.7037901999999576 # Madrid
lat2 = 41.3850639; lon2 = 2.1734034999999494 # Barcelona
dresult = 505. #km
## CONVERSION: latlon --> new ref
x1,y1,p1 = latlon2ref(lat1,lon1,sproj4)
x2,y2,p2 = latlon2ref(lat2,lon2,sproj4)
# distance calculation
dx = x2 - x1
dy = y2 - y1
d = sqrt(dx*dx+dy*dy)/1000.
print('Distance Madrid-Barcelona: Real = %.3f km Calculated = %.3f km'%(dresult,d))
## CONVERSION: new ref --> latlon
print('\nMadrid:',ref2latlon(y1,x1,p1),'Original:',(lat1,lon1))
print('Barcelona:',ref2latlon(y2,x2,p2),'Original:',(lat2,lon2))
""" MULTIPLE CONVERSION (Japan) """
import numpy as np
# input parameters
sproj4 = "+proj=utm +zone=54 +ellps=GRS80 +towgs84=-146.414,507.337,680.507,0,0,0,0 +units=m +no_defs"
lat1 = np.array([47.6,47.5,47.4,47.3,47.2,47.1,47])
lon1 = np.array([120,120.125,120.25,120.375,120.5,120.625,120.75])
# CONVERSION: latlon --> new ref
x,y,p = latlon2ref(lat1,lon1,sproj4)
# CONVERSION: new ref --> latlon
lat2, lon2 = ref2latlon(y,x,p)
print('Original Latitudes:',lat1)
print('Calculated Latitudes:',lat2)
print('Original Longitudes:',lon1)
print('Calculated Longitudes:',lon2)