Pada bahasan sebelumnya tentang Raspberry Pi dan GPS kita telah berhasil mendapatkan data GPS menggunakan gpsd daemon. Kali ini, kita akan sedikit bermain dengan python untuk membaca data latitude/longitude dari GPS dan memplotnya pada OpenStreeMap seperti pada contoh di bawah ini.
Untuk mengecek apakah GPS anda sudah dapat konek ke komputer atau laptop anda, silahkan ikuti tutorial di link ini Raspberry Pi dan GPS. Tutorial pada link tersebut tidak hanya untuk Raspberry Pi, tapi bisa juga untuk OS Linux lainnya.
Install packages yang dibutuhkan
Jika anda sudah mengikuti petunjuk pada link ini Raspberry Pi dan GPS, maka ketika anda mengetik cgps -s, informasi GPS anda akan tampil seperti di bawah ini
langkah berikutnya adalah menginstall python-gps dan smopy.
> sudo apt-get install python-gps
> sudo pip install smopy
Saatnya untuk menguji GPS anda menggunakan python
Langkah pertama, simpan code berikut ini dengan nama "StreetMap.py".
import smopy as smp
import numpy as np
def interp(delta,x0,x1,lon,x):
m = delta/(x0-x1)
lon2 = m*x + lon + delta - m * x0
return lon2
def OpenStreetMap(lat,lon,map,zoom):
# For comparison to find other locations
delta = 10**-3
x0, y0 = map.to_pixels(lat+delta, lon+delta)
x1, y1 = map.to_pixels(lat,lon)
# Check quadrant
if x1 <= 128. and y1 <= 128.:
Quadrant = 1
# Map 2
x2 = -x1
# Map3
x3 = -y1
myX = np.round(x1+256)
myY = np.round(y1+256)
elif x1 <= 128. and y1 > 128.:
Quadrant = 2
# Map 2
x2 = -x1
# Map3
x3 = 2*256 - y1
myX = np.round(x1+256)
myY = np.round(y1)
elif x1 > 128. and y1 > 128.:
Quadrant = 3
# Map 2
x2 = 2*256 - x1
# Map3
x3 = 2*256 - y1
myX = np.round(x1)
myY = np.round(y1)
else:
Quadrant = 4
# Map 2
x2 = 2*256 - x1
# Map3
x3 = -y1
myX = np.round(x1)
myY = np.round(y1+256)
lon2 = interp(delta,x0,x1,lon,x2)
lat2 = lat
lat3 = interp(delta,y0,y1,lat,x3)
lon3 = lon2
lat4 = lat3
lon4 = lon
map2 = smp.Map(lat2,lon2,z=zoom)
map3 = smp.Map(lat3,lon3,z=zoom)
map4 = smp.Map(lat4,lon4,z=zoom)
if Quadrant == 1:
image2 = map.to_numpy()
image1 = map2.to_numpy()
image4 = map3.to_numpy()
image3 = map4.to_numpy()
elif Quadrant == 2:
image3 = map.to_numpy()
image4 = map2.to_numpy()
image1 = map3.to_numpy()
image2 = map4.to_numpy()
elif Quadrant == 3:
image4 = map.to_numpy()
image3 = map2.to_numpy()
image2 = map3.to_numpy()
image1 = map4.to_numpy()
else:
image1 = map.to_numpy()
image2 = map2.to_numpy()
image3 = map3.to_numpy()
image4 = map4.to_numpy()
image41 = np.vstack((image4,image1))
image32 = np.vstack((image3,image2))
image_all = np.dstack(([image41],[image32]))[0]
image_new = image_all[myY-128:myY+128,myX-128:myX+128,::]
return image_all, myX, myY
import numpy as np
def interp(delta,x0,x1,lon,x):
m = delta/(x0-x1)
lon2 = m*x + lon + delta - m * x0
return lon2
def OpenStreetMap(lat,lon,map,zoom):
# For comparison to find other locations
delta = 10**-3
x0, y0 = map.to_pixels(lat+delta, lon+delta)
x1, y1 = map.to_pixels(lat,lon)
# Check quadrant
if x1 <= 128. and y1 <= 128.:
Quadrant = 1
# Map 2
x2 = -x1
# Map3
x3 = -y1
myX = np.round(x1+256)
myY = np.round(y1+256)
elif x1 <= 128. and y1 > 128.:
Quadrant = 2
# Map 2
x2 = -x1
# Map3
x3 = 2*256 - y1
myX = np.round(x1+256)
myY = np.round(y1)
elif x1 > 128. and y1 > 128.:
Quadrant = 3
# Map 2
x2 = 2*256 - x1
# Map3
x3 = 2*256 - y1
myX = np.round(x1)
myY = np.round(y1)
else:
Quadrant = 4
# Map 2
x2 = 2*256 - x1
# Map3
x3 = -y1
myX = np.round(x1)
myY = np.round(y1+256)
lon2 = interp(delta,x0,x1,lon,x2)
lat2 = lat
lat3 = interp(delta,y0,y1,lat,x3)
lon3 = lon2
lat4 = lat3
lon4 = lon
map2 = smp.Map(lat2,lon2,z=zoom)
map3 = smp.Map(lat3,lon3,z=zoom)
map4 = smp.Map(lat4,lon4,z=zoom)
if Quadrant == 1:
image2 = map.to_numpy()
image1 = map2.to_numpy()
image4 = map3.to_numpy()
image3 = map4.to_numpy()
elif Quadrant == 2:
image3 = map.to_numpy()
image4 = map2.to_numpy()
image1 = map3.to_numpy()
image2 = map4.to_numpy()
elif Quadrant == 3:
image4 = map.to_numpy()
image3 = map2.to_numpy()
image2 = map3.to_numpy()
image1 = map4.to_numpy()
else:
image1 = map.to_numpy()
image2 = map2.to_numpy()
image3 = map3.to_numpy()
image4 = map4.to_numpy()
image41 = np.vstack((image4,image1))
image32 = np.vstack((image3,image2))
image_all = np.dstack(([image41],[image32]))[0]
image_new = image_all[myY-128:myY+128,myX-128:myX+128,::]
return image_all, myX, myY
Dan langkah kedua, simpan kode di bawah ini dengan nama "MyLocation.py" dan letakkan di folder yang sama dengan file "StreetMap.py".
import matplotlib.pyplot as plt
import StreetMap as sm
import smopy as smp
import gps
# For reading gps data
session = gps.gps()
session.stream(gps.WATCH_ENABLE|gps.WATCH_NEWSTYLE)
OnOff = 1
while OnOff == 1:
report = session.next()
if report['class'] == 'TPV':
lat = report.lat
lon = report.lon
OnOff = 0
# To plot your location on OpenStreetMap
zoom = 5 # Zoom Level (Maximum is 19)
map = smp.Map(lat,lon,z=zoom)
myLoc, x, y = sm.OpenStreetMap(lat,lon,map,zoom)
plt.plot(x,y,'r^',markersize=8)
plt.imshow(myLoc)
plt.axis('off')
plt.show()
import StreetMap as sm
import smopy as smp
import gps
# For reading gps data
session = gps.gps()
session.stream(gps.WATCH_ENABLE|gps.WATCH_NEWSTYLE)
OnOff = 1
while OnOff == 1:
report = session.next()
if report['class'] == 'TPV':
lat = report.lat
lon = report.lon
OnOff = 0
# To plot your location on OpenStreetMap
zoom = 5 # Zoom Level (Maximum is 19)
map = smp.Map(lat,lon,z=zoom)
myLoc, x, y = sm.OpenStreetMap(lat,lon,map,zoom)
plt.plot(x,y,'r^',markersize=8)
plt.imshow(myLoc)
plt.axis('off')
plt.show()
Parameter zoom pada kode di atas digunakan untuk memperbesar peta. Langkah terakhir, jalankan program tersebut dengan mengetik
> python MyLocation.py
Hasilnya kurang lebih seperti di bawah ini
ZOOM = 5 |
ZOOM = 7 |
ZOOM = 9 |
ZOOM = 11 |
ZOOM = 19 |
Kode python dapat juga didownload di link ini download.
Selamat mencoba dan semoga bermanfaat.
No comments:
Post a Comment