Skip to content

Instantly share code, notes, and snippets.

@andrashann
Created August 24, 2023 20:43
Show Gist options
  • Save andrashann/56e54180378f3d185bb5c831ac961b76 to your computer and use it in GitHub Desktop.
Save andrashann/56e54180378f3d185bb5c831ac961b76 to your computer and use it in GitHub Desktop.
take a gpx track file and a gpx file with waypoints and keep only the points within a certain buffer of the track
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 9,
"id": "4b102c9d-4872-4990-9902-08122c0b6291",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"import geopandas as gpd\n",
"# https://github.com/geopandas/geopandas/issues/1816#issuecomment-910760384\n",
"\n",
"TRACK_FILE = 'ak_utvonal.gpx'\n",
"POINTS_FILE = 'caches_nowpt.gpx'\n",
"CRS = 23700 # projected epsg id\n",
"BUFFER = 3000 # meters\n",
"OUT_FILE = 'ak_caches_nowpt.gpx'\n"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "c86a960a-576f-41eb-a99a-f9517c139330",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"~/gpxbuffer/env/lib/python3.10/site-packages/IPython/core/interactiveshell.py:3400: FutureWarning: The `op` parameter is deprecated and will be removed in a future release. Please use the `predicate` parameter instead.\n",
" if await self.run_code(code, result, async_=asy):\n"
]
}
],
"source": [
"track_df = gpd.read_file(TRACK_FILE, layer='tracks', crs='epsg:4326').to_crs(epsg=CRS)\n",
"\n",
"buffer = track_df.buffer(BUFFER)\n",
"buffer_df = gpd.GeoDataFrame(geometry=buffer, crs=f'epsg:{CRS}')\n",
"\n",
"points_df = gpd.read_file(POINTS_FILE, layer='waypoints', crs='epsg:4326').to_crs(epsg=CRS)\n",
"\n",
"selected_points = gpd.sjoin(points_df, buffer_df, op='within').to_crs(epsg=4326)\n",
"selected_points = selected_points[['ele', 'time', 'name', 'cmt', 'desc', 'url', 'urlname', 'sym', 'type',\n",
" 'geometry', 'index_right']]\n",
"\n",
"selected_points.to_file(OUT_FILE, 'GPX', GPX_USE_EXTENSIONS=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8eb5afed-1bcb-42a1-8f2e-9458ca306c28",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.10"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment