This commit is contained in:
FabioISTI 2020-04-03 18:03:57 +02:00
parent d691045b2c
commit bd95e973db
8 changed files with 120 additions and 102 deletions

View File

@ -13,7 +13,12 @@
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="src/test/resources"/>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
@ -24,6 +29,10 @@
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>

View File

@ -5,6 +5,11 @@
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.wst.common.project.facet.core.builder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
@ -15,9 +20,15 @@
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.wst.validation.validationbuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
</natures>
</projectDescription>

View File

@ -1,13 +1,2 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.7
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.release=disabled
org.eclipse.jdt.core.compiler.source=1.7

View File

@ -60,9 +60,14 @@
<dependency>
<groupId>org.gcube.spatial.data</groupId>
<artifactId>gis-interface</artifactId>
<version>2.4.2</version>
<version>[2.4.0-SNAPSHOT,3.0.0)</version>
</dependency>
<dependency>
<groupId>org.gcube.spatial.data</groupId>
<artifactId>geonetwork</artifactId>
<version>3.4.1-SNAPSHOT</version>
</dependency>
<!-- TEST -->
<dependency>

View File

@ -2,10 +2,7 @@ package org.gcube.application.geoportal;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
@ -18,6 +15,7 @@ import java.util.Locale;
import org.gcube.application.geoportal.model.CentroidRecord;
import org.gcube.application.geoportal.model.DataParsingException;
import org.gcube.application.geoportal.model.PostgisTable;
import org.gcube.application.geoportal.utils.Files;
import lombok.extern.slf4j.Slf4j;
@ -57,7 +55,7 @@ public class PostgisDBManager {
Connection conn=null;
File sqlFolder=getFileFromResources("sql");
File sqlFolder=Files.getFileFromResources("sql");
if(!sqlFolder.isDirectory()) throw new RuntimeException(sqlFolder.getAbsolutePath()+" is not a folder.");
conn=DriverManager.getConnection(url,user,pwd);
@ -66,7 +64,7 @@ public class PostgisDBManager {
Statement stmt=conn.createStatement();
for(File f:sqlFolder.listFiles()) {
log.info("Executing "+f.getName());
String sql=readFile(f.getAbsolutePath(),Charset.forName("UTF-8"));
String sql=Files.readFileAsString(f.getAbsolutePath(),Charset.forName("UTF-8"));
stmt.execute(sql);
}
@ -77,26 +75,10 @@ public class PostgisDBManager {
}
private static String readFile(String path, Charset encoding)
throws IOException
{
byte[] encoded = Files.readAllBytes(Paths.get(path));
return new String(encoded, encoding);
}
private static File getFileFromResources(String fileName) {
ClassLoader classLoader =PostgisDBManager.class.getClassLoader();
URL resource = classLoader.getResource(fileName);
if (resource == null) {
throw new IllegalArgumentException("file is not found!");
} else {
return new File(resource.getFile());
}
}
private Connection conn=null;
@ -140,16 +122,21 @@ public class PostgisDBManager {
}
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));
ResultSet rs=conn.createStatement().executeQuery("Select ST_AsText(ST_Extent("+table.getGeometryColumnName()+")) as extent from "+table.getTablename());
if(rs.next())
return PostgisTable.BBOX.parseST_Extent(rs.getString("extent"));
else throw new SQLException("No extent returned");
}
public PreparedStatement prepareInsertStatement(PostgisTable target, boolean createTable) throws SQLException {
if(createTable) {
conn.createStatement().executeUpdate(target.getCreateStatement());
String createStmt=target.getCreateStatement();
log.debug("Executing create : "+createStmt);
conn.createStatement().executeUpdate(createStmt);
}
return conn.prepareStatement(target.getInsertionStatement());
String insertStmt=target.getInsertionStatement();
log.debug("Preparing insert statement : "+insertStmt);
return conn.prepareStatement(insertStmt);
}
public void deleteTable(String tableName) throws SQLException {

View File

@ -16,7 +16,9 @@ import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@RequiredArgsConstructor
@Getter
public class PostgisTable {
@ -25,11 +27,13 @@ public class PostgisTable {
@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)
log.debug("Parsing BBOX "+extent);
Matcher m=pattern.matcher(extent);
if(m.groupCount()!=4) throw new DataParsingException("Invalid extent format "+extent);
Double minLong=Double.parseDouble(m.group(0));
@ -38,7 +42,7 @@ public class PostgisTable {
Double maxLat=Double.parseDouble(m.group(3));
return new BBOX(maxLat, maxLong, minLat, minLong);
}
@NonNull
private Double maxLat;
@NonNull
@ -48,34 +52,33 @@ public class PostgisTable {
@NonNull
private Double minLong;
}
private static final NumberFormat DECIMAL_FORMAT=NumberFormat.getInstance(Locale.US);
private static final String GEOMETRY_COLUMN_NAME="geom";
public static final PostgisTable fromCSVHeaders(List<String> csvHeaders, GeometryType expectedType) {
String tableName=UUID.randomUUID().toString().toLowerCase();
String tableName="l"+UUID.randomUUID().toString().toLowerCase().replaceAll("-", "_");
ArrayList<String> fields=new ArrayList<String>();
for(String csvHeader:csvHeaders)
fields.add(sanitizeFieldName(csvHeader));
String geometryColumnName=null;
String geometryColumnName=GEOMETRY_COLUMN_NAME;
GeometryType geometryColumnType=expectedType;
fields.add(GEOMETRY_COLUMN_NAME);
switch(expectedType) {
case POINT :{
fields.add("point");
geometryColumnName="point";
case POINT :{
if(!fields.contains("xcoord")) throw new RuntimeException("No xcoord in headers");
if(!fields.contains("ycoord")) throw new RuntimeException("No ycoord in headers");
break;
}
default : {
geometryColumnName="wkt";
if(!fields.contains(geometryColumnName)) throw new RuntimeException("No "+geometryColumnName+" in headers");
if(!fields.contains("wkt")) throw new RuntimeException("No wkt in headers");
break;
}
}
@ -95,22 +98,18 @@ 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+"( ");
for(String field:fieldNames) {
String fieldDefinition=null;
switch(field) {
case("point"):{
fieldDefinition="geometry (POINT,4326)";
break;
}
case("wkt"):{
switch(field) {
case(GEOMETRY_COLUMN_NAME):{
switch(geometryColumnType) {
case LINE:{
fieldDefinition="geometry (MULTILINESTRING,4326)";
@ -120,7 +119,10 @@ public class PostgisTable {
fieldDefinition="geometry (MULTIPOLYGON,4326)";
break;
}
default : throw new RuntimeException("INCOHERENT DEFINITION");
case POINT:{
fieldDefinition="geometry (POINT,4326)";
break;
}
}
break;
}
@ -151,35 +153,35 @@ public class PostgisTable {
public String getInsertionStatement() {
StringBuilder fieldList=new StringBuilder();
StringBuilder fieldInsertion=new StringBuilder();
for(String field:fieldNames) {
fieldList.append(field+",");
if(field.equals(geometryColumnName)) {
fieldInsertion.append("ST_GeomFromText(?, 4326)");
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) 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;
@ -200,7 +202,7 @@ public class PostgisTable {
}
}
}
private static String sanitizeFieldName(String fieldName) {
return fieldName.toLowerCase().replaceAll(" ", "_");
}

View File

@ -1,11 +1,11 @@
package org.gcube.application.geoportal;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import org.gcube.application.geoportal.model.CentroidRecord;
import org.gcube.application.geoportal.model.DatabaseConnection;
import org.gcube.application.geoportal.utils.MainUtils;
public class Centroids {
@ -26,7 +26,7 @@ public class Centroids {
String cmd=args[0];
Map<String,String> options=asMap(args);
Map<String,String> options=MainUtils.asMap(args);
System.out.println("CMD : "+cmd);
System.out.println("Options : "+options);
@ -35,7 +35,7 @@ public class Centroids {
switch(cmd) {
case "single":{
PostgisDBManager.init(getMandatory("DBURL",options), getMandatory("DBUSER",options), getMandatory("DBPWD",options));
PostgisDBManager.init(MainUtils.getMandatory("DBURL",options), MainUtils.getMandatory("DBUSER",options), MainUtils.getMandatory("DBPWD",options));
PostgisDBManager db=PostgisDBManager.get();
@ -52,9 +52,9 @@ public class Centroids {
case "csv":{
Uploader u=new Uploader();
File csvFile=new File(getMandatory("csvFile", options));
File csvFile=new File(MainUtils.getMandatory("csvFile", options));
DatabaseConnection postgresDB=new DatabaseConnection(getMandatory("DBUSER",options), getMandatory("DBPWD",options),getMandatory("DBURL",options));
DatabaseConnection postgresDB=new DatabaseConnection(MainUtils.getMandatory("DBUSER",options), MainUtils.getMandatory("DBPWD",options),MainUtils.getMandatory("DBURL",options));
u.insertCSVCentroids(csvFile, postgresDB, null);
}
@ -65,23 +65,5 @@ public class Centroids {
}
private static final String getMandatory(String key,Map<String,String> map) {
if(map.containsKey(key)) return map.get(key);
else throw new RuntimeException("Missing mandatory parameter "+key);
}
private static final String getOptional(String key,Map<String,String> map, String defaultValue) {
if(map.containsKey(key)) return map.get(key);
else return defaultValue;
}
private static Map<String,String> asMap(String[] args){
HashMap<String,String> toReturn=new HashMap<>();
for(String arg:args) {
if(arg.contains("="))
toReturn.put(arg.substring(2, arg.indexOf("=")), // skipping first 2 chars "--XX="
arg.substring(arg.indexOf("=")+1));
}
return toReturn;
}
}

View File

@ -1,9 +1,42 @@
package org.gcube.application.geoportal.db;
import java.io.File;
import org.gcube.application.geoportal.model.PostgisTable.GeometryType;
import org.gcube.application.geoportal.utils.CSV;
import org.gcube.application.geoportal.utils.Files;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
public class PostgisTableTests {
public PostgisTableTests() {
// TODO Auto-generated constructor stub
private static File csvFolder=null;
@BeforeClass
public static void init() {
csvFolder=Files.getFileFromResources("csv");
}
@Test
public void testParseCSV() {
for(String csv:csvFolder.list()) {
GeometryType type=GeometryType.POINT;
if(csv.contains("linee"))
type=GeometryType.LINE;
else if(csv.contains("poligoni"))
type=GeometryType.POLYGON;
try {
CSV.tableFromCSV(csvFolder.getAbsolutePath()+File.separator+csv, type);
}catch(Throwable t) {
if(!csv.contains("wrong"))
Assert.fail(t.getMessage());
}
}
}
}