บทความนี้ผมจะพาทุกท่านไประบุตำแหน่งที่เราอยู่ใน Python ด้วยเทคโนโลยี Geolocation ครับ
Geolocation คือการระบุพิกัดละติจูดและลองติจูด เพื่อบอกตำแหน่งที่เราอยู่ อาคัยทั้งฐานข้อมูลที่อยู่พิกัดจาก IP Address และ GPS สำหรับบนคอมพิวเตอร์ส่วนใหญ่จะไม่มี GPS กัน ผมเลยใช้ Geolocation จาก IP Address แทน
สำหรับบน Python เราต้องใช้โมดูลเพิ่มเติมด้วย
ในการจับคู่ข้อมูล IP Address กับฐานข้อมูลที่อยู่พิกัด ผมเลือกฐานข้อมูล GeoLite City ของ Maxmind ซึ่งใช้งานได้ฟรีครับ (CC-BY-SA 3.0) โหลด GeoLite City ได้จาก http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
เมื่อโหลดมาเสร็จแล้วแตกไว้โฟลเดอร์ ในการเรียกใช้ฐานข้อมูล GeoLite City ของ Maxmind ต้องใช้โมดูลเสริมที่มีชื่อว่า pygeoip (GPL 3)สามารถติดตั้งได้ด้วย pip ด้วยคำสั่ง
pip install pygeoipหลังจากนั้นเรามาเริ่มโค้ดกันเลยครับ
import pygeoip
rawdata = pygeoip.GeoIP('GeoLiteCity.dat')
def ipquery(ip):
data = rawdata.record_by_name(ip)
country = data['country_name']
city = data['city']
longi = data['longitude']
lat = data['latitude']
print('[x] '+str(city)+',' +str(country))
print('[x] Latitude: '+str(lat)+ ', Longitude: '+ str(longi))
a = ipquery("49.230.***.**")
เสร็จแล้วบันทึกไฟล์ไว้ที่โฟลเดอร์เดียวกันกับไฟล์ GeoLiteCity.datผลลัพธ์
[x] None,Thailandหากเราจะเขียนให้ดึงข้อมูลจาก IP Address ปัจจุบันนี้ไปหาตำแหน่ง เราต้องเขียนคำสั่งด้วยค่า IP Address ปัจจุบันของเรามาจะได้
[x] Latitude: 13.**, Longitude: 100.4***
from urllib.request import urlopen
import json
ip = urlopen('http://httpbin.org/ip').read() #ดึงค่า IP Address จากเว็บ http://httpbin.org/ip
ip = ip.decode('utf-8')
ip = json.loads(ip)
import pygeoip
rawdata = pygeoip.GeoIP('GeoLiteCity.dat')
def ipquery(ip):
data = rawdata.record_by_name(ip)
country = data['country_name']
city = data['city']
longi = data['longitude']
lat = data['latitude']
print('[x] '+str(city)+',' +str(country))
print('[x] Latitude: '+str(lat)+ ', Longitude: '+ str(longi))
a = ipquery(ip['origin'])
ผลลัพธ์[x] None,Thailandติดตามบทความต่อไปนะครับ
[x] Latitude: 13.**, Longitude: 100.4***
ขอบคุณครับ

มีประโยชน์มากครับ เอาไปใช้ในโปรเจคที่กำลังทำไ้ด้พอดี ถ้า GPS ใช้ module เดียวกันหรือเปล่าครับ
ตอบลบใช้โมดูลคนละตัวกันครับ เพราะโมดูลในบทความใช้ที่อยู่ IP Address ในการระบุตำแหน่งครับ ถ้าจะใช้ GPS ต้องมีการดึงค่าพิกัดละติจูดและลองติจูดจาก GPS module ของอุปกรณ์มาเก็บแทนครับ
ลบรายละเอียดตามนี้ครับ
https://www.sparkfun.com/tutorials/403
http://www.danmandle.com/blog/getting-gpsd-to-work-with-python/
ขอบคุณทีให้คำแนะนำครับ
ตอบลบ