I have several line Shapefiles, each displays a hiking trails and they all have a disastrous chaos in their attribute table (let us call the fields A,B, C,D and they are not equal in the tables). The name of the shapefile is the name of trail (nameA.shp etc.).
I'd like to clean the attribute table automatically to have the name of the shapefile as a new field "name" and if there are more than one object, the should be named like "nameA1; nameA2; and so on).
I started to this by hand but it takes hours. Any ideas?
I need this step to finally merge all shapefiles to a single one without losing the name of the trails.
I use QGIS las palma on a Mac.
edit 01.06.2017 This is the script after I ran it:
Python Console
Use iface to access QGIS API interface or Type help(iface) for more info
import os
from glob import glob
from PyQt4.QtCore import QVariant
files = []
#CHANGE NR1 --> THE PATH
start_dir = r"/Users/Kevin_privat/Desktop/Shapedateien Test"
pattern = "*.shp"
for dirpath, dirnames, filenames in os.walk(start_dir):
files.extend(glob(os.path.join(dirpath,pattern)))
#CHANGE NR2 --> THE COORDINATE SYSTEM AND LAYER NAME
all_lines = QgsVectorLayer('LineString?crs=EPSG:4326‘, "all_my_lines", "memory")
File "<input>", line 1
all_lines = QgsVectorLayer('LineString?crs=EPSG:4326â, "all_my_lines", "memory")
^
SyntaxError: EOL while scanning string literal
all_lines_pr = all_lines.dataProvider()
Traceback (most recent call last):
File "<input>", line 1, in <module>
NameError: name 'all_lines' is not defined
all_lines_pr.addAttributes([QgsField('new_id', QVariant.Double)])
Traceback (most recent call last):
File "<input>", line 1, in <module>
NameError: name 'all_lines_pr' is not defined
all_lines.updateFields()
Traceback (most recent call last):
File "<input>", line 1, in <module>
NameError: name 'all_lines' is not defined
for i in files:
os.path.basename(i)
linelayer = QgsVectorLayer(i, os.path.basename(i), "ogr")
features_linelayer = linelayer.getFeatures()
lines_list = []
for feature in features_linelayer:
new_feature = QgsFeature()
new_feature.setGeometry(feature.geometry())
new_feature.setAttributes([os.path.basename(i)+'_'+str(feature.id())])
lines_list.append(new_feature)
all_lines_pr.addFeatures(lines_list)
QgsMapLayerRegistry.instance().addMapLayer(all_lines)
File "<input>", line 12
QgsMapLayerRegistry.instance().addMapLayer(all_lines)
^
SyntaxError: invalid syntax