[FWTools] ogr: how to create a data source?

Frank Warmerdam warmerdam at pobox.com
Tue Oct 18 11:15:18 EDT 2005


On 10/18/05, Bryan Keith <bryan at geomega.com> wrote:
> If this isn't the correct list for this question, let me know.
>
> I have some Python code that creates a new shapefile.  It uses these ogr
> calls:
>
> ogrSDS = ogr.Open(os.path.abspath(""),1)
> ogrSLayerNew = ogrSDS.CreateLayer(myfilename, None, geomtype)
>
> This works fine if a shapefile already exists in the directory.
> However, when no shapefiles (or any other data sources) exist in the
> directory then the ogr.Open method fails.  Is there a way to create a
> data source?  Or what am I supposed to do here?

Bryan,

>From the "tigerpoly.py" example script, you can see the following
sequence for creating a shapefile datasource.  If "outfile" is a filename
ending in .shp then a single shapefile is created.  Otherwise OGR will
attempt to create a directory of that name.

Note that the only way to start creating shapefiles in an existing empty
directory is to open single-file datasources.

shp_driver = ogr.GetDriverByName( 'ESRI Shapefile' )
shp_ds = shp_driver.CreateDataSource( outfile )

Best regards,
--
---------------------------------------+--------------------------------------
I set the clouds in motion - turn up   | Frank Warmerdam, warmerdam at pobox.com
light and sound - activate the windows | http://pobox.com/~warmerdam
and watch the world go round - Rush    | Geospatial Programmer for Rent



More information about the FWTools mailing list