Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Geos 11353 map ml default image format #367

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/en/user/source/extensions/mapml/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ The link is generated so that it always work, if the CRS configured for the laye

**MapML Output Format**

The output image format for the MapML resource should be specified using the format_options parameter with a key called ``mapml-wms-format``. If provided, the provided mime type must be a valid WMS format specifier. If not provided, it defaults to ``image/png``.
The output image format for the MapML resource should be specified using the format_options parameter with a key called ``mapml-wms-format``. If provided, the provided mime type must be a valid WMS format specifier. If not provided, it defaults to to the format set with the Default Mime Type dropdown under MapML Settings in the Publishing tab of the Edit Layer settings page.

Example::

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,15 @@ public final class MapMLConstants {
/** DIMENSION */
public static final String DIMENSION = "dimension";

/** DEFAULT MIME TYPE */
public static final String MIME = "mime";

/** MAPML_DIMENSION */
public static final String MAPML_DIMENSION = MAPML_PREFIX + DIMENSION;

/** MAPML_DIMENSION */
public static final String MAPML_MIME = MAPML_PREFIX + MIME;

/** SHARD_LIST */
public static final String SHARD_LIST = "shardList";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ public class MapMLDocumentBuilder {
public static final String MINIMUM_WIDTH_HEIGHT = "1";
private static final int BYTES_PER_PIXEL_TRANSPARENT = 4;
private static final int BYTES_PER_KILOBYTE = 1024;
public static final String DEFAULT_MIME_TYPE = "image/png";

private final WMS wms;

private final GeoServer geoServer;
Expand All @@ -129,7 +131,7 @@ public class MapMLDocumentBuilder {

private String defaultStyle;
private String layerTitle;
private String imageFormat;
private String imageFormat = DEFAULT_MIME_TYPE;
private String baseUrl;
private String baseUrlPattern;
private Boolean enableSharding;
Expand Down Expand Up @@ -326,7 +328,7 @@ public void initialize() throws ServiceException {
projType = mapMLLayerMetadata.getProjType();
layerTitle = layerTitlesCommaDelimited;
layerMeta = mapMLLayerMetadata.getLayerMeta();
imageFormat = (String) format.orElse("image/png");
imageFormat = (String) format.orElse(mapMLLayerMetadata.getDefaultMimeType());
baseUrl = ResponseUtils.baseURL(request);
baseUrlPattern = baseUrl;
// handle shard config
Expand Down Expand Up @@ -402,6 +404,7 @@ private MapMLLayerMetadata layersToOneMapMLLayerMetadata(List<RawLayer> layers)
mapMLLayerMetadata.setQueryable(layersToQueryable(layers));
mapMLLayerMetadata.setLayerLabel(layersToLabel(layers));
mapMLLayerMetadata.setProjType(projType);
mapMLLayerMetadata.setDefaultMimeType(imageFormat);

return mapMLLayerMetadata;
}
Expand Down Expand Up @@ -541,6 +544,7 @@ private MapMLLayerMetadata layerToMapMLLayerMetadata(RawLayer layer, String styl
String styleName = style != null ? style : "";
String cqlFilter = null;
boolean tileLayerExists = false;
String defaultMimeType = DEFAULT_MIME_TYPE;
if (isLayerGroup) {
layerGroupInfo = (LayerGroupInfo) layer.getPublishedInfo();
if (layerGroupInfo == null) {
Expand All @@ -558,6 +562,10 @@ private MapMLLayerMetadata layerToMapMLLayerMetadata(RawLayer layer, String styl
queryable = !layerGroupInfo.isQueryDisabled();
layerName = layerGroupInfo.getName();
layerTitle = getTitle(layerGroupInfo, layerName);
defaultMimeType =
Optional.ofNullable(layerGroupInfo.getMetadata().get(MapMLConstants.MAPML_MIME))
.orElse(DEFAULT_MIME_TYPE)
.toString();
} else {
layerInfo = (LayerInfo) layer.getPublishedInfo();
resourceInfo = layerInfo.getResource();
Expand All @@ -573,6 +581,10 @@ private MapMLLayerMetadata layerToMapMLLayerMetadata(RawLayer layer, String styl
layerTitle = getTitle(layerInfo, layerName);
// set the actual style name from the layer info
if (style == null) styleName = layerInfo.getDefaultStyle().getName();
defaultMimeType =
Optional.ofNullable(resourceInfo.getMetadata().get(MapMLConstants.MAPML_MIME))
.orElse(DEFAULT_MIME_TYPE)
.toString();
}
ProjType projType = parseProjType();
cqlFilter = cql != null ? cql : "";
Expand Down Expand Up @@ -600,7 +612,8 @@ private MapMLLayerMetadata layerToMapMLLayerMetadata(RawLayer layer, String styl
tileLayerExists,
useTiles,
useFeatures,
cqlFilter);
cqlFilter,
defaultMimeType);
}

/**
Expand Down Expand Up @@ -1792,7 +1805,7 @@ private String buildGetMap(
String formatOptions =
MapMLConstants.MAPML_WMS_MIME_TYPE_OPTION
+ ":"
+ escapeHtml4((String) format.orElse("image/png"));
+ escapeHtml4((String) format.orElse(imageFormat));
kvp.put("format_options", formatOptions);
kvp.put("SERVICE", "WMS");
kvp.put("REQUEST", "GetMap");
Expand Down Expand Up @@ -1938,6 +1951,7 @@ static class MapMLLayerMetadata {
private ReferencedEnvelope bbbox;

private String layerLabel;
private String defaultMimeType;

/**
* get if the layer uses features
Expand Down Expand Up @@ -1974,6 +1988,7 @@ public void setUseFeatures(boolean useFeatures) {
* @param styleName String
* @param tileLayerExists boolean
* @param useTiles boolean
* @param defaultMimeType String
*/
public MapMLLayerMetadata(
LayerInfo layerInfo,
Expand All @@ -1991,7 +2006,8 @@ public MapMLLayerMetadata(
boolean tileLayerExists,
boolean useTiles,
boolean useFeatures,
String cqFilter) {
String cqFilter,
String defaultMimeType) {
this.layerInfo = layerInfo;
this.bbox = bbox;
this.isLayerGroup = isLayerGroup;
Expand All @@ -2008,6 +2024,7 @@ public MapMLLayerMetadata(
this.useTiles = useTiles;
this.useFeatures = useFeatures;
this.cqlFilter = cqFilter;
this.defaultMimeType = defaultMimeType;
}

/** Constructor */
Expand Down Expand Up @@ -2366,5 +2383,23 @@ public String getCqlFilter() {
public void setCqlFilter(String cqlFilter) {
this.cqlFilter = cqlFilter;
}

/**
* get the default mime type
*
* @return String
*/
public String getDefaultMimeType() {
return defaultMimeType;
}

/**
* set the default mime type
*
* @param defaultMimeType String
*/
public void setDefaultMimeType(String defaultMimeType) {
this.defaultMimeType = defaultMimeType;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,21 @@ <h3>
</ul>
</fieldset>
</li>
<li>
<fieldset>
<legend>
<span><wicket:message key="mapmlDefaultMimeSection">Default Mimetype</wicket:message></span>
</legend>
<ul>
<li>
<label for="mime">
<wicket:message key="mapmlDefaultMime">Mime</wicket:message>
</label>
<select id="mime" wicket:id="mime"></select>
</li>
</ul>
</fieldset>
</li>
</ul>
</wicket:panel>
</form>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
*/
package org.geoserver.mapml;

import static org.geoserver.mapml.MapMLConstants.MAPML_USE_TILES;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
Expand All @@ -12,6 +14,10 @@
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.OnChangeAjaxBehavior;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.ListMultipleChoice;
Expand All @@ -32,11 +38,16 @@
import org.geoserver.catalog.PublishedType;
import org.geoserver.catalog.ResourceInfo;
import org.geoserver.catalog.ResourcePool;
import org.geoserver.gwc.GWC;
import org.geoserver.gwc.layer.GeoServerTileLayer;
import org.geoserver.gwc.layer.GeoServerTileLayerInfo;
import org.geoserver.web.GeoServerApplication;
import org.geoserver.web.publish.PublishedConfigurationPanel;
import org.geoserver.web.util.MapModel;
import org.geoserver.web.wicket.ParamResourceModel;
import org.geoserver.wms.WMS;
import org.geotools.util.logging.Logging;
import org.geowebcache.layer.TileLayer;

/**
* Resource configuration panel for MapML
Expand All @@ -48,8 +59,16 @@ public class MapMLLayerConfigurationPanel extends PublishedConfigurationPanel<La
static final Logger LOGGER = Logging.getLogger(MapMLLayerConfigurationPanel.class);

private static final long serialVersionUID = 1L;
public static final String PNG_MIME_TYPE = "image/png";
ListMultipleChoice<String> featureCaptionAttributes;

private static final String MIME_PATTERN = "png|jpeg";

public static final Pattern mimePattern =
Pattern.compile(MIME_PATTERN, Pattern.CASE_INSENSITIVE);

DropDownChoice<String> mime;

/**
* Adds MapML configuration panel
*
Expand Down Expand Up @@ -80,6 +99,15 @@ public MapMLLayerConfigurationPanel(final String panelId, final IModel<LayerInfo
new PropertyModel<MetadataMap>(model, MapMLConstants.RESOURCE_METADATA),
MapMLConstants.MAPML_USE_TILES);
CheckBox useTiles = new CheckBox(MapMLConstants.USE_TILES, useTilesModel);
useTiles.add(
new OnChangeAjaxBehavior() {
@Override
protected void onUpdate(AjaxRequestTarget ajaxRequestTarget) {
ajaxRequestTarget.add(mime);
boolean useTilesChecked = useTiles.getConvertedInput();
mime.setChoices(getAvailableMimeTypes(model.getObject(), useTilesChecked));
}
});
add(useTiles);

// add the checkbox to select features or not
Expand All @@ -93,6 +121,15 @@ public MapMLLayerConfigurationPanel(final String panelId, final IModel<LayerInfo
useFeatures.setEnabled(false);
}
}
useFeatures.add(
new OnChangeAjaxBehavior() {
@Override
protected void onUpdate(AjaxRequestTarget target) {
target.add(mime);
// if we are using features, we don't use a default mime type
mime.setEnabled(!useFeatures.getConvertedInput());
}
});
add(useFeatures);

// add the checkbox to enable sharding or not
Expand Down Expand Up @@ -130,6 +167,31 @@ public MapMLLayerConfigurationPanel(final String panelId, final IModel<LayerInfo
dimension.setNullValid(true);
add(dimension);

MapModel<String> mimeModel =
new MapModel<>(
new PropertyModel<MetadataMap>(model, MapMLConstants.RESOURCE_METADATA),
MapMLConstants.MAPML_MIME);
boolean useTilesFromModel =
Boolean.TRUE.equals(
model.getObject()
.getResource()
.getMetadata()
.get(MAPML_USE_TILES, Boolean.class));
mime =
new DropDownChoice<>(
MapMLConstants.MIME,
mimeModel,
getAvailableMimeTypes(model.getObject(), useTilesFromModel));
mime.setOutputMarkupId(true);
mime.setNullValid(false);
// if we are using features, we don't use a mime type
if (useFeaturesModel.getObject() != null) {
String useFeaturesString = String.valueOf(useFeaturesModel.getObject());
boolean useFeaturesBoolean = Boolean.parseBoolean(useFeaturesString);
mime.setEnabled(!useFeaturesBoolean);
}
add(mime);

featureCaptionAttributes =
new ListMultipleChoice<>(
MapMLConstants.FEATURE_CAPTION_ATTRIBUTES,
Expand All @@ -147,6 +209,46 @@ public MapMLLayerConfigurationPanel(final String panelId, final IModel<LayerInfo
add(featureCaptionTemplate);
}

/**
* Get the available mime types for the layer
*
* @param layer the layer to get the mime types for
* @return a list of strings of mime types
*/
public static List<String> getAvailableMimeTypes(PublishedInfo layer, boolean useTiles) {
List<String> mimeTypes = new ArrayList<>();
if (useTiles) {
GWC gwc = GWC.get();
if (gwc != null) {
try {
TileLayer tileLayer = gwc.getTileLayerByName(layer.prefixedName());
// if the useTiles flag is set and the cache is enabled we get cache mime types
if (tileLayer instanceof GeoServerTileLayer && tileLayer.isEnabled()) {
GeoServerTileLayer geoServerTileLayer = (GeoServerTileLayer) tileLayer;
GeoServerTileLayerInfo info = geoServerTileLayer.getInfo();
mimeTypes.addAll(
info.getMimeFormats().stream()
.filter(mimeType -> mimePattern.matcher(mimeType).find())
.collect(Collectors.toList()));
return mimeTypes;
}
} catch (IllegalArgumentException e) {
LOGGER.fine("No tile layer found for " + layer.prefixedName());
}
}
}
// if the useTiles flag is not set or the tile cache is not enabled we get WMS mime types
WMS wms = WMS.get();
if (wms != null) {
mimeTypes.addAll(
wms.getAllowedMapFormatNames().stream()
.filter(mimeType -> mimePattern.matcher(mimeType).find())
.collect(Collectors.toList()));
}

return mimeTypes;
}

/**
* @param layer a LayerInfo for the layer
* @return a list of strings of dimension names from the layer info
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,24 @@ <h3><wicket:message key="mapmlSectionHeading">MapML Settings</wicket:message></h
</ul>
</fieldset>
</li>
<li>
<fieldset>
<legend>
<span><wicket:message
key="mapmlDefaultMimeSection">Default Mimetype</wicket:message></span>
</legend>
<ul>
<li>
<label for="mime">
<wicket:message key="mapmlDefaultMime">Mime</wicket:message>
</label>
<select id="mime" wicket:id="mime"></select>
</li>
</ul>
</fieldset>
</li>
</ul>
</wicket:panel>
</form>
</body>
</html>
</html>
Loading
Loading