Commit 19128b8d authored by Matija Obreza's avatar Matija Obreza

Merge branch '409-climatedatacontroller-v1' into 'master'

Resolve "ClimateDataController v1"

Closes #409

See merge request genesys-pgr/genesys-server!341
parents 422a67ba d11de751
/*
* Copyright 2019 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.api.v1;
import io.swagger.annotations.Api;
import org.genesys.worldclim.WorldClimUtil;
import org.genesys2.server.api.ApiBaseController;
import org.genesys2.server.exception.NotFoundElement;
import org.genesys2.server.model.impl.TileClimate;
import org.genesys2.server.service.ClimateDataService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController("climateApi1")
@PreAuthorize("isAuthenticated()")
@RequestMapping(ClimateDataController.API_BASE)
@Api(tags = { "climate" })
public class ClimateDataController {
/** The Constant API_BASE. */
public static final String API_BASE = ApiBaseController.APIv1_BASE + "/climate";
/** The Constant LOG. */
private static final Logger LOG = LoggerFactory.getLogger(ClimateDataController.class);
@Autowired
private ClimateDataService climateDataService;
@GetMapping("/current/{latitude},{longitude}")
public TileClimate getCurrentClimate(@PathVariable("latitude") double latitude, @PathVariable("longitude") double longitude) {
Long tileIndex = WorldClimUtil.getWorldclim25Tile(longitude, latitude);
if (tileIndex == null) {
throw new NotFoundElement("No current climate data for specified (lat, lon)");
}
return climateDataService.climateForTile(tileIndex);
}
@GetMapping("/past/{latitude},{longitude}")
public TileClimate getPastClimate(@PathVariable("latitude") double latitude, @PathVariable("longitude") double longitude) {
throw new NotFoundElement("Past climate data not available");
}
@GetMapping("/future/{latitude},{longitude}")
public TileClimate getFutureClimate(@PathVariable("latitude") double latitude, @PathVariable("longitude") double longitude) {
throw new NotFoundElement("Future climate data not available");
}
}
......@@ -24,19 +24,20 @@ import javax.persistence.Table;
* The TileClimate entity maintains data from worldclim.org.
*/
@Entity
@Table(name = "tile_climate", indexes = { @Index(columnList = "alt"), @Index(columnList = "tmin1"), @Index(columnList = "tmax1"), @Index(columnList = "tmean1"),
@Index(columnList = "prec1"), @Index(columnList = "tmin2"), @Index(columnList = "tmax2"), @Index(columnList = "tmean2"), @Index(columnList = "prec2"),
@Index(columnList = "tmin3"), @Index(columnList = "tmax3"), @Index(columnList = "tmean3"), @Index(columnList = "prec3"), @Index(columnList = "tmin4"),
@Index(columnList = "tmax4"), @Index(columnList = "tmean4"), @Index(columnList = "prec4"), @Index(columnList = "tmin5"), @Index(columnList = "tmax5"),
@Index(columnList = "tmean5"), @Index(columnList = "prec5"), @Index(columnList = "tmin6"), @Index(columnList = "tmax6"), @Index(columnList = "tmean6"),
@Index(columnList = "prec6"), @Index(columnList = "tmin7"), @Index(columnList = "tmax7"), @Index(columnList = "tmean7"), @Index(columnList = "prec7"),
@Index(columnList = "tmin8"), @Index(columnList = "tmax8"), @Index(columnList = "tmean8"), @Index(columnList = "prec8"), @Index(columnList = "tmin9"),
@Index(columnList = "tmax9"), @Index(columnList = "tmean9"), @Index(columnList = "prec9"), @Index(columnList = "tmin10"), @Index(columnList = "tmax10"),
@Index(columnList = "tmean10"), @Index(columnList = "prec10"), @Index(columnList = "tmin11"), @Index(columnList = "tmax11"), @Index(columnList = "tmean11"),
@Index(columnList = "prec11"), @Index(columnList = "tmin12"), @Index(columnList = "tmax12"), @Index(columnList = "tmean12"), @Index(columnList = "prec12"),
@Table(name = "tile_climate", indexes = { @Index(columnList = "alt"), @Index(columnList = "tmean1"),
@Index(columnList = "prec1"), @Index(columnList = "tmean2"), @Index(columnList = "prec2"),
@Index(columnList = "tmean3"), @Index(columnList = "prec3"),
@Index(columnList = "tmean4"), @Index(columnList = "prec4"),
@Index(columnList = "tmean5"), @Index(columnList = "prec5"), @Index(columnList = "tmean6"),
@Index(columnList = "prec6"), @Index(columnList = "tmean7"), @Index(columnList = "prec7"),
@Index(columnList = "tmean8"), @Index(columnList = "prec8"),
@Index(columnList = "tmean9"), @Index(columnList = "prec9"),
@Index(columnList = "tmean10"), @Index(columnList = "prec10"), @Index(columnList = "tmean11"),
@Index(columnList = "prec11"), @Index(columnList = "tmean12"), @Index(columnList = "prec12"),
@Index(columnList = "bio1"), @Index(columnList = "bio2"), @Index(columnList = "bio3"), @Index(columnList = "bio4"), @Index(columnList = "bio5"),
@Index(columnList = "bio6"), @Index(columnList = "bio7"), @Index(columnList = "bio8"), @Index(columnList = "bio9"), @Index(columnList = "bio10"),
@Index(columnList = "bio11"), @Index(columnList = "bio12"), @Index(columnList = "bio13"), @Index(columnList = "bio14"), @Index(columnList = "bio15") })
@Index(columnList = "bio11"), @Index(columnList = "bio12"), @Index(columnList = "bio13"), @Index(columnList = "bio14"), @Index(columnList = "bio15"),
@Index(columnList = "bio16"), @Index(columnList = "bio17"), @Index(columnList = "bio18"), @Index(columnList = "bio19") })
public class TileClimate {
@Id
private long tileIndex;
......@@ -58,6 +59,10 @@ public class TileClimate {
private Double bio13;
private Double bio14;
private Double bio15;
private Double bio16;
private Double bio17;
private Double bio18;
private Double bio19;
private Double prec1;
private Double prec2;
......@@ -253,6 +258,38 @@ public class TileClimate {
public void setBio15(Double bio15) {
this.bio15 = bio15;
}
public Double getBio16() {
return bio16;
}
public void setBio16(Double bio16) {
this.bio16 = bio16;
}
public Double getBio17() {
return bio17;
}
public void setBio17(Double bio17) {
this.bio17 = bio17;
}
public Double getBio18() {
return bio18;
}
public void setBio18(Double bio18) {
this.bio18 = bio18;
}
public Double getBio19() {
return bio19;
}
public void setBio19(Double bio19) {
this.bio19 = bio19;
}
public Double getPrec1() {
return prec1;
......
......@@ -49,6 +49,10 @@ public class ClimateFilter {
public NumberFilter<Double> bio13;
public NumberFilter<Double> bio14;
public NumberFilter<Double> bio15;
public NumberFilter<Double> bio16;
public NumberFilter<Double> bio17;
public NumberFilter<Double> bio18;
public NumberFilter<Double> bio19;
public NumberFilter<Double> prec1;
public NumberFilter<Double> prec2;
......@@ -164,6 +168,18 @@ public class ClimateFilter {
if (bio15 != null) {
and.and(bio15.buildQuery(qtileClimate.bio15));
}
if (bio16 != null) {
and.and(bio16.buildQuery(qtileClimate.bio16));
}
if (bio17 != null) {
and.and(bio17.buildQuery(qtileClimate.bio17));
}
if (bio18 != null) {
and.and(bio18.buildQuery(qtileClimate.bio18));
}
if (bio19 != null) {
and.and(bio19.buildQuery(qtileClimate.bio19));
}
if (prec1 != null) {
and.and(prec1.buildQuery(qtileClimate.prec1));
......
......@@ -5421,3 +5421,122 @@ databaseChangeLog:
- sql:
comment: Count and assign value to totalValue field
sql: update kpiexecutionrun k set k.totalValue = (select sum(obs.value) from kpiobservation obs where obs.executionRunId = k.id)
- changeSet:
id: 1548776089000-1
author: mobreza
comment: Added missing bioXX climate variables
changes:
- addColumn:
tableName: tile_climate
columns:
- column:
name: bio16
type: DOUBLE
- column:
name: bio17
type: DOUBLE
- column:
name: bio18
type: DOUBLE
- column:
name: bio19
type: DOUBLE
# Drop index on monthly tmin
- dropIndex: # tmin1
indexName: UK_osv1n6fdkko1w04dran4q6sqj
tableName: tile_climate
- dropIndex: # 2
indexName: UK_70m5a2khfktckjjgwgmtxocwk
tableName: tile_climate
- dropIndex: # 3
indexName: UK_1rob7nrwm6fm6ko8a35lcmxfm
tableName: tile_climate
- dropIndex: # 4
indexName: UK_5v65e9k2tfbir947cqm55vdq8
tableName: tile_climate
- dropIndex: # 5
indexName: UK_4h3frp1qsyycc3am354sc0dwr
tableName: tile_climate
- dropIndex: # 6
indexName: UK_nxldxplaepwobn5gml71uovp9
tableName: tile_climate
- dropIndex: # 7
indexName: UK_to58xnxvs6delb8ookwlqd0ql
tableName: tile_climate
- dropIndex: # 8
indexName: UK_ovombpuiisygohodcfbn63dph
tableName: tile_climate
- dropIndex: # 9
indexName: UK_d3fqcoklwlhn5wqy68sj9eogc
tableName: tile_climate
- dropIndex: # 10
indexName: UK_j2j87af43hir92s162xmauatw
tableName: tile_climate
- dropIndex: # 11
indexName: UK_2403f93etp2aaro78eym0srau
tableName: tile_climate
- dropIndex: # 12
indexName: UK_mkw7lqemsxf2m6atcrd541xjj
tableName: tile_climate
# Drop index on monthly tmax
- dropIndex: # tmax1
indexName: UK_g0hq4s4dfmc4ddugrt9we0go9
tableName: tile_climate
- dropIndex: # tmax2
indexName: UK_2crtla6lgbt4pp79dytbpm5l4
tableName: tile_climate
- dropIndex: # tmax3
indexName: UK_sex5d71nmjaia88fv02s9t0o0
tableName: tile_climate
- dropIndex: # tmax4
indexName: UK_qds8uyasghug6v4rypne8keee
tableName: tile_climate
- dropIndex: # tmax5
indexName: UK_bytx1fddqookdwgpbrghfr89m
tableName: tile_climate
- dropIndex: # tmax6
indexName: UK_salfee9vtb7dgasoby1ick3td
tableName: tile_climate
- dropIndex: # tmax7
indexName: UK_ew9r0cn0917bjbmji2jfy3adi
tableName: tile_climate
- dropIndex: # tmax8
indexName: UK_lv1hyx4745wlf8np6imdp3kbd
tableName: tile_climate
- dropIndex: # tmax9
indexName: UK_qrxv9muxshywoq4uwpyqaaewg
tableName: tile_climate
- dropIndex: # tmax10
indexName: UK_iaqpx9y134h9lcvawhpl5asbj
tableName: tile_climate
- dropIndex: # tmax11
indexName: UK_f8qrv981c3olu7bmdu3dn8dx2
tableName: tile_climate
- dropIndex: # tmax12
indexName: UK_3hsn25byf33ce3dm0o8403qcw
tableName: tile_climate
- createIndex:
columns:
- column:
name: bio16
indexName: UK_tllx8suofq27njhc60y6h1ii6
tableName: tile_climate
- createIndex:
columns:
- column:
name: bio17
indexName: UK_tllx8suofq27njhc60y6h1ii7
tableName: tile_climate
- createIndex:
columns:
- column:
name: bio18
indexName: UK_tllx8suofq27njhc60y6h1ii8
tableName: tile_climate
- createIndex:
columns:
- column:
name: bio19
indexName: UK_tllx8suofq27njhc60y6h1ii9
tableName: tile_climate
/*
* Copyright 2019 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.genesys.test.server.api.v1;
import static org.hamcrest.Matchers.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import org.genesys.test.base.AbstractApiTest;
import org.genesys.worldclim.WorldClimUtil;
import org.genesys2.server.api.v1.ClimateDataController;
import org.genesys2.server.model.impl.TileClimate;
import org.genesys2.server.persistence.TileClimateRepository;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.transaction.annotation.Transactional;
/**
* @author Maxym Borodenko
*/
public class ClimateDataControllerTest extends AbstractApiTest {
private static final Double LONGITUDE = 32.85;
private static final Double LATITUDE = 39.61666667;
private static final Long TILE_INDEX = WorldClimUtil.getWorldclim25Tile(LONGITUDE, LATITUDE);
@Autowired
private TileClimateRepository tileClimateRepository;
@After
@Override
@Transactional
public void cleanup() throws Exception {
tileClimateRepository.deleteAll();
super.cleanup();
}
@Before
@Override
@Transactional
public void beforeTest() throws Exception {
super.beforeTest();
tileClimateRepository.save(new TileClimate(TILE_INDEX));
}
@Test
public void getCurrentClimateTest() throws Exception {
/*@formatter:off*/
mockMvc
.perform(get(ClimateDataController.API_BASE.concat("/current/{latitude},{longitude}"), LATITUDE, LONGITUDE)
.contentType(MediaType.APPLICATION_JSON))
// .andDo(MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(jsonPath("$.tileIndex", is(TILE_INDEX.intValue())));
/*@formatter:on*/
}
@Test
public void getPastClimateTestFail() throws Exception {
/*@formatter:off*/
mockMvc
.perform(get(ClimateDataController.API_BASE.concat("/past/{latitude},{longitude}"), LATITUDE, LONGITUDE)
.contentType(MediaType.APPLICATION_JSON))
// .andDo(MockMvcResultHandlers.print())
.andExpect(status().isNotFound())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8));
/*@formatter:on*/
}
@Test
public void getFutureClimateTestFail() throws Exception {
/*@formatter:off*/
mockMvc
.perform(get(ClimateDataController.API_BASE.concat("/future/{latitude},{longitude}"), LATITUDE, LONGITUDE)
.contentType(MediaType.APPLICATION_JSON))
// .andDo(MockMvcResultHandlers.print())
.andExpect(status().isNotFound())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8));
/*@formatter:on*/
}
}
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