0

I'd like to iterate through a GeoDataFrame that contains a column of LineStrings and return a list of points; one from each LineString that is closest to a reference point.

#### .  Import data from Overpass API

import overpy

api = overpy.Overpass()

pHigh = api.query("""
(
node[highway=primary](34.978011,-116.697296, 36.747345, -113.976445);
way[highway=primary](34.978011,-116.697296, 36.747345, -113.976445);
rel[highway=primary](34.978011,-116.697296, 36.747345, -113.976445);
);
(._;>;);
out center;
""")

##### . Make a shapefile

import fiona 
from fiona.crs import from_epsg
schema = {'geometry': 'LineString', 'properties': {'Name':'str:80'}}
shapeout = "test.shp"
with fiona.open(shapeout, 'w',crs=from_epsg(4326),driver='ESRI 
Shapefile', schema=schema) as output:
    for way in pHigh.ways:
        # the shapefile geometry use (lon,lat) 
         line = {'type': 'LineString', 'coordinates':[(node.lon, 
        node.lat) for node in way.nodes]}
        prop = {'Name': way.tags.get("name", "n/a")}
        output.write({'geometry': line, 'properties':prop})  


##### .  Make a geopandas dataframe

import geopandas as gpd

gpanda = gpd.read_file("test.shp")  


##### . Point to measure distance from 

from shapely.geometry import LineString, Point

lat=36.069173
long= -115.013759

point=Point(lat,long)


##### .  return the coordinates for the nearest point on the linestring 
#####    relative to the reference point ('point').  Doesn't Work!

result=[]
for index, row in gpanda.iterrows():
    result += row.geometry.interpolate(row.geometry.project(point))

##### . Seems to work for individual rows...

gpanda.geometry[1:2].interpolate(gpanda.geometry[1:2].project(point))

This may be the wrong approach, I really just want an accurate measure of the nearest highway to many points. This way I would have the nearest point from each highway, which I could use to calculate the great circle distance from the reference point and find the minimum.

1

You might want to try rtree. It is the fastest way to retrieve nearest geometries especially when you have a large number of geometries involved. An example of geopandas itself implementing r-tree method is provided by the excellent Geoff Beoing

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.