Commit dc09a340 authored by Matija Obreza's avatar Matija Obreza
Browse files

Added geoJSON support, load accession information in map click

parent dcd188ed
......@@ -56,7 +56,7 @@ public interface GenesysFilterService {
List<LabelValue<String>> autocomplete(String filter, String ac);
void listGeo(ObjectNode jsonTree, RowCallbackHandler rowHandler);
void listGeo(ObjectNode jsonTree, Integer limit, RowCallbackHandler rowHandler);
void listGeoTile(boolean distinct, ObjectNode jsonTree, int zoom, int x, int y, RowCallbackHandler rowCallbackHandler);
void listGeoTile(boolean distinct, ObjectNode jsonTree, Integer limit, int zoom, int xtile, int ytile, RowCallbackHandler rowHandler);
}
......@@ -16,10 +16,16 @@
package org.genesys2.server.service;
import java.io.IOException;
public interface MappingService {
void clearCache();
String filteredKml(String jsonFilter);
String filteredGeoJson(String string, Integer limit) throws IOException;
byte[] getTile(String jsonFilter, int zoom, int xtile, int ytile);
public static class CoordUtil {
......@@ -104,6 +110,4 @@ public interface MappingService {
return (int) ((Math.floor(((180.0 + lng) / 360.0) * pixAtZoom)) - tile * 256);
}
}
void clearCache();
}
......@@ -665,12 +665,12 @@ public class GenesysFilterServiceImpl implements GenesysFilterService {
}
@Override
public void listGeo(ObjectNode jsonTree, RowCallbackHandler rowHandler) {
listGeoTile(false, jsonTree, -1, 0, 0, rowHandler);
public void listGeo(ObjectNode jsonTree, Integer limit, RowCallbackHandler rowHandler) {
listGeoTile(false, jsonTree, limit, -1, 0, 0, rowHandler);
}
@Override
public void listGeoTile(final boolean distinct, ObjectNode jsonTree, int zoom, int xtile, int ytile, RowCallbackHandler rowHandler) {
public void listGeoTile(final boolean distinct, ObjectNode jsonTree, Integer limit, int zoom, int xtile, int ytile, RowCallbackHandler rowHandler) {
Iterator<Entry<String, JsonNode>> fields = jsonTree.fields();
while (fields.hasNext()) {
Entry<String, JsonNode> entry = fields.next();
......@@ -747,8 +747,14 @@ public class GenesysFilterServiceImpl implements GenesysFilterService {
sb.append(sbf.toString());
LOG.info("Parameter count: " + params.size());
LOG.info("Count query:\n" + sb.toString());
if (limit != null) {
sb.append(" limit " + limit);
}
if (LOG.isDebugEnabled()) {
LOG.debug("Parameter count: " + params.size());
LOG.debug("Count query:\n" + sb.toString());
}
this.jdbcTemplate.query(new PreparedStatementCreator() {
@Override
......
......@@ -37,6 +37,7 @@ import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Service;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
@Service
......@@ -80,7 +81,7 @@ public class MappingServiceImpl implements MappingService {
sb.append("<kml xmlns=\"http://www.opengis.net/kml/2.2\">");
sb.append("<Document>");
filterService.listGeo(jsonTree, new RowCallbackHandler() {
filterService.listGeo(jsonTree, null, new RowCallbackHandler() {
@Override
public void processRow(ResultSet rs) throws SQLException {
try {
......@@ -107,6 +108,51 @@ public class MappingServiceImpl implements MappingService {
}
}
@Override
public String filteredGeoJson(String jsonFilter, Integer limit) throws IOException {
ObjectNode jsonTree = (ObjectNode) mapper.readTree(jsonFilter);
LOG.debug(jsonTree.toString());
final ObjectNode geoJson = mapper.createObjectNode();
geoJson.put("type", "FeatureCollection");
final ArrayNode featuresArray = geoJson.arrayNode();
geoJson.put("features", featuresArray);
filterService.listGeo(jsonTree, limit, new RowCallbackHandler() {
@Override
public void processRow(ResultSet rs) throws SQLException {
try {
ObjectNode feature = featuresArray.objectNode();
feature.put("type", "Feature");
feature.put("id", rs.getLong("id"));
ObjectNode geometry;
feature.put("geometry", geometry = feature.objectNode());
geometry.put("type", "Point");
ArrayNode coordArray;
geometry.put("coordinates", coordArray = geometry.arrayNode());
coordArray.add(rs.getDouble("longitude"));
coordArray.add(rs.getDouble("latitude"));
ObjectNode properties;
feature.put("properties", properties = feature.objectNode());
properties.put("acceNumb", rs.getString("acceNumb"));
properties.put("instCode", rs.getString("instCode"));
properties.put("datum", rs.getString("datum"));
properties.put("uncertainty", rs.getString("uncertainty"));
featuresArray.add(feature);
} catch (SQLException e) {
LOG.warn(e.getMessage());
throw e;
}
}
});
return geoJson.toString();
}
@PreAuthorize("hasRole('ADMINISTRATOR')")
@Override
@CacheEvict(value = "tileserver", allEntries = true)
......@@ -142,7 +188,7 @@ public class MappingServiceImpl implements MappingService {
// bi.setRGB(i, 255, Color.red.getRGB());
// }
filterService.listGeoTile(true, jsonTree, zoom, xtile, ytile, new RowCallbackHandler() {
filterService.listGeoTile(true, jsonTree, null, zoom, xtile, ytile, new RowCallbackHandler() {
@Override
public void processRow(ResultSet rs) throws SQLException {
try {
......
......@@ -36,12 +36,12 @@ import org.genesys2.server.service.MappingService;
import org.genesys2.server.service.TaxonomyService;
import org.genesys2.server.service.TraitService;
import org.genesys2.server.service.impl.GenesysFilterServiceImpl.LabelValue;
import org.genesys2.spring.ResourceNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.http.MediaType;
import org.springframework.social.ResourceNotFoundException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
......@@ -338,6 +338,13 @@ public class ExplorerController extends BaseController {
public String map(ModelMap model, @RequestParam(value = "crop", required = false, defaultValue = "") String cropName,
@RequestParam(value = "filter", required = true) String jsonFilter) {
ObjectNode jsonTree = updateFilterWithCrop(cropName, jsonFilter);
model.addAttribute("jsonFilter", jsonTree.toString());
return "/accession/map";
}
private ObjectNode updateFilterWithCrop(String cropName, String jsonFilter) {
Crop crop = null;
if (StringUtils.isNotBlank(cropName)) {
crop = cropService.getCrop(cropName);
......@@ -362,17 +369,15 @@ public class ExplorerController extends BaseController {
} catch (IOException e) {
_logger.error(e.getMessage(), e);
}
model.addAttribute("jsonFilter", jsonTree.toString());
return "/accession/map";
return jsonTree;
}
@RequestMapping(value = "/explore/kml", produces = "application/vnd.google-earth.kml+xml")
@ResponseBody
public String kml(@RequestParam(value = "crop", required = false, defaultValue = "") String cropName,
@RequestParam(value = "filter", required = true) String jsonFilter) {
return mappingService.filteredKml(jsonFilter);
ObjectNode jsonTree = updateFilterWithCrop(cropName, jsonFilter);
return mappingService.filteredKml(jsonTree.toString());
}
@RequestMapping(value = "/explore/tile/{zoom}/{x}/{y}", produces = MediaType.IMAGE_PNG_VALUE)
......@@ -386,4 +391,19 @@ public class ExplorerController extends BaseController {
throw new ResourceNotFoundException(e.getMessage());
}
}
@RequestMapping(value = "/explore/geoJson", produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public String geoJson(@RequestParam(value = "crop", required = false, defaultValue = "") String cropName,
@RequestParam(value = "limit", required = false, defaultValue = "") Integer limit,
@RequestParam(value = "filter", required = true) String jsonFilter) throws IOException {
ObjectNode jsonTree = updateFilterWithCrop(cropName, jsonFilter);
try {
return mappingService.filteredGeoJson(jsonTree.toString(), limit);
} catch (IOException e) {
throw e;
}
}
}
......@@ -21,6 +21,13 @@ import org.springframework.web.bind.annotation.ResponseStatus;
@ResponseStatus(value = HttpStatus.NOT_FOUND)
public class ResourceNotFoundException extends RuntimeException {
public ResourceNotFoundException() {
}
public ResourceNotFoundException(String string) {
super(string);
}
private static final long serialVersionUID = -3351336944403763100L;
}
......@@ -38,7 +38,7 @@
subdomains: ['1','2','3','4']
}).addTo(map);
//.add(po.image().url("http://a.tile.openstreetmap.org/{Z}/{X}/{Y}.png"))
$("#selectArea").hide();
var filterJson=${jsonFilter};
......@@ -67,6 +67,62 @@
$("#selectArea").on("click", function(e) {
this.href=this.href+'?filter='+JSON.stringify(filterJson);
});
var loadDetailsTimeout = null;
var clickMarker=null;
map.on("click", function(e) {
if (clickMarker!=null) {
map.removeLayer(clickMarker);
clickMarker=null;
}
if (map.getZoom()>4) {
if (loadDetailsTimeout!=null) {
clearTimeout(loadDetailsTimeout);
}
var point=this.latLngToLayerPoint(e.latlng);
point.x-=5; point.y+=5;
var sw=this.layerPointToLatLng(point);
point.x+=10; point.y-=10;
var ne=this.layerPointToLatLng(point);
loadDetailsTimeout=setTimeout(function() {
var filterBounds=JSON.parse(JSON.stringify(filterJson));
filterBounds.latitude=[{range:[sw.lat, ne.lat]}];
filterBounds.longitude=[{range:[sw.lng, ne.lng]}];
//console.log(JSON.stringify(filterBounds));
$.ajax("<c:url value="/explore/geoJson"><c:param name="crop" value="${crop.shortName}" /><c:param name="limit" value="11" /></c:url>&filter="+JSON.stringify(filterBounds), {
type : "GET",
dataType : 'json',
success : function(respObject) {
if (respObject.features == null || respObject.features.length == 0)
return;
var c="";
respObject.features.forEach(function(a, b) {
if (b<10)
c+="<a href='<c:url value="/acn/id/" />" + a.id + "'>" + a.properties.acceNumb + " " + a.properties.instCode + "</a><br />";
else
c+="...";
});
clickMarker=L.rectangle([sw, ne], {stroke: false, fill: true}).addTo(map);
clickMarker.bindPopup(c).openPopup();
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(textStatus);
console.log(errorThrown);
}
});
}, 200);
}
});
map.on("dblclick", function(e) {
if (loadDetailsTimeout!=null) {
//console.log("cleared" + loadDetailsTimeout);
clearTimeout(loadDetailsTimeout);
loadDetailsTimeout=null;
}
});
});
</script>
</content>
......
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