You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user-java@ibatis.apache.org by Westgård Trond <tr...@imr.no> on 2008/10/10 10:18:02 UTC

IBATIS and PostGIS and the geometry datatype

I have problems to insert/update  geometry objects in my PostgreSQL/PostGIS database using IBATIS.

 

I have written a TypeHandlerCallback class and managed to write an example that gets point geometries out from the database into my VO object,

But then it comes to a "complete stop".

 

I have looked into the hibernatespatial project that add handling of geometries to the hibernate ORM tool.

 

Has anyone done something similar for IBATIS ?

 

Please help me someone.

 

Best regards

 

Trond Westgård

Senior engineer

Institute of Marine Research

Bergen, Norway

 


RE: IBATIS and PostGIS and the geometry datatype

Posted by Westgård Trond <tr...@imr.no>.
What I meant with a "complete stop" was that I did run out of ideas. I hope someone that uses iBATIS also has experience with PostGIS and maybe get an example source code of a functioning TypeHandlerCallBack.

Here is mine:

package ff.classes;

import com.ibatis.sqlmap.client.extensions.ParameterSetter;
import com.ibatis.sqlmap.client.extensions.ResultGetter;
import com.ibatis.sqlmap.client.extensions.TypeHandlerCallback;
import com.ibatis.sqlmap.engine.type.ResultGetterImpl;
import com.vividsolutions.jts.io.ParseException;
import java.sql.SQLException;
import java.sql.Types;
//import java.util.logging.Level;
//import java.util.logging.Logger;

//import org.postgis.PGgeometry;
//import org.postgis.Geometry;
import org.postgresql.util.PGobject;
//import org.postgresql.geometric.PGpoint;
import com.vividsolutions.jts.geom.Geometry;
//import com.vividsolutions.jts.io.WKTReader;
//import com.vividsolutions.jts.io.WKBReader;
import org.postgis.jts.JtsBinaryParser;

public class GeometryTypehandler implements TypeHandlerCallback {

    public void setParameter(ParameterSetter setter, Object parameter) throws SQLException {
        // DOES NOT WORK
        if (parameter == null) {
            setter.setNull(Types.NULL);
        } else {
            setter.setObject(parameter);
        }
    }

    public Object getResult(ResultGetter getter) throws SQLException {
        // WORKS ....
        String classname = getter.getClass().getName().toString();
        if (classname.equals("com.ibatis.sqlmap.engine.type.ResultGetterImpl")) {
            ResultGetterImpl myrgimpl = (ResultGetterImpl) getter;
            byte mybytes[] = myrgimpl.getBytes();
            if (mybytes.length < 1) {
                return null;
            }
            JtsBinaryParser myParser = new JtsBinaryParser();
            String sgeom = "";
            String sb = "";
            for (int i = 0; i < mybytes.length; i++) {
                byte b = mybytes[i];
                int bi = b;
                switch (bi) {
                    case 48: sb = "0";break;
                    case 49: sb = "1";break;
                    case 50: sb = "2";break;
                    case 51: sb = "3";break;
                    case 52: sb = "4";break;
                    case 53: sb = "5";break;
                    case 54: sb = "6";break;
                    case 55: sb = "7";break;
                    case 56: sb = "8";break;
                    case 57: sb = "9";break;
                    case 65: sb = "A";break;
                    case 66: sb = "B";break;
                    case 67: sb = "C";break;
                    case 68: sb = "D";break;
                    case 69: sb = "E";break;
                    case 70: sb = "F";break;
                    default: sb = "X";
                }
                sgeom = sgeom + sb;
            }
            Geometry mygeom = myParser.parse(sgeom);
            // Geometry mygeom = myParser.parse(mybytes); // DOES NOT WORK !!!!!!??????
            return mygeom;
        } else {
            Object value = (Object) getter.getObject();
            if (getter.wasNull()) {
                return null;
            }
            Class myClass = value.getClass();
            //String myClassPackage = myClass.getPackage().getName(); // RETURNS: org.postgis
            String myClassName = myClass.getName(); // RETURNS: org.postgresql.util.PGobject
            if (myClassName.equals("org.postgresql.util.PGobject")) {
                PGobject go = (PGobject) value;
                String govalue = go.getValue();
                JtsBinaryParser myParser = new JtsBinaryParser();
                Geometry mygeom = myParser.parse(govalue);
                // double lon = mygeom.getCoordinate().x;
                // double lat = mygeom.getCoordinate().y;
                // int srid = mygeom.getSRID();
                return mygeom;
            } else {
                return null;
            }
        }

    }

    public Object valueOf(String s) {
        return s;
    }
}

Regards

Trond 

-----Opprinnelig melding-----
Fra: Larry Meadors [mailto:larry.meadors@gmail.com] 
Sendt: 10. oktober 2008 14:01
Til: user-java@ibatis.apache.org
Emne: Re: IBATIS and PostGIS and the geometry datatype

Can you explain what you mean by "complete stop"?

Mass confusion? Runtime exception? Server crash? Black hole?

Larry


On Fri, Oct 10, 2008 at 2:18 AM, Westgård Trond <tr...@imr.no> wrote:
>
> But then it comes to a "complete stop".
>

Re: IBATIS and PostGIS and the geometry datatype

Posted by Larry Meadors <la...@gmail.com>.
Can you explain what you mean by "complete stop"?

Mass confusion? Runtime exception? Server crash? Black hole?

Larry


On Fri, Oct 10, 2008 at 2:18 AM, Westgård Trond <tr...@imr.no> wrote:
>
> But then it comes to a "complete stop".
>