Outputting raster data as WMS services in GeoServer

Unfortunately, PgRaster support is still not common and GeoServer does not support consuming PgRaster yet. This section shows how to use the PostgreSQL database to load a raster and then expose it via GeoServer.

We will make use of PgRaster a bit later though - we will write a simple WMS GetMap request handler.

Exposing a GeoServer layer based on a raster stored in PostgreSQL is not as straightforward as it was with vector tables.

In order to create a raster layer, we first need to install an Image Mosaic JDBC plugin: http://docs.geoserver.org/stable/en/user/data/raster/imagemosaicjdbc.html.

Do make sure to install the plugin appropriate to your GeoServer version.

Installing the plugin is rather straightforward and you should not encounter any difficulties doing so. It is just a matter of downloading and copying the content of a plugin archive (usually a JAR file) into GeoServer's WEB-INFlib directory.

In order to install the plugin, you will need to shut down your GeoServer instance and once the plugin is installed, you should bring it back.

Having installed our plugin, we now need to configure a data source for the raster layer. This is where some manual work is required. A detailed manual on how to configure GeoServer to consume raster data is here: http://docs.geoserver.org/stable/en/user/tutorials/imagemosaic-jdbc/imagemosaic-jdbc_tutorial.html, but since it is a bit XML-ish/SQL-ish, you find a tutorial excerpt in following section, so our configuration goes smoothly.

All the files that we are preparing here can be found in this chapter's code directory.

Let's create some raster coverage configuration files first. We will need three XML files; create them in the GEOSERVER_DATA_DIR/coverages:

  • ne_raster.postgis.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?> 
<!DOCTYPE ImageMosaicJDBCConfig [
<!ENTITY mapping PUBLIC "mapping" "mapping.postgis.xml">
<!ENTITY connect PUBLIC "connect" "connect.postgis.xml">
]>
<config version="1.0">
<coverageName name="ne_raster"/>
<coordsys name="EPSG:4326"/>
<scaleop interpolation="1"/>
<verify cardinality="false"/>
&mapping;
&connect;
</config>
  • connect.postgis.xml
<connect> 
<dstype value="DBCP"/>
<username value="postgres"/>
<password value="postgres"/>
<jdbcUrl value="jdbc:postgresql://localhost:5434/mastering_postgis"/>
<driverClassName value="org.postgresql.Driver"/>
<maxActive value="10"/>
<maxIdle value="0"/>
</connect>
  • mapping.postgis.xml
<spatialExtension name="postgis"/> 
<mapping>
<masterTable name="mosaic" >
<coverageNameAttribute name="name"/>
<maxXAttribute name="maxx"/>
<maxYAttribute name="maxy"/>
<minXAttribute name="minx"/>
<minYAttribute name="miny"/>
<resXAttribute name="resx"/>
<resYAttribute name="resy"/>
<tileTableNameAtribute name="tiletable" />
<spatialTableNameAtribute name="spatialtable" />
</masterTable>
<tileTable>
<blobAttributeName name="data" />
<keyAttributeName name="location" />
</tileTable>
<spatialTable>
<keyAttributeName name="location" />
<geomAttributeName name="geom" />
<tileMaxXAttribute name="maxx"/>
<tileMaxYAttribute name="maxy"/>
<tileMinXAttribute name="minx"/>
<tileMinYAttribute name="minx"/>
</spatialTable>
</mapping>

Once ready with the XML files, navigate to GEOSERVER_DATA_DIR/coverages, create a ne_raster_scripts directory, and then execute the following command:

java -jar GEOSERVER_WEB_SERVER_WEBAPPS/geoserver/WEB-INF/lib/ gt-imagemosaic-jdbc-16.0.jar ddl -config / GEOSERVER_DATA_DIR/coverages/ne_raster.postgis.xml -spatialTNPrefix ne_raster -pyramids 6 -statementDelim ";" -srs 4326 -targetDir ne_raster_sqlscripts

This should output some SQL scripts toGEOSERVER_DATA_DIR/coverages/ne_raster_scripts that will let us create the appropriate tables for the raster data. Scripts will create tables in the public schema, so this may be a bit inconvenient.

In the preceding command, you should adjust some paths:
  • GEOSERVER_WEB_SERVER_WEBAPPS - should be your GeoServer's webapps folder, where you have your GeoServer instance deployed
  • GEOSERVER_DATA_DIR - GeoServer's data directory.

When you're ready, just execute the generated scripts:

  • Createmeta.sql
  • Add_ne_raster.sql

This should create some new tables in the public schema.

Once our database is prepared, we can take care of processing the raster. First we need to tile it using gdal_retile:

gdal_retile.py -co TFW=YES -r near -ps 256 256 -of GTiff -levels 6 -targetDir tiles NE2_50M_SR_W.tif  
I am using a smaller raster here for a simple reason: a larger one would likely cause some troubles related to limited memory associated with our GeoServer's Jetty container.

With the tiles ready, we can now import them into the database. Before we do so, let's ensure that a PostgreSQL driver is located in the lib/ext directory of the Java runtime (you can copy the file from your geoserver/WEB-INF/lib directory; my Windows box had a postgresql-9.4.1211.jar file).

The final raster processing step is to use the imagemosaik plugin again to import the generated tiles into the database:

java -jar GEOSERVER_WEB_SERVER_WEBAPPS/geoserver/WEB-INF/lib/gt-imagemosaic-jdbc-16.0.jar import -config GEOSERVER_DATA_DIR/coverages/ne_raster.postgis.xml -spatialTNPrefix ne_raster -tileTNPrefix ne_raster -dir tiles -ext tif

Or final step is to set up a layer in GeoServer. In order to do so, click Stores under the Data section and click the Add new Store link. Next, pick the ImageMosaicJDBC link and create a data source called webgis_raster under the mastering_postgis workspace. In the URL field, you will have to provide a file URL to the ne_raster.postgis.xml we created in the GEOSERVER_DATA_DIR/coverages directory. When you click the Save button, you should be taken to the Create layer screen:

Click the Publish link and you will be taken to the layer configuration screen. Make sure to verify the projection information and to reload the band definitions and save the layer. Provided that you have configured the layer properly, you should now be able to preview it:

If you happen to preview the requests the browser is issuing, you may find that the URLs are similar to the following one that pulls an image for the Iberian peninsula:

http://localhost:8080/geoserver/mastering_postgis/wms?SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&FORMAT=image%2Fjpeg&TRANSPARENT=true&tiled=true&STYLES&LAYERS=mastering_postgis%3Ane_raster&tilesOrigin=-179.9833333333%2C-90.0166666658&WIDTH=256&HEIGHT=256&SRS=EPSG%3A4326&BBOX=-11.25%2C33.75%2C0%2C45

There is a GeoServer plugin meant to simplify all the work we have done so far. It is called PgRaster and you can obtain more information on it here: http://geoserver.readthedocs.io/en/latest/community/pgraster/pgraster.html;
Older versions of the PGRaster plugin can be found here: https://repo.boundlessgeo.com/release/org/geoserver/community/gs-pgraster/.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset