Subversion Repositories hibernate-spatial

Compare Revisions

Ignore whitespace Rev 140 → Rev 141

/trunk/hibernate-spatial-sqlserver/src/test/java/org/hibernatespatial/sqlserver/convertors/AbstractConvertorTest.java
New file
0,0 → 1,75
/*
* $Id$
*
* This file is part of Hibernate Spatial, an extension to the
* hibernate ORM solution for geographic data.
*
* Copyright © 2009 Geovise BVBA
*
* This work was partially supported by the European Commission,
* under the 6th Framework Programme, contract IST-2-004688-STP.
*
* This library 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; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* For more information, visit: http://www.hibernatespatial.org/
*/
 
package org.hibernatespatial.sqlserver.convertors;
 
import org.junit.BeforeClass;
import org.hibernatespatial.sqlserver.DataSourceUtils;
 
import java.util.Map;
import java.util.HashMap;
 
import com.vividsolutions.jts.geom.Geometry;
 
/**
* @author Karel Maesen, Geovise BVBA.
* Date: Nov 2, 2009
*/
public class AbstractConvertorTest {
 
Map<Integer, Geometry> decodedGeoms;
Map<Integer, byte[]> rawResults;
Map<Integer, byte[]> encodedGeoms;
 
@BeforeClass
public static void beforeClass() {
DataSourceUtils.removeReadTestData();
DataSourceUtils.loadReadTestData();
}
 
 
public void doDecoding(OpenGisType type) {
rawResults = DataSourceUtils.rawByteArrays(type.toString());
decodedGeoms = new HashMap<Integer, Geometry>();
 
for (Integer id : rawResults.keySet()) {
Geometry geometry = Decoders.decode(rawResults.get(id));
decodedGeoms.put(id, geometry);
}
}
 
public void doEncoding() {
encodedGeoms = new HashMap<Integer, byte[]>();
for (Integer id : decodedGeoms.keySet()) {
Geometry geom = decodedGeoms.get(id);
byte[] bytes = Encoders.encode(geom);
encodedGeoms.put(id, bytes);
}
}
}
 
Property changes:
Added: svn:keywords
+ "Id"
/trunk/hibernate-spatial-sqlserver/src/test/java/org/hibernatespatial/sqlserver/convertors/PointConvertorTest.java
New file
0,0 → 1,101
/*
* $Id$
*
* This file is part of Hibernate Spatial, an extension to the
* hibernate ORM solution for geographic data.
*
* Copyright © 2009 Geovise BVBA
*
* This work was partially supported by the European Commission,
* under the 6th Framework Programme, contract IST-2-004688-STP.
*
* This library 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; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* For more information, visit: http://www.hibernatespatial.org/
*/
 
package org.hibernatespatial.sqlserver.convertors;
 
import org.junit.Test;
import org.junit.Before;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import org.hibernatespatial.mgeom.MCoordinate;
 
import com.vividsolutions.jts.geom.Point;
import com.vividsolutions.jts.geom.Coordinate;
import static junit.framework.Assert.assertEquals;
 
import java.util.Arrays;
 
/**
* @author Karel Maesen, Geovise BVBA.
* Date: Nov 2, 2009
*/
public class PointConvertorTest extends AbstractConvertorTest {
 
@Before
public void setup() {
doDecoding(OpenGisType.POINT);
doEncoding();
}
 
@Test
public void test_verify_srid() {
assertEquals(0, decodedGeoms.get(1).getSRID());
assertEquals(4326, decodedGeoms.get(2).getSRID());
assertEquals(31370, decodedGeoms.get(3).getSRID());
}
 
@Test
public void test_class() {
for (Integer id : decodedGeoms.keySet()) {
assertEquals(Point.class, decodedGeoms.get(id).getClass());
}
}
 
@Test
public void test_coordinates() {
Coordinate expected;
Coordinate received;
expected = new Coordinate(10.0, 5.0);
assertEquals(expected, decodedGeoms.get(1).getCoordinate());
expected = new Coordinate(52.25, 2.53);
assertEquals(expected, decodedGeoms.get(2).getCoordinate());
expected = new Coordinate(150000.0, 200000.0);
assertEquals(expected, decodedGeoms.get(3).getCoordinate());
expected = new MCoordinate(10.0, 2.0, 1.0, 3.0);
assertEquals(expected, decodedGeoms.get(4).getCoordinate());
}
 
@Test
public void test_encoding() {
for (Integer id : encodedGeoms.keySet()) {
assertTrue(Arrays.equals(rawResults.get(id), encodedGeoms.get(id)));
}
}
 
@Test
public void test_test_empty_point() {
//TODO -- How?
}
 
@Test
public void test_no_srid() {
//TODO -- How?
}
 
 
}
Property changes:
Added: svn:keywords
+ "Id"
/trunk/hibernate-spatial-sqlserver/src/test/java/org/hibernatespatial/sqlserver/DataSourceUtils.java
New file
0,0 → 1,179
/*
* $Id$
*
* This file is part of Hibernate Spatial, an extension to the
* hibernate ORM solution for geographic data.
*
* Copyright © 2009 Geovise BVBA
*
* This work was partially supported by the European Commission,
* under the 6th Framework Programme, contract IST-2-004688-STP.
*
* This library 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; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* For more information, visit: http://www.hibernatespatial.org/
*/
 
package org.hibernatespatial.sqlserver;
 
import org.apache.commons.dbcp.BasicDataSource;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import javax.sql.DataSource;
import java.util.*;
import java.io.InputStream;
import java.io.IOException;
import java.sql.*;
 
/**
* <p>Unit test support class.</p>
*
* @author Karel Maesen, Geovise BVBA.
* Date: Nov 2, 2009
*/
public class DataSourceUtils {
 
private static Logger LOGGER = LoggerFactory.getLogger(DataSourceUtils.class);
static Properties properties;
 
static {
InputStream is = null;
try {
is = Thread.currentThread().getContextClassLoader().getResourceAsStream("hibernate-spatial-sqlsever-test.properties");
properties = new Properties();
properties.load(is);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (is != null) try {
is.close();
} catch (IOException e) {
//nothing to do
}
}
}
 
private static final DataSource dataSource = createBasicDataSource();
 
private static class GeomTest {
//TODO -- how to define EMPTY Geomtries in SQL?
final static String[] DATA = new String[]{
"insert into geomtest values (1, 'POINT', Geometry::STGeomFromText('POINT(10 5)', 0))",
"insert into geomtest values (2, 'POINT', Geometry::STGeomFromText('POINT(52.25 2.53)', 4326))",
"insert into geomtest values (3, 'POINT', Geometry::STGeomFromText('POINT(150000 200000)', 31370))",
"insert into geomtest values (4, 'POINT', Geometry::STGeomFromText('POINT(10.0 2.0 1.0 3.0)', 4326))",
};
}
 
private static DataSource createBasicDataSource() {
String url = properties.getProperty("jdbcUrl");
String user = properties.getProperty("dbUsername");
String pwd = properties.getProperty("dbPassword");
BasicDataSource result = new BasicDataSource();
result.setDriverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
result.setUrl(url);
result.setUsername(user);
result.setPassword(pwd);
return result;
}
 
public static DataSource getDataSource() {
return dataSource;
}
 
 
public static void removeReadTestData() {
Connection cn = null;
try {
cn = getDataSource().getConnection();
PreparedStatement pmt = cn.prepareStatement("delete from geomtest");
if (!pmt.execute()) {
int updateCount = pmt.getUpdateCount();
LOGGER.info("Removing " + updateCount + " rows.");
}
pmt.close();
} catch (SQLException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} finally {
try {
cn.close();
} catch (SQLException e) {
// nothing to do
}
}
}
 
 
public static void loadReadTestData() {
Connection cn = null;
try {
cn = getDataSource().getConnection();
Statement stmt = cn.createStatement();
for (int i = 0; i < GeomTest.DATA.length; i++) {
LOGGER.debug("adding stmt: " + GeomTest.DATA[i]);
stmt.addBatch(GeomTest.DATA[i]);
}
int[] insCounts = stmt.executeBatch();
stmt.close();
LOGGER.info("Loaded " + sum(insCounts) + " rows.");
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
cn.close();
} catch (SQLException e) {
// nothing to do
}
}
}
 
public static Map<Integer, byte[]> rawByteArrays(String type) {
Map<Integer, byte[]> map = new HashMap<Integer, byte[]>();
Connection cn = null;
try {
cn = getDataSource().getConnection();
PreparedStatement pstmt = cn.prepareStatement("select id, geom from geomtest where type = ? order by id");
pstmt.setString(1, type);
ResultSet results = pstmt.executeQuery();
while (results.next()) {
Integer id = results.getInt(1);
byte[] bytes = results.getBytes(2);
map.put(id, bytes);
}
 
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
cn.close();
} catch (SQLException e) {
// nothing we can do.
}
}
return map;
 
}
 
private static int sum(int[] insCounts) {
int result = 0;
for (int idx = 0; idx < insCounts.length; idx++) {
result += insCounts[idx];
}
return result;
}
 
}
Property changes:
Added: svn:keywords
+ "Id"
/trunk/hibernate-spatial-sqlserver/src/test/resources/hibernate-spatial-sqlsever-test.properties
New file
0,0 → 1,8
# This property file contains site specific configuration
# for the MS SQLServer 2008 instance
#
 
jdbcUrl = jdbc:sqlserver://192.168.0.103:1433;databaseName=HBS
dbUsername = hbs
dbPassword = hbs
 
Property changes:
Added: svn:keywords
+ "Id"
/trunk/hibernate-spatial-sqlserver/src/test/resources/log4j.properties
New file
0,0 → 1,49
### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
 
### direct messages to file hibernate.log ###
#log4j.appender.file=org.apache.log4j.FileAppender
#log4j.appender.file.File=hibernate.log
#log4j.appender.file.layout=org.apache.log4j.PatternLayout
#log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
 
### set log levels - for more verbose logging change 'info' to 'debug' ###
 
log4j.rootLogger=debug, stdout
 
log4j.logger.org.hibernate=info
#log4j.logger.org.hibernate=debug
 
log4j.logger.org.hibernatespatial.sqlserver=debug
 
### log HQL query parser activity
#log4j.logger.org.hibernate.hql.ast.AST=debug
 
### log just the SQL
#log4j.logger.org.hibernate.SQL=debug
 
### log JDBC bind parameters ###
log4j.logger.org.hibernate.type=info
#log4j.logger.org.hibernate.type=debug
 
### log schema export/update ###
#log4j.logger.org.hibernate.tool.hbm2ddl=debug
 
### log HQL parse trees
#log4j.logger.org.hibernate.hql=debug
 
### log cache activity ###
#log4j.logger.org.hibernate.cache=debug
 
### log transaction activity
#log4j.logger.org.hibernate.transaction=debug
 
### log JDBC resource acquisition
#log4j.logger.org.hibernate.jdbc=debug
 
### enable the following line if you want to track down connection ###
### leakages when using DriverManagerConnectionProvider ###
#log4j.logger.org.hibernate.connection.DriverManagerConnectionProvider=trace
Property changes:
Added: svn:keywords
+ "Id"
/trunk/hibernate-spatial-sqlserver/src/main/java/org/hibernatespatial/sqlserver/DialectProvider.java
New file
0,0 → 1,52
/*
* $Id$
*
* This file is part of Hibernate Spatial, an extension to the
* hibernate ORM solution for geographic data.
*
* Copyright © 2009 Geovise BVBA
*
* This work was partially supported by the European Commission,
* under the 6th Framework Programme, contract IST-2-004688-STP.
*
* This library 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; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* For more information, visit: http://www.hibernatespatial.org/
*/
 
package org.hibernatespatial.sqlserver;
 
import org.hibernatespatial.spi.SpatialDialectProvider;
import org.hibernatespatial.SpatialDialect;
 
/**
* @author Karel Maesen, Geovise BVBA.
* Date: Nov 2, 2009
*/
public class DialectProvider implements SpatialDialectProvider {
 
 
public SpatialDialect createSpatialDialect(String dialect) {
return new SQLServer2008SpatialDialect();
}
 
public SpatialDialect getDefaultDialect() {
return new SQLServer2008SpatialDialect();
}
 
public String[] getSupportedDialects() {
return new String[]{SQLServer2008SpatialDialect.class.getCanonicalName()};
}
}
Property changes:
Added: svn:keywords
+ "Id"
/trunk/hibernate-spatial-sqlserver/src/main/java/org/hibernatespatial/sqlserver/SQLServer2008SpatialDialect.java
New file
0,0 → 1,64
/*
* $Id$
*
* This file is part of Hibernate Spatial, an extension to the
* hibernate ORM solution for geographic data.
*
* Copyright © 2009 Geovise BVBA
*
* This work was partially supported by the European Commission,
* under the 6th Framework Programme, contract IST-2-004688-STP.
*
* This library 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; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* For more information, visit: http://www.hibernatespatial.org/
*/
 
package org.hibernatespatial.sqlserver;
 
import org.hibernatespatial.SpatialDialect;
import org.hibernate.dialect.SQLServerDialect;
import org.hibernate.usertype.UserType;
 
/**
* @author Karel Maesen, Geovise BVBA.
* Date: Nov 2, 2009
*/
public class SQLServer2008SpatialDialect extends SQLServerDialect implements SpatialDialect {
 
public String getSpatialRelateSQL(String s, int i, boolean b) {
return null; //To change body of implemented methods use File | Settings | File Templates.
}
 
public String getSpatialFilterExpression(String s) {
return null; //To change body of implemented methods use File | Settings | File Templates.
}
 
public UserType getGeometryUserType() {
return new SQLServer2008GeometryUserType();
}
 
public String getSpatialAggregateSQL(String s, int i) {
return null; //To change body of implemented methods use File | Settings | File Templates.
}
 
public String getDbGeometryTypeName() {
return "GEOMETRY";
}
 
public boolean isTwoPhaseFiltering() {
return false;
}
}
Property changes:
Added: svn:keywords
+ "Id"
/trunk/hibernate-spatial-sqlserver/src/main/java/org/hibernatespatial/sqlserver/convertors/Encoder.java
New file
0,0 → 1,43
/*
* $Id$
*
* This file is part of Hibernate Spatial, an extension to the
* hibernate ORM solution for geographic data.
*
* Copyright © 2009 Geovise BVBA
*
* This work was partially supported by the European Commission,
* under the 6th Framework Programme, contract IST-2-004688-STP.
*
* This library 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; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* For more information, visit: http://www.hibernatespatial.org/
*/
 
package org.hibernatespatial.sqlserver.convertors;
 
import com.vividsolutions.jts.geom.Geometry;
 
/**
* @author Karel Maesen, Geovise BVBA.
* Date: Nov 2, 2009
*/
public interface Encoder<T extends Geometry> {
 
public SqlGeometryV1 encode(T geom);
 
public boolean accepts(Geometry geom);
 
}
Property changes:
Added: svn:keywords
+ "Id"
/trunk/hibernate-spatial-sqlserver/src/main/java/org/hibernatespatial/sqlserver/convertors/PointDecoder.java
New file
0,0 → 1,68
/*
* $Id$
*
* This file is part of Hibernate Spatial, an extension to the
* hibernate ORM solution for geographic data.
*
* Copyright © 2009 Geovise BVBA
*
* This work was partially supported by the European Commission,
* under the 6th Framework Programme, contract IST-2-004688-STP.
*
* This library 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; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* For more information, visit: http://www.hibernatespatial.org/
*/
 
package org.hibernatespatial.sqlserver.convertors;
 
import com.vividsolutions.jts.geom.Point;
import com.vividsolutions.jts.geom.Coordinate;
import org.hibernatespatial.mgeom.MGeometryFactory;
 
/**
* @author Karel Maesen, Geovise BVBA.
* Date: Nov 2, 2009
*/
class PointDecoder implements Decoder<Point> {
 
//TODO -- get GeometryFactory from HSExtension
private final MGeometryFactory geometryFactory = new MGeometryFactory();
 
PointDecoder() {
//TODO -- check how to construct these items.
}
 
public Point decode(SqlGeometryV1 sqlNative) {
if (!accepts(sqlNative))
throw new IllegalArgumentException("Point convertor received object of type " + sqlNative.openGisType());
if (sqlNative.isEmpty())
return geometryFactory.createPoint((Coordinate) null);
Point result = geometryFactory.createPoint(sqlNative.getCoordinate(0));
setSrid(sqlNative, result);
return result;
}
 
private void setSrid(SqlGeometryV1 sqlNative, Point result) {
if (sqlNative.getSrid() != null)
result.setSRID(sqlNative.getSrid());
}
 
public boolean accepts(SqlGeometryV1 sqlNative) {
return (sqlNative.openGisType() == OpenGisType.POINT);
}
 
 
}
Property changes:
Added: svn:keywords
+ "Id"
/trunk/hibernate-spatial-sqlserver/src/main/java/org/hibernatespatial/sqlserver/convertors/Encoders.java
New file
0,0 → 1,65
/*
* $Id$
*
* This file is part of Hibernate Spatial, an extension to the
* hibernate ORM solution for geographic data.
*
* Copyright © 2009 Geovise BVBA
*
* This work was partially supported by the European Commission,
* under the 6th Framework Programme, contract IST-2-004688-STP.
*
* This library 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; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* For more information, visit: http://www.hibernatespatial.org/
*/
 
package org.hibernatespatial.sqlserver.convertors;
 
import com.vividsolutions.jts.geom.Geometry;
 
import java.util.List;
import java.util.ArrayList;
 
/**
* @author Karel Maesen, Geovise BVBA.
* Date: Nov 2, 2009
*/
public class Encoders {
 
final private static List<Encoder<? extends Geometry>> ENCODERS = new ArrayList<Encoder<? extends Geometry>>();
 
 
static {
//Encoders
ENCODERS.add(new PointEncoder());
 
}
 
private static Encoder<? extends Geometry> encoderFor(Geometry geom) {
for (Encoder<? extends Geometry> encoder : ENCODERS) {
if (encoder.accepts(geom))
return encoder;
}
throw new IllegalArgumentException("No encoder for type " + geom.getGeometryType());
}
 
public static <T extends Geometry> byte[] encode(T geom) {
Encoder<T> encoder = (Encoder<T>) encoderFor(geom);
SqlGeometryV1 nativeSql = encoder.encode(geom);
return SqlGeometryV1.store(nativeSql);
}
 
}
Property changes:
Added: svn:keywords
+ "Id"
/trunk/hibernate-spatial-sqlserver/src/main/java/org/hibernatespatial/sqlserver/convertors/SqlGeometryV1.java
New file
0,0 → 1,278
/*
* $Id$
*
* This file is part of Hibernate Spatial, an extension to the
* hibernate ORM solution for geographic data.
*
* Copyright © 2009 Geovise BVBA
*
* This work was partially supported by the European Commission,
* under the 6th Framework Programme, contract IST-2-004688-STP.
*
* This library 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; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* For more information, visit: http://www.hibernatespatial.org/
*/
 
package org.hibernatespatial.sqlserver.convertors;
 
import org.hibernatespatial.mgeom.MCoordinate;
 
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
 
import com.vividsolutions.jts.geom.Coordinate;
 
/**
* @author Karel Maesen, Geovise BVBA.
* Date: Nov 2, 2009
*/
class SqlGeometryV1 {
 
public static final byte SUPPORTED_VERSION = 1;
 
private static final byte hasZValuesMask = 1;
private static final byte hasMValuesMask = 2;
private static final byte isValidMask = 4;
private static final byte isSinglePointMask = 8;
private static final byte isSingleLineSegment = 16;
 
private ByteBuffer buffer;
private Integer srid;
private byte version;
private byte serializationPropertiesByte;
private int numberOfPoints;
private Point[] points;
private double[] mValues;
private double[] zValues;
 
 
private SqlGeometryV1(byte[] bytes) {
buffer = ByteBuffer.wrap(bytes);
buffer.order(ByteOrder.LITTLE_ENDIAN);
}
 
public SqlGeometryV1() {
}
 
//TODO -- refactor: all iterations in separate methods.
public static byte[] store(SqlGeometryV1 sqlNative) {
int capacity = sqlNative.calculateCapacity();
ByteBuffer buffer = ByteBuffer.allocate(capacity);
buffer.order(ByteOrder.LITTLE_ENDIAN);
buffer.putInt(sqlNative.srid);
buffer.put(SUPPORTED_VERSION);
buffer.put(sqlNative.serializationPropertiesByte);
if (!sqlNative.isSinglePoint() && !sqlNative.isSingleLineSegment()) {
buffer.putInt(sqlNative.numberOfPoints);
}
for (int i = 0; i < sqlNative.points.length; i++) {
buffer.putDouble(sqlNative.points[i].x);
buffer.putDouble(sqlNative.points[i].y);
}
if (sqlNative.hasZValues()) {
for (int i = 0; i < sqlNative.zValues.length; i++) {
buffer.putDouble(sqlNative.zValues[i]);
}
}
if (sqlNative.hasMValues()) {
for (int i = 0; i < sqlNative.mValues.length; i++) {
buffer.putDouble(sqlNative.mValues[i]);
}
}
return buffer.array();
}
 
public static SqlGeometryV1 load(byte[] bytes) {
SqlGeometryV1 result = new SqlGeometryV1(bytes);
result.parse();
return result;
}
 
public MCoordinate getCoordinate(int index) {
MCoordinate coordinate = new MCoordinate();
coordinate.x = points[index].x;
coordinate.y = points[index].y;
if (hasZValues()) coordinate.z = zValues[index];
if (hasMValues()) coordinate.m = mValues[index];
return coordinate;
}
 
public void setCoordinate(int index, Coordinate coordinate) {
if (points == null) {
initPointArrays();
}
Point pnt = new Point(coordinate.x, coordinate.y);
points[index] = pnt;
if (hasZValues()) {
zValues[index] = coordinate.z;
}
if (hasMValues()) {
mValues[index] = ((MCoordinate) coordinate).m;
}
}
 
public boolean isEmpty() {
return this.numberOfPoints == 0;
}
 
public OpenGisType openGisType() {
if (isValid() && getNumPoints() == 1)
return OpenGisType.POINT;
return OpenGisType.INVALD_TYPE;
}
 
public void setHasZValues() {
serializationPropertiesByte |= hasZValuesMask;
}
 
public void setHasMValues() {
serializationPropertiesByte |= hasMValuesMask;
}
 
public void setIsValid() {
serializationPropertiesByte |= isValidMask;
}
 
public void setIsSinglePoint() {
serializationPropertiesByte |= isSinglePointMask;
}
 
public void setIsSingleLineSegment() {
serializationPropertiesByte |= isSingleLineSegment;
}
 
public int getNumPoints() {
return this.numberOfPoints;
}
 
public void setNumberOfPoints(int num) {
this.numberOfPoints = num;
}
 
public void initPointArrays() {
this.points = new Point[this.numberOfPoints];
if (hasMValues())
this.mValues = new double[this.numberOfPoints];
if (hasZValues())
this.zValues = new double[this.numberOfPoints];
}
 
private void parse() {
srid = buffer.getInt();
version = buffer.get();
//TODO -- create a specific parse exception for this.
if (!isCompatible())
throw new IllegalStateException("Version mismatch. Expected version " + SUPPORTED_VERSION + ", but received version " + version);
serializationPropertiesByte = buffer.get();
determineNumberOfPoints();
readPoints();
if (hasZValues())
readZValues();
if (hasMValues())
readMValues();
}
 
private int calculateCapacity() {
if (openGisType() == OpenGisType.POINT) {
int capacity = 22;
if (hasZValues())
capacity += 8;
if (hasMValues())
capacity += 8;
return capacity;
}
throw new IllegalArgumentException("Can't determine the capacity for type " + openGisType());
}
 
private void readPoints() {
points = new Point[numberOfPoints];
for (int i = 0; i < numberOfPoints; i++) {
double x = buffer.getDouble();
double y = buffer.getDouble();
points[i] = new Point(x, y);
}
}
 
private void readZValues() {
zValues = new double[numberOfPoints];
for (int i = 0; i < numberOfPoints; i++) {
zValues[i] = buffer.getDouble();
}
}
 
 
private void readMValues() {
mValues = new double[numberOfPoints];
for (int i = 0; i < numberOfPoints; i++) {
mValues[i] = buffer.getDouble();
}
}
 
private void determineNumberOfPoints() {
if (isSinglePoint()) {
numberOfPoints = 1;
return;
}
if (isSingleLineSegment()) {
numberOfPoints = 2;
return;
}
numberOfPoints = buffer.getInt();
}
 
private boolean isCompatible() {
return version == SUPPORTED_VERSION;
}
 
public void setSrid(Integer srid) {
this.srid = (srid == null) ? -1 : srid;
}
 
public Integer getSrid() {
return srid != -1 ? srid : null;
}
 
private boolean hasZValues() {
return (serializationPropertiesByte & hasZValuesMask) != 0;
}
 
private boolean hasMValues() {
return (serializationPropertiesByte & hasMValuesMask) != 0;
}
 
private boolean isValid() {
return (serializationPropertiesByte & isValidMask) != 0;
}
 
private boolean isSinglePoint() {
return (serializationPropertiesByte & isSinglePointMask) != 0;
}
 
private boolean isSingleLineSegment() {
return (serializationPropertiesByte & isSingleLineSegment) != 0;
}
 
private static class Point {
final double x;
final double y;
 
Point(double x, double y) {
this.x = x;
this.y = y;
}
 
}
}
Property changes:
Added: svn:keywords
+ "Id"
/trunk/hibernate-spatial-sqlserver/src/main/java/org/hibernatespatial/sqlserver/convertors/Decoder.java
New file
0,0 → 1,44
/*
* $Id$
*
* This file is part of Hibernate Spatial, an extension to the
* hibernate ORM solution for geographic data.
*
* Copyright © 2009 Geovise BVBA
*
* This work was partially supported by the European Commission,
* under the 6th Framework Programme, contract IST-2-004688-STP.
*
* This library 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; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* For more information, visit: http://www.hibernatespatial.org/
*/
 
package org.hibernatespatial.sqlserver.convertors;
 
import com.vividsolutions.jts.geom.Geometry;
 
/**
* @author Karel Maesen, Geovise BVBA.
* Date: Nov 2, 2009
*/
public interface Decoder<T extends Geometry> {
 
public T decode(SqlGeometryV1 nativeGeom);
 
boolean accepts(SqlGeometryV1 nativeGeom);
 
 
}
Property changes:
Added: svn:keywords
+ "Id"
/trunk/hibernate-spatial-sqlserver/src/main/java/org/hibernatespatial/sqlserver/convertors/OpenGisType.java
New file
0,0 → 1,44
/*
* $Id$
*
* This file is part of Hibernate Spatial, an extension to the
* hibernate ORM solution for geographic data.
*
* Copyright © 2009 Geovise BVBA
*
* This work was partially supported by the European Commission,
* under the 6th Framework Programme, contract IST-2-004688-STP.
*
* This library 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; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* For more information, visit: http://www.hibernatespatial.org/
*/
 
package org.hibernatespatial.sqlserver.convertors;
 
/**
* @author Karel Maesen, Geovise BVBA.
* Date: Nov 2, 2009
*/
enum OpenGisType {
POINT,
LINESTRING,
POLYGON,
MULTIPOINT,
MULTILINESTRING,
MLINESTRING,
MULTIMLINESTRING,
INVALD_TYPE
}
Property changes:
Added: svn:keywords
+ "Id"
/trunk/hibernate-spatial-sqlserver/src/main/java/org/hibernatespatial/sqlserver/convertors/Decoders.java
New file
0,0 → 1,83
/*
* $Id$
*
* This file is part of Hibernate Spatial, an extension to the
* hibernate ORM solution for geographic data.
*
* Copyright © 2009 Geovise BVBA
*
* This work was partially supported by the European Commission,
* under the 6th Framework Programme, contract IST-2-004688-STP.
*
* This library 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; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* For more information, visit: http://www.hibernatespatial.org/
*/
 
package org.hibernatespatial.sqlserver.convertors;
 
import com.vividsolutions.jts.geom.Geometry;
 
import java.util.List;
import java.util.ArrayList;
 
/**
* @author Karel Maesen, Geovise BVBA.
* Date: Nov 2, 2009
*/
public class Decoders {
 
final private static List<Decoder<? extends Geometry>> DECODERS = new ArrayList<Decoder<? extends Geometry>>();
final private static List<Encoder<? extends Geometry>> ENCODERS = new ArrayList<Encoder<? extends Geometry>>();
 
 
static {
//Decoders
DECODERS.add(new PointDecoder());
 
//Encoders
ENCODERS.add(new PointEncoder());
 
}
 
 
private static Decoder<? extends Geometry> decoderFor(SqlGeometryV1 object) {
for (Decoder<? extends Geometry> decoder : DECODERS) {
if (decoder.accepts(object))
return decoder;
}
throw new IllegalArgumentException("No decoder for type " + object.openGisType());
}
 
private static Encoder<? extends Geometry> encoderFor(Geometry geom) {
for (Encoder<? extends Geometry> encoder : ENCODERS) {
if (encoder.accepts(geom))
return encoder;
}
throw new IllegalArgumentException("No encoder for type " + geom.getGeometryType());
}
 
public static Geometry decode(byte[] raw) {
SqlGeometryV1 sqlGeom = SqlGeometryV1.load(raw);
Decoder<?> decoder = decoderFor(sqlGeom);
return decoder.decode(sqlGeom);
}
 
public static <T extends Geometry> SqlGeometryV1 encode(T geom) {
Encoder<T> encoder = (Encoder<T>) encoderFor(geom);
return encoder.encode(geom);
}
 
}
Property changes:
Added: svn:keywords
+ "Id"
/trunk/hibernate-spatial-sqlserver/src/main/java/org/hibernatespatial/sqlserver/convertors/PointEncoder.java
New file
0,0 → 1,72
/*
* $Id$
*
* This file is part of Hibernate Spatial, an extension to the
* hibernate ORM solution for geographic data.
*
* Copyright © 2009 Geovise BVBA
*
* This work was partially supported by the European Commission,
* under the 6th Framework Programme, contract IST-2-004688-STP.
*
* This library 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; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* For more information, visit: http://www.hibernatespatial.org/
*/
 
package org.hibernatespatial.sqlserver.convertors;
 
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.Point;
import com.vividsolutions.jts.geom.Coordinate;
import org.hibernatespatial.mgeom.MGeometry;
import org.hibernatespatial.mgeom.MCoordinate;
 
/**
* @author Karel Maesen, Geovise BVBA.
* Date: Nov 2, 2009
*/
class PointEncoder implements Encoder<Point> {
 
public SqlGeometryV1 encode(Point geom) {
SqlGeometryV1 sqlGeom = new SqlGeometryV1();
sqlGeom.setSrid(geom.getSRID());
sqlGeom.setIsSinglePoint();
sqlGeom.setIsValid();
sqlGeom.setNumberOfPoints(1);
Coordinate coordinate = geom.getCoordinate();
if (!is3DPoint(coordinate)) {
sqlGeom.setHasZValues();
}
if (!isMPoint(coordinate)) {
sqlGeom.setHasMValues();
}
sqlGeom.setCoordinate(0, coordinate);
return sqlGeom;
}
 
private boolean isMPoint(Coordinate coordinate) {
return (coordinate instanceof MCoordinate) &&
Double.isNaN(((MCoordinate) coordinate).m);
}
 
private boolean is3DPoint(Coordinate coordinate) {
return Double.isNaN(coordinate.z);
}
 
public boolean accepts(Geometry geom) {
return geom instanceof Point;
}
}
Property changes:
Added: svn:keywords
+ "Id"
/trunk/hibernate-spatial-sqlserver/src/main/java/org/hibernatespatial/sqlserver/SQLServer2008GeometryUserType.java
New file
0,0 → 1,64
/*
* $Id$
*
* This file is part of Hibernate Spatial, an extension to the
* hibernate ORM solution for geographic data.
*
* Copyright © 2009 Geovise BVBA
*
* This work was partially supported by the European Commission,
* under the 6th Framework Programme, contract IST-2-004688-STP.
*
* This library 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; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* For more information, visit: http://www.hibernatespatial.org/
*/
 
package org.hibernatespatial.sqlserver;
 
import org.hibernatespatial.AbstractDBGeometryType;
import org.hibernatespatial.sqlserver.convertors.Decoders;
import org.hibernatespatial.sqlserver.convertors.Encoders;
import com.vividsolutions.jts.geom.Geometry;
 
import java.sql.Connection;
import java.sql.Types;
 
/**
* @author Karel Maesen, Geovise BVBA.
* Date: Nov 2, 2009
*/
public class SQLServer2008GeometryUserType extends AbstractDBGeometryType {
 
public Geometry convert2JTS(Object obj) {
if (obj == null)
return null;
if (!(obj instanceof byte[])) {
throw new IllegalArgumentException("Expected byte array."); //TODO -- improve errorhandling
}
byte[] raw = (byte[]) obj;
return Decoders.decode(raw);
}
 
public Object conv2DBGeometry(Geometry geom, Connection connection) {
if (geom == null)
throw new IllegalArgumentException("Null geometry passed."); //TODO -- is this correct here?
return Encoders.encode(geom);
}
 
public int[] sqlTypes() {
return new int[]{Types.ARRAY};
}
}
Property changes:
Added: svn:keywords
+ "Id"
/trunk/hibernate-spatial-sqlserver/src/main/resources/META-INF/services/org.hibernatespatial.spi.SpatialDialectProvider
New file
0,0 → 1,0
org.hibernatespatial.sqlserver.DialectProvider
Property changes:
Added: svn:keywords
+ "Id"
/trunk/hibernate-spatial-sqlserver/pom.xml
New file
0,0 → 1,47
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 
<!-- $Id$ -->
 
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.hibernatespatial</groupId>
<artifactId>hibernate-spatial-maven</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../hibernate-spatial-maven</relativePath>
</parent>
<artifactId>hibernate-spatial-sqlserver</artifactId>
<packaging>jar</packaging>
<name>Microsoft SQLServer DialectProvider</name>
<url>http://www.hibernatespatial.org/hibernate-spatial-sqlserver</url>
<description>
A dialect provider for Microsoft SQLServer 2008.
</description>
 
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>hibernate-spatial</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>hibernate-spatial</artifactId>
<version>${project.version}</version>
<classifier>tests</classifier>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.microsoft</groupId>
<artifactId>sqljdbc</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.2.2</version>
</dependency>
</dependencies>
 
</project>
Property changes:
Added: svn:keywords
+ "Id"
/trunk/hibernate-spatial-sqlserver
Property changes:
Added: svn:ignore
+ hibernate-spatial-sqlserver.ipr
hibernate-spatial-sqlserver.iml