0

I've been looking for a way to convert txt file columns to shapefile's. With the code below I am able to divide txt columns, what I don't know is how to store all those columns in a point shapefile, any idea? I am still looking for the answer.

f = open(txtRuta)
contents = f.read()
f.close()
new_contents = contents.replace('\n', ' ')
new_contents = new_contents.strip(' ')

f = open(txtRuta, 'w')
f.write(new_contents)
f.close()

new_contents = re.sub(r" + ", " ",  new_contents)

itemsInString = new_contents.split(" ")
lon= ((len(itemsInString)/9))

idx = []
aa = []
gpstime = []
x = []
y = []
z = []
bb = []
cc = []
dd = []

numbeer = 0

for index, item in enumerate(itemsInString):
    if numbeer == 9:
        numbeer = 0

    if numbeer == 0:
        idx.append(int(item))
    elif numbeer == 1:
        aa.append(float(item))
    elif numbeer == 2:
        gpstime.append(int(item))
    elif numbeer == 3:
        x.append(float(item))
    elif numbeer == 4:
        y.append(float(item))
    elif numbeer == 5:
        z.append(float(item))
    elif numbeer == 6:
        bb.append(float(item))
    elif numbeer == 7:
        cc.append(float(item))
    elif numbeer == 8:
        dd.append(float(item))
    numbeer = numbeer + 1
for i in range(0, lon):

    line = ogr.Geometry(ogr.wkbLineString)
    line.AddPoint(x[i], y[i], z[i])
    print line.ExportToWkt()

EDIT:

From GDAL/OGR cookbook, code below works for .txt to .shp, configure it as you need it.

f = open(txtRuta)
contents = f.read()
f.close()
new_contents = contents.replace('\n', ' ')
new_contents = new_contents.strip(' ')

f = open(txtRuta, 'w')
f.write(new_contents)
f.close()

new_contents = re.sub(r" + ", " ",  new_contents)

itemsInString = new_contents.split(" ")
lon= ((len(itemsInString)/9))

idx = []
aa = []
gpstime = []
x = []
y = []
z = []
bb = []
cc = []
dd = []
larg = len(idx)

numbeer = 0

for index, item in enumerate(itemsInString):
    if numbeer == 9:
        numbeer = 0

    if numbeer == 0:
        idx.append(int(item))
    elif numbeer == 1:
        aa.append(float(item))
    elif numbeer == 2:
        gpstime.append(int(item))
    elif numbeer == 3:
        x.append(float(item))
    elif numbeer == 4:
        y.append(float(item))
    elif numbeer == 5:
        z.append(float(item))
    elif numbeer == 6:
        bb.append(float(item))
    elif numbeer == 7:
        cc.append(float(item))
    elif numbeer == 8:
        dd.append(float(item))
    numbeer = numbeer + 1

shpout = shpRuta + "\\" + pNombre + ".shp"
driver = ogr.GetDriverByName("ESRI Shapefile")
data_source = driver.CreateDataSource(shpout)
srs = osr.SpatialReference()
srs.ImportFromEPSG(int(epsgUse))

layer = data_source.CreateLayer(pNombre, srs, ogr.wkbPoint)

layer.CreateField(ogr.FieldDefn("id", ogr.OFTInteger))
layer.CreateField(ogr.FieldDefn("aa", ogr.OFTReal))
layer.CreateField(ogr.FieldDefn("gpstime", ogr.OFTInteger))
layer.CreateField(ogr.FieldDefn("x", ogr.OFTReal))
layer.CreateField(ogr.FieldDefn("y", ogr.OFTReal))
layer.CreateField(ogr.FieldDefn("z", ogr.OFTReal))
layer.CreateField(ogr.FieldDefn("bb", ogr.OFTReal))
layer.CreateField(ogr.FieldDefn("cc", ogr.OFTReal))
layer.CreateField(ogr.FieldDefn("dd", ogr.OFTReal))


for i in range(0, lon):

    feature = ogr.Feature(layer.GetLayerDefn())
    feature.SetField("id", idx[i])
    feature.SetField("aa", aa[i])
    feature.SetField("gpstime", gpstime[i])
    feature.SetField("x", x[i])
    feature.SetField("y", y[i])
    feature.SetField("z", z[i])
    feature.SetField("bb", bb[i])
    feature.SetField("cc", cc[i])
    feature.SetField("dd", dd[i])

    wkt = "POINT(%f %f)" % (float(x[i]) , float(y[i]))

    point = ogr.CreateGeometryFromWkt(wkt)

    feature.SetGeometry(point)
    layer.CreateFeature(feature)
    feature.Destroy()

data_source.Destroy()
1

This is quite easy once you have discovered the Python OGR Cookbook.

sticking to two together is left as an exercise for the reader.

  • Thank you, I can't upvote, but I finished it... Is it ok if I edit my post with the fully working version so anyone struggling can use it? – Thriskel Sep 1 '17 at 16:04

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.