How to re-write python program into classes so can integrate easily onto GUI (PyQt5)

asked 2020-10-29 02:16:34 -0500

edwin4project gravatar image

How can I re-write my python program into classes? I have watched some basic OOP crash course on youtube but I still do not know how to transform the below code into classes. It would be great if someone can give me some advice thanks!!

The reason why I want to rewrite the python code below is because it will then be easier for me to integrate it onto a GUI.

Below is the OSM_extension_FINALIZED.py

from mpl_toolkits.basemap import Basemap import matplotlib.pyplot as plt from matplotlib.patches import Circle import numpy as np import datetime import shutil import sys import threading import time

import math import urllib2 import StringIO import requests import serial from pynmea import nmea import pynmea2 from io import BytesIO from PIL import Image

i=0 alt = 0

def deg2num(lat_deg, lon_deg, zoom): #converts lat_deg and lon_deg to tile number lat_rad = math.radians(lat_deg) n = 2.0 ** zoom xtile = int((lon_deg + 180.0) / 360.0 * n) ytile = int((1.0 - math.log(math.tan(lat_rad) + (1 / math.cos(lat_rad))) / math.pi) / 2.0 * n)

return (xtile, ytile)

def num2deg(xtile, ytile, zoom): #converts tile numer to lat_deg and lon_deg

n = 2.0 ** zoom lon_deg = xtile / n * 360.0 - 180.0 lat_rad = math.atan(math.sinh(math.pi * (1 - 2 * ytile / n))) lat_deg = math.degrees(lat_rad)

return (lat_deg, lon_deg)

def getImageCluster(lat_deg, lon_deg, delta_lat, delta_long, zoom): headers = {"User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36"} smurl = r"http://a.tile.openstreetmap.org/{0}..."

xmin, ymax = deg2num(lat_deg -0.0025, lon_deg -0.002, zoom) #Adjust lat/lon_deg to change the position of main tile
xmax, ymin = deg2num(lat_deg + delta_lat, lon_deg +0.001 + delta_long, zoom)

bbox_ul = num2deg(xmin, ymin, zoom) 
bbox_ll = num2deg(xmin, ymax + 1, zoom)
print '(UPPER LEFT), (LOWER LEFT)'
print bbox_ul, bbox_ll
print ' '

bbox_ur = num2deg(xmax + 1, ymin, zoom)
bbox_lr = num2deg(xmax + 1, ymax + 1, zoom)
print '(UPPER RIGHT), (LOWER RIGHT)'
print bbox_ur, bbox_lr
print ' '

Cluster = Image.new('RGB',((xmax-xmin+1)*256-1,(ymax-ymin+1)*256-1) ) 
for xtile in range(xmin-1, xmax+1):
    for ytile in range(ymin-1,  ymax+1):
        try:
            imgurl = smurl.format(zoom, xtile, ytile)
            print("Opening: " + imgurl)
            tile = Image.open('SG_'+str(xtile)+'_'+str(ytile)+'.png') #SG_(column)_(row)
            Cluster.paste(tile, box = ((xtile-xmin)*256 ,  (ytile-ymin)*255))
        except: 
            print("Couldn't open image")
            tile = None


return Cluster, [bbox_ll[1], bbox_ll[0], bbox_ur[1], bbox_ur[0]]

def check_serial(): init_serial()

def init_serial(): #To configure and open the serial port global ser

ser = serial.Serial(
    #port = '/dev/ttyACM0', #Use this for Linux
    port = 'COM10', #Use this for Windows
    baudrate = 38400,
    parity = serial.PARITY_NONE,
    stopbits = serial.STOPBITS_ONE,
    bytesize = serial.EIGHTBITS,
)
ser.timeout = 1
ser.isOpen()

print 'Opening ' + ser.name
print ' '

thread()

def stream_serial():

line = ser.readline()

# print "This is to annoy you."

def thread(): #threads - run idependent of main loop thread1 = threading.Thread(target = save_raw) #saves the raw GPS data over serial while the main program runs # thread2 = threading.Thread ... (more)

edit retag flag offensive close merge delete