1 | | In morphology, the dilation operation followed erosion is an extremely useful technique that allows for the simplification of complex shapes. For example, the technique can be used to produce a simplified coastline, where bays and fjords are ignored, and that can later be used as a mask for further operations. |
| 1 | In morphology, the dilation operation followed by erosion is an extremely useful technique that allows for the simplification of complex shapes. For example, the technique can be used to [http://blog.opengeo.org/2010/11/22/removing-complexities produce a simplified coastline], where bays and fjords are ignored, and that can later be used as a mask for further operations. Below is an illustration of the process of this technique for simplifying the convoluted coastline of Newfoundland, Canada, using GSHHS. |
| 2 | |
| 3 | At first glance, it would seem that simply using one of the lower resolution coastline datasets from GSHHS would achieve the same result, but they were generated using a different procedure, which is not equivalent. |
| 4 | |
| 5 | = Import GSHHS polygons into PostGIS = |
| 6 | |
| 7 | [ftp://ftp.soest.hawaii.edu/pwessel/gshhs/GSHHS_shp_2.2.0.zip GSHHS shapefiles] provided by the maintainers can be imported into a PostGIS database (named gshhs here) using: |
| 8 | |
| 9 | {{{ |
| 10 | shp2pgsql -s 4326 -I -g geom GSHHS_f_L1.shp gshhs_f_l1 | psql -d gshhs |
| 11 | }}} |
| 12 | |
| 13 | = Subset coastlines = |
| 14 | |
| 15 | Before performing any dilation/erosion, it is helpful to subset the coastline polygons for efficiency purposes. The {{{gid}}} field in the table holding the GSHHS data identifies a single polygon. The island of Newfoundland has {{{gid=23}}}: |
| 16 | |
| 17 | {{{ |
| 18 | CREATE OR REPLACE VIEW newfoundland_subset_f |
| 19 | SELECT gshhs_f_l1.gid, gshhs_f_l1.geom |
| 20 | FROM gshhs_f_l1 |
| 21 | WHERE gshhs_f_l1.gid = 23; |
| 22 | }}} |
| 23 | |
| 24 | = Dilation and erosion = |
| 25 | |
| 26 | PostGIS can perform these two operations via {{{ST_Buffer}}}. For the purposes outlined here, the process involves buffering positively (dilating) the full resolution coastline by a given amount {{{k}}}, and then buffering the result negatively (eroding) by the same amount. The following shell script applies the technique using different values for {{{k}}}. It uses the utility {{{ogr2ogr}}} to export the PostGIS objects to an ASCII table in GMT (Generic Mapping Tools) format: |
| 27 | |
| 28 | {{{ |
| 29 | PREFIX="newfoundland" |
| 30 | COAST="newfoundland_subset_f.gmt" |
| 31 | BUFOUT="newfoundland_subset_f_bufout" |
| 32 | BUFIN="newfoundland_subset_f_bufin" |
| 33 | |
| 34 | ogr2ogr -f GMT ${COAST} \ |
| 35 | PG:"host=localhost user=username password=password dbname=gshhs" \ |
| 36 | ${COAST%%.gmt} |
| 37 | for buf in ${BUFFERS[@]}; do |
| 38 | cat <<EOF > ${GMT_TMPDIR}/buffers.sql |
| 39 | CREATE OR REPLACE VIEW ${COAST%%.gmt}_bufout AS |
| 40 | SELECT gid, ST_Buffer(geom, ${buf}) FROM ${COAST%%.gmt}; |
| 41 | CREATE OR REPLACE VIEW ${COAST%%.gmt}_bufin AS |
| 42 | SELECT gid, ST_Buffer(st_buffer, -${buf}) FROM ${COAST%%.gmt}_bufout; |
| 43 | EOF |
| 44 | psql -f ${GMT_TMPDIR}/buffers.sql gshhs |
| 45 | ogr2ogr -f GMT ${COAST%%.gmt}_bufout${buf}.gmt \ |
| 46 | PG:"host=localhost user=username password=password dbname=gshhs" \ |
| 47 | ${COAST%%.gmt}_bufout |
| 48 | ogr2ogr -f GMT ${COAST%%.gmt}_bufin${buf}.gmt \ |
| 49 | PG:"host=localhost user=username password=password dbname=gshhs" \ |
| 50 | ${COAST%%.gmt}_bufin |
| 51 | done |
| 52 | }}} |
| 53 | |
| 54 | The animated {{{GIF}}} image below illustrates the results. |