Example #1
0
 def write_line_shp(line_list, out_shp):
     """Export ESRI Shapefile -- Line feature"""
     print('Write line shapefile: %s' % out_shp)
     driver = ogr_GetDriverByName(str('ESRI Shapefile'))
     if driver is None:
         print('ESRI Shapefile driver not available.')
         sys.exit(1)
     if os.path.exists(out_shp):
         driver.DeleteDataSource(out_shp)
     ds = driver.CreateDataSource(out_shp.rpartition(os.sep)[0])
     if ds is None:
         print('ERROR Output: Creation of output file failed.')
         sys.exit(1)
     lyr = ds.CreateLayer(str(out_shp.rpartition(os.sep)[2].split('.')[0]),
                          None, wkbLineString)
     for l in line_list:
         line = ogr_Geometry(wkbLineString)
         for i in l:
             line.AddPoint(i[0], i[1])
         templine = ogr_CreateGeometryFromJson(line.ExportToJson())
         feature = ogr_Feature(lyr.GetLayerDefn())
         feature.SetGeometry(templine)
         lyr.CreateFeature(feature)
         feature.Destroy()
     ds.Destroy()
Example #2
0
 def raster2shp(rasterfile,
                vectorshp,
                layername=None,
                fieldname=None,
                band_num=1,
                mask='default'):
     """Convert raster to ESRI shapefile"""
     FileClass.remove_files(vectorshp)
     FileClass.check_file_exists(rasterfile)
     # this allows GDAL to throw Python Exceptions
     gdal.UseExceptions()
     src_ds = gdal.Open(rasterfile)
     if src_ds is None:
         print('Unable to open %s' % rasterfile)
         sys.exit(1)
     try:
         srcband = src_ds.GetRasterBand(band_num)
     except RuntimeError as e:
         # for example, try GetRasterBand(10)
         print('Band ( %i ) not found, %s' % (band_num, e))
         sys.exit(1)
     if mask == 'default':
         maskband = srcband.GetMaskBand()
     elif mask is None or mask.upper() == 'NONE':
         maskband = None
     else:
         mask_ds = gdal.Open(mask)
         maskband = mask_ds.GetRasterBand(1)
     #  create output datasource
     if layername is None:
         layername = FileClass.get_core_name_without_suffix(rasterfile)
     drv = ogr_GetDriverByName(str('ESRI Shapefile'))
     dst_ds = drv.CreateDataSource(vectorshp)
     srs = None
     if src_ds.GetProjection() != '':
         srs = osr_SpatialReference()
         srs.ImportFromWkt(src_ds.GetProjection())
     dst_layer = dst_ds.CreateLayer(str(layername), srs=srs)
     if fieldname is None:
         fieldname = layername.upper()
     fd = ogr_FieldDefn(str(fieldname), OFTInteger)
     dst_layer.CreateField(fd)
     dst_field = 0
     result = gdal.Polygonize(srcband,
                              maskband,
                              dst_layer,
                              dst_field, ['8CONNECTED=8'],
                              callback=None)
     return result
Example #3
0
 def raster2shp(rasterfile, vectorshp, layername=None, fieldname=None,
                band_num=1, mask='default'):
     """Convert raster to ESRI shapefile"""
     FileClass.remove_files(vectorshp)
     FileClass.check_file_exists(rasterfile)
     # this allows GDAL to throw Python Exceptions
     gdal.UseExceptions()
     src_ds = gdal.Open(rasterfile)
     if src_ds is None:
         print('Unable to open %s' % rasterfile)
         sys.exit(1)
     try:
         srcband = src_ds.GetRasterBand(band_num)
     except RuntimeError as e:
         # for example, try GetRasterBand(10)
         print('Band ( %i ) not found, %s' % (band_num, e))
         sys.exit(1)
     if mask == 'default':
         maskband = srcband.GetMaskBand()
     elif mask is None or mask.upper() == 'NONE':
         maskband = None
     else:
         mask_ds = gdal.Open(mask)
         maskband = mask_ds.GetRasterBand(1)
     #  create output datasource
     if layername is None:
         layername = FileClass.get_core_name_without_suffix(rasterfile)
     drv = ogr_GetDriverByName(str('ESRI Shapefile'))
     dst_ds = drv.CreateDataSource(vectorshp)
     srs = None
     if src_ds.GetProjection() != '':
         srs = osr_SpatialReference()
         srs.ImportFromWkt(src_ds.GetProjection())
     dst_layer = dst_ds.CreateLayer(str(layername), srs=srs)
     if fieldname is None:
         fieldname = layername.upper()
     fd = ogr_FieldDefn(str(fieldname), OFTInteger)
     dst_layer.CreateField(fd)
     dst_field = 0
     result = gdal.Polygonize(srcband, maskband, dst_layer, dst_field,
                              ['8CONNECTED=8'], callback=None)
     return result
Example #4
0
 def write_line_shp(line_list, out_shp):
     """Export ESRI Shapefile -- Line feature"""
     print('Write line shapefile: %s' % out_shp)
     driver = ogr_GetDriverByName(str('ESRI Shapefile'))
     if driver is None:
         print('ESRI Shapefile driver not available.')
         sys.exit(1)
     if os.path.exists(out_shp):
         driver.DeleteDataSource(out_shp)
     ds = driver.CreateDataSource(out_shp.rpartition(os.sep)[0])
     if ds is None:
         print('ERROR Output: Creation of output file failed.')
         sys.exit(1)
     lyr = ds.CreateLayer(str(out_shp.rpartition(os.sep)[2].split('.')[0]), None, wkbLineString)
     for l in line_list:
         line = ogr_Geometry(wkbLineString)
         for i in l:
             line.AddPoint(i[0], i[1])
         templine = ogr_CreateGeometryFromJson(line.ExportToJson())
         feature = ogr_Feature(lyr.GetLayerDefn())
         feature.SetGeometry(templine)
         lyr.CreateFeature(feature)
         feature.Destroy()
     ds.Destroy()