Skip to content

Commit

Permalink
Merge pull request #1319 from ebocher/fix_userspatial_ref
Browse files Browse the repository at this point in the history
Removed User_Spatial_Ref table to manage local srid when a prj is read
  • Loading branch information
ebocher authored Sep 15, 2022
2 parents 2cf93f8 + c4dd9d8 commit 92be487
Show file tree
Hide file tree
Showing 15 changed files with 58 additions and 201 deletions.
32 changes: 2 additions & 30 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,30 +1,2 @@
## Changelog for v2.1.0

+ Fix javadoc issues.
+ Remove `slf4j-simple` dependency (#1261)
+ Fix geometry_columns view function in order to escape table name with reserved database word (#1269)
+ Add an option in ASC file reader in order to add the asc content into an existing table (#1270)
+ Change batch size to use the optimal size (#1275)
+ Add new constructors to TableLocation (#1264)
+ Add `ST_AsEWKB` function(#1271)
+ Add `ST_Multi` function (#1268)
+ Fix GeoJSON driver on H2GIS does not use the good object type for geometry (#1277)
+ Fix estimated extend on schema (#1286)
+ `TableLocation.parse` now restore a `TableLocation.toString()` to the initial state
+ Make test run again without PostGIS instance
+ ST_DistanceSphere throws an exception when the two input srids are different (#1292)
+ Remove OrbisParent dependency
+ Update H2 to 2.1.212 and fix poly2tri dep
+ Move all OSGI dependencies to h2gis-functions-osgi and to postgis-jts-osgi
+ Move classes used for datasource creation (`H2GISOsgiDBFactory` and `DataSourceFactoryImpl`) to `*-osgi` package and keep test purpose class (`HGISSimpleDBFactory`) in `h2gis-functions`
+ Fix `ST_OrderingEquals` and `ST_AsBinary` parameters to handle the case of a null geometry
+ Fix ST_Accum and ST_Union to filter empty geometry
+ Fix `FindGeometryMetadata` to handle the case of case-sensitive names
+ Move header url into valid html `<a>` tags.
+ Make the method `PostGISDBFactory.createDataSource(Properties)` static
+ Fix ST_Union to return null if the entry geometry is null
+ Improve st_node to remove duplicate lines
+ Update H2 to 2.1.214
+ Fix an issue on writing as geojson a table with a null value typed as `TIME`
+ Add a mechanism to manage non-standardized prj file
+ Upgrade to JTS 1.19.0
## Changelog for v2.2.0
+ Remove the mechanism to manage non-standardized prj file
1 change: 1 addition & 0 deletions h2gis-api/src/main/java/org/h2gis/api/DriverFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
*/
public interface DriverFunction {


/**
* A linked table is created instantly but work only if the DataBase is
* local. A copy will transfer Data from the File to the remote/local
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,11 @@ private H2GISOsgiDBFactory() {

/**
* Open the connection to an existing database
* @param dbName
* @return
* @param dbName name of the database
* @return a connection to the database
* @throws SQLException
* @throws ClassNotFoundException
*/
public static Connection openSpatialDataBase(String dbName) throws SQLException, ClassNotFoundException {
public static Connection openSpatialDataBase(String dbName) throws SQLException {
String dbFilePath = getDataBasePath(dbName);
String databasePath = "jdbc:h2:"+ dbFilePath + H2_PARAMETERS;
org.h2.Driver.load();
Expand All @@ -73,8 +72,8 @@ public static Connection createSpatialDataBase(String dbName)throws SQLException

/**
* Return the path of the file database
* @param dbName
* @return
* @param dbName name of the database
* @return a path to the database
*/
private static String getDataBasePath(String dbName) {
if(dbName.startsWith("file://")) {
Expand Down Expand Up @@ -155,8 +154,8 @@ public static DataSource createDataSource(String dbName ,boolean initSpatial, St

/**
*
* @param dbName
* @param h2_PARAMETERS
* @param dbName path to the database
* @param h2_PARAMETERS Additional h2 parameters
* @return
*/
private static String initDBFile( String dbName, String h2_PARAMETERS ) {
Expand All @@ -180,10 +179,9 @@ private static String initDBFile( String dbName, String h2_PARAMETERS ) {
* @param initSpatial If true add spatial features to the database
* @param h2Parameters Additional h2 parameters
* @return Connection
* @throws SQLException
* @throws ClassNotFoundException
* @throws SQLException throws an exception if the spatial functions can not be loaded
*/
public static Connection createSpatialDataBase(String dbName,boolean initSpatial, String h2Parameters )throws SQLException, ClassNotFoundException {
public static Connection createSpatialDataBase(String dbName,boolean initSpatial, String h2Parameters )throws SQLException {
String databasePath = initDBFile(dbName, h2Parameters);
org.h2.Driver.load();
// Keep a connection alive to not close the DataBase on each unit test
Expand All @@ -201,10 +199,9 @@ public static Connection createSpatialDataBase(String dbName,boolean initSpatial
* @param dbName filename
* @param initSpatial If true add spatial features to the database
* @return Connection
* @throws SQLException
* @throws ClassNotFoundException
* @throws SQLException throws an exception if the spatial functions can not be loaded
*/
public static Connection createSpatialDataBase(String dbName, boolean initSpatial )throws SQLException, ClassNotFoundException {
public static Connection createSpatialDataBase(String dbName, boolean initSpatial )throws SQLException {
return createSpatialDataBase(dbName, initSpatial, H2_PARAMETERS);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public static Connection openSpatialDataBase(String dbName) throws SQLException
String databasePath = "jdbc:h2:"+ dbFilePath + H2_PARAMETERS;
org.h2.Driver.load();
// Keep a connection alive to not close the DataBase on each unit test
return DriverManager.getConnection(databasePath,
return DriverManager.getConnection(databasePath,
DEFAULT_USER, DEFAULT_PASSWORD);
}
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,6 @@ public static void load(Connection connection, String BundleSymbolicName, String
String packagePrepend = BundleSymbolicName+":"+BundleVersion+":";
registerH2GISFunctions(connection,packagePrepend);
registerSpatialTables(connection);
registerUserSpatialRefSystemTable(connection);
}

/**
Expand All @@ -350,7 +349,6 @@ public static void load(Connection connection) throws SQLException {
}
registerH2GISFunctions(connection, "");
registerSpatialTables(connection);
registerUserSpatialRefSystemTable(connection);
}

/**
Expand Down Expand Up @@ -387,15 +385,6 @@ public static void registerSpatialTables(Connection connection) throws SQLExcept
}
}
}
/**
* Register a class to manage some user spatial reference system to store prj information when
* CTS is not able to detect the official SRID from a prj file
* @param connection Open connection
* @throws java.sql.SQLException
*/
public static void registerUserSpatialRefSystemTable(Connection connection) throws SQLException {
UserSpatialRef.init(connection);
}

/**
* Return a string property of the function
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public static String[] openFile(Connection connection, String fileName, String t
st.execute(String.format("CREATE TABLE %s COMMENT %s ENGINE %s WITH %s",
tableName_,StringUtils.quoteStringSQL(fileName),
StringUtils.quoteJavaString(driverDef.getClassName()),StringUtils.quoteJavaString(fileName)));
return new String[]{tableName_};
return new String[]{tableName_};
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ public Value getField(long rowId, int columnId) throws IOException {
public DbaseFileReader getDbaseFileReader() {
return dbaseFileReader;
}



}

Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public class SHPDriver implements FileDriver {
public File prjFile;
private int srid =0;


/**
* @param geometryFieldIndex The geometry field index in getRow() array.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

import org.cts.parser.prj.PrjKeyParameters;
import org.cts.parser.prj.PrjParser;
import org.h2gis.functions.spatial.crs.UserSpatialRef;
import org.h2gis.utilities.TableLocation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -43,8 +42,8 @@
*/
public class PRJUtil {
private static final Logger log = LoggerFactory.getLogger(PRJUtil.class);


/**
* Return the SRID value stored in a prj file
*
Expand Down Expand Up @@ -72,7 +71,7 @@ public static int getSRID(File prjFile) throws IOException {
srid = Integer.parseInt(authorityNameWithKey[1]);
}
else{
srid = UserSpatialRef.getUserSRID(prjString);
log.debug("The prj doesn't contain any EPSG identifier. \n A default srid equals to 0 will be added.");
}
}
else{
Expand Down Expand Up @@ -161,17 +160,7 @@ public static void writePRJ(Connection connection, int srid, File fileName) thro
printWriter = new PrintWriter(fileName);
printWriter.println(rs.getString(1));
} else {
//Let's try if this srid exists in the user table USER_SPATIAL_REF_SYS
try (PreparedStatement ps_user = connection.prepareStatement("SELECT SRTEXT FROM " + UserSpatialRef.USER_SPATIAL_REF_SYS_TABLE + " WHERE SRID = ?")){
ps_user.setInt(1, srid);
ResultSet rs_user = ps_user.executeQuery();
if (rs_user.next()) {
printWriter = new PrintWriter(fileName);
printWriter.println(rs_user.getString(1));
} else {
log.warn("This SRID { " + srid + " } is not supported. \n The PRJ file won't be created.");
}
}
log.warn("This SRID { " + srid + " } is not supported. \n The PRJ file won't be created.");
}
} finally {
if (printWriter != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,30 +85,6 @@ public Map<String, String> getParameters(String code) throws RegistryException {

return v;
}
//We look on the user spatial ref system table if we can find prj information
PreparedStatement userPS = connection.prepareStatement("SELECT srtext FROM "+UserSpatialRef.USER_SPATIAL_REF_SYS_TABLE+ " where srid=?");
userPS.setInt(1, Integer.valueOf(code));
ResultSet userRS = userPS.executeQuery();
if (userRS.next()) {
if(prjParser==null){
prjParser=new PrjParser();
}
String prjText = userRS.getString(1);
String authcode = "user:" + code;
if(prjText.isEmpty()){
throw new RegistryException("No translation for srid user "+ authcode +" to PROJ format is known");
}

Map<String, String> v = prjParser.getParameters(prjText);

if (!v.containsKey(ProjKeyParameters.title)) {
v.put(ProjKeyParameters.title, authcode);
}
prepStmt.close();

return v;
}

} catch (SQLException ex) {
throw new RegistryException("Cannot obtain the CRS parameters", ex);
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -72,22 +72,22 @@ public static Geometry getFurthestCoordinate(Point point, Geometry geom) throws
}
double minDistance = Double.POSITIVE_INFINITY;
Coordinate pointCoordinate = point.getCoordinate();
Set<Coordinate> closestCoordinates = new HashSet<Coordinate>();
Set<Point> closestCoordinates = new HashSet<Point>();
for (Coordinate c : geom.getCoordinates()) {
double distance = c.distance(pointCoordinate);
if (Double.compare(distance, minDistance) == 0) {
closestCoordinates.add(c);
closestCoordinates.add(GEOMETRY_FACTORY.createPoint(c));
}
if (Double.compare(distance, minDistance) < 0) {
minDistance = distance;
closestCoordinates.clear();
closestCoordinates.add(c);
closestCoordinates.add(GEOMETRY_FACTORY.createPoint(c));
}
}
if (closestCoordinates.size() == 1) {
return GEOMETRY_FACTORY.createPoint(closestCoordinates.iterator().next());
return closestCoordinates.iterator().next();
}
return GEOMETRY_FACTORY.createMultiPoint(
closestCoordinates.toArray(new Coordinate[0]));
closestCoordinates.toArray(new Point[0]));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@
import org.h2.util.StringUtils;
import org.h2gis.functions.factory.H2GISDBFactory;
import org.h2gis.functions.io.file_table.H2TableIndex;
import org.h2gis.utilities.GeometryTableUtilities;
import org.h2gis.utilities.GeometryTypeCodes;
import org.h2gis.utilities.JDBCUtilities;
import org.h2gis.utilities.TableLocation;
import org.h2gis.utilities.*;
import org.h2gis.utilities.dbtypes.DBTypes;
import org.junit.jupiter.api.*;
import org.locationtech.jts.geom.Geometry;
Expand All @@ -52,14 +49,8 @@ public static void tearUp() throws Exception {
connection = H2GISDBFactory.createSpatialDataBase(DB_NAME);
}

@BeforeEach
public void tearUp2() throws Exception {
// Keep a connection alive to not close the DataBase on each unit test
connection = H2GISDBFactory.openSpatialDataBase(DB_NAME);
}

@AfterEach
public void tearDown() throws Exception {
@AfterAll
public static void tearDown() throws Exception {
connection.close();
}

Expand Down Expand Up @@ -257,6 +248,17 @@ public void readSHPConstraintTest() throws SQLException {
st.execute("drop table shptable");
}

@Test
public void readReadPRJWithoutEPSGTest() throws SQLException {
Statement st = connection.createStatement();
st.execute("drop table if exists shptable");
st.execute("CALL FILE_TABLE('"+SHPEngineTest.class.getResource("waternetwork_without_epsg.shp").getPath()+"', 'SHPTABLE');");
GeometryMetaData geomMeta = GeometryTableUtilities.getMetaData(connection, TableLocation.parse("SHPTABLE", DBTypes.H2GIS),"THE_GEOM");
assertEquals(GeometryTypeCodes.MULTILINESTRING, geomMeta.geometryTypeCode);
assertTrue(geomMeta.getSRID()==0);
st.execute("drop table shptable");
}

@Test
public void testAddIndexOnTableLink() throws SQLException {
Statement st = connection.createStatement();
Expand Down
Loading

0 comments on commit 92be487

Please sign in to comment.