Bounding Box from table

This commit is contained in:
FabioISTI 2020-04-03 12:42:42 +02:00
parent 3d044c9475
commit 725eef9d57
5 changed files with 135 additions and 10 deletions

View File

@ -9,14 +9,14 @@ import java.nio.file.Paths;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.NumberFormat;
import java.util.List;
import java.util.Locale;
import java.util.UUID;
import org.gcube.application.geoportal.model.CentroidRecord;
import org.gcube.application.geoportal.model.DataParsingException;
import org.gcube.application.geoportal.model.PostgisTable;
import lombok.extern.slf4j.Slf4j;
@ -139,7 +139,11 @@ public class PostgisDBManager {
return deleteCentroids.executeUpdate();
}
public PostgisTable.BBOX evaluateBoundingBox(PostgisTable table) throws SQLException, DataParsingException {
ResultSet rs=conn.createStatement().executeQuery("Select ST_Extent("+table.getGeometryColumnName()+") as extent from "+table.getTablename());
rs.next();
return PostgisTable.BBOX.parseST_Extent(rs.getString(1));
}
public PreparedStatement prepareInsertStatement(PostgisTable target, boolean createTable) throws SQLException {
if(createTable) {

View File

@ -20,6 +20,7 @@ import org.gcube.application.geoportal.model.DatabaseConnection;
import org.gcube.application.geoportal.model.InvalidRecordException;
import org.gcube.application.geoportal.model.LayerDescriptor;
import org.gcube.application.geoportal.model.PostgisTable;
import org.gcube.spatial.data.gis.GISInterface;
import lombok.extern.slf4j.Slf4j;
@ -155,7 +156,7 @@ public class Uploader {
parser.close();
}
GISInterface gis=GISInterface.get(descriptors)
//publishDBTable
// create feature type

View File

@ -0,0 +1,30 @@
package org.gcube.application.geoportal.model;
public class DataParsingException extends Exception {
public DataParsingException() {
// TODO Auto-generated constructor stub
}
public DataParsingException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
public DataParsingException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
public DataParsingException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
public DataParsingException(String message, Throwable cause, boolean enableSuppression,
boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
// TODO Auto-generated constructor stub
}
}

View File

@ -1,21 +1,58 @@
package org.gcube.application.geoportal.model;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import lombok.Getter;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.ToString;
@RequiredArgsConstructor
@Getter
public class PostgisTable {
@RequiredArgsConstructor
@Getter
@ToString
public static class BBOX{
private static Pattern pattern = Pattern.compile("^(-?)(0|([1-9][0-9]*))(\\.[0-9]+)?$");
public static BBOX parseST_Extent(String extent) throws DataParsingException {
//BOX(11.9122574810083 44.2514144864263,11.9761128271586 44.2912342569845)
Matcher m=pattern.matcher(extent);
if(m.groupCount()!=4) throw new DataParsingException("Invalid extent format "+extent);
Double minLong=Double.parseDouble(m.group(0));
Double minLat=Double.parseDouble(m.group(1));
Double maxLong=Double.parseDouble(m.group(2));
Double maxLat=Double.parseDouble(m.group(3));
return new BBOX(maxLat, maxLong, minLat, minLong);
}
@NonNull
private Double maxLat;
@NonNull
private Double maxLong;
@NonNull
private Double minLat;
@NonNull
private Double minLong;
}
private static final NumberFormat DECIMAL_FORMAT=NumberFormat.getInstance(Locale.US);
public static final PostgisTable fromCSVHeaders(List<String> csvHeaders, GeometryType expectedType) {
@ -58,8 +95,11 @@ public class PostgisTable {
private String geometryColumnName;
@NonNull
private GeometryType geometryColumnType;
@Setter
private BBOX boundingBox=null;
public String getCreateStatement() {
StringBuilder stmt=new StringBuilder();
stmt.append("CREATE TABLE "+tablename+"( ");
@ -112,12 +152,53 @@ public class PostgisTable {
StringBuilder fieldList=new StringBuilder();
StringBuilder fieldInsertion=new StringBuilder();
return "Insert into "+tablename+" "+fieldList+" VALUES "+fieldInsertion;
for(String field:fieldNames) {
fieldList.append(field+",");
if(field.equals(geometryColumnName)) {
fieldInsertion.append("ST_GeomFromText(?, 4326)");
}else fieldInsertion.append("?,");
}
fieldList.deleteCharAt(fieldList.lastIndexOf(","));
fieldInsertion.deleteCharAt(fieldInsertion.lastIndexOf(","));
return "Insert into "+tablename+" ("+fieldList+") VALUES ("+fieldInsertion+")";
}
public void fillPreparedStatement(Map<String,String> rowValues, PreparedStatement toFill) {
public void fillPreparedStatement(Map<String,String> rowValues, PreparedStatement toFill) throws SQLException {
int psFieldIndex=0;
for(String field:fieldNames) {
psFieldIndex++;
switch(field) {
case("point"):{
String xRepresentation=DECIMAL_FORMAT.format(Double.parseDouble(rowValues.get("xcoord")));
String yRepresentation=DECIMAL_FORMAT.format(Double.parseDouble(rowValues.get("ycoord")));
toFill.setString(psFieldIndex, "POINT("+xRepresentation+" "+
yRepresentation+")");
break;
}
case("xcoord"):
case("ycoord"):
case("year"):
case ("fid"):
case ("id"):
case ("objectid"):{
toFill.setInt(psFieldIndex, Integer.parseInt(rowValues.get(field)));
break;
}
default:{
toFill.setString(psFieldIndex, rowValues.get(field));
break;
}
}
}
}
private static String sanitizeFieldName(String fieldName) {

View File

@ -0,0 +1,9 @@
package org.gcube.application.geoportal.db;
public class PostgisTableTests {
public PostgisTableTests() {
// TODO Auto-generated constructor stub
}
}