As I see from the GDAL/OGR available solutions, most of the new OGR data sources (shapefile etc.) are being created from an already existing similar file. This way many steps of directly creating Layers, Fields and Attributes are avoided and thus makes life much easier to manipulate the existing structure of a vector layer.
I wonder however how to :
- Create a simple geometry (a line)
- Create a shapefile from scratch
- Put the geometry inside the shapefile and save it to the disc
Here is the code that I have so-far prepared.
import sys from osgeo import ogr from osgeo import osr # Creating a line geometry linegeo = ogr.Geometry(ogr.wkbLineString) linegeo.AddPoint(54,37) linegeo.AddPoint(62,35) linegeo.AddPoint(70,38) linegeo.AddPoint(74,41.5) # Set up the shapefile driver driver = ogr.GetDriverByName("ESRI Shapefile") # create the data source ds = driver.CreateDataSource("line.shp") # create the spatial reference system, WGS84 srs = osr.SpatialReference() srs.ImportFromEPSG(4326) # create one layer layer = ds.CreateLayer("line", srs, ogr.wkbLineString)
At this point I think I need to somehow use the "ogr.FieldDefn()" in order to place the above created line geometry inside the newly created layer.
How do I do that?