# How to get equidistant points from a linestring (geographical coordinates) [closed]

I want to resample geographical coordinates based on a specific number of values, let's say, 1663 for the following case:

``````-78.0599088 -11.89402416
-78.04317744 -11.88622134
-78.0267798 -11.87700462
-78.010353 -11.8692050399999
-77.9953194 -11.86129017
-77.96128788 -11.8449840599999
-77.92870572 -11.82838707
-77.89554864 -11.8117820699999
-77.86357524 -11.79488952
-77.83013412 -11.77942518
-77.7978615599999 -11.76223743
-77.765589 -11.7456140699999
-77.73216732 -11.72927727
-77.6996085599999 -11.7117892799999
-77.6673594 -11.6965884599999
-77.63510052 -11.6819618399999
-77.6045808 -11.6618759099999
-77.57262108 -11.6432262
-77.5406624399999 -11.62628883
-77.5072638 -11.6099197199999
-77.4753066 -11.5923951899999
-77.4427813199999 -11.57658786
-77.4093902399999 -11.5599159
-77.38064244 -11.5446833099999
``````

However, the tricky part here is to keep the first and last positions, and to use open-source software tools (such as GDAL, AWK, GMT or other bash shell command line tools, that would be great).

As example I am looking for something similar to the "Equidistant points (fixed number)" option of XTools Pro: https://help.xtools.pro/pro/12.2/en/XTools_Pro_Components/Feature_conversions/Convert_Features_to_Points.htm

Here an expected output, a line of distance X from which 7 points (node or vertex) were created considering the first and last positions: If you are willing to work with python, `shapely` provides a simple solution to creating equidistant points along a linestring. Look at the `interpolate` method of `LineString` and use the `normalized` keyword.

``````from shapely.geometry import LineString
import csv

with open('input_xy.txt') as fin:
xy_floats = map(lambda x: (float(x), float(x)), list(reader))
line = LineString(xy_floats)

num_points = 9  # includes first and last

new_points = [line.interpolate(i/float(num_points - 1), normalized=True) for i in range(num_points)]

with open('output_xy.txt', 'w') as fout:
writer = csv.writer(fout)
writer.writerows([ [point.x, point.y] for point in new_points])

``````

This can easily be made into a script to run from the terminal.

``````#! /usr/bin/env python
"""Usage: this_script.py n_points input output"""
from shapely.geometry import LineString
import csv
import sys

num_points = int(sys.argv)
assert num_points > 1

input_file = sys.argv
output_file = sys.argv

with open(input_file) as fin:
xy_floats = map(lambda x: (float(x), float(x)), list(reader))
line = LineString(xy_floats)

new_points = [line.interpolate(i/float(num_points - 1), normalized=True) for i in range(num_points)]

with open(output_file, 'w') as fout:
writer = csv.writer(fout)
writer.writerows([ [point.x, point.y] for point in new_points])

``````

The result given your input line from your question with num_points = 9

``````-78.0599088 -11.89402416
-77.97449470360738 -11.851312073514835
-77.88914288834313 -11.808397710496353
-77.80385170138601 -11.765427665066841
-77.71873436817096 -11.722062151019506
-77.63279867449748 -11.680446927424219
-77.55074893375361 -11.63163444785823
-77.46587976283878 -11.587813734410211
-77.38064244 -11.5446833099999
``````

You can also use a library like `fiona` for writing spatial formats from the shapely objects.

• Thank you for the answer, I never used python before, if I run the script an error pops up: `Traceback (most recent call last): File "./a.py", line 16, in <module> xy_floats = map(lambda x: (float(x), float(x)), list(reader)) File "./a.py", line 16, in <lambda> xy_floats = map(lambda x: (float(x), float(x)), list(reader)) ValueError: invalid literal for float(): -77.57262108 -11.6432262 ` – Gery Jan 20 at 12:35
• if I use the python console it appears an error in "reader": `>>> with open('x.txt') as fin: ... reader=csv.reader(fin) File "<stdin>", line 2 reader=csv.reader(fin) ^ IndentationError: expected an indented block ` – Gery Jan 20 at 12:39
• The above errors were solved when I use a CSV (comma-separated values) file instead of a space-separated file, thanks again Genius! – Gery Jan 20 at 13:59