Hosting COG to Geoserver

In this exercise, we’ll host tiff file create via some other python script to AWS, and then expose it as Geoserver layer.

Installing COG plugin

Geoserver now allows us to create GeoTIFF store along with ability to connect cloud storage to access COG using plugin. You can install geoserver plugin using UI or with terminal. If you are not aware about how to install plugin.

Creating COG store using UI

Head over to store homepage , and click on add new store

click on GeoTIFF

Check the Cloud Optimized GeoTIFF to enable after which more settings are available.

  • URL – put valid url of s3
  • RANGE READER SETTINGS – Select http if you are putting url, otherwise select s3 (for AWS S3) or gs (Google Cloud Storage) if you are using URL from these cloud providers.
  • Username/Password – If the cloud data is private use credentials to access data

Once data is entered, create layer as you create with normal steps, make sure that EPSG code and Bounds are correct. You can also select the styling as per your requirement.

Click on Layer preview to visualise data as per styling

Creating COG store using REST API

We’ll checkout JavaScript-Fetch API to present all requests

We’ll start with coverage store.

var myHeaders = new Headers();
myHeaders.append("Authorization", "Basic YWRtaW46Z2Vvc2VydmVy");
myHeaders.append("Content-Type", "application/json");

var workspace_name = 'cite'
var store_name = 'cog_stre'
var raw = JSON.stringify({
  "coverageStore": {
    "workspace": workspace_name,
    "name": store_name,
    "enabled":true,
    "metadata": [
      {
        "entry": {
          "@key": "CogSettings.Key",
          "cogSettings": {
            "useCachingStream": false,
            "rangeReaderSettings": "HTTP"
          }
        }
      }
    ],
    "type": "GeoTIFF",
    "url": "cog://<HTTPS_LINL>"
  }
});

var requestOptions = {
  method: 'POST',
  headers: myHeaders,
  body: raw,
  redirect: 'follow'
};

fetch(`localhost:8080/geoserver/rest/workspaces/{workspace_name}/coveragestores/`, requestOptions)
  .then(response => response.text())
  .then(result => console.log(result))
  .catch(error => console.log('error', error));

Once storage is created, we can publish layer from store using name of the tiff file.

var myHeaders = new Headers();
myHeaders.append("Authorization", "Basic YWRtaW46Z2Vvc2VydmVy");
myHeaders.append("Content-Type", "application/json");

var name = 'desired_file'
var original_name = 'name-of-tiff-in-aws'
var workspace_name = 'cite'
var store_name = 'cog_stre'

var raw = JSON.stringify({
  "coverage": {
    "name": name,
    "nativeName": original_name
  }
});

var requestOptions = {
  method: 'POST',
  headers: myHeaders,
  body: raw,
  redirect: 'follow'
};

fetch(`http://localhost:8080/geoserver/rest/{workspace_name}/cite/coveragestores/{store_name}/coverages/`, requestOptions)
  .then(response => response.text())
  .then(result => console.log(result))
  .catch(error => console.log('error', error));

Optionally, if you want to update style, you can use following code snippet

var myHeaders = new Headers();
myHeaders.append("Authorization", "Basic YWRtaW46Z2Vvc2VydmVy");
myHeaders.append("Content-Type", "application/json");

var style_name = "ndvi"
var layer_name = 'layer_name'
var raw = JSON.stringify({
  "layer": {
    "defaultStyle": {
      "name": style_name
    }
  }
});

var requestOptions = {
  method: 'PUT',
  headers: myHeaders,
  body: raw,
  redirect: 'follow'
};

fetch(`http://localhost:8080/geoserver/rest/layers/{layer_name}/`, requestOptions)
  .then(response => response.text())
  .then(result => console.log(result))
  .catch(error => console.log('error', error));

Leave a Reply

Your email address will not be published.