{"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":"This notebook demonstrates the python code to determine if a horse's postion (lat lon) is in a small rectangle at the finish line on the dirt track at Saratoga. I gathered the lat and lon of the points of the box from google maps, so it will not be perfectly accurate as I picked the points by hand, but should help you be able to determine who was the first to cross the finish line barring any photo finishes.  It could also be used to calculate the horses order at the different distance poles on the race track during the race call.\n\nIMPORTANT: Remember that the horses may race past the finish line once before they cross it to determine the winner and this may be different at Aqueduct and Belmont. This will depend on the length of the race.\n\nHere is a link to the google map for Sartaoga with a pin on the finish line at the inside rail. https://www.google.com/maps/@43.0721191,-73.7704986,51m/data=!3m1!1e3?hl=en-US\n\nIf you use / improve this code, please share it.","metadata":{}},{"cell_type":"code","source":"#poly is a polygon at the dirt finish line at saratoga\npoly=[[43.072239, -73.7706697],[43.072235, -73.770706],[43.072481, -73.770881],[43.072474,-73.770889]]\n\n#horse lat and long position\n# below position should be just across line and return true\nlst=[[43.072242,-73.770708]]\n\n#below position should be just before the finish line and not be in the finish box and return false\n#lst=[[43.072270,-73.770676]]\n         \nfrom shapely.geometry import Point\nfrom shapely.geometry.polygon import Polygon\nimport pandas as pd \n\n#finsih polygon to dataframe\ndf_poly = pd.DataFrame(poly, columns =['Lat', 'Lon'], dtype = float)\n\n#horse coordinate to check if in finish poly\nx = pd.DataFrame(lst, columns =['Lat', 'Lon'], dtype = float)\n#df output if it is in the finish poly\ndf_points = pd.DataFrame(lst, columns =['Lat', 'Lon'], dtype = float) \n\npolygon = Polygon([tuple(x) for x in df_poly[['Lat', 'Lon']].to_numpy()])\n\nprint (\"Finsh Line Box\",polygon)\n\ndf_points['Within'] = df_points.apply(lambda x: polygon.contains(Point(x['Lat'], x['Lon'])), axis=1)\n\ndf_points","metadata":{"execution":{"iopub.status.busy":"2022-08-22T21:48:35.401727Z","iopub.execute_input":"2022-08-22T21:48:35.402147Z","iopub.status.idle":"2022-08-22T21:48:35.447896Z","shell.execute_reply.started":"2022-08-22T21:48:35.402116Z","shell.execute_reply":"2022-08-22T21:48:35.446676Z"},"trusted":true},"execution_count":null,"outputs":[]}]}