Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

How would you create a shapefile consisting of 2 lines using Python OGR?

First line is no problem, but how do you reuse MyLine and Feature objects to create the second line?

Ultimately this will be in a loop with thousands of lines created...

from osgeo import ogr
driver = ogr.GetDriverByName('ESRI Shapefile')
datasource = driver.CreateDataSource('c:/temp/testlines.shp')
layer = datasource.CreateLayer('layerName',geom_type=ogr.wkbLineString)

#create first line 0,0 to 10,0:
myLine = ogr.Geometry(type=ogr.wkbLineString)
myLine.AddPoint_2D(0,0)
myLine.AddPoint_2D(10,0)
feature.SetGeometryDirectly(myLine)
layer.CreateFeature(feature)

#create next line 0,0 to 0,10?:
#how to you reuse the myLine, feature objects?
#myLine.DeletePoints()#something like this?
myLine.AddPoint_2D(0,0)
myLine.AddPoint_2D(0,10)
#feature.NesGeometry() #something lik this?
feature.SetGeometry(myLine)
layer.CreateFeature(feature)
share|improve this question
1  
Why not just create a new Geometry and Feature object with each iteration? –  blah238 Dec 6 '12 at 3:39
    
Yes different than arcpy where i typically reuse geometry objects which i assumed was faster than creating new geometry objects..my ignorance..new to OGR, experienced in arcpy –  Dave Dec 6 '12 at 23:59
add comment

2 Answers 2

You could make myLine into a factory function:

def myLine():
    return ogr.Geometry(type=ogr.wkbLineString)

and then for each line you need to create:

line = myLine()
line.AddPoint_2D(0,0)
line.AddPoint_2D(0,10)
feature.SetGeometryDirectly(line)

However this would be a minor gain. You could become more expressive and concise in this way:

def myLine(coords):
    line = ogr.Geometry(type=ogr.wkbLineString)
    for xy in coords:
        line.AddPoint_2D(xy[0],xy[1])
    return line

where coords is a list of tuples:

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

and it can even be longer than 2:

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

and you would use it just like this:

line = myLine([(0,0), (0,10)])
feature.SetGeometryDirectly(line)

Incidentally, this follows quite closely the GeoJSON specification for LINESTRING geometries.

share|improve this answer
1  
I'd possibly also make the shapefile creation part into a function too so you can re-use the code later. The principle of DRY ("Don't Repeat Yourself") is a good one in coding. I have a Python module that I have slowly built up of reusable bits of code like this and when I write some new code, I can simply import my module or functions from it. If you are getting into scripting, then becoming familiar with the Eclipse IDE will also be a benefit in the long run. –  MappaGnosis Dec 6 '12 at 9:50
    
Yes different than arcpy where i typically reuse geometry objects which i assumed was faster than creating new geometry objects..my ignorance..new to OGR, experienced in arcpy –  Dave Dec 7 '12 at 0:01
    
To extend Sylvester's remarks---if you go with Eclipse IDE for Python work, I've found PyDev to be a helpful plugin, extending the Eclipse dashboard with Python perspectives, as well as improved tooling (code completion, debug support, compile support, etc). –  elrobis Dec 7 '12 at 13:54
add comment

According to the ogr.Geometry documentation myLine.Empty() should do the trick.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.