postgis/postgis-java

Can not cast the geography columns

p0mp0k0 opened this issue · 4 comments

JDBC driver can not support new Geography type, and can not cast to a correctly java object.
eg. POINTZ

Which class are you attempting to cast to?

I also want support for the Geography type, as in a PGgeography type with all the associated types, linestring etc

It appears that the well known text format and binary formats for Geography and Geometry are identical.

I have tried 3 options so far.
The first was

def addGeographies(conn: Connection ) {
val pgconn = conn.asInstanceOf[PGConnection]
val geogClass=classOf[PGgeometry]
pgconn.addDataType("geography", geogClass);
pgconn.addDataType("public.geography", geogClass);
pgconn.addDataType(""public"."geography"", geogClass);
}

This works, it loads the geography type as a geometry and it would be up to the client to know that it really is a Geography

The second that also works is that I made a sub package geography and copied relevant geometry classes and basically changed the names from geometry to geography. That works and I can load the PGgeometry,

geography.zip

However as it is basically copy paste and edit it would be better to go the generic route, to not duplicate code that is essentially the same.

My idea was to define interface Geospatial, abstract class AbstractGeospatial with methods common to Geometry and Geography and then have those 2 classes extends AbstractGeospatial.


Then you have Point<Geospatial> which would either be a Point<Geography> or a Point<Geometry>

However I ran into a problem with that as Point<Geospatial> is also a Geospatial as I have it defined
so you could have a Point<Point<Geospatial>> which doesnt make sense.

genericGeography.zip

However this although it compiles is not complete,

I believe a super type would need to be created to avoid runtime type erasure of whether it is a Geometry or a Geography so that the relevant type can be instantiated.

http://gafter.blogspot.com/2006/12/super-type-tokens.html

Id like to propose that as a start, but I have just realized that my skills at writing a generic library are not so good. Would anyone else be in to helping?

I am attaching a patch as well of my efforts so far

geographyPatch.txt

With the merging of changes on pull request #74, queries returning geography types within the database can now be read in as geometry instances, but the client code will have to track if the query was for geometry or geography. This is not a perfect solution, but at least it's not possible.

The current geometry class hierarchy is very old (almost 2 decades!) and would need a lot of work to be retrofitted to support newer features (such as #54 and #61). All thing considered, the best option on the table currently is to completely redesign and reimplement a new modern class hierarchy, and there is already work that has recently been done to do exactly this (ref work by @sebasbaumh on https://github.com/sebasbaumh/postgis-java-ng).

I hope to revive discussions about moving forward with this in the near future.

merged geography type support with pull request #80