Skip to content

Commit

Permalink
Merge pull request #24 from ebocher/fix_tablename
Browse files Browse the repository at this point in the history
Improve table name management
  • Loading branch information
ebocher committed Apr 14, 2021
2 parents 583b3ca + 49212cd commit ee8eae5
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 70 deletions.
3 changes: 2 additions & 1 deletion src/main/java/org/h2gis/geotools/H2GISDataStoreFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ public void setBaseDirectory(File baseDirectory) {
*/
public File getBaseDirectory() {
return baseDirectory;
}
}


@Override
protected void setupParameters(Map parameters) {
Expand Down
30 changes: 22 additions & 8 deletions src/main/java/org/h2gis/geotools/H2GISDialect.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public class H2GISDialect extends BasicSQLDialect {
put(GeometryCollection.class, "GEOMETRYCOLLECTION");
put(LinearRing.class, "LINEARRING");
}
};
};

boolean functionEncodingEnabled = true;
//Since H2GIS 2.0
Expand Down Expand Up @@ -497,7 +497,8 @@ public void encodePrimaryKey(String column, StringBuffer sql) {
public void postCreateTable(String schemaName,
SimpleFeatureType featureType, Connection cx) throws SQLException {
schemaName = schemaName != null ? schemaName : "PUBLIC";
String tableName = featureType.getName().getLocalPart();
String tableName = TableLocation.parse(schemaName+"."+featureType.getName().getLocalPart(), DBTypes.H2).toString();

Statement st = null;
try {
st = cx.createStatement();
Expand Down Expand Up @@ -542,12 +543,26 @@ public void postCreateTable(String schemaName,
//setup the geometry type
if (dimensions == 3) {
geomType = geomType + "Z";
} else if (dimensions > 3) {
throw new IllegalArgumentException("H2GIS only supports geometries with 2 and 3 dimensions, current value: " + dimensions);
} else if (dimensions == 4) {
geomType = geomType + "ZM";
} else if (dimensions > 4) {
throw new IllegalArgumentException(
"H2GIS only supports geometries with 2 and 3 dimensions, current value: "
+ dimensions);
}

sql = "ALTER TABLE \"" + schemaName + "\".\"" + tableName + "\" "
+ "ADD CHECK ST_SRID( \"" + gd.getLocalName() + "\")= " + srid + ";";

sql
= "ALTER TABLE "
+ tableName
+ " "
+ "ALTER COLUMN "
+ gd.getLocalName()
+ " "
+ "TYPE geometry ("
+ geomType
+ ", "
+ srid
+ ");";
LOGGER.fine(sql);
st.execute(sql);
}
Expand Down Expand Up @@ -746,5 +761,4 @@ public Version getPostgreSQLVersion(Connection conn) throws SQLException {
}
return h2Version;
}

}
80 changes: 19 additions & 61 deletions src/test/java/org/h2gis/geotools/H2GISTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,6 @@ void getFeaturesFilter3() throws SQLException, IOException, CQLException {
st.execute("drop table LANDCOVER");
}


@Test
void testBboxFilter() throws SQLException, IOException, CQLException, ParseException {
st.execute("drop table if exists LANDCOVER");
Expand Down Expand Up @@ -337,16 +336,16 @@ void testVirtualTable() throws SQLException, IOException, ParseException {
ds.dropVirtualTable("LANDCOVER_CEREAL");
st.execute("drop table LANDCOVER");
}
@Test

@Test
void testVirtualTableFromSQL() throws SQLException, IOException, ParseException {
st.execute("drop table if exists LANDCOVER");
st.execute("CREATE TABLE LANDCOVER ( FID INTEGER, NAME CHARACTER VARYING(64),"
+ " THE_GEOM GEOMETRY(POLYGON));"
+ "INSERT INTO LANDCOVER VALUES(1, 'Green Forest', 'POLYGON((110 330, 210 330, 210 240, 110 240, 110 330))');"
+ "INSERT INTO LANDCOVER VALUES(2, 'Cereal', 'POLYGON((200 220, 310 220, 310 160, 200 160, 200 220))');"
+ "INSERT INTO LANDCOVER VALUES(3, 'Building', 'POLYGON((90 130, 140 130, 140 110, 90 110, 90 130))');");
VirtualTable vTable = new VirtualTable("LANDCOVER_CEREAL", "SELECT * FROM PUBLIC.LANDCOVER WHERE FID=2");
VirtualTable vTable = new VirtualTable("LANDCOVER_CEREAL", "SELECT * FROM PUBLIC.LANDCOVER WHERE FID=2");
ds.createVirtualTable(vTable);
SimpleFeatureType type = ds.getSchema("LANDCOVER_CEREAL");
assertNotNull(type);
Expand All @@ -359,7 +358,6 @@ void testVirtualTableFromSQL() throws SQLException, IOException, ParseException
st.execute("drop table LANDCOVER");
}


@Test
void testH2GISFileTable() throws SQLException, IOException {
st.execute("drop table if exists LANDCOVER");
Expand Down Expand Up @@ -397,18 +395,18 @@ void updateGeometry_Columns() throws SQLException, IOException, SchemaException

@Test
void testGeometryTypes() throws SQLException, IOException {
String sql = "DROP TABLE IF EXISTS GEOMTYPES; CREATE TABLE GEOMTYPES(G GEOMETRY, G_S GEOMETRY(GEOMETRY, 1), P GEOMETRY(POINT), P_S GEOMETRY(POINT, 1),\n" +
" PZ1 GEOMETRY(POINT Z), PZ2 GEOMETRY(POINTZ), PZ1_S GEOMETRY(POINT Z, 1), PZ2_S GEOMETRY(POINTZ, 1),\n" +
" PM GEOMETRY(POINT M), PZM GEOMETRY(POINT ZM), PZM_S GEOMETRY(POINT ZM, -100),\n" +
" LS GEOMETRY(LINESTRING), PG GEOMETRY(POLYGON),\n" +
" MP GEOMETRY(MULTIPOINT), MLS GEOMETRY(MULTILINESTRING), MPG GEOMETRY(MULTIPOLYGON),\n" +
" GC GEOMETRY(GEOMETRYCOLLECTION),PGZ GEOMETRY(POLYGONZ),PGM GEOMETRY(POLYGONM),PGZM GEOMETRY(POLYGONZM));\n" +
"INSERT INTO GEOMTYPES VALUES ('POINT EMPTY', 'SRID=1;POINT EMPTY', 'POINT EMPTY', 'SRID=1;POINT EMPTY',\n" +
" 'POINT Z EMPTY', 'POINT Z EMPTY', 'SRID=1;POINT Z EMPTY', 'SRID=1;POINTZ EMPTY',\n" +
" 'POINT M EMPTY', 'POINT ZM EMPTY', 'SRID=-100;POINT ZM EMPTY',\n" +
" 'LINESTRING EMPTY', 'POLYGON EMPTY',\n" +
" 'MULTIPOINT EMPTY', 'MULTILINESTRING EMPTY', 'MULTIPOLYGON EMPTY',\n" +
" 'GEOMETRYCOLLECTION EMPTY','POLYGON Z EMPTY','POLYGON M EMPTY','POLYGON ZM EMPTY');";
String sql = "DROP TABLE IF EXISTS GEOMTYPES; CREATE TABLE GEOMTYPES(G GEOMETRY, G_S GEOMETRY(GEOMETRY, 1), P GEOMETRY(POINT), P_S GEOMETRY(POINT, 1),\n"
+ " PZ1 GEOMETRY(POINT Z), PZ2 GEOMETRY(POINTZ), PZ1_S GEOMETRY(POINT Z, 1), PZ2_S GEOMETRY(POINTZ, 1),\n"
+ " PM GEOMETRY(POINT M), PZM GEOMETRY(POINT ZM), PZM_S GEOMETRY(POINT ZM, -100),\n"
+ " LS GEOMETRY(LINESTRING), PG GEOMETRY(POLYGON),\n"
+ " MP GEOMETRY(MULTIPOINT), MLS GEOMETRY(MULTILINESTRING), MPG GEOMETRY(MULTIPOLYGON),\n"
+ " GC GEOMETRY(GEOMETRYCOLLECTION),PGZ GEOMETRY(POLYGONZ),PGM GEOMETRY(POLYGONM),PGZM GEOMETRY(POLYGONZM));\n"
+ "INSERT INTO GEOMTYPES VALUES ('POINT EMPTY', 'SRID=1;POINT EMPTY', 'POINT EMPTY', 'SRID=1;POINT EMPTY',\n"
+ " 'POINT Z EMPTY', 'POINT Z EMPTY', 'SRID=1;POINT Z EMPTY', 'SRID=1;POINTZ EMPTY',\n"
+ " 'POINT M EMPTY', 'POINT ZM EMPTY', 'SRID=-100;POINT ZM EMPTY',\n"
+ " 'LINESTRING EMPTY', 'POLYGON EMPTY',\n"
+ " 'MULTIPOINT EMPTY', 'MULTILINESTRING EMPTY', 'MULTIPOLYGON EMPTY',\n"
+ " 'GEOMETRYCOLLECTION EMPTY','POLYGON Z EMPTY','POLYGON M EMPTY','POLYGON ZM EMPTY');";
st.execute(sql);
SimpleFeatureSource fs = (SimpleFeatureSource) ds.getFeatureSource("GEOMTYPES");
SimpleFeatureType schema = fs.getSchema();
Expand Down Expand Up @@ -456,51 +454,11 @@ void testGeometryTypes() throws SQLException, IOException {
assertTrue(geomType.getBinding().isAssignableFrom(Polygon.class));
}


@Test
void testIterateFeatures() throws IOException, SQLException, CQLException {
String inputFile ="/home/ebocher/Autres/data/IGN/BD_parcellaire_ign_lrgf93/PARCELLE.SHP";
inputFile = "/home/ebocher/Autres/data/IGN/data_cadastre/parc_dgi/Parc_dgi.shp";
//inputFile = "/home/ebocher/Autres/data/DONNEES RENNES/Reseau_Rennes.shp";
//st.execute("drop table if exists PARCELS");
//st.execute("CALL FILE_TABLE('" + inputFile + "', 'PARCELS');");
//st.execute("DROP TABLE IF EXISTS output_table_test");

st.execute("drop table if exists PARCELS");
st.execute("CREATE TABLE PARCELS ( FID INTEGER, NAME CHARACTER VARYING(64)"
+ " );"
+ "INSERT INTO PARCELS VALUES(1, 'Green Forest');"
+ "INSERT INTO PARCELS VALUES(2, 'Cereal');"
+ "INSERT INTO PARCELS VALUES(3, 'Building');");

long start = System.currentTimeMillis();

SimpleFeatureSource fs = ds.getFeatureSource("PARCELS");
SimpleFeatureCollection features = fs.getFeatures();
SimpleFeatureIterator iterator = features.features();
try {
while (iterator.hasNext()) {
SimpleFeature feature = iterator.next();
System.out.println(feature.getAttribute("FID"));
}
} finally {
iterator.close(); // IMPORTANT
}

/*ds.createSchema(transformed.getSchema());
FeatureStore<SimpleFeatureType,SimpleFeature> featStore =
(FeatureStore<SimpleFeatureType,SimpleFeature>)ds.getFeatureSource("OUTPUT_TABLE_TEST_F");
featStore.addFeatures(simpleFeatureCollection);*/

long end = System.currentTimeMillis();
System.out.println("Times " + (end - start) / 1000);
}

/**
/**
* Generate a path for the database
*
* @param dbName
* @return
* @return
*/
private static String getDataBasePath(String dbName) {
if (dbName.startsWith("file://")) {
Expand All @@ -509,4 +467,4 @@ private static String getDataBasePath(String dbName) {
return new File("target/test-resources/" + dbName).getAbsolutePath();
}
}
}
}

0 comments on commit ee8eae5

Please sign in to comment.