safe.gui.tools.shake_grid.shake_grid module

Functionality related to convert format file.

InaSAFE Disaster risk assessment tool developed by AusAid and World Bank

Contact : ole.moller.nielsen@gmail.com

Note

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

Initially this was adapted from shake_event.py and now realtime uses this.

class safe.gui.tools.shake_grid.shake_grid.ShakeGrid(title, source, grid_xml_path, output_dir=None, output_basename=None, algorithm_filename_flag=True)[source]

Bases: object

A converter for USGS shakemap grid.xml files to geotiff.

create_keyword_file(algorithm)[source]

Create keyword file for the raster file created.

Basically copy a template from keyword file in converter data and add extra keyword (usually a title)

Parameters:algorithm (str) – Which re-sampling algorithm to use. valid options are ‘nearest’ (for nearest neighbour), ‘invdist’ (for inverse distance), ‘average’ (for moving average). Defaults to ‘nearest’ if not specified. Note that passing re-sampling alg parameters is currently not supported. If None is passed it will be replaced with ‘nearest’.
extract_date_time(the_time_stamp)[source]

Extract the parts of a date given a timestamp as per below example.

Parameters:the_time_stamp (str) – The ‘event_timestamp’ attribute from grid.xml.

# now separate out its parts # >>> e = “2012-08-07T01:55:12WIB” #>>> e[0:10] #‘2012-08-07’ #>>> e[12:-3] #‘01:55:11’ #>>> e[-3:] #’WIB’ (WIB = Western Indonesian Time)

grid_file_path()[source]

Validate that grid file path points to a file.

Returns:The grid xml file path.
Return type:str
Raises:GridXmlFileNotFoundError
mmi_to_contours(force_flag=True, algorithm='nearest')[source]

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

Parameters:
  • force_flag (bool) – (Optional). Whether to force the regeneration of contour product. Defaults to False.
  • algorithm (str Only enforced if theForceFlag is true!) – (Optional) Which interpolation algorithm to use to create the underlying raster. Defaults to ‘nearest’.
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.

mmi_to_delimited_file(force_flag=True)[source]

Save mmi_data to delimited text file suitable for gdal_grid.

The output file will be of the same format as strings returned from mmi_to_delimited_text().

Parameters:force_flag (bool) – Whether to force the regeneration of the output file. Defaults to False.
Returns:The absolute file system path to the delimited text file.
Return type:str

Note

An accompanying .csvt will be created which gdal uses to determine field types. The csvt will contain the following string: “Real”,”Real”,”Real”. These types will be used in other conversion operations. For example to convert the csv to a shp you would do:

ogr2ogr -select mmi -a_srs EPSG:4326 mmi.shp mmi.vrt mmi
mmi_to_delimited_text()[source]

Return the mmi data as a delimited test string.

Returns:A delimited text string that can easily be written to disk for e.g. use by gdal_grid.
Return type:str

The returned string will look like this:

123.0750,01.7900,1
123.1000,01.7900,1.14
123.1250,01.7900,1.15
123.1500,01.7900,1.16
etc...
mmi_to_raster(force_flag=False, algorithm='nearest')[source]

Convert the grid.xml’s mmi column to a raster using gdal_grid.

A geotiff file will be created.

Unfortunately no python bindings exist for doing this so we are going to do it using a shell call.

Example of the gdal_grid call we generate:

gdal_grid -zfield "mmi" -a invdist:power=2.0:smoothing=1.0            -txe 126.29 130.29 -tye 0.802 4.798 -outsize 400 400 -of GTiff            -ot Float16 -l mmi mmi.vrt mmi.tif

Note

It is assumed that gdal_grid is in your path.

Parameters:
  • force_flag (bool) – Whether to force the regeneration of the output file. Defaults to False.
  • algorithm (str) – Which re-sampling algorithm to use. valid options are ‘nearest’ (for nearest neighbour), ‘invdist’ (for inverse distance), ‘average’ (for moving average). Defaults to ‘nearest’ if not specified. Note that passing re-sampling alg parameters is currently not supported. If None is passed it will be replaced with ‘nearest’.
Returns:

Path to the resulting tif file.

Return type:

str

Note

For interest you can also make quite beautiful smoothed raster using this:

gdal_grid -zfield “mmi” -a_srs EPSG:4326 -a invdist:power=2.0:smoothing=1.0 -txe 122.45 126.45 -tye -2.21 1.79 -outsize 400 400 -of GTiff -ot Float16 -l mmi mmi.vrt mmi-trippy.tif

mmi_to_shapefile(force_flag=False)[source]

Convert grid.xml’s mmi column to a vector shp file using ogr2ogr.

An ESRI shape file will be created.

Parameters:force_flag – bool (Optional). Whether to force the regeneration of the output file. Defaults to False.
Returns:Path to the resulting tif file.
Return type:str

Example of the ogr2ogr call we generate:

ogr2ogr -select mmi -a_srs EPSG:4326 mmi.shp mmi.vrt mmi

Note

It is assumed that ogr2ogr is in your path.

mmi_to_vrt(force_flag=True)[source]

Save the mmi_data to an ogr vrt text file.

Parameters:force_flag (bool) – Whether to force the regeneration of the output file. Defaults to False.
Returns:The absolute file system path to the .vrt text file.
Return type:str
Raises:None
parse_grid_xml()[source]

Parse the grid xyz and calculate the bounding box of the event.

Raises:GridXmlParseError

The grid xyz dataset looks like this:

<?xml version="1.0" encoding="US-ASCII" standalone="yes"?>
<shakemap_grid xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://earthquake.usgs.gov/eqcenter/shakemap"
xsi:schemaLocation="http://earthquake.usgs.gov
http://earthquake.usgs.gov/eqcenter/shakemap/xml/schemas/
shakemap.xsd" event_id="20120807015938" shakemap_id="20120807015938"
shakemap_version="1" code_version="3.5"
process_timestamp="2012-08-06T18:28:37Z" shakemap_originator="us"
map_status="RELEASED" shakemap_event_type="ACTUAL">
<event magnitude="5.1" depth="206" lat="2.800000"
    lon="128.290000" event_timestamp="2012-08-07T01:55:12WIB"
    event_network="" event_description="Halmahera, Indonesia    " />
<grid_specification lon_min="126.290000" lat_min="0.802000"
    lon_max="130.290000" lat_max="4.798000"
    nominal_lon_spacing="0.025000" nominal_lat_spacing="0.024975"
    nlon="161" nlat="161" />
<grid_field index="1" name="LON" units="dd" />
<grid_field index="2" name="LAT" units="dd" />
<grid_field index="3" name="PGA" units="pctg" />
<grid_field index="4" name="PGV" units="cms" />
<grid_field index="5" name="MMI" units="intensity" />
<grid_field index="6" name="PSA03" units="pctg" />
<grid_field index="7" name="PSA10" units="pctg" />
<grid_field index="8" name="PSA30" units="pctg" />
<grid_field index="9" name="STDPGA" units="pctg" />
<grid_field index="10" name="URAT" units="" />
<grid_field index="11" name="SVEL" units="ms" />
<grid_data>
126.2900 04.7980 0.01 0.02 1.16 0.05 0.02 0 0.5 1 600
126.3150 04.7980 0.01 0.02 1.16 0.05 0.02 0 0.5 1 600
126.3400 04.7980 0.01 0.02 1.17 0.05 0.02 0 0.5 1 600
126.3650 04.7980 0.01 0.02 1.17 0.05 0.02 0 0.5 1 600
...
... etc

Note

We could have also obtained some of this data from the grid.xyz and event.xml but the grid.xml is preferred because it contains clear and unequivical metadata describing the various fields and attributes. Also it provides all the data we need in a single file.

set_contour_properties(input_file)[source]

Set the X, Y, RGB, ROMAN attributes of the contour layer.

Parameters:input_file (str) – (Required) Name of the contour layer.
Raise:InvalidLayerError if anything is amiss with the layer.
safe.gui.tools.shake_grid.shake_grid.convert_mmi_data(grid_xml_path, title, source, output_path=None, algorithm=None, algorithm_filename_flag=True)[source]

Convenience function to convert a single file.

Parameters:
  • grid_xml_path (str) – Path to the xml shake grid file.
  • title (str) – The title of the earthquake.
  • source (str) – The source of the shake data.
  • output_path (str) – Specify which path to use as an alternative to the default.
  • algorithm (str) – Type of algorithm to be used.
  • algorithm_filename_flag (bool) – Flag whether to use the algorithm in the output file’s name.
Returns:

A path to the resulting raster file.

Return type:

str

safe.gui.tools.shake_grid.shake_grid.data_dir()[source]

Return the path to the standard data dir for e.g. geonames data

Returns:Returns the default data directory.
Return type:str