Skip to content

Commit

Permalink
Add styles directory to WMS config (#470)
Browse files Browse the repository at this point in the history
* Add wms styles directory to config

* Refactor to helper function to reduce duplication
  • Loading branch information
tdrwenski authored Feb 26, 2024
1 parent def5377 commit 3e3cdc4
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
8 changes: 8 additions & 0 deletions docs/userguide/src/site/pages/thredds/ThreddsConfigRef.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ The following shows all the configuration options available in the WMS section o
<allow>true</allow>
<allowRemote>false</allowRemote>
<paletteLocationDir>wmsPalettes</paletteLocationDir>
<stylesLocationDir>wmsStyles</stylesLocationDir>
<maxImageWidth>2048</maxImageWidth>
<maxImageHeight>2048</maxImageHeight>
</WMS>
Expand All @@ -259,6 +260,13 @@ where they are contained.
* More information on the format of palette files can also be found in the
[ncWMS documentation](https://reading-escience-centre.gitbooks.io/ncwms-user-guide/content/06-development.html#:~:text=To%20add%20new,in%20hexadecimal%20notation.).
* If you created palette files for TDS 4.x and would like to use them in TDS 5.x, an open source tool named [Magic Palette Converter](https://github.com/billyz313/magic-palette-converter){:target="_blank"} for THREDDS is available to assist in the conversion (special thanks to [Billy Ashmall](https://github.com/Unidata/tds/discussions/346){:target="_blank"}!)
* `stylesLocationDir`: optionally specify the location of the directory containing your own style files, by specifying the directory
where they are contained.
* If the directory location starts with a `/`, the path is absolute, otherwise it is relative to `${tds.content.root.path}/thredds/`.
* The default directory for custom styles files is `${tds.content.root.path}/thredds/wmsStyles`.
* If you don't specify a custom styles directory, or specify it incorrectly, the default directory will be used.
* More information on the format of style files can also be found in the
[ncWMS documentation](https://reading-escience-centre.gitbooks.io/ncwms-user-guide/content/06-development.html#styles).
* `maxImageWidth`: the maximum image width in pixels that this WMS service will return.
* `maxImageHeight`: the maximum image height in pixels that this WMS service will return.

Expand Down
30 changes: 24 additions & 6 deletions tds/src/main/java/thredds/server/config/TdsConfigMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import thredds.server.wms.ThreddsWmsCatalogue;
import thredds.server.wms.config.WmsDetailedConfig;
import uk.ac.rdg.resc.edal.graphics.utils.ColourPalette;
import uk.ac.rdg.resc.edal.graphics.utils.SldTemplateStyleCatalogue;

/**
* Centralize the mapping of threddsConfig.xml configuration settings to the data objects used by
Expand Down Expand Up @@ -173,6 +174,7 @@ enum WmsConfigMappings {
WMS_ALLOW("WMS.allow", null, "true"),
WMS_ALLOW_REMOTE("WMS.allowRemote", null, "false"),
WMS_PALETTE_LOCATION_DIR("WMS.paletteLocationDir", null, null),
WMS_STYLES_LOCATION_DIR("WMS.stylesLocationDir", null, null),
WMS_MAXIMUM_IMAGE_WIDTH("WMS.maxImageWidth", null, "2048"),
WMS_MAXIMUM_IMAGE_HEIGHT("WMS.maxImageHeight", null, "2048"),
WMS_CONFIG_FILE("WMS.configFile", null, null);
Expand Down Expand Up @@ -200,14 +202,14 @@ String getValueFromThreddsConfig() {

static void load(WmsConfigBean wmsConfig, TdsContext tdsContext) {
final String defaultPaletteLocation = tdsContext.getThreddsDirectory() + "/wmsPalettes";
final String defaultStylesLocation = tdsContext.getThreddsDirectory() + "/wmsStyles";
final String defaultWmsConfigFile = tdsContext.getThreddsDirectory() + "/wmsConfig.xml";

wmsConfig.setAllow(Boolean.parseBoolean(WMS_ALLOW.getValueFromThreddsConfig()));
wmsConfig.setAllowRemote(Boolean.parseBoolean(WMS_ALLOW_REMOTE.getValueFromThreddsConfig()));

String paletteLocation = WMS_PALETTE_LOCATION_DIR.getValueFromThreddsConfig();
if (paletteLocation == null)
paletteLocation = defaultPaletteLocation;
final String paletteLocation =
getValueFromThreddsConfigOrDefault(WMS_PALETTE_LOCATION_DIR, defaultPaletteLocation);
wmsConfig.setPaletteLocationDir(paletteLocation);
try {
ColourPalette.addPaletteDirectory(new File(paletteLocation));
Expand All @@ -221,9 +223,17 @@ static void load(WmsConfigBean wmsConfig, TdsContext tdsContext) {
}
}

String wmsConfigFile = WMS_CONFIG_FILE.getValueFromThreddsConfig();
if (wmsConfigFile == null)
wmsConfigFile = defaultWmsConfigFile;
final String stylesLocation = getValueFromThreddsConfigOrDefault(WMS_STYLES_LOCATION_DIR, defaultStylesLocation);
wmsConfig.setStylesLocationDir(stylesLocation);
try {
SldTemplateStyleCatalogue.getStyleCatalogue().addStylesInDirectory(new File(stylesLocation));
} catch (FileNotFoundException e) {
if (!stylesLocation.equals(defaultStylesLocation)) {
startupLog.warn("Could not find custom styles directory {}", stylesLocation, e);
}
}

final String wmsConfigFile = getValueFromThreddsConfigOrDefault(WMS_CONFIG_FILE, defaultWmsConfigFile);

WmsDetailedConfig wdc = WmsDetailedConfig.fromLocation(wmsConfigFile);
if (wdc == null) {
Expand Down Expand Up @@ -261,6 +271,14 @@ static void load(WmsConfigBean wmsConfig, TdsContext tdsContext) {
}
}

private static String getValueFromThreddsConfigOrDefault(WmsConfigMappings property, String defaultValue) {
final String value = property.getValueFromThreddsConfig();
if (value == null) {
return defaultValue;
}
return value;
}

enum TdsUpdateConfigMappings {
TDSUPDAATE_LOGVERSIONINFO("TdsUpdateConfig.logVersionInfo", null, "true");

Expand Down
9 changes: 9 additions & 0 deletions tds/src/main/java/thredds/server/config/WmsConfigBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class WmsConfigBean {
private boolean allow;
private boolean allowRemote;
private String paletteLocationDir;
private String stylesLocationDir;
private int maxImageWidth;
private int maxImageHeight;

Expand Down Expand Up @@ -49,6 +50,14 @@ public void setPaletteLocationDir(String paletteLocationDir) {
this.paletteLocationDir = paletteLocationDir;
}

public String getStylesLocationDir() {
return stylesLocationDir;
}

public void setStylesLocationDir(String stylesLocationDir) {
this.stylesLocationDir = stylesLocationDir;
}

public int getMaxImageWidth() {
return maxImageWidth;
}
Expand Down

0 comments on commit 3e3cdc4

Please sign in to comment.