[Python] Get boundary co ordinates of a house and the driveway, assume house lat lon is given

Discussão em 'Python' iniciado por Stack, Agosto 26, 2025.

  1. Stack

    Stack Membro Participativo

    The code that follows provides direction coordinates from one location to another. I've created a couple lines using those coordinates (see the attached image). In order to draw borderline polygons, I now want to obtain the driveway and home border coordinates.


    1. Line drawn with current code [​IMG],


    2. Required polygons for house and driveway with orange and light green polygons - manually drawn [​IMG]

    GEOAPIFY_API_KEY=''

    import requests, folium
    import pandas as pd
    from shapely.geometry import LineString
    from geopy.distance import geodesic

    latlng1="40.07391781459824, -77.57405421223055"
    latlng2="40.07416000507259, -77.5720264622554"

    url = "https://api.geoapify.com/v1/routing?waypoints=40.052899785508956,-77.51053821302062|40.05312394414919,-77.5112494099691&mode=walk&apiKey=API_KEY"

    payload={}
    headers = {}
    response = requests.request("GET", url, headers=headers, data=payload)
    print(response.text)

    start_lat, start_lon = -77.5112494099691, 40.05312394414919
    end_lat, end_lon = -77.511094, 40.053239

    def create_map(response):
    # use the response
    mls = response.json()['features'][0]['geometry']['coordinates']
    points = [(i[1], i[0]) for i in mls[0]]

    m = folium.Map(max_zoom=40,zoom_start=20, min_zoom=20)


    # add marker for the start and ending points
    # for point in [points[0], points[-1]]:
    # folium.Marker(point).add_to(m)

    offset_distance = 0.00008
    route_line = LineString(points)
    # Choose 'left' or 'right' for the offset direction
    offset_line_left = route_line.parallel_offset(offset_distance, 'left')
    offset_line_right = route_line.parallel_offset(offset_distance, 'right')

    # add the lines

    # ############

    point1 = (40.05312394414919, -77.5112494099691)
    point2 = (40.053239, -77.511094)

    ep1 = 40.053239 + 0.00005 # adding 5 meters more
    ep2 = -77.511094 + 0.00005
    extented_point = (ep1, ep2)
    # Create a Folium map centered between the two points

    # Add a PolyLine to draw the straight line
    folium.PolyLine([point1, extented_point], color="blue", weight=6, opacity=1).add_to(m)

    point3 = (40.052853,-77.510601)
    point4 = (40.052899785508956,-77.51053821302062)


    # Add a PolyLine to draw the straight line
    folium.PolyLine([point3, point4], color="green", weight=6, opacity=1).add_to(m)

    # Add markers for the start and end points (optional)
    # folium.Marker(point1, popup="Start Point").add_to(m)
    # folium.Marker(point2, popup="End Point").add_to(m)

    # #############
    print("\n\n")
    print(list(offset_line_left.coords))

    folium.PolyLine(points, weight=6, color="red" ,opacity=1).add_to(m)
    folium.PolyLine(list(offset_line_left.coords), color="yellow" , weight=6, opacity=1).add_to(m)
    folium.PolyLine(list(offset_line_right.coords), color="black", weight=6, opacity=1).add_to(m)


    # create optimal zoom
    df = pd.DataFrame(mls[0]).rename(columns={0:'Lon', 1:'Lat'})[['Lat', 'Lon']]
    sw = df[['Lat', 'Lon']].min().values.tolist()
    ne = df[['Lat', 'Lon']].max().values.tolist()

    m.fit_bounds([sw, ne])
    # Add an Esri World Street Map tile layer

    return m
    m = create_map(response)


    folium.TileLayer(
    tiles='https://mt1.google.com/vt/lyrs=s&x={x}&y={y}&z={z}',
    attr='Google Satellite',
    name='Google Satellite',
    overlay=True,
    control=True,
    max_zoom=40,
    min_zoom=20
    ).add_to(m)

    # Add a layer control to toggle between tile layers (if you have multiple)
    folium.LayerControl().add_to(m)
    m.save('./maps/route_map.html')

    Continue reading...

Compartilhe esta Página