Commit ba22497d authored by Matija Obreza's avatar Matija Obreza

Fix: calculation of accession#tileIndex for worldclim.org 2.5min data

parent 9c432517
......@@ -499,7 +499,7 @@
<dependency>
<groupId>org.genesys-pgr</groupId>
<artifactId>worldclim-reader</artifactId>
<version>0.0.4</version>
<version>0.0.5-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.genesys-pgr</groupId>
......
......@@ -230,7 +230,13 @@ public abstract class AccessionData extends AuditedVersionedModel implements IdU
AccessionGeo geo = this.accessionId.getGeo();
if (geo != null) {
tileIndex = WorldClimUtil.getTileIndex(5, geo.getLongitude(), geo.getLatitude());
Double longitude = geo.getLongitude();
Double latitude = geo.getLatitude();
if ((longitude != null && longitude == 0) && (latitude != null && latitude == 0)) {
tileIndex = WorldClimUtil.getWorldclim25Tile(longitude, latitude);
} else {
tileIndex = null;
}
}
}
......
......@@ -82,12 +82,12 @@ public class AccessionGeo extends BasicModel implements GeoReferencedEntity, Acc
trimStringsToNull();
// Treat (0, 0) as null
if (((this.longitude != null && this.longitude == 0) || this.longitude == null) && ((this.latitude != null && this.latitude == 0) || this.latitude == null)) {
if ((this.longitude != null && this.longitude == 0) && (this.latitude != null && this.latitude == 0)) {
this.longitude = null;
this.latitude = null;
}
tileIndex = WorldClimUtil.getTileIndex(5, this.longitude, this.latitude);
tileIndex = WorldClimUtil.getWorldclim25Tile(this.longitude, this.latitude);
}
public long getVersion() {
......
......@@ -19,6 +19,7 @@ package org.genesys2.server.mvc.admin;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
......@@ -51,6 +52,7 @@ import org.genesys2.server.model.genesys.PDCI;
import org.genesys2.server.model.genesys.QAccession;
import org.genesys2.server.model.genesys.QAccessionId;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.persistence.AccessionGeoRepository;
import org.genesys2.server.persistence.AccessionIdRepository;
import org.genesys2.server.persistence.AccessionRepository;
import org.genesys2.server.persistence.FaoInstituteRepository;
......@@ -157,6 +159,9 @@ public class AdminController {
@Autowired
private AccessionProcessor accessionProcessor;
@Autowired
private AccessionGeoRepository accessionGeoRepository;
@Autowired
private PDCIRepository pdciRepository;
......@@ -347,20 +352,25 @@ public class AdminController {
return accessions;
}
ArrayList<AccessionGeo> toSave = new ArrayList<>();
accessions.forEach(accession -> {
AccessionGeo geo = accession.getAccessionId().getGeo();
if (geo != null) {
Long index = WorldClimUtil.getTileIndex(5, geo.getLongitude(), geo.getLatitude());
Long index = WorldClimUtil.getWorldclim25Tile(geo.getLongitude(), geo.getLatitude());
geo.setTileIndex(index);
if (ObjectUtils.compare(index, accession.getTileIndex()) != 0) {
updateAccessionTileIndex(accession, index);
}
toSave.add(geo);
} else {
if (accession.getTileIndex() != null) {
updateAccessionTileIndex(accession, null);
}
}
});
accessionGeoRepository.save(toSave);
LOG.debug("Regenerated {} tileIndexes", accessions.size());
return accessions;
......
......@@ -19,6 +19,7 @@ import java.nio.MappedByteBuffer;
import java.util.HashSet;
import java.util.List;
import org.genesys.worldclim.grid.generic.Header;
import org.genesys2.server.model.genesys.AccessionGeo;
import org.genesys2.server.model.impl.TileClimate;
import org.genesys2.server.model.json.WorldclimJson;
......@@ -28,7 +29,7 @@ import org.genesys2.server.model.json.WorldclimJson;
*/
public interface ClimateDataService {
void worldclimUpdate(String variableName, HashSet<Long> ids, MappedByteBuffer buffer, short nullValue, double factor);
void worldclimUpdate(String variableName, HashSet<Long> ids, MappedByteBuffer buffer, Header header, double factor);
TileClimate climateForTile(Long tileIndex);
......
......@@ -23,6 +23,7 @@ import java.util.List;
import java.util.Map;
import org.apache.commons.beanutils.BeanUtils;
import org.genesys.worldclim.grid.generic.Header;
import org.genesys2.server.model.genesys.AccessionGeo;
import org.genesys2.server.model.impl.TileClimate;
import org.genesys2.server.model.json.WorldclimJson;
......@@ -65,9 +66,11 @@ public class ClimateDataServiceImpl implements ClimateDataService {
*/
@Override
@Transactional
public void worldclimUpdate(String variableName, HashSet<Long> tileIndexes, MappedByteBuffer buffer, short nullValue, double factor) {
public void worldclimUpdate(String variableName, HashSet<Long> tileIndexes, MappedByteBuffer buffer, Header header, double factor) {
LOG.debug("Updating {} for tileIndexes: {}", variableName, tileIndexes.size());
short nullValue = (short) header.getNoDataValue();
Map<Long, TileClimate> rowMap = findOrMakeRows(tileIndexes);
for (final Long tileIndex : tileIndexes) {
......
......@@ -170,7 +170,6 @@ public class WorldClimUpdater implements InitializingBean {
GenericGridFile ggf = new GenericGridFile(worldClimDir, variableName);
Header header = ggf.readHeader();
short nullValue = (short) header.getNoDataValue();
MappedByteBuffer buffer = ggf.mapDataBuffer();
double factor = 1.0;
......@@ -186,7 +185,7 @@ public class WorldClimUpdater implements InitializingBean {
Set<Long> tileIndexSet = getExistingTileIndexes();
if (missingTileIndexes())
tileIndexSet = generateTileIndexes();
tileIndexSet.addAll(generateTileIndexes());
List<Long> tileIndexes = new ArrayList<Long>(tileIndexSet);
int batchSize = 1000;
......@@ -196,7 +195,7 @@ public class WorldClimUpdater implements InitializingBean {
for (int i = 5; i >= 0; i--) {
try {
climateDataService.worldclimUpdate(variableName, ids, buffer, nullValue, factor);
climateDataService.worldclimUpdate(variableName, ids, buffer, header, factor);
break; // break try-5-times loop
} catch (DataIntegrityViolationException e) {
if (i == 0) {
......@@ -240,15 +239,17 @@ public class WorldClimUpdater implements InitializingBean {
LOG.debug("Calculating tileIndex at position {} of {}", startIndex, accessionIds.size());
List<AccessionGeo> acGeo = accessionGeoRepository.findForAccessions(ids);
for (AccessionGeo geo : acGeo) {
Double lon = geo.getLongitude();
Double lat = geo.getLatitude();
Double longitude = geo.getLongitude();
Double latitude = geo.getLatitude();
Long tileIndex = null;
if ((longitude != null && longitude == 0) && (latitude != null && latitude == 0)) {
tileIndex = WorldClimUtil.getWorldclim25Tile(longitude, latitude);
}
Long tileIndex = WorldClimUtil.getTileIndex(5, lon, lat);
if (tileIndex != null) {
if (! tileIndex.equals(geo.getTileIndex())) {
geo.setTileIndex(tileIndex);
toSave.add(geo);
}
geo.setTileIndex(tileIndex);
toSave.add(geo);
tileIndexSet.add(tileIndex);
} else if (geo.getTileIndex() != null) {
geo.setTileIndex(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