1

I have two dataframes containing linestrings and polygons.

Example:

LINESTRING (7470683.872444116 5535684.148437066, 7469929.808435988 5533461.643992055, 7469962.299334303 5532960.522156481)

POLYGON ((7470480.135291019 5535154.495749012, 7470503.630338011 5535150.050740123, 7470523.315377379 5535150.050740123, 7470548.715428181 5535147.510735041, 7470569.670470092 5535145.605731232, 7470589.355509464 5535136.080712179, 7470627.455585664 5535129.730699481, 7470646.65736445 5535127.152853454, 7470632.250166068 5535055.919058787, 7470624.22647908 5535060.319664435, 7470610.679785319 5535050.582978293, 7470597.556425741 5535039.999623794, 7470583.586397797 5535039.152955431, 7470543.792984879 5535045.502968134, 7470533.475397702 5535047.81553565, 7470522.045374842 5535045.275530567, 7470508.075346902 5535047.81553565, 7470499.185329122 5535050.990541998, 7470483.31029737 5535060.515561052, 7470473.78527832 5535066.230572477, 7470460.45025165 5535070.040580099, 7470448.385227518 5535084.64560931, 7470438.225207198 5535111.95066392, 7470435.685202118 5535133.540707104, 7470443.940218629 5535146.875733772, 7470452.830236409 5535155.765751552, 7470459.18024911 5535161.480762983, 7470480.135291019 5535154.495749012))

Is there any method I could use to calculate the minimal distance between a 'road' (in dataframe containing linestrings) and polygon? I'm trying geopandas since I have some experience with pandas.

closed as unclear what you're asking by whyzar, BERA, xunilk, MaryBeth, aldo_tapia Mar 22 '18 at 17:32

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • Welcome to GIS SE! Take a look at the tour for new users to see how to structure your question. Adding a code snippet of what you've tried and where you're stuck or what didn't work would be helpful here. – GISKid Mar 9 '17 at 21:06
  • Do you want to calculate distance from a road to any nearest polygon or distance from every road to every polygon? Could you be more clear? – Kadir Şahbaz Mar 22 '18 at 15:23
0

There are overlay in your geometry. Every time that I calculate distance from each other, I receive no distance. Result always zero. By the way, I consider that both have the same coordinate reference system. If they have an another, you should inform us.

I think that it would have distance higher than zero only if they don't overlay.

Plot of geometries

Here the script:

import geopandas as gpd
from shapely.wkt import loads

line_wkt = 'LINESTRING (7470683.872444116 5535684.148437066, 7469929.808435988 5533461.643992055, 7469962.299334303 5532960.522156481)'

poly_wkt = 'POLYGON ((7470480.135291019 5535154.495749012, 7470503.630338011 5535150.050740123, 7470523.315377379 5535150.050740123, 7470548.715428181 5535147.510735041, 7470569.670470092 5535145.605731232, 7470589.355509464 5535136.080712179, 7470627.455585664 5535129.730699481, 7470646.65736445 5535127.152853454, 7470632.250166068 5535055.919058787, 7470624.22647908 5535060.319664435, 7470610.679785319 5535050.582978293, 7470597.556425741 5535039.999623794, 7470583.586397797 5535039.152955431, 7470543.792984879 5535045.502968134, 7470533.475397702 5535047.81553565, 7470522.045374842 5535045.275530567, 7470508.075346902 5535047.81553565, 7470499.185329122 5535050.990541998, 7470483.31029737 5535060.515561052, 7470473.78527832 5535066.230572477, 7470460.45025165 5535070.040580099, 7470448.385227518 5535084.64560931, 7470438.225207198 5535111.95066392, 7470435.685202118 5535133.540707104, 7470443.940218629 5535146.875733772, 7470452.830236409 5535155.765751552, 7470459.18024911 5535161.480762983, 7470480.135291019 5535154.495749012))'

line = loads(line_wkt)
g_line = gpd.GeoSeries(line)

poly = loads(poly_wkt)
g_poly = gpd.GeoSeries(poly)

g_poly.plot(ax=g_line.plot(alpha=1), alpha=1)

g_line.distance(g_poly)  
-1

I'm not familiar with geopandas, but just searching the documentation, I found the following:

distance(other): returns Series with minimum distance from each entry to other

Also, this link explains that,

Any of the attributes calls or methods described for a GeoSeries will work on a GeoDataFrame – effectively, they are just applied to the “geometry” GeoSeries.

Further, this similar post might help.

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