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

I'm trying to create a shapefile layer based on the spatial projection of an already existing raster. (Ultimately, this will be used in gdal.Polygonize to get a shape from my raster data.) However, I am encountering an error when trying to use CreateLayer.

  • raster_uri- location of the raster file that I want to make into a shapefile. I know this exists.
  • out_uri- Location I want the new shapefile to print to.
  • layer_name- String layer name input.
  • filed_name- String field name input.

This is the code that I was using.

raster = gdal.Open(raster_uri)
driver = ogr.GetDriverByName("ESRI Shapefile")
ds = driver.CreateDataSource(out_uri)

spat_ref = osr.SpatialReference()
proj = raster.GetProjectionRef()

layer = ds.CreateLayer(layer_name, spat_ref, ogr.wkbPolygon)

It yields this error message:

TypeError: in method 'DataSource_CreateLayer', argument 2 of type 'char const *'

Outputing the type of spat_ref shows up as <class 'osgeo.osr.SpatialReference'> so I'm unsure what char const * it's complaining about.

Interestingly, if I try to do this all from a python shell using the same file, layer creates just fine. Any ideas on what might be the issue?

share|improve this question
up vote 3 down vote accepted

As it turns out, creating a layer name on an ESRI shapefile can't be done using a unicode string. Since way back in my file system, I was dynamically pulling layer names from a set of file names, I was allowing for unicode, but it is unsupported with that CreateLayer(). The issue was (sort of fixed) by doing the following:

layer_name = layer_name.encode('utf-8')
layer = ds.CreateLayer(layer_name, spat_ref, ogr.wkbPolygon)

Now it runs just fine, though with a slightly smaller potential character set.

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.