Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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)

#create next line 0,0 to 0,10?:
#how to you reuse the myLine, feature objects?
#myLine.DeletePoints()#something like this?
#feature.NesGeometry() #something lik this?
share|improve this question
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 to OGR, experienced in arcpy – Dave Dec 6 '12 at 23:59

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()

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:
    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)])

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

share|improve this answer
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 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

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

share|improve this answer

Your Answer


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.