Skip to content

Commit

Permalink
MBStyle - Add style generation support
Browse files Browse the repository at this point in the history
  • Loading branch information
tbarsballe committed May 8, 2017
1 parent 760dc3f commit 8efc3f9
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@
* This code is licensed under the GPL 2.0 license, available at the root
* application directory.
*/
package org.geoserver.community.mbstyle.web;
package org.geoserver.community.mbstyle;

import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Reader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.geoserver.catalog.SLDHandler;
import org.geoserver.catalog.StyleHandler;
import org.geoserver.catalog.StyleType;
import org.geoserver.platform.resource.FileSystemResourceStore;
import org.geoserver.platform.resource.Resource;
import org.geotools.mbstyle.MapBoxStyle;
Expand All @@ -31,6 +36,22 @@ public class MBStyleHandler extends StyleHandler {

public static final String MIME_TYPE = "application/vnd.geoserver.mbstyle+json";

static final Map<StyleType, String> TEMPLATES = new HashMap<StyleType, String>();
static {
try {
TEMPLATES.put(StyleType.POINT, IOUtils.toString(MBStyleHandler.class
.getResourceAsStream("template_point.json")));
TEMPLATES.put(StyleType.POLYGON, IOUtils.toString(MBStyleHandler.class
.getResourceAsStream("template_polygon.json")));
TEMPLATES.put(StyleType.LINE, IOUtils.toString(MBStyleHandler.class
.getResourceAsStream("template_line.json")));
TEMPLATES.put(StyleType.RASTER, IOUtils.toString(MBStyleHandler.class
.getResourceAsStream("template_raster.json")));
} catch (IOException e) {
throw new RuntimeException("Error loading up the style templates", e);
}
}

private SLDHandler sldHandler;

protected MBStyleHandler(SLDHandler sldHandler) {
Expand Down Expand Up @@ -101,4 +122,25 @@ public String mimeType(Version version) {
return MIME_TYPE;
}

@Override
public String getFileExtension() {
return "json";
}

@Override
public String getCodeMirrorEditMode() {
return "application/json";
}

@Override
public String getStyle(StyleType type, Color color, String colorName, String layerName) {
String template = TEMPLATES.get(type);
if (template == null) {
throw new UnsupportedOperationException("MBStyle does not support generating " + type + " styles.");
}
String colorCode = Integer.toHexString(color.getRGB());
colorCode = colorCode.substring(2, colorCode.length());
return template.replace("${colorName}", colorName).replace(
"${colorCode}", "#" + colorCode).replace("${layerName}", layerName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd.spring-beans.dtd">

<beans>
<bean id="MBStyleHandler" class="org.geoserver.community.mbstyle.web.MBStyleHandler">
<bean id="MBStyleHandler" class="org.geoserver.community.mbstyle.MBStyleHandler">
<constructor-arg ref="sldHandler"/>
</bean>
</beans>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"version": 8,
"name": "${colorName} line",
"layers": [
{
"id": "${colorName} line",
"type": "line",
"paint": {
"line-color": "${colorCode}",
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"version": 8,
"name": "${colorName} circle",
"layers": [
{
"id": "${colorName} circle",
"type": "circle",
"paint": {
"circle-color": "${colorCode}",
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"version": 8,
"name": "${colorName} polygon",
"layers": [
{
"id": "${colorName} polygon",
"type": "fill",
"paint": {
"fill-color": "${colorCode}",
"fill-outline-color":"#000000"
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "${layerName}",
"version": 8,
"layers": [
{
"id": "${layerName} raster",
"type": "raster",
"paint": {
"raster-opacity": 1
}
}
]
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.geoserver.community.mbstyle.web;

import org.geoserver.catalog.Styles;
import org.geoserver.community.mbstyle.MBStyleHandler;
import org.geoserver.test.GeoServerSystemTestSupport;
import org.geotools.styling.*;
import org.junit.Test;
Expand Down

0 comments on commit 8efc3f9

Please sign in to comment.