This Python script converts a point shapefile to a line shapefile. Points are assigned to a specific line based on a common numeric identifier. For each line, the algorithm first removes duplicated points and then creates a distance matrix storing the distances between each point pair. It assumes line extremes are represented by the pair having the largest distance and picks the first point of this pair as the initial point of a new line. Starting from this point, a line is created by repetitively adding the nearest point to each previously selected point.
This script was created due to the need of converting point data for which the available tools in GIS software were not effective because of duplicated points and unpredictable point sequences. This version is still experimental and should be applied only to linear features with a simple, almost straight geometry. While successfully working in some practical cases, its results cannot guaranteed to be correct in general cases.
Language: Python 2.6
Required Python modules are numpy and ogr. The script was created using Python(x,y), which already incorporates these modules. The FWTools software has the older Numeric module instead of the recent numpy. Therefore, the script should not work in FWTools, or you should modify from numpy to Numeric terminology. Other Python distributions with numpy ogr and could work without problems.
Created and tested with Python(x,y) 22.214.171.124 under Windows Vista.
The input is a point shapefile with an integer field representing line identifiers.
The script run from the shell using the following command line:
python pt2line_05.py par.txt
where pt2line_05.py is the script name and par.txt is the text file that contains required parameters. An example of a parameter file content is:
flt_pts.shp #name of the input point shapefile
flt_lines.shp #name of the output line shapefile
flt_id #numeric identifier of each line
The output data consists of a line shapefile. Apart from the id field, a field stores the number of points that consitute each created line.
The Python script was used to successfully convert a set of points representing faults in the Valnerina zone near Piedipaterno (Umbria, Italy). The below figures illustrate the data set prior and after conversion by the script. The geometry of converted lines is quite simple and this explains the success of the algorithm. Data and code can be freely downloaded.
Fault data in Valnerina zone, represented by a point shapefile.
The same data, converted into a line shapefile by the script.