Skip to content

Instantly share code, notes, and snippets.

@johngrantuk
Last active July 16, 2017 15:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johngrantuk/b96529305e5983b216db05d4d776d3fc to your computer and use it in GitHub Desktop.
Save johngrantuk/b96529305e5983b216db05d4d776d3fc to your computer and use it in GitHub Desktop.
"""
13/07/18
Reads csv file and plots lat/lng positions.
Markers are coloured to match satellite number when signal > -100 or are coloured red when signal = -100
"""
import cartopy.crs as ccrs
import numpy as np
import matplotlib.pyplot as plt
from datetime import datetime
import traceback
f = open('FileName.csv','r')
latArr = []
lngArr = []
strength = []
satLngArr = []
lines = f.readlines() # Read file and close
f.close()
startDate = datetime(2017, 7, 2) # Used to skip data out of required range
print("Parsing data...")
for line in lines: # Iterate over lines in file
try:
print('.', end='')
splitLine = line.split(',') # Data comma separated - 2017-07-10 22:31:59:203,Processing UpdatePacket: [':', '1', '0', '0', '1', '0', '0', '1.63', '17.15', '246.57', '114.11', '57.008263', '-5.827861', '310.00', '1', 'NAN', '0', '2', '0', 'c\n']
if len(splitLine) <= 1: # Check line is a data packet
continue
if splitLine[1] == "Processing UpdatePacket: [':'": # Check line is a data packet
currentDateTime = datetime.strptime(splitLine[0], '%Y-%m-%d %H:%M:%S:%f') # Parse date time
if currentDateTime > startDate: # Check data falls within required range
st = splitLine[8].strip() # Strip spaces off start and end
st = st.strip("'") # Strip unwanted quotation marks
strength.append(float(st)) # Save strength info
lat = splitLine[12].strip() # Save lat info
lat = lat.strip("'")
latArr.append(float(lat))
lng = splitLine[13].strip() # Save lng info
lng = lng.strip("'")
lngArr.append(float(lng))
satlng = splitLine[14].strip() # Parse satellite info
satlng = float(satlng.strip("'"))
if satlng == 310: # Satellite data is displayed using colour.
colour = 'b'
elif satlng == 60:
colour = 'g'
elif satlng == 302:
colour = 'y'
else:
colour = 'k'
if float(st) == -100: # Red means there was no signal at that point
colour = 'r'
satLngArr.append(colour) # Save sat info for plotting
else:
continue
except Exception:
print("Error")
print(traceback.format_exc())
print(line)
print(len(splitLine))
print("Plotting...")
strengthNpArray = np.array(strength) # Used for scaling marker
area = (strengthNpArray)/(strengthNpArray) # All markers same size for easier viewing. 1 point radii
# area = np.pi * (strengthNpArray)**2 # Use to scales marker relative to strength. 0 to 15 point radii
ax = plt.axes(projection=ccrs.Mercator()) # Map projection
ax.coastlines(resolution='10m') # Adds coastline to map at highest resolution
plt.scatter(lngArr, latArr, s=area, c=satLngArr, alpha=0.5, transform=ccrs.Geodetic()) # Plot
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment