So I have a shapefile with 16k linestrings, and one polygon of a smaller area.

My intention is to do intersection and keep only the lines or fragments of lines that lie inside the polygon, following this tutorial: http://geopandas.org/geometric_manipulations.html

This is the output of the geometry of the large shapefile:

> network
0        LINESTRING Z (524669.5552664882 6066308.874914
16047    LINESTRING Z (735457.2357274202 5693634.842955...
Name: geometry, Length: 16048, dtype: object

this the polygon:

> shtest_utm geometry 0  POLYGON ((781557.9314549405 5812460.808320792,...

This is my code:

file = 'test.geojson'
with open(file) as f:
    data = geojson.load(f)
vecfilename = 'strecken_polyline.shp'
vector = GeoDataFrame.from_file(vecfilename)
vector.crs = {'init': 'epsg:4326', 'no_defs': True}
vector_utm = vector.to_crs(epsg = 32632)
shtest = GeoDataFrame.from_features(data["features"])
shtest.crs = {'init': 'epsg:4326', 'no_defs': True}
shtest_utm = shtest.to_crs(epsg = 32632)
network = vector_utm['geometry']
inter = network.intersection(shtest_utm)

And the output of inter is:

inter 0 () ... 16047 () Length: 16048, dtype: object

Is this a normal behavior? I would not expect so. Like that I can not see if there is any line remaining.

Furthermore, .plot() does not work, but that is another problem.


When you pass a GeoSeries of geometries to network.intersection(..), it will try to 'align' the passed series with the calling one, meaning that geopandas will try to take the intersection of the first element in network with the first element in the passed series, the second with the second, etc.

In this case you only have a single polygon for which you want to calculate the intersections, therefore you should pass a single polygon object, so geopandas calculates the intersection with this same polygon for all elements in network:

shtest_utm_polygon = shtest_utm.geometry.iloc[0]
inter = network.intersection(shtest_utm_polygon)
  • sorry for the superlate reply, somehow I missed you answer. Unfortunately, I think your answer is not what I am asking. My doubt is why features that not instersect, instead of get removed, appear as empty.
    – ImanolUr
    Mar 23 '18 at 16:03

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.