I have errors with the following PyProcessing code below.
- The first error I get is
TypeError: can’t multiply sequence by non-int of type ‘float’
on the line 'v_lat=v_lat*0.0174533 ’
I think this is due to v_lat being a string and needs to be converted to a floating number.
- So I changed ‘v_lat=row[1]’ to ‘v_lat=float(row[1])’ and I get this error
‘ValueError: invalid literal for float: Latitude’
Appreciate any help/tips with how I can convert string to floating number, assuming this is the cause of the problem. Thanks.
PyProcessing code is:
convert latitude and longitude values to x,y,z co-ordinates
import csv
c_csvfile=‘latlon1.csv’
c_eradius=6378.147 # earth radius, used with lat and lon to calc x y z coordinates
c_radian=0.0174533 # pi/180
def setup():
size(500,500)
background(255)
statsFileHandle=open(c_csvfile)
statsData=csv.reader(statsFileHandle)
for row in statsData:
v_time=row[0]
v_lat=row[1]
v_lon=row[2]
convert to radians
v_lat=v_lat*0.0174533
v_lon=v_lon*0.0174533
#convert to x,y,z
v_x=c_eradius*cos(v_lat)cos(v_lon)
v_y=c_eradiuscos(v_lat)sin(v_lon)
v_z=c_eradiussin(v_lat)
print(str(v_time)+" : "+str(v_lat)+" : "+str(v_lon))
latlon1.csv file has these values:
Time |
Latitude |
Longitude |
2021-06-23 21:41:57 |
-34.0459032 |
151.1109382 |
2021-06-23 21:42:00 |
-34.0459232 |
151.1109829 |
2021-06-23 21:42:04 |
-34.0460907 |
151.1113508 |
2021-06-23 21:42:08 |
-34.0461909 |
151.1115345 |
2021-06-23 21:42:15 |
-34.0461581 |
151.1113933 |
2021-06-23 21:42:19 |
-34.0461696 |
151.1113905 |
2021-06-23 21:42:29 |
-34.0461639 |
151.1114605 |
2021-06-23 21:42:37 |
-34.0461209 |
151.1114227 |
2021-06-23 21:42:38 |
-34.0461209 |
151.1114227 |
1 Like
Your “latlon1.csv” file has a title header for its 3 column names.
Obviously neither “Latitude” nor “Longitude” can be converted to a float().
I’m not well versed on Python’s builtin library so I had to look up on how to use its csv module.
This is what I’ve come up w/. However I haven’t tested it at all. Use it at your own risk:
global coords
coords = []
with open('latlon1.csv') as csvFile:
# Sniff 1st 256 bytes of file for header presence:
hasHeader = csv.Sniffer().has_header(csvFile.read(256))
csvFile.seek(0) # go back to begining of file after the sniff read
csvReader = csv.reader(csvFile)
if hasHeader:
header = next(csvReader) # trick to skip CSV header
print header
for row in csvReader: # file safe to iterate now
stamp = row[0]
lat = radians(float(row[1]))
lon = radians(float(row[2]))
print stamp, lat, lon
coords.append((stamp, lat, lon))
Although mysteriously absent in Python Mode’s web reference:
You should take a look on Processing’s loadTable(), which forum members here are more familiar w/:
3 Likes
Thanks @GoToLoop
Yes, thanks for highlighting that the error refers to the heading line.
I have been caught before with this heading line thing…
I thought it was looking at the values rows and that it was referring the value as a string and somehow it knows that it was of latitude format, so I tried to convert it to a float.
So after skipping the first row, the sketch works as expected.
Cheers
2 Likes
@GoToLoop
so I skipped first row by creating and checking an on/off boolean variable for first row, so as not to process this first row.
I also used the radians function as you have suggested above instead of calculating it ‘manually’
Thanks
You can also multiply by DEG_TO_RAD for the same effect:
lat = radians(float(row[1]))
→ lat = float(row[1]) * DEG_TO_RAD
Didn’t next() work for you? next(csvReader)
Placing a check inside a loop makes it more complex & slow IMO.
Alternatively you can turn the csv.reader() into a list() or tuple() and then slice its 1st element: [1:]
global coords
coords = []
with open('latlon1.csv') as csvFile:
for row in tuple(csv.reader(csvFile))[1:]: # skips 1st header row
stamp = row[0]
lat = float(row[1]) * DEG_TO_RAD
lon = float(row[2]) * DEG_TO_RAD
print stamp, lat, lon
coords.append((stamp, lat, lon))
3 Likes
Thank you for the above, suggestions noted and will test them out. Cheers.