Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ST_AsEWKB function #1278

Merged
merged 5 commits into from
Feb 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
+ Fix javadoc issues.
+ Remove `slf4j-simple` dependency (#1261)
+ Fix geometry_columns view function in order to escape table name with reserved database word (#1269)
+ Change batch size to use the optimal size (#1275)
+ Change batch size to use the optimal size (#1275)
+ Add ST_AsEWKB function(#1271)
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,8 @@ public static Function[] getBuiltInsFunctions() {
new ST_Force3DM(),
new ST_VariableBuffer(),
new ST_SubDivide(),
new ST_MemSize()
new ST_MemSize(),
new ST_AsEWKB()
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* H2GIS is a library that brings spatial support to the H2 Database Engine
* <http://www.h2database.com>. H2GIS is developed by CNRS
* <http://www.cnrs.fr/>.
*
* This code is part of the H2GIS project. H2GIS is free software;
* you can redistribute it and/or modify it under the terms of the GNU
* Lesser General Public License as published by the Free Software Foundation;
* version 3.0 of the License.
*
* H2GIS is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details <http://www.gnu.org/licenses/>.
*
*
* For more information, please consult: <http://www.h2gis.org/>
* or contact directly: info_at_h2gis.org
*/

package org.h2gis.functions.spatial.convert;

import org.h2.util.geometry.JTSUtils;
import org.h2.value.Value;
import org.h2gis.api.DeterministicScalarFunction;
import org.locationtech.jts.geom.Geometry;

import java.sql.SQLException;

/**
* Convert a Geometry value into an Extended Well-Known Binary.
* @author Nicolas Fortin
*/
public class ST_AsEWKB extends DeterministicScalarFunction {
/**
* Default constructor
*/
public ST_AsEWKB() {
addProperty(PROP_REMARKS, "Convert a geometry into EWKB representation.");
}

@Override
public String getJavaStaticMethod() {
return "asEWKB";
}

/**
* Convert a Geometry value into an Extended Well-Known Binary.
* @param value Geometry instance
* @return The bytes representation
*/
public static byte[] asEWKB(Value value) throws SQLException {
switch (value.getValueType()) {
case Value.NULL:
case Value.GEOMETRY:
return value.getBytes();
default:
throw new SQLException("ST_AsEWKB only supports geometry value");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/**
* H2GIS is a library that brings spatial support to the H2 Database Engine
* <http://www.h2database.com>. H2GIS is developed by CNRS
* <http://www.cnrs.fr/>.
*
* This code is part of the H2GIS project. H2GIS is free software;
* you can redistribute it and/or modify it under the terms of the GNU
* Lesser General Public License as published by the Free Software Foundation;
* version 3.0 of the License.
*
* H2GIS is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details <http://www.gnu.org/licenses/>.
*
*
* For more information, please consult: <http://www.h2gis.org/>
* or contact directly: info_at_h2gis.org
*/

package org.h2gis.functions.spatial.convert;

import org.h2gis.functions.factory.H2GISDBFactory;
import org.h2gis.functions.factory.H2GISFunctions;
import org.h2gis.functions.spatial.geometry.DummySpatialFunction;
import org.h2gis.functions.spatial.geometry.GeometryFormsTest;
import org.h2gis.unitTest.GeometryAsserts;
import org.junit.jupiter.api.*;
import org.locationtech.jts.geom.*;
import org.locationtech.jts.geom.impl.CoordinateArraySequence;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import static org.junit.jupiter.api.Assertions.*;


/**
* @author Sylvain Palominos
* @author Erwan Bocher
*
*/
public class GeometryConvertTest {
private static final GeometryFactory FACTORY = new GeometryFactory();

private static Connection connection;
private Statement st;

@BeforeAll
public static void tearUp() throws Exception {
// Keep a connection alive to not close the DataBase on each unit test
connection = H2GISDBFactory.createSpatialDataBase(GeometryConvertTest.class.getSimpleName());
}

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

@BeforeEach
public void setUpStatement() throws Exception {
st = connection.createStatement();
}

@AfterEach
public void tearDownStatement() throws Exception {
st.close();
}

@Test
public void AsEWKB1() throws SQLException {
ResultSet rs = st.executeQuery("SELECT RAWTOHEX(ST_AsEWKB(ST_GeomFromText('POLYGON((0 0,0 1,1 1,1 0,0 0))',4326)));");
rs.next();
assertEquals("0020000003000010e600000001000000050000000000000000000000000000000000000000000000003ff00000000000003ff00000000000003ff00000000000003ff0000000000000000000000000000000000000000000000000000000000000", rs.getString(1));
}

@Test
public void AsEWKB2() throws SQLException {
ResultSet rs = st.executeQuery("SELECT RAWTOHEX(ST_AsEWKB(ST_GeomFromText('POINT M(1 2 3)',4326)));");
rs.next();
assertEquals("0060000001000010e63ff000000000000040000000000000004008000000000000", rs.getString(1));
}

@Test
public void AsEWKB3() throws SQLException {
ResultSet rs = st.executeQuery("SELECT RAWTOHEX(ST_AsEWKB(ST_GeomFromText('POINT Z (1 2 3)',4326)));");
rs.next();
assertEquals("00a0000001000010e63ff000000000000040000000000000004008000000000000", rs.getString(1));
}

@Test
public void AsEWKB4() throws SQLException {
ResultSet rs = st.executeQuery("SELECT RAWTOHEX(ST_AsEWKB(ST_GeomFromText('POINT ZM (1 2 3 4)',4326)));");
rs.next();
assertEquals("00e0000001000010e63ff0000000000000400000000000000040080000000000004010000000000000", rs.getString(1));
}

}