0

I have a set or coordinates which forms lines from two points (p1-p2, p2-p3,...., p(n-1)-p(n)), I want to create a SHP file in order to draw several lines from these points, which have several features (Length, Slope, Velocity, etc).

So far, I have been able to create the shape file and create fields, but I can not manage to fill these fields or create a geometry lines to associate with. Can anybody point me to an example to successfully draw lines on a shapefile using a foor loop, because coordinates points are in the order of magnitude of 10^2.

#IMPORTAR LIBRERIAS
import ogr,os
import osgeo.osr as osr

#DRIVERNAME
driver = ogr.GetDriverByName('ESRI Shapefile')
ds = driver.CreateDataSource("PROYECTO_" + shp_name + os.path.sep + "00_GIS" + os.path.sep + "00_IN" + os.path.sep + shp_name + '.shp')

#REFERENCIA ESPACIAL
srs = osr.SpatialReference()
srs.ImportFromEPSG(32717) #WGS84 17S

#CREAR SHAPE de PUNTOS (ogr.wbkPoint) O LINEA (ogr.wkbLineString)
if shp_type == "linea":
    layer = ds.CreateLayer(shp_name, srs, ogr.wkbLineString)
if shp_type == "punto":
    layer = ds.CreateLayer(shp_name, srs, ogr.wkbPoint)

#AGREGAR CAMPOS
#NOMBRE TRAMO (Numero o Caracter)
fieldDefn = ogr.FieldDefn('Ramal', ogr.OFTString)
layer.CreateField(fieldDefn)

#NOMBRE TIPO DE ALCANTARILLADO (Sanitario, Pluvial, Combinado)
fieldDefn = ogr.FieldDefn('Tipo', ogr.OFTString)
layer.CreateField(fieldDefn)

# NOMBRE TIPO DE ALCANTARILLADO (Sanitario, Pluvial, Combinado)
fieldDefn = ogr.FieldDefn('Material', ogr.OFTString)
layer.CreateField(fieldDefn)

# LONGITUD
fieldDefn = ogr.FieldDefn('Longitud', ogr.OFTString)
layer.CreateField(fieldDefn)
0

For each pair of coordinates:

  1. Create geometry object and add each coordinate (see here).

  2. Create a feature object, set the geometry object and any attribute info, add it to the layer (similar to here)

  • Hi Nick, thank you for the guidance. I will post my result code for anyone with the same question. – Chelo Barros Oct 22 at 15:51
0

Thank to Nick's answer i was able to get a working code for drawing multiples lines from a coordinates set, not sure if it is efficient but it works. Greetings, Marcelo.

 coords = [(0, 0), (0, 10), (10, 0)]

def shp_out(coords):

# IMPORTAR LIBRERIAS
import ogr
import osgeo.osr as osr

# DRIVERNAME
driver = ogr.GetDriverByName('ESRI Shapefile')
ds = driver.CreateDataSource('OUT.shp')

# REFERENCIA ESPACIAL
srs = osr.SpatialReference()
srs.ImportFromEPSG(32717)  # WGS84 17S

# CREAR SHAPE LINEA (ogr.wkbLineString)
layer = ds.CreateLayer(shp_name, srs, ogr.wkbLineString)

# AGREGAR CAMPOS
# NOMBRE TRAMO (Numero o Caracter)
fieldDefn = ogr.FieldDefn('FIELD_1', ogr.OFTString)
layer.CreateField(fieldDefn)
line = ogr.Geometry(ogr.wkbLineString)
for i in range(len(coords)-1):



    line.AddPoint(coords[i][0], coords[i][1])
    line.AddPoint(coords[i + 1][0], coords[i + 1][0])

    #ADD FIELD
    feature = ogr.Feature(layer.GetLayerDefn())
    feature.SetField("FIELD_1", 'VALUE1')
    feature.SetGeometry(line)

    # Create the feature in the layer (geojson)
    layer.CreateFeature(feature)
    # Destroy the feature to free resources
    feature.Destroy()

or if you wan to have a different line for each data point pair, (REALLY bad idea to use globlas for this, but I can not figure it out any other way).

    for i in range(len(coords) - 1):
         globals()["line" + str(i)] = ogr.Geometry(ogr.wkbLineString)
         globals()["line" + str(i)].AddPoint(coords[i][0], coords[i][1])
         globals()["line" + str(i)].AddPoint(coords[i + 1][0], coords[i + 1][0])

        feature = ogr.Feature(layer.GetLayerDefn())
        for j in range(len(campos)):
             feature.SetField(campos[j], 'VALUE2')
        feature.SetGeometry(globals()["line" + str(i)])

        # Create the feature in the layer (geojson)
        layer.CreateFeature(feature)
        # Destroy the feature to free resources
        feature.Destroy()
        globals()["line" + str(i)].Destroy()

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.