def mmi_to_contours(self, force_flag=True, algorithm=USE_ASCII):
        """Extract contours from the event's tif file.

        Contours are extracted at a 0.5 MMI interval. The resulting file will
        be saved in the extract directory. In the easiest use case you can

        :param force_flag:  (Optional). Whether to force the
         regeneration of contour product. Defaults to False.
        :type force_flag: bool

        :param algorithm: (Optional) Which interpolation algorithm to
                  use to create the underlying raster. Defaults to 'nearest'.
        :type algorithm: str
         **Only enforced if theForceFlag is true!**

        :returns: An absolute filesystem path pointing to the generated
            contour dataset.
        :exception: ContourCreationError

         simply do::

           shake_grid = ShakeGrid()
           contour_path = shake_grid.mmi_to_contours()

        which will return the contour dataset for the latest event on the
        ftp server.
        """
        LOGGER.debug('mmi_to_contours requested.')
        # TODO: Use sqlite rather?
        output_file_base = os.path.join(
            self.output_dir,
            '%s-contours-%s.' % (self.output_basename, algorithm))
        output_file = output_file_base + 'shp'
        if os.path.exists(output_file) and force_flag is not True:
            return output_file
        elif os.path.exists(output_file):
            try:
                os.remove(output_file_base + 'shp')
                os.remove(output_file_base + 'shx')
                os.remove(output_file_base + 'dbf')
                os.remove(output_file_base + 'prj')
            except OSError:
                LOGGER.exception(
                    'Old contour files not deleted'
                    ' - this may indicate a file permissions issue.')

        tif_path = self.mmi_to_raster(force_flag, algorithm)
        # Based largely on
        # http://svn.osgeo.org/gdal/trunk/autotest/alg/contour.py
        driver = ogr.GetDriverByName('ESRI Shapefile')
        ogr_dataset = driver.CreateDataSource(output_file)
        if ogr_dataset is None:
            # Probably the file existed and could not be overriden
            raise ContourCreationError(
                'Could not create datasource for:\n%s. Check that the file '
                'does not already exist and that you do not have file system '
                'permissions issues' % output_file)
        layer = ogr_dataset.CreateLayer('contour')
        field_definition = ogr.FieldDefn('ID', ogr.OFTInteger)
        layer.CreateField(field_definition)
        field_definition = ogr.FieldDefn('MMI', ogr.OFTReal)
        layer.CreateField(field_definition)
        # So we can fix the x pos to the same x coord as centroid of the
        # feature so labels line up nicely vertically
        field_definition = ogr.FieldDefn('X', ogr.OFTReal)
        layer.CreateField(field_definition)
        # So we can fix the y pos to the min y coord of the whole contour so
        # labels line up nicely vertically
        field_definition = ogr.FieldDefn('Y', ogr.OFTReal)
        layer.CreateField(field_definition)
        # So that we can set the html hex colour based on its MMI class
        field_definition = ogr.FieldDefn('RGB', ogr.OFTString)
        layer.CreateField(field_definition)
        # So that we can set the label in it roman numeral form
        field_definition = ogr.FieldDefn('ROMAN', ogr.OFTString)
        layer.CreateField(field_definition)
        # So that we can set the label horizontal alignment
        field_definition = ogr.FieldDefn('ALIGN', ogr.OFTString)
        layer.CreateField(field_definition)
        # So that we can set the label vertical alignment
        field_definition = ogr.FieldDefn('VALIGN', ogr.OFTString)
        layer.CreateField(field_definition)
        # So that we can set feature length to filter out small features
        field_definition = ogr.FieldDefn('LEN', ogr.OFTReal)
        layer.CreateField(field_definition)

        tif_dataset = gdal.Open(tif_path, GA_ReadOnly)
        # see http://gdal.org/java/org/gdal/gdal/gdal.html for these options
        band = 1
        contour_interval = 0.5
        contour_base = 0
        fixed_level_list = []
        use_no_data_flag = 0
        no_data_value = -9999
        id_field = 0  # first field defined above
        elevation_field = 1  # second (MMI) field defined above
        try:
            gdal.ContourGenerate(
                tif_dataset.GetRasterBand(band),
                contour_interval,
                contour_base,
                fixed_level_list,
                use_no_data_flag,
                no_data_value,
                layer,
                id_field,
                elevation_field)
        except Exception as e:
            LOGGER.exception('Contour creation failed')
            raise ContourCreationError(str(e))
        finally:
            del tif_dataset
            ogr_dataset.Release()

        # Copy over the standard .prj file since ContourGenerate does not
        # create a projection definition
        projection_path = os.path.join(
            self.output_dir,
            '%s-contours-%s.prj' % (self.output_basename, algorithm))
        source_projection_path = resources_path(
            'converter_data', 'mmi-contours.prj')
        shutil.copyfile(source_projection_path, projection_path)

        # Lastly copy over the standard qml (QGIS Style file)
        qml_path = os.path.join(
            self.output_dir,
            '%s-contours-%s.qml' % (self.output_basename, algorithm))
        source_qml_path = resources_path('converter_data', 'mmi-contours.qml')
        shutil.copyfile(source_qml_path, qml_path)

        # Now update the additional columns - X,Y, ROMAN and RGB
        try:
            set_contour_properties(output_file)
        except InvalidLayerError:
            raise

        return output_file
def mmi_to_contours ( self , force_flag = True , algorithm = USE_ASCII ) : LOGGER . debug ( 'mmi_to_contours requested.' ) output_file_base = os . path . join ( self . output_dir , '%s-contours-%s.' % ( self . output_basename , algorithm ) ) output_file = output_file_base + 'shp' if os . path . exists ( output_file ) and force_flag is not True : return output_file elif os . path . exists ( output_file ) : try : os . remove ( output_file_base + 'shp' ) os . remove ( output_file_base + 'shx' ) os . remove ( output_file_base + 'dbf' ) os . remove ( output_file_base + 'prj' ) except OSError : LOGGER . exception ( 'Old contour files not deleted' ' - this may indicate a file permissions issue.' ) tif_path = self . mmi_to_raster ( force_flag , algorithm ) driver = ogr . GetDriverByName ( 'ESRI Shapefile' ) ogr_dataset = driver . CreateDataSource ( output_file ) if ogr_dataset is None : raise ContourCreationError ( 'Could not create datasource for:\n%s. Check that the file ' 'does not already exist and that you do not have file system ' 'permissions issues' % output_file ) layer = ogr_dataset . CreateLayer ( 'contour' ) field_definition = ogr . FieldDefn ( 'ID' , ogr . OFTInteger ) layer . CreateField ( field_definition ) field_definition = ogr . FieldDefn ( 'MMI' , ogr . OFTReal ) layer . CreateField ( field_definition ) field_definition = ogr . FieldDefn ( 'X' , ogr . OFTReal ) layer . CreateField ( field_definition ) field_definition = ogr . FieldDefn ( 'Y' , ogr . OFTReal ) layer . CreateField ( field_definition ) field_definition = ogr . FieldDefn ( 'RGB' , ogr . OFTString ) layer . CreateField ( field_definition ) field_definition = ogr . FieldDefn ( 'ROMAN' , ogr . OFTString ) layer . CreateField ( field_definition ) field_definition = ogr . FieldDefn ( 'ALIGN' , ogr . OFTString ) layer . CreateField ( field_definition ) field_definition = ogr . FieldDefn ( 'VALIGN' , ogr . OFTString ) layer . CreateField ( field_definition ) field_definition = ogr . FieldDefn ( 'LEN' , ogr . OFTReal ) layer . CreateField ( field_definition ) tif_dataset = gdal . Open ( tif_path , GA_ReadOnly ) band = 1 contour_interval = 0.5 contour_base = 0 fixed_level_list = [ ] use_no_data_flag = 0 no_data_value = - 9999 id_field = 0 elevation_field = 1 try : gdal . ContourGenerate ( tif_dataset . GetRasterBand ( band ) , contour_interval , contour_base , fixed_level_list , use_no_data_flag , no_data_value , layer , id_field , elevation_field ) except Exception as e : LOGGER . exception ( 'Contour creation failed' ) raise ContourCreationError ( str ( e ) ) finally : del tif_dataset ogr_dataset . Release ( ) projection_path = os . path . join ( self . output_dir , '%s-contours-%s.prj' % ( self . output_basename , algorithm ) ) source_projection_path = resources_path ( 'converter_data' , 'mmi-contours.prj' ) shutil . copyfile ( source_projection_path , projection_path ) qml_path = os . path . join ( self . output_dir , '%s-contours-%s.qml' % ( self . output_basename , algorithm ) ) source_qml_path = resources_path ( 'converter_data' , 'mmi-contours.qml' ) shutil . copyfile ( source_qml_path , qml_path ) try : set_contour_properties ( output_file ) except InvalidLayerError : raise return output_file
Extract contours from the event's tif file.

        Contours are extracted at a 0.5 MMI interval. The resulting file will
        be saved in the extract directory. In the easiest use case you can

        :param force_flag:  (Optional). Whether to force the
         regeneration of contour product. Defaults to False.
        :type force_flag: bool

        :param algorithm: (Optional) Which interpolation algorithm to
                  use to create the underlying raster. Defaults to 'nearest'.
        :type algorithm: str
         **Only enforced if theForceFlag is true!**

        :returns: An absolute filesystem path pointing to the generated
            contour dataset.
        :exception: ContourCreationError

         simply do::

           shake_grid = ShakeGrid()
           contour_path = shake_grid.mmi_to_contours()

        which will return the contour dataset for the latest event on the
        ftp server.