openstreetmapgeopandasosmnx

Retrieve campsites within a specific radius around a gpx route from OSM


Coming spring I will be walking to Rome from Amsterdam. I have a route planned out. Now I would like to find campsites around the route and find the best way in which I never have to walk more than 35 km between campsites around the route. Does anyone have any suggestions?


Solution

  • I got it to work with the following code:

    import gpxpy
    import shapely.geometry as geometry
    from shapely.geometry import LineString
    import pyproj 
    import osmnx as ox
    import geopandas as gpd
    import shapely
    from pyproj import Transformer
    
    #parse the GPX file
    with open('path/file.gpx', 'r') as gpx_file:
        gpx = gpxpy.parse(gpx_file)
    
    track_points = [(point.latitude, point.longitude) for point in gpx.tracks[0].segments[0].points]
    
    # check if the GPX file's coordinates are in degrees
    if -180 <= track_points[0][0] <= 180 and -180 <= track_points[0][1] <= 180:
        print("The coordinates were in degrees")
        transformer = Transformer.from_crs("EPSG:4326", "EPSG:3857", always_xy=True)
        track_points = [transformer.transform(point[1], point[0]) for point in track_points]
    else :
        print("The coordinates are in metres")
    
    track_linestring = LineString(track_points)
    
    # Create the buffer
    buffer_distance = 3000 # this is a radius of 1.5 km
    buffer_polygon = track_linestring.buffer(buffer_distance)
    
    # Define the original and desired projections
    inProj = pyproj.Proj(init='epsg:3857') # Web Mercator
    outProj = pyproj.Proj(init='epsg:4326') # WGS 84
    
    # Convert the coordinates of the Polygon object
    buffer_polygon_4326 = shapely.ops.transform(lambda x, y: pyproj.transform(inProj, outProj, x, y), buffer_polygon)
    
    # extract all the tourism places within the buffer zone
    tourism_places = ox.geometries.geometries_from_polygon(buffer_polygon_4326, tags={'tourism': 'True'})