2 from shapely
.geometry
import MultiLineString
6 def contour2shp(data
, shp_path
, geot
, projection
):
9 for idx
, c
in enumerate(data
['contours']):
10 if len(c
['polygons'])>0:
11 time
= c
['time_begin'].replace('T','_')[:-1]
12 newfilename
= osp
.join(shp_path
, suffix
+'_'+time
+'.shp')
13 print('> creating shp file: %s' % newfilename
)
15 multi
= MultiLineString(c
['polygons'])
16 # Now convert it to a shapefile with OGR
17 driver
= ogr
.GetDriverByName('Esri Shapefile')
18 dataset
= driver
.CreateDataSource(newfilename
)
19 layer
= dataset
.CreateLayer('', srs
= projection
, geom_type
= ogr
.wkbMultiLineString
)
21 layer
.CreateField(ogr
.FieldDefn('id', ogr
.OFTInteger
))
22 defn
= layer
.GetLayerDefn()
23 # Create a new feature (attribute and geometry)
24 feat
= ogr
.Feature(defn
)
25 feat
.SetField('id', 123)
26 # Make a geometry, from Shapely object
27 geom
= ogr
.CreateGeometryFromWkb(multi
.wkb
)
28 feat
.SetGeometry(geom
)
29 layer
.CreateFeature(feat
)
30 # Save and close everything
31 shpfiles
.append(newfilename
)
32 ds
= layer
= feat
= geom
= None