I have a huge area of linestrings and I want to get the features of each portion of area. All the linestring parts are stored in a python list and they are not in order.

One portion of area is shown in the figure 1.enter image description here

I want to create a bounded box on specific percentage of my area and then get the feature out of each linestring which is intersecting with bounded box. This is illustrated in figure 2. The red box is bounded box on the linestring and it should get the percentage of each intersecting lineenter image description here

My problem is that I also want it to slide to the whole area from one starting point. I have seen that python shapely has a box but dont know how to slide it to the whole area. Figure 2 and 3 shows how I want sliding.enter image description here.

This is simple illustration in reality I want a box that slides from start of my area to the end of my area which is stored in python list.

Shows bounded box is sliding on the area enter image description here

Figure 1 to 3 show how it should be sliding.

Is it possible to implement in python? I want to get features of both side of linestring which is a road or is there some other method?

  • Do you want the box to slide along your line strings?
    – RoperMaps
    Oct 9, 2017 at 16:06
  • Yes, and it should intersect both linestrings
    – mimetype
    Oct 9, 2017 at 16:06

1 Answer 1


If I am not misunderstanding your request, following code can be useful:

import fiona
from shapely.geometry import shape
from shapely.affinity import translate

path_line = '/home/zeito/pyqgis_data/linestring.shp'
path_box = '/home/zeito/pyqgis_data/box.shp'

line_file = fiona.open(path_line)
box_file = fiona.open(path_box)

lines = [ feat["geometry"] for feat in line_file ]
box = [ feat["geometry"] for feat in box_file ]

geom_line = [ shape(line) for line in lines ]

geom_box = shape(box[0])

init_point = (414292.587778, 4469753.3899)
final_point = (411712.772831, 4463873.81165)

xoff =  final_point[0] - init_point[0]
yoff =  final_point[1] - init_point[1]

new_geom_box = translate(geom_box, xoff= xoff, yoff= yoff)

print new_geom_box

for geom in geom_line:
    print geom.intersection(new_geom_box)

It uses 'translate' method, from shapely.affinity, for sliding an arbitrary bounding box that is overlapping two linestrings (see following image); from init_point to final_point specified inside code.

Before runnig above code, system looks like following image:

enter image description here

After running the code, these geometries were printed at Python Console:

POLYGON ((410112.616106789 4462767.206057594, 410112.616106789 4464975.827552281, 413712.3030732681 4464975.827552281, 413712.3030732681 4462767.206057594, 410112.616106789 4462767.206057594))
LINESTRING (411526.0185637082 4464975.827552281, 410528.5765983656 4462767.206057594)
LINESTRING (412896.2136284537 4464975.827552281, 412692.4310537892 4464497.99254962, 411865.7867590904 4462767.206057594) 

With the help of QuickWKT plugin of QGIS, I visualized corresponding geometries. Bounding box was effectively displaced to final point. It was also obtained each portion of original linestrings intersecting bounding box in this new position.

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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