Try converting latitude / longitude and world coordinates to each other with python

What I wanted to do

--I want to aggregate latitude and longitude information in units divided into meshes. ――Therefore, I wanted to place the latitude and longitude in the section xy with reduced resolution. -Google Maps API fromLatLngToPoint I wanted to do the same thing offline in Python

What i did

Based on the information in TRAIL NOTE's http://www.trail-note.net/tech/coordinate/, I tried to make it a function that converts latitude / longitude and world coordinates with python.

Implementation

I just copied the contents of ↑.

from math import pi
from math import tanh
from math import sin
from math import asin
from numpy import arctanh

# refer from http://www.trail-note.net/tech/coordinate/

def tile2latlon(x, y, z):
    L = 85.05112878
    lon = ((x / 2.0**(z+7) )-1) * 180
    lat = 180/pi * (asin(tanh(-pi/2**(z+7)*y + arctanh(sin(pi/180*L)))))
    return [lat, lon]

def latlon2tile2(lat, lon, z):
    L = 85.05112878
    x = int((lon/180 + 1) * 2**(z+7))
    y = int( (2**(z+7) / pi * ( -arctanh(sin(pi*lat/180)) + arctanh(sin(pi*L/180)) ) ))
    return [x,y]

Verification

Enter a known latitude and longitude and check it on Map of Geographical Survey Institute.

#simbashi
lat = 35.666280
lon = 139.758375
# lat = 30.335927
# lon = 130.504283

pix = 256
# lat/lon to tile
for z in range(15,19):
    a,b = latlon2tile2(lat,lon,z)
    print [a,b]
    print 'http://cyberjapandata.gsi.go.jp/xyz/std/{0}/{1:d}/{2:d}.png'.format(z,a/pix,b/pix)
    a,b = tile2latlon(a,b,z)
    print [a,b]

When executed, it will be output like this, so it matches the original lat / long, and the accuracy increases as the zoom level increases. I confirmed the location on the actual map. (Map of Geographical Survey Institute is up to zoom level 18)

[7450910, 3303678]
http://cyberjapandata.gsi.go.jp/xyz/std/15/29105/12904.png
[35.66629207402928, 139.75836753845215]
[14901820, 6607356]
http://cyberjapandata.gsi.go.jp/xyz/std/16/58210/25809.png
[35.66629207402928, 139.75836753845215]
[29803640, 13214713]
http://cyberjapandata.gsi.go.jp/xyz/std/17/116420/51619.png
[35.66628335763601, 139.75836753845215]
[59607281, 26429426]
http://cyberjapandata.gsi.go.jp/xyz/std/18/232840/103239.png
[35.66628335763601, 139.75837290287018]

Items to be confirmed

--I don't really understand the geodetic system --There seems to be a library such as pyproj, so it may be better to use this. If you know how to use it, please let me know!

Recommended Posts

Try converting latitude / longitude and world coordinates to each other with python
Latitude / longitude coordinates ↔ UTM coordinate conversion with python
[Python] Convert general-purpose container and class to each other
Try to operate DB with Python and visualize with d3
Try to bring up a subwindow with PyQt5 and Python
Try to operate Facebook with Python
Try converting cloudmonkey CLI to python3 -1
Try to display google map and geospatial information authority map with python
[Python] Try to recognize characters from images with OpenCV and pyocr
Try to reproduce color film with Python
Fractal to make and play with Python
Try converting to tidy data with pandas
Try to make foldl and foldr with Python: lambda. Also time measurement
Hello World and face detection with OpenCV 4.3 + Python
Python code to convert region mesh code to latitude / longitude
Scraping tabelog with python and outputting to CSV
MessagePack-Try to link Java and Python with RPC
Try to make BOT by linking spreadsheet and Slack with python 2/2 (python + gspread + slackbot)
Try to solve the man-machine chart with Python
Try to draw a life curve with python
Try to make BOT by linking spreadsheet and Slack with python 1/2 (python + gspread + slackbot)
Try to communicate with EV3 and PC! (MQTT)
Say hello to the world with Python with IntelliJ
Try to make a "cryptanalysis" cipher with Python
Try to automatically generate Python documents with Sphinx
WEB scraping with python and try to make a word cloud from reviews
Try to make a dihedral group with Python
[Python learning part 3] Convert pandas DataFrame, Series, and standard List to each other
Distance calculation between two latitude and longitude points with python (using spherical trigonometry)
Try to detect fish with python + OpenCV2.4 (unfinished)
Perform a Twitter search from Python and try to generate sentences with Markov chains.
Give latitude and longitude point sequence data and try to identify the road from OpenStreetMap data
Procedure to load MNIST with python and output to png
Try to solve the programming challenge book with python3
[First API] Try to get Qiita articles with Python
Try to make a command standby tool with python
I want to handle optimization with python and cplex
Try to solve the internship assignment problem with Python
Install selenium on Mac and try it with python
Try to automate pdf format report creation with Python
YOLP: Extract latitude and longitude with Yahoo! Geocoder API.
Something to enjoy with Prim Pro (X-Play) and Python
Convert latitude, longitude, GPS altitude to 3D Cartesian coordinates
Try scraping with Python.
Try to generate a cyclic peptide from an amino acid sequence with Python and RDKit
Easy to use Nifty Cloud API with botocore and python
Try to make it using GUI and PyQt in Python
screen and split screen with python and ssh login to remote server
Try Amazon Simple Workflow Service (SWF) with Python and boto3
Try to display various information useful for debugging with python
[AWS] Try adding Python library to Layer with SAM + Lambda (Python)
[Python] How to play with class variables with decorator and metaclass
Send experiment results (text and images) to slack with Python
Try to link iTunes and Hue collection case with MQTT
How to do Bulk Update with PyMySQL and notes [Python]
Try to automate the operation of network devices with Python
[Let's play with Python] Image processing to monochrome and dots
Convert video to black and white with ffmpeg + python + opencv
I tried to make GUI tic-tac-toe with Python and Tkinter
It's Halloween so I'll try to hide it with Python
Just try to receive a webhook in ngrok and python