-1

I have a shape file for road names - which is actually polyline geometry. And I also have address points, I am trying to find the road name of each address point using the polyline shape file. Is there a way to work out this? I used spatial join, unfortunately it does not work as the polyline geometry will not have all the points. Can someone suggest how to work out this?

Since no one answered this, elaborating the question further. So I have shapefile with multiple poly line coordinates (each line string will have min and max coordinates of a line like the below one)

        min      max
x 168.04510 168.0480
y -44.39949 -44.3987

I need to find out if the point (168.04665153,-44.3990252) is on the line made of the coordinates above. Let me know if further details required. Happy to explain.

Thanks in advance.

ds_user
  • 1,899
  • 2
  • 25
  • 61

1 Answers1

0

I think the problem is in fact finding the nearest street to each point. You can import your shapefiles using GeoPandas.read_file(), and then use the following function to find street's names. For illustrative purpose, I am just gonna create a dummy GeoDataFrame, but it is easier for people to answer your question if you can provide a sample of your data. The function works as follows: first, you create a buffer around your Point, then you get every lines within this circle (pt.buffer()). Then you compute the distance between the point and each of these lines and select the one which is closest.

l1 = LineString([(0,0), (2,3)])
l2 = LineString([(2,3), (3,0)])
pt = Point([1.5,1])

df = pd.DataFrame(['street1', 'street2'], columns=['street_name'])    
gdf = gpd.GeoDataFrame(df ,geometry = [l1,l2])
gdf

#    street_name    geometry
#0  street1     LINESTRING (0 0, 2 3)
#1  street2     LINESTRING (2 3, 3 0)

def get_street_name(gdf, pt):    
    dist = []
    for line in gdf.intersection(pt.buffer(3)):
        ind = gdf[gdf.geometry == line].index[0]
        dist.append([line.distance(pt), ind])
    dist.sort()
    true_line_index = dist[0][1]
    street_name = gdf.iloc[true_line_index].loc['street_name']

    return street_name

get_street_name(gdf,pt)
# out: 'street1'

This solution is not fast, you can make it faster using spatial index (see here). I have answered a similar question here.

Alz
  • 727
  • 6
  • 11
  • Thanks for your answer. I will try this today. However my points are lat,long points. Do I have to project the coordinate system before doing this? And can you please add rtree indexing on this script if possible? – ds_user Aug 15 '17 at 17:22
  • shapely assumes cartesian coordinates. I suggest to project your data to avoid errors. It should be pretty easy to add spatial index if you follow the steps in the tutorial or the other link. I will be happy to help if you have specific problem implementing it. – Alz Aug 15 '17 at 18:03