PostGIS geometry

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

PostGIS geometry

Benoit PESTY

Hello,

 

I have made a dirty modification to the PostGreSQL datatype in order to work with PostGIS geometry objects.

 

I have created a new data type “GeometryType” and added a new line in the PostgresqlDataTypeFactory.

 

It would be nice if this could be added in the distribution (Sorry, I should propose a patch but I work with an old version of DBUnit for some obscure reasons and it would be a lot of work).

 

Best regards,

 

Benoit Pesty

 

-------------------

 

package org.dbunit.ext.postgresql;

 

import java.lang.reflect.Constructor;

import java.lang.reflect.InvocationTargetException;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Types;

 

import org.dbunit.dataset.datatype.AbstractDataType;

import org.dbunit.dataset.datatype.TypeCastException;

 

/**

 * Adapter to handle conversion between PostGIS Geometry type and Strings.

*/

public class GeometryType extends AbstractDataType {

 

      public GeometryType() {

            super("geometry", Types.OTHER, String.class, false);

      }

 

      public Object getSqlValue(int column, ResultSet resultSet) throws SQLException, TypeCastException {

            return resultSet.getString(column);

      }

 

      public void setSqlValue(Object geom, int column, PreparedStatement statement) throws SQLException, TypeCastException {

            statement.setObject(column, getGeometry(geom, statement.getConnection()));

      }

 

      public Object typeCast(Object arg0) throws TypeCastException {

            return arg0.toString();

      }

 

      private Object getGeometry(Object value, Connection connection) throws TypeCastException {

 

            Object tempgeom = null;

 

            try {

                  Class aPGIntervalClass = super.loadClass("org.postgis.PGgeometry", connection);

                  Constructor ct = aPGIntervalClass.getConstructor(new Class[] { String.class });

                  tempgeom = ct.newInstance(new Object[] { value });

            } catch (ClassNotFoundException e) {

                  throw new TypeCastException(value, this, e);

            } catch (InvocationTargetException e) {

                  throw new TypeCastException(value, this, e);

            } catch (NoSuchMethodException e) {

                  throw new TypeCastException(value, this, e);

            } catch (IllegalAccessException e) {

                  throw new TypeCastException(value, this, e);

            } catch (InstantiationException e) {

                  throw new TypeCastException(value, this, e);

            }

 

            return tempgeom;

      }

}



The New Busy think 9 to 5 is a cute idea. Combine multiple calendars with Hotmail. Get busy.
------------------------------------------------------------------------------

_______________________________________________
dbunit-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/dbunit-user
Reply | Threaded
Open this post in threaded view
|

Re: PostGIS geometry

Jeff Jensen-2

Thanks…

You should add it to an enhancement request so it does not get lost in the email history until someone can get to it.

Additionally, do you have tests to add with it?  Confidence is higher for adding with tests.

 

 

From: Benoit PESTY [mailto:[hidden email]]
Sent: Tuesday, April 13, 2010 8:49 AM
To: [hidden email]
Subject: [dbunit-user] PostGIS geometry

 

Hello,

 

I have made a dirty modification to the PostGreSQL datatype in order to work with PostGIS geometry objects.

 

I have created a new data type “GeometryType” and added a new line in the PostgresqlDataTypeFactory.

 

It would be nice if this could be added in the distribution (Sorry, I should propose a patch but I work with an old version of DBUnit for some obscure reasons and it would be a lot of work).

 

Best regards,

 

Benoit Pesty

 

-------------------

 

package org.dbunit.ext.postgresql;

 

import java.lang.reflect.Constructor;

import java.lang.reflect.InvocationTargetException;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Types;

 

import org.dbunit.dataset.datatype.AbstractDataType;

import org.dbunit.dataset.datatype.TypeCastException;

 

/**

 * Adapter to handle conversion between PostGIS Geometry type and Strings.

*/

public class GeometryType extends AbstractDataType {

 

      public GeometryType() {

            super("geometry", Types.OTHER, String.class, false);

      }

 

      public Object getSqlValue(int column, ResultSet resultSet) throws SQLException, TypeCastException {

            return resultSet.getString(column);

      }

 

      public void setSqlValue(Object geom, int column, PreparedStatement statement) throws SQLException, TypeCastException {

            statement.setObject(column, getGeometry(geom, statement.getConnection()));

      }

 

      public Object typeCast(Object arg0) throws TypeCastException {

            return arg0.toString();

      }

 

      private Object getGeometry(Object value, Connection connection) throws TypeCastException {

 

            Object tempgeom = null;

 

            try {

                  Class aPGIntervalClass = super.loadClass("org.postgis.PGgeometry", connection);

                  Constructor ct = aPGIntervalClass.getConstructor(new Class[] { String.class });

                  tempgeom = ct.newInstance(new Object[] { value });

            } catch (ClassNotFoundException e) {

                  throw new TypeCastException(value, this, e);

            } catch (InvocationTargetException e) {

                  throw new TypeCastException(value, this, e);

            } catch (NoSuchMethodException e) {

                  throw new TypeCastException(value, this, e);

            } catch (IllegalAccessException e) {

                  throw new TypeCastException(value, this, e);

            } catch (InstantiationException e) {

                  throw new TypeCastException(value, this, e);

            }

 

            return tempgeom;

      }

}

 


The New Busy think 9 to 5 is a cute idea. Combine multiple calendars with Hotmail. Get busy.


------------------------------------------------------------------------------

_______________________________________________
dbunit-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/dbunit-user