2

I have code written in ArcMap which uses the package ArcPy which I cannot use in a stand-alone Python script, but I want to do it with GDAL without ArcMap. Basically it goes through the lines in the shapefile and create points every 20 meters and stores all these points in a new shapefile.

I have tried using the interpolate method in fiona and am just very lost.

Could someone help?

6

I have tried using the interpolate method in fiona and am just very lost.

With Shapely (interpolate()) and Fiona

# open the line shapefile
import fiona
line = fiona.open('line.shp')
# example with the first feature
firstline = line.next()
# transform to shapely geometry
from shapely.geometry import shape, mapping
first = shape(firstline['geometry'])
# length of the LineString
length = first.length

enter image description here

# creation of the resulting shapefile
schema = {'geometry': 'Point','properties': {'id': 'int'},}
with fiona.open('result.shp', 'w', 'ESRI Shapefile', schema)  as output:
    # create points every 20 meters along the line
    for distance in range(0,int(length),20):
         point = first.interpolate(distance)   
         output.write({'geometry':mapping(point),'properties': {'id':1}})

enter image description here

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.