{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"### Animating the path of a smartphone GPS signal\nFor the GPS positions we shall be using data provided by the [Google Smartphone Decimeter Challenge 2022](https://www.kaggle.com/competitions/smartphone-decimeter-2022)","metadata":{}},{"cell_type":"code","source":"%matplotlib inline\nfrom matplotlib import rc\nfrom IPython.display import HTML\nimport matplotlib.animation as animation\n\nimport numpy as np \nimport pandas as pd\n\nimport matplotlib.pyplot as plt\n#plt.style.use('fivethirtyeight')\nplt.rcParams.update({'font.size': 14})\n#plt.rcParams[\"figure.figsize\"] = (8, 8)\nimport seaborn as sns\nimport plotly.express as px\n\nimport datetime\nfrom datetime import timedelta, date\n\nimport cartopy.crs as ccrs\nimport cartopy.io.img_tiles as cimgt","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-05-03T10:32:53.824878Z","iopub.execute_input":"2022-05-03T10:32:53.825223Z","iopub.status.idle":"2022-05-03T10:32:53.834778Z","shell.execute_reply.started":"2022-05-03T10:32:53.825184Z","shell.execute_reply":"2022-05-03T10:32:53.833701Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"read in the data","metadata":{}},{"cell_type":"code","source":"GPS_data = pd.read_csv(\"../input/smartphone-decimeter-2022/train/2020-12-10-US-SJC-2/GooglePixel4/ground_truth.csv\")\n# convert Unix epoch time (\"UnixTimeMillis\") to a datetime\nGPS_data[\"time\"] = pd.to_datetime(GPS_data[\"UnixTimeMillis\"],unit='ms')","metadata":{"execution":{"iopub.status.busy":"2022-05-03T10:32:53.951455Z","iopub.execute_input":"2022-05-03T10:32:53.952269Z","iopub.status.idle":"2022-05-03T10:32:53.962819Z","shell.execute_reply.started":"2022-05-03T10:32:53.952220Z","shell.execute_reply":"2022-05-03T10:32:53.961911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"get a map of the region of interest using [Cartopy](https://scitools.org.uk/cartopy/docs/latest/index.html)","metadata":{}},{"cell_type":"code","source":"Lat_min = round(GPS_data[\"LatitudeDegrees\"].min(),3) - 0.001\nLat_max = round(GPS_data[\"LatitudeDegrees\"].max(),3) + 0.001\nLon_min = round(GPS_data[\"LongitudeDegrees\"].min(),3) - 0.001\nLon_max = round(GPS_data[\"LongitudeDegrees\"].max(),3) + 0.001\n\nrequest = cimgt.OSM()\nextent  = [Lon_min, Lon_max, Lat_min, Lat_max]","metadata":{"execution":{"iopub.status.busy":"2022-05-03T10:32:53.997777Z","iopub.execute_input":"2022-05-03T10:32:53.998357Z","iopub.status.idle":"2022-05-03T10:32:54.006376Z","shell.execute_reply.started":"2022-05-03T10:32:53.998304Z","shell.execute_reply":"2022-05-03T10:32:54.005299Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"now create our animation","metadata":{}},{"cell_type":"code","source":"%%capture\nn_steps = 75\n\nfig = plt.figure(figsize=(20,5))\ndef animate(time_stepper):\n    i = time_stepper * 8 # plot every 8th row, i.e. every 8 seconds\n    ax = fig.add_subplot(1,1,1, projection=request.crs)\n    ax.set_extent(extent)\n    ax.add_image(request,16)\n    tmp_Lat = GPS_data.iloc[i]['LatitudeDegrees']\n    tmp_Lon = GPS_data.iloc[i]['LongitudeDegrees']\n    ax.scatter(tmp_Lon,tmp_Lat, color='red', s=100, transform=ccrs.PlateCarree())\n\nanim = animation.FuncAnimation(fig, animate, frames=n_steps, save_count=n_steps, repeat=False)\nHTML(anim.to_jshtml())","metadata":{"execution":{"iopub.status.busy":"2022-05-03T11:25:34.106336Z","iopub.execute_input":"2022-05-03T11:25:34.106796Z","iopub.status.idle":"2022-05-03T11:30:23.807818Z","shell.execute_reply.started":"2022-05-03T11:25:34.106748Z","shell.execute_reply":"2022-05-03T11:30:23.807161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Now press play to watch the animation","metadata":{}},{"cell_type":"code","source":"HTML(anim.to_jshtml())","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-05-03T11:30:30.459184Z","iopub.execute_input":"2022-05-03T11:30:30.459444Z","iopub.status.idle":"2022-05-03T11:30:30.775595Z","shell.execute_reply.started":"2022-05-03T11:30:30.459418Z","shell.execute_reply":"2022-05-03T11:30:30.774397Z"},"trusted":true},"execution_count":null,"outputs":[]}]}