{"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":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport glob\nimport matplotlib.pyplot as plt\nfrom tqdm.notebook import tqdm\nfrom pathlib import Path\nimport plotly.express as px\n\n#Ignore warnings\nimport warnings\nwarnings.filterwarnings('ignore')\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-05-02T22:34:21.355797Z","iopub.execute_input":"2022-05-02T22:34:21.356105Z","iopub.status.idle":"2022-05-02T22:34:21.984972Z","shell.execute_reply.started":"2022-05-02T22:34:21.356077Z","shell.execute_reply":"2022-05-02T22:34:21.983942Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Thank you Kuto for your magnificent, pleasant script.  By the way, I ruined it.\n\nhttps://www.kaggle.com/code/kuto0633/road-detection-and-creating-grid-points","metadata":{}},{"cell_type":"code","source":"#Code by Kuto https://www.kaggle.com/code/kuto0633/road-detection-and-creating-grid-points\n\ndata_dir = Path(\"../input/smartphone-decimeter-2022\")\ntrain_df = pd.read_csv(data_dir / \"train/2021-04-29-US-MTV-2/SamsungGalaxyS20Ultra/ground_truth.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-05-02T21:58:36.883758Z","iopub.execute_input":"2022-05-02T21:58:36.884172Z","iopub.status.idle":"2022-05-02T21:58:36.897269Z","shell.execute_reply.started":"2022-05-02T21:58:36.884144Z","shell.execute_reply":"2022-05-02T21:58:36.896326Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by Kuto https://www.kaggle.com/code/kuto0633/road-detection-and-creating-grid-points\n\n# get all ground truth dataframe\ngt_df = pd.DataFrame()\nfor (MessageType, Provider), df in tqdm(train_df.groupby([\"MessageType\", \"Provider\"])):\n    path = data_dir / f\"train/2021-04-29-US-MTV-2/SamsungGalaxyS20Ultra/ground_truth.csv\"\n    df = pd.read_csv(path)  \n    gt_df = pd.concat([gt_df, df]).reset_index(drop=True)   \ngt_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-05-02T22:12:41.818297Z","iopub.execute_input":"2022-05-02T22:12:41.818607Z","iopub.status.idle":"2022-05-02T22:12:41.883020Z","shell.execute_reply.started":"2022-05-02T22:12:41.818570Z","shell.execute_reply":"2022-05-02T22:12:41.882127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by Kuto https://www.kaggle.com/code/kuto0633/road-detection-and-creating-grid-points\n\nfig = px.scatter_mapbox(gt_df,\n\n                    # Here, plotly gets, (x,y) coordinates\n                    lat=\"LatitudeDegrees\",\n                    lon=\"LongitudeDegrees\",\n                    text='Provider',\n\n                    #Here, plotly detects color of series\n                    color=\"MessageType\",\n                    labels=\"MessageType\",\n\n                    zoom=9,\n                    center={\"lat\":37.395817, \"lon\":-122.102916},\n                    height=600,\n                    width=800)\nfig.update_layout(mapbox_style='stamentoner')\n#fig.update_layout(margin={\"r\": 0, \"t\": 0, \"l\": 0, \"b\": 0})\nfig.update_layout(title_text=\"I lost my GPS!\")\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-05-02T22:29:58.808178Z","iopub.execute_input":"2022-05-02T22:29:58.809058Z","iopub.status.idle":"2022-05-02T22:29:58.888301Z","shell.execute_reply.started":"2022-05-02T22:29:58.809015Z","shell.execute_reply":"2022-05-02T22:29:58.887208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by Kuto https://www.kaggle.com/code/kuto0633/road-detection-and-creating-grid-points\n\ntarget_message = '2021-04-29-US-SJC-2'\ntarget_gt_df = gt_df[gt_df[\"MessageType\"]==target_message].reset_index(drop=True)\ntarget_message","metadata":{"execution":{"iopub.status.busy":"2022-05-02T22:16:12.052869Z","iopub.execute_input":"2022-05-02T22:16:12.053187Z","iopub.status.idle":"2022-05-02T22:16:12.061924Z","shell.execute_reply.started":"2022-05-02T22:16:12.053153Z","shell.execute_reply":"2022-05-02T22:16:12.060996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by Kuto https://www.kaggle.com/code/kuto0633/road-detection-and-creating-grid-points\n\nfig = px.scatter_mapbox(target_gt_df,\n\n                    # Here, plotly gets, (x,y) coordinates\n                    lat=\"LatitudeDegrees\",\n                    lon=\"LongitudeDegrees\",\n                    text='Provider',\n\n                    #Here, plotly detects color of series\n                    color=\"MessageType\",\n                    labels=\"Provider\",\n\n                    zoom=15,\n                    center={\"lat\":37.395817, \"lon\":-122.102916},\n                    height=600,\n                    width=800)\nfig.update_layout(mapbox_style='stamentoner')\n#fig.update_layout(margin={\"r\": 0, \"t\": 0, \"l\": 0, \"b\": 0})\nfig.update_layout(title_text=\"GPS is lost again!\")\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-05-02T23:12:07.035868Z","iopub.execute_input":"2022-05-02T23:12:07.036186Z","iopub.status.idle":"2022-05-02T23:12:07.115179Z","shell.execute_reply.started":"2022-05-02T23:12:07.036151Z","shell.execute_reply":"2022-05-02T23:12:07.114247Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Geographical library","metadata":{}},{"cell_type":"markdown","source":"![](https://i.ytimg.com/vi/310Es1ERf1M/maxresdefault.jpg)youtube.com","metadata":{}},{"cell_type":"code","source":"!pip install osmnx momepy geopandas","metadata":{"execution":{"iopub.status.busy":"2022-05-02T22:30:48.086032Z","iopub.execute_input":"2022-05-02T22:30:48.086349Z","iopub.status.idle":"2022-05-02T22:31:00.953376Z","shell.execute_reply.started":"2022-05-02T22:30:48.086313Z","shell.execute_reply":"2022-05-02T22:31:00.952260Z"},"_kg_hide-output":true,"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from shapely.geometry import Point\nimport osmnx as ox\nimport momepy\nimport geopandas as gpd","metadata":{"execution":{"iopub.status.busy":"2022-05-02T22:35:07.575904Z","iopub.execute_input":"2022-05-02T22:35:07.576206Z","iopub.status.idle":"2022-05-02T22:35:07.581069Z","shell.execute_reply.started":"2022-05-02T22:35:07.576178Z","shell.execute_reply":"2022-05-02T22:35:07.580111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by Kuto https://www.kaggle.com/code/kuto0633/road-detection-and-creating-grid-points\n\n# change pd.DataFrame -> gpd.GeoDataFrame\ntarget_gt_df[\"geometry\"] = [Point(p) for p in target_gt_df[[\"LatitudeDegrees\", \"LongitudeDegrees\"]].to_numpy()]\ntarget_gt_gdf = gpd.GeoDataFrame(target_gt_df, geometry=target_gt_df[\"geometry\"])\ntarget_gt_gdf.head(5)","metadata":{"execution":{"iopub.status.busy":"2022-05-02T22:36:16.245697Z","iopub.execute_input":"2022-05-02T22:36:16.246165Z","iopub.status.idle":"2022-05-02T22:36:16.265981Z","shell.execute_reply.started":"2022-05-02T22:36:16.246134Z","shell.execute_reply":"2022-05-02T22:36:16.265121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by Kuto https://www.kaggle.com/code/kuto0633/road-detection-and-creating-grid-points\n\ntarget_gt_gdf.plot();","metadata":{"execution":{"iopub.status.busy":"2022-05-02T22:37:16.765089Z","iopub.execute_input":"2022-05-02T22:37:16.765426Z","iopub.status.idle":"2022-05-02T22:37:16.959677Z","shell.execute_reply.started":"2022-05-02T22:37:16.765391Z","shell.execute_reply":"2022-05-02T22:37:16.958568Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Trying to fix the code. And not getting it.","metadata":{}},{"cell_type":"code","source":"#Code by https://stackoverflow.com/questions/62075847/using-qgis-and-shaply-error-geosgeom-createlinearring-r-returned-a-null-pointer\n\n...\nfrom shapely import speedups\n...\n\nspeedups.disable()","metadata":{"execution":{"iopub.status.busy":"2022-05-02T22:45:34.171600Z","iopub.execute_input":"2022-05-02T22:45:34.172165Z","iopub.status.idle":"2022-05-02T22:45:34.176738Z","shell.execute_reply.started":"2022-05-02T22:45:34.172127Z","shell.execute_reply":"2022-05-02T22:45:34.175826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by https://stackoverflow.com/questions/62075847/using-qgis-and-shaply-error-geosgeom-createlinearring-r-returned-a-null-pointer\n\n\"\"\"\nThe shapely.speedups module contains performance enhancements written in C.\nThey are automaticaly installed when Python has access to a compiler and\nGEOS development headers during installation, and are enabled by default.\n\"\"\"","metadata":{"execution":{"iopub.status.busy":"2022-05-02T22:46:20.902787Z","iopub.execute_input":"2022-05-02T22:46:20.903072Z","iopub.status.idle":"2022-05-02T22:46:20.909572Z","shell.execute_reply.started":"2022-05-02T22:46:20.903042Z","shell.execute_reply":"2022-05-02T22:46:20.908566Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by https://stackoverflow.com/questions/62075847/using-qgis-and-shaply-error-geosgeom-createlinearring-r-returned-a-null-pointer\n\nfrom shapely.geometry import shape","metadata":{"execution":{"iopub.status.busy":"2022-05-02T22:46:40.819721Z","iopub.execute_input":"2022-05-02T22:46:40.820029Z","iopub.status.idle":"2022-05-02T22:46:40.824941Z","shell.execute_reply.started":"2022-05-02T22:46:40.819998Z","shell.execute_reply":"2022-05-02T22:46:40.823606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by https://stackoverflow.com/questions/62075847/using-qgis-and-shaply-error-geosgeom-createlinearring-r-returned-a-null-pointer\n\nimport shapely\nshapely.speedups.disable()","metadata":{"execution":{"iopub.status.busy":"2022-05-02T22:48:07.724329Z","iopub.execute_input":"2022-05-02T22:48:07.724792Z","iopub.status.idle":"2022-05-02T22:48:07.728149Z","shell.execute_reply.started":"2022-05-02T22:48:07.724742Z","shell.execute_reply":"2022-05-02T22:48:07.727561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by Kuto https://www.kaggle.com/code/kuto0633/road-detection-and-creating-grid-points\n\n# get road data from open street map by osmnx\n#offset = 0.1**5\n#bbox = target_gt_gdf.bounds + [-offset, -offset, offset, offset]\n#east = bbox[\"minx\"].min()\n#west = bbox[\"maxx\"].max()\n#south = bbox[\"miny\"].min()\n#north = bbox[\"maxy\"].max()\n#G = ox.graph.graph_from_bbox(north, south, east, west, network_type='drive')","metadata":{"execution":{"iopub.status.busy":"2022-05-02T22:48:13.014711Z","iopub.execute_input":"2022-05-02T22:48:13.015155Z","iopub.status.idle":"2022-05-02T22:48:13.051514Z","shell.execute_reply.started":"2022-05-02T22:48:13.015105Z","shell.execute_reply":"2022-05-02T22:48:13.049808Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#InvalidGeometryError: Null geometry supports no operations\n\nI have No clue how to fix that.","metadata":{}},{"cell_type":"markdown","source":"#At least one map. Though I don't know where I am. I'm still lost!","metadata":{}},{"cell_type":"code","source":"#Code by Juan Cruz Martinez https://livecodestream.dev/post/how-to-plot-your-data-on-maps-using-python-and-folium/\n#When I wrote on the tooltip: Click Here For More Info, I could only read in the viewer but not in the edit\n\nimport folium\n\n#We're in Palo Alto, are we Samsung?\n\nm = folium.Map(location=[37.395817, -122.102916], zoom_start=15)\n\ntooltip = \"Where my Samsung brought me\"\nfolium.TileLayer('stamentoner').add_to(m)#That changes the map to black/white\n\nmarker = folium.CircleMarker(\n    location=[37.395817,-122.102916 ],\n    radius=50,\n    popup=\"<stong>Palo Alto with my Samsung Galaxy</stong>\",\n    tooltip=tooltip)\nmarker.add_to(m)\n\nm","metadata":{"execution":{"iopub.status.busy":"2022-05-02T23:07:51.806196Z","iopub.execute_input":"2022-05-02T23:07:51.806518Z","iopub.status.idle":"2022-05-02T23:07:51.826299Z","shell.execute_reply.started":"2022-05-02T23:07:51.806479Z","shell.execute_reply":"2022-05-02T23:07:51.825512Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#I gave up. No GPS signal. I'm not shy to embarasse myself.","metadata":{}},{"cell_type":"markdown","source":"#Acknowledgement:\n\nhttps://www.kaggle.com/code/kuto0633/road-detection-and-creating-grid-points\n\nI'm sorry to have ruined your code Kuto.","metadata":{}},{"cell_type":"markdown","source":"#Probably, many are laughing at \"my\" code.\n\nHowever, it's exactly what happens when people lose their GPS. They are non-coders like me. And obviously they blame their expensive gadget. Those are the ones that buy anything smart just because they are suppose to be smart (smartphones/smartwatches/smartwhatever). Therefore, it's better not laughing at US. ","metadata":{}}]}