Skip to content

Commit

Permalink
Merge branch 'TEIID-3305' of https://github.com/Tom9729/teiid
Browse files Browse the repository at this point in the history
  • Loading branch information
shawkins committed Jan 27, 2015
2 parents 9a6a5fe + 1f45f4f commit 446634b
Show file tree
Hide file tree
Showing 12 changed files with 455 additions and 154 deletions.
Expand Up @@ -167,8 +167,12 @@ public class SourceSystemFunctions {

public static final String ST_ASTEXT = "st_astext"; //$NON-NLS-1$
public static final String ST_ASBINARY = "st_asbinary"; //$NON-NLS-1$
public static final String ST_ASGEOJSON = "st_asgeojson"; //$NON-NLS-1$
public static final String ST_ASGML = "st_asgml"; //$NON-NLS-1$
public static final String ST_GEOMFROMTEXT = "st_geomfromtext"; //$NON-NLS-1$
public static final String ST_GEOMFROMWKB = "st_geomfromwkb"; //$NON-NLS-1$
public static final String ST_GEOMFROMGEOJSON = "st_geomfromgeojson"; //$NON-NLS-1$
public static final String ST_GEOMFROMGML = "st_geomfromgml"; //$NON-NLS-1$
public static final String ST_INTERSECTS = "st_intersects"; //$NON-NLS-1$
public static final String ST_CONTAINS = "st_contains"; //$NON-NLS-1$
public static final String ST_CROSSES = "st_crosses"; //$NON-NLS-1$
Expand Down
Expand Up @@ -2,5 +2,7 @@
<module xmlns="urn:jboss:module:1.0" name="com.fasterxml.jackson">
<resources>
<resource-root path="jackson-core-${version.com.fasterxml.jackson.core}.jar" />
<resource-root path="jackson-databind-${version.com.fasterxml.jackson.core}.jar" />
<resource-root path="jackson-annotations-${version.com.fasterxml.jackson.core}.jar" />
</resources>
</module>
Expand Up @@ -8,6 +8,7 @@
<resource-root path="Saxon-HE-${version.net.sourceforge.saxon}.jar" />
<resource-root path="nux-${version.nux}.jar" />
<resource-root path="jts-${version.jts}.jar" />
<resource-root path="jts2geojson-${version.jts2geojson}.jar" />

<resource-root path="deployments" />
</resources>
Expand Down Expand Up @@ -44,7 +45,8 @@
<module name="org.jboss.marshalling"/>
<module name="org.jboss.marshalling.river"/>
<module name="org.jboss.as.ee"/>
<module name="org.jboss.metadata"/>
<module name="org.jboss.metadata"/>
<module name="com.fasterxml.jackson"/>
<!-- These dependencies here for ra.xml description -->
<module name="org.jboss.as.connector"/>
<module name="org.jboss.ironjacamar.api"/>
Expand Down
6 changes: 6 additions & 0 deletions engine/pom.xml
Expand Up @@ -123,6 +123,11 @@
<groupId>com.vividsolutions</groupId>
<artifactId>jts</artifactId>
</dependency>

<dependency>
<groupId>org.wololo</groupId>
<artifactId>jts2geojson</artifactId>
</dependency>

<dependency>
<groupId>xom</groupId>
Expand All @@ -133,6 +138,7 @@
<groupId>jaxen</groupId>
<artifactId>jaxen</artifactId>
</dependency>

</dependencies>

</project>
Expand Up @@ -51,11 +51,46 @@ public static BlobType asBlob(GeometryType geometry) {
return new BlobType(b);
}

@TeiidFunction(name=SourceSystemFunctions.ST_ASGEOJSON,
category=FunctionCategoryConstants.GEOMETRY,
nullOnNull=true)
public static ClobType asGeoJson(GeometryType geometry)
throws FunctionExecutionException {
return GeometryUtils.geometryToGeoJson(geometry);
}

@TeiidFunction(name=SourceSystemFunctions.ST_ASGML,
category=FunctionCategoryConstants.GEOMETRY,
nullOnNull=true)
public static ClobType asGml(GeometryType geometry)
throws FunctionExecutionException {
return GeometryUtils.geometryToGml(geometry);
}


@TeiidFunction(name=SourceSystemFunctions.ST_GEOMFROMTEXT,
category=FunctionCategoryConstants.GEOMETRY,
nullOnNull=true)
public static GeometryType geomFromText(ClobType wkt)
throws FunctionExecutionException {
return GeometryUtils.geometryFromClob(wkt);
}

@TeiidFunction(name=SourceSystemFunctions.ST_GEOMFROMTEXT,
category=FunctionCategoryConstants.GEOMETRY,
nullOnNull=true,
pushdown=PushDown.CAN_PUSHDOWN)
public static GeometryType geomFromText(ClobType wkt, int srid)
throws FunctionExecutionException {
return GeometryUtils.geometryFromClob(wkt, srid);
}

@TeiidFunction(name=SourceSystemFunctions.ST_GEOMFROMWKB,
category=FunctionCategoryConstants.GEOMETRY,
nullOnNull=true,
alias="ST_GEOMFROMBINARY")
public static GeometryType geoFromBlob(BlobType wkb) throws FunctionExecutionException {
public static GeometryType geoFromBlob(BlobType wkb)
throws FunctionExecutionException {
return GeometryUtils.geometryFromBlob(wkb);
}

Expand All @@ -64,23 +99,41 @@ public static GeometryType geoFromBlob(BlobType wkb) throws FunctionExecutionExc
pushdown=PushDown.CAN_PUSHDOWN,
nullOnNull=true,
alias="ST_GEOMFROMBINARY")
public static GeometryType geoFromBlob(BlobType wkb, int srid) throws FunctionExecutionException {
public static GeometryType geoFromBlob(BlobType wkb, int srid)
throws FunctionExecutionException {
return GeometryUtils.geometryFromBlob(wkb, srid);
}

@TeiidFunction(name=SourceSystemFunctions.ST_GEOMFROMGEOJSON,
category=FunctionCategoryConstants.GEOMETRY,
nullOnNull=true)
public static GeometryType geomFromGeoJson(ClobType clob)
throws FunctionExecutionException {
return GeometryUtils.geometryFromGeoJson(clob);
}

@TeiidFunction(name=SourceSystemFunctions.ST_GEOMFROMTEXT,
@TeiidFunction(name=SourceSystemFunctions.ST_GEOMFROMGEOJSON,
category=FunctionCategoryConstants.GEOMETRY,
nullOnNull=true)
public static GeometryType geomFromText(ClobType wkt) throws FunctionExecutionException {
return GeometryUtils.geometryFromClob(wkt);
public static GeometryType geomFromGeoJson(ClobType clob, int srid)
throws FunctionExecutionException {
return GeometryUtils.geometryFromGeoJson(clob, srid);
}

@TeiidFunction(name=SourceSystemFunctions.ST_GEOMFROMTEXT,
@TeiidFunction(name=SourceSystemFunctions.ST_GEOMFROMGML,
category=FunctionCategoryConstants.GEOMETRY,
nullOnNull=true,
pushdown=PushDown.CAN_PUSHDOWN)
public static GeometryType geomFromText(ClobType wkt, int srid) throws FunctionExecutionException {
return GeometryUtils.geometryFromClob(wkt, srid);
nullOnNull=true)
public static GeometryType geomFromGml(ClobType gml)
throws FunctionExecutionException {
return GeometryUtils.geometryFromGml(gml);
}

@TeiidFunction(name=SourceSystemFunctions.ST_GEOMFROMGML,
category=FunctionCategoryConstants.GEOMETRY,
nullOnNull=true)
public static GeometryType geomFromGml(ClobType gml, int srid)
throws FunctionExecutionException {
return GeometryUtils.geometryFromGml(gml, srid);
}

@TeiidFunction(name=SourceSystemFunctions.ST_INTERSECTS,
Expand Down
113 changes: 104 additions & 9 deletions engine/src/main/java/org/teiid/query/function/GeometryUtils.java
Expand Up @@ -22,6 +22,8 @@

package org.teiid.query.function;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
Expand All @@ -34,19 +36,29 @@
import org.teiid.core.types.GeometryType;

import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.io.InputStreamInStream;
import com.vividsolutions.jts.io.ParseException;
import com.vividsolutions.jts.io.WKBReader;
import com.vividsolutions.jts.io.WKBWriter;
import com.vividsolutions.jts.io.WKTReader;
import com.vividsolutions.jts.io.gml2.GMLReader;
import com.vividsolutions.jts.io.gml2.GMLWriter;
import javax.xml.parsers.ParserConfigurationException;
import org.wololo.geojson.GeoJSON;
import org.wololo.jts2geojson.GeoJSONReader;
import org.wololo.jts2geojson.GeoJSONWriter;
import org.xml.sax.SAXException;

/**
* Utility methods for geometry
* TODO: determine if we should use buffermanager to minimize memory footprint
* TODO: Split into GeometryFilterUtils and GeometryConvertUtils. - Tom
*/
public class GeometryUtils {

public static ClobType geometryToClob(GeometryType geometry) throws FunctionExecutionException {
public static ClobType geometryToClob(GeometryType geometry)
throws FunctionExecutionException {
Geometry jtsGeometry = getGeometry(geometry);
return new ClobType(new ClobImpl(jtsGeometry.toText()));
}
Expand All @@ -56,15 +68,14 @@ public static GeometryType geometryFromClob(ClobType wkt)
return geometryFromClob(wkt, GeometryType.UNKNOWN_SRID);
}

public static GeometryType geometryFromClob(ClobType wkt, int srid) throws FunctionExecutionException {
public static GeometryType geometryFromClob(ClobType wkt, int srid)
throws FunctionExecutionException {
Reader r = null;
try {
WKTReader reader = new WKTReader();
WKBWriter writer = new WKBWriter();
r = wkt.getCharacterStream();
Geometry jtsGeometry = reader.read(r);
byte[] bytes = writer.write(jtsGeometry);
return new GeometryType(bytes, srid);
return getGeometryType(jtsGeometry, srid);
} catch (ParseException e) {
throw new FunctionExecutionException(e);
} catch (SQLException e) {
Expand All @@ -78,7 +89,80 @@ public static GeometryType geometryFromClob(ClobType wkt, int srid) throws Funct
}
}
}


public static ClobType geometryToGeoJson(GeometryType geometry)
throws FunctionExecutionException {
Geometry jtsGeometry = getGeometry(geometry);
GeoJSONWriter writer = new GeoJSONWriter();
try {
GeoJSON geoJson = writer.write(jtsGeometry);
return new ClobType(new ClobImpl(geoJson.toString()));
} catch (Exception e) {
throw new FunctionExecutionException(e);
}
}

public static GeometryType geometryFromGeoJson(ClobType json)
throws FunctionExecutionException {
return geometryFromGeoJson(json, GeometryType.UNKNOWN_SRID);
}

public static GeometryType geometryFromGeoJson(ClobType json, int srid)
throws FunctionExecutionException {
try {
GeoJSONReader reader = new GeoJSONReader();
String jsonText = ClobType.getString(json);
Geometry jtsGeometry = reader.read(jsonText);
return getGeometryType(jtsGeometry);
} catch (SQLException e) {
throw new FunctionExecutionException(e);
} catch (IOException e) {
throw new FunctionExecutionException(e);
}
}

public static ClobType geometryToGml(GeometryType geometry)
throws FunctionExecutionException {
Geometry jtsGeometry = getGeometry(geometry);
GMLWriter writer = new GMLWriter();
String gmlText = writer.write(jtsGeometry);
return new ClobType(new ClobImpl(gmlText));
}

public static GeometryType geometryFromGml(ClobType gml)
throws FunctionExecutionException {
return geometryFromGml(gml, GeometryType.UNKNOWN_SRID);
}

public static GeometryType geometryFromGml(ClobType gml, int srid)
throws FunctionExecutionException {
GMLReader reader = new GMLReader();
GeometryFactory gf = new GeometryFactory();
Geometry jtsGeometry = null;
Reader r = null;
try {
r = gml.getCharacterStream();
jtsGeometry = reader.read(r, gf);
} catch (IOException e) {
throw new FunctionExecutionException(e);
} catch (ParserConfigurationException e) {
throw new FunctionExecutionException(e);
} catch (SAXException e) {
throw new FunctionExecutionException(e);
} catch (SQLException e) {
throw new FunctionExecutionException(e);
} finally {
if (r != null) {
try {
r.close();
} catch (Exception e) {
// Nothing
}
}
}
return getGeometryType(jtsGeometry, srid);
}

public static GeometryType geometryFromBlob(BlobType wkb)
throws FunctionExecutionException {
return geometryFromBlob(wkb, GeometryType.UNKNOWN_SRID);
Expand All @@ -93,7 +177,7 @@ public static GeometryType geometryFromBlob(BlobType wkb, int srid) throws Funct
getGeometry(gt);
return gt;
}

public static Boolean intersects(GeometryType geom1, GeometryType geom2) throws FunctionExecutionException {
Geometry g1 = getGeometry(geom1);
Geometry g2 = getGeometry(geom2);
Expand Down Expand Up @@ -136,12 +220,23 @@ public static Boolean overlaps(GeometryType geom1, GeometryType geom2) throws Fu
return g1.overlaps(g2);
}

static Geometry getGeometry(GeometryType geom)
public static GeometryType getGeometryType(Geometry jtsGeom) {
return getGeometryType(jtsGeom, jtsGeom.getSRID());
}

public static GeometryType getGeometryType(Geometry jtsGeom, int srid) {
WKBWriter writer = new WKBWriter();
byte[] bytes = writer.write(jtsGeom);
return new GeometryType(bytes, srid);
}

public static Geometry getGeometry(GeometryType geom)
throws FunctionExecutionException {
return getGeometry(geom, geom.getSrid());
}

static Geometry getGeometry(GeometryType geom, int srid) throws FunctionExecutionException {
public static Geometry getGeometry(GeometryType geom, int srid)
throws FunctionExecutionException {
InputStream is1 = null;
try {
WKBReader reader = new WKBReader();
Expand Down

0 comments on commit 446634b

Please sign in to comment.