Commit 9624b929 authored by Matija Obreza's avatar Matija Obreza
Browse files

Renamed Mapfilter* to SavedMap*

parent b44215d6
......@@ -16,12 +16,16 @@
package org.genesys2.server.servlet.controller;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
......@@ -117,7 +121,7 @@ public class ExplorerController extends BaseController {
/**
* Browse all
*
*
* @param model
* @param page
* @return
......@@ -305,11 +309,9 @@ public class ExplorerController extends BaseController {
return mappingService.filteredKml(jsonTree.toString());
}
/**
* Change color of the tile
*
*
* @param color
* @param imageBytes
* @return
......@@ -329,4 +331,58 @@ public class ExplorerController extends BaseController {
throw e;
}
}
@RequestMapping(value = "/explore/tile/{zoom}/{x}/{y}", produces = MediaType.IMAGE_PNG_VALUE)
public void tile(@PathVariable("zoom") int zoom, @PathVariable("x") int x, @PathVariable("y") int y,
@RequestParam(value = "filter", required = true) String jsonFilter, @RequestParam(value = "color", required = false) String color,
HttpServletResponse response) {
try {
byte[] image = mappingService.getTile(jsonFilter, zoom, x, y);
image = changeColor(color, image);
response.getOutputStream().write(image, 0, image.length);
} catch (final IOException e) {
_logger.warn(e.getMessage());
throw new RuntimeException("Could not render image", e);
} catch (final Throwable e) {
_logger.error(e.getMessage(), e);
throw new ResourceNotFoundException(e.getMessage());
}
}
private byte[] changeColor(String color, byte[] imageBytes) {
if (StringUtils.isBlank(color)) {
return imageBytes;
}
if (!color.startsWith("#"))
color = "#" + color;
if (_logger.isDebugEnabled())
_logger.debug("Changing color to " + color);
try {
final Color newColor = Color.decode(color);
if (newColor.equals(Color.yellow)) {
return imageBytes;
}
final MapColorsFilter mcf = new MapColorsFilter(Color.yellow.getRGB(), newColor.getRGB());
final ByteArrayInputStream bios = new ByteArrayInputStream(imageBytes);
final BufferedImage image = mcf.filter(ImageIO.read(bios), null);
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, "PNG", baos);
return baos.toByteArray();
} catch (final NumberFormatException e) {
_logger.warn("Cannot get color for " + color);
return imageBytes;
} catch (final IOException e) {
_logger.warn(e.getMessage());
return imageBytes;
}
}
}
package org.genesys2.server.servlet.controller;
import org.genesys2.server.servlet.model.MapFilter;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
@Component
@Scope("session")
public class FilterSelectionBean implements Serializable {
private static final long serialVersionUID = -1084615112110837714L;
private final List<MapFilter> filters = new ArrayList<>();
public List<MapFilter> getFilters() {
return filters;
}
public MapFilter getFilter(String title) {
MapFilter mapFilter = new MapFilter();
mapFilter.setTitle(title);
return filters.get(filters.indexOf(mapFilter));
}
public void addFilter(MapFilter filter) {
filters.add(filter);
}
}
package org.genesys2.server.servlet.controller;
import com.jhlabs.image.MapColorsFilter;
import org.apache.commons.lang.StringUtils;
import org.genesys2.server.service.MappingService;
import org.genesys2.server.servlet.model.MapFilter;
import org.genesys2.spring.ResourceNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;
@Controller
@Scope("request")
public class MapFilterController extends BaseController {
@Autowired
private MappingService mappingService;
@Autowired
private FilterSelectionBean filterSelectionBean;
@RequestMapping(value = "/filter/get", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public List<MapFilter> getMapFilters() {
return filterSelectionBean.getFilters();
}
@RequestMapping(value = "/filter/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public boolean saveMapFilter(@RequestBody MapFilter filter) {
filterSelectionBean.addFilter(filter);
return true;
}
@RequestMapping(value = "/explore/tile/{zoom}/{x}/{y}", produces = MediaType.IMAGE_PNG_VALUE)
public void tile(@PathVariable("zoom") int zoom, @PathVariable("x") int x, @PathVariable("y") int y,
@RequestParam(value = "filter", required = true) String jsonFilter, @RequestParam(value = "color", required = false) String color,
@RequestParam(value = "title", required = false) String title, HttpServletResponse response) {
MapFilter mapFilter = null;
if ((StringUtils.isNotBlank(title))) {
mapFilter = filterSelectionBean.getFilter(title);
color = mapFilter.getColor();
jsonFilter = mapFilter.getFilter();
}
try {
byte[] image = mappingService.getTile(jsonFilter, zoom, x, y);
image = changeColor(color, image);
response.getOutputStream().write(image, 0, image.length);
} catch (final IOException e) {
_logger.warn(e.getMessage());
throw new RuntimeException("Could not render image", e);
} catch (final Throwable e) {
_logger.error(e.getMessage(), e);
throw new ResourceNotFoundException(e.getMessage());
}
}
private byte[] changeColor(String color, byte[] imageBytes) {
if (color == null) {
return imageBytes;
}
_logger.info("Changing color to " + color);
try {
final Color newColor = Color.decode(color);
if (newColor.equals(Color.yellow)) {
return imageBytes;
}
final MapColorsFilter mcf = new MapColorsFilter(Color.yellow.getRGB(), newColor.getRGB());
final ByteArrayInputStream bios = new ByteArrayInputStream(imageBytes);
final BufferedImage image = mcf.filter(ImageIO.read(bios), null);
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, "PNG", baos);
return baos.toByteArray();
} catch (final NumberFormatException e) {
_logger.warn("Cannot get color for " + color);
return imageBytes;
} catch (final IOException e) {
_logger.warn(e.getMessage());
return imageBytes;
}
}
}
/**
* Copyright 2014 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
package org.genesys2.server.servlet.controller;
import org.genesys2.server.servlet.model.SavedMap;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
@Component
@Scope("session")
public class SavedMapsBean implements Serializable {
private static final long serialVersionUID = -1084615112110837714L;
private final List<SavedMap> savedMaps = new ArrayList<>();
public List<SavedMap> getSavedMaps() {
return savedMaps;
}
public SavedMap newSavedMap(String title) {
SavedMap savedMap = new SavedMap();
savedMap.setTitle(title);
return savedMaps.get(savedMaps.indexOf(savedMap));
}
public void addSavedMap(SavedMap filter) {
savedMaps.add(filter);
}
}
/**
* Copyright 2014 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
package org.genesys2.server.servlet.controller;
import java.util.List;
import org.genesys2.server.service.MappingService;
import org.genesys2.server.servlet.model.SavedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@Scope("request")
public class SavedMapsController extends BaseController {
@Autowired
private MappingService mappingService;
@Autowired
private SavedMapsBean savedMapsBean;
@RequestMapping(value = "/savedmaps/get", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public List<SavedMap> getMapFilters() {
return savedMapsBean.getSavedMaps();
}
@RequestMapping(value = "/savedmaps/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public boolean saveMapFilter(@RequestBody SavedMap filter) {
savedMapsBean.addSavedMap(filter);
return true;
}
}
/**
* Copyright 2014 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
package org.genesys2.server.servlet.model;
import java.io.Serializable;
public class MapFilter implements Serializable {
public class SavedMap implements Serializable {
private String title;
private String filter;
private String color;
......@@ -37,9 +53,9 @@ public class MapFilter implements Serializable {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MapFilter mapFilter = (MapFilter) o;
SavedMap savedMap = (SavedMap) o;
if (title != null ? !title.equals(mapFilter.title) : mapFilter.title != null) return false;
if (title != null ? !title.equals(savedMap.title) : savedMap.title != null) return false;
return true;
}
......
......@@ -316,9 +316,10 @@ selection.empty-list-warning=You have not added any accessions to the list.
selection.add-many=Check and add
selection.add-many.accessionIds=List accession IDs as used in Genesys separated by space or new line.
filters=Filters
filters.saved=Saved filters
filters.save=Save filter
savedmaps=Remember current map
savedmaps.list=Map list
savedmaps.save=Remember map
filter.enter.title=Enter filter title
filters.page.title=Data filters
filters.view=Current filters
......
......@@ -33,14 +33,14 @@
<li class="active dropdown form-horizontal pull-right" >
<a id="get-filters" href="#"><spring:message code="filters.saved"/></a>
<a id="get-filters" href="#"><spring:message code="savedmaps.list"/></a>
<ul id="enabled-filters" class="dropdown-menu"></ul>
</li>
<li style="margin-right: 5px" class="active form-horizontal pull-right" data-toggle="modal" data-target="#modal-dialog">
<a href="#"><spring:message code="filters.save"/></a>
<a href="#"><spring:message code="savedmaps.save"/></a>
</li>
</ul>
</div>
......@@ -51,7 +51,7 @@
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
<h4 class="modal-title" id="modal-label"><spring:message code="filters.save"/></h4>
<h4 class="modal-title" id="modal-label"><spring:message code="savedmaps.save"/></h4>
</div>
<div class="modal-body">
<input type="text" class="form-control" placeholder="<spring:message code="filter.enter.title"/>" id="filter-title">
......@@ -92,7 +92,7 @@
var filter=${jsonFilter};
$.ajax({
url : "/filter/save",
url : "/savedmaps/save",
type : "post",
dataType : "json",
contentType: 'application/json; charset=utf-8',
......@@ -112,15 +112,14 @@
$("#get-filters").on("click", function(event) {
event.preventDefault();
$.ajax({
url: "/filter/get",
url: "/savedmaps/get",
type: "get",
dataType: "json",
contentType: 'application/json; charset=utf-8',
success: function (data) {
$.each(data, function (idx, filter) {
// var li = "<li><a href='#' class='saved-filter' fil='"+filter.filter+"'>" + filter.title + "</a></li>";
var li = "<li><label class='saved-filter' fil='"+filter.filter+"'><input style='margin-right: 10px;margin-left: 5px' type='checkbox'>" + filter.title + "</label></li>";
var li = "<li><label class='saved-filter' x-fil='"+filter.filter+"' x-color='" + filter.color + "'><input style='margin-right: 10px;margin-left: 5px' type='checkbox'>" + filter.title + "</label></li>";
if ($("#enabled-filters").is(":visible")) {
$("#enabled-filters").append(li);
......@@ -136,7 +135,6 @@
$("#enabled-filters").toggle();
});
var globalTitle="";
var map = L.map('map').setView([20,0], 2);
L.tileLayer('https://otile{s}-s.mqcdn.com/tiles/1.0.0/sat/{z}/{x}/{y}.png', {
attribution: "MapQuest",
......@@ -144,7 +142,7 @@
subdomains: ['1','2','3','4'],
opacity: 0.6
}).addTo(map);
L.tileLayer("{s}/explore/tile/{z}/{x}/{y}?filter=" + '${jsonFilter}'+"&title="+globalTitle, {
L.tileLayer("{s}/explore/tile/{z}/{x}/{y}?filter=" + '${jsonFilter}', {
attribution: "<a href='${props.baseUrl}'>Genesys</a>",
styleId: 22677,
subdomains: [${props.tileserverCdn}]
......@@ -160,28 +158,25 @@
var layers={};
$("body").on("click", ".saved-filter", function () {
$("body").on("click", ".saved-filter", function (e) {
var title = $(this).text();
var filter = $(this).attr("fil");
globalTitle = title;
var savedFilterObj=JSON.parse(filter);
$.each(savedFilterObj.crop,function(idx,cropName){
if (typeof filterJsonObj.crop == 'undefined') {
filterJsonObj.crop=[];
}
filterJsonObj.crop.push(cropName);
});
var filter = $(this).attr("x-fil");
var tilesColor = $(this).attr("x-color").substring(1);
var savedFilterObj=JSON.parse(filter);
if ($(this).find("input:checkbox").is(":checked")) {
layers[title] = L.tileLayer("{s}/explore/tile/{z}/{x}/{y}?filter=" + filter + "&title=" + title, {
attribution: "<a href='${props.baseUrl}'>Genesys</a>",
styleId: 22677,
subdomains: [${props.tileserverCdn}]
}).addTo(map);
if (layers[title] == null) {
layers[title] = L.tileLayer("{s}/explore/tile/{z}/{x}/{y}?filter=" + filter + "&color=" + tilesColor, {
attribution: "<a href='${props.baseUrl}'>Genesys</a>",
styleId: 22677,
subdomains: [${props.tileserverCdn}]
}).addTo(map);
}
} else {
map.removeLayer(layers[title]);
if (layers[title] != null) {
map.removeLayer(layers[title]);
layers[title]=null;
}
}
});
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment