adding requests to conda install docs
[JPSSData.git] / contour2shp.py
blobf99e8703a8f83a07497c43d99db222a8b58cbd92
1 from osgeo import ogr
2 from shapely.geometry import MultiLineString
3 import json, sys, math
4 import os.path as osp
6 def contour2shp(data, shp_path, geot, projection):
7 suffix = 'TIGN_G'
8 shpfiles = []
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)
14 # Multiline object
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)
20 # Add one attribute
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
34 return shpfiles