1 package org
.argeo
.geotools
.jcr
;
5 import org
.apache
.commons
.logging
.Log
;
6 import org
.apache
.commons
.logging
.LogFactory
;
7 import org
.argeo
.ArgeoException
;
8 import org
.argeo
.jcr
.gis
.GisNames
;
9 import org
.argeo
.jcr
.gis
.GisTypes
;
10 import org
.geotools
.geometry
.DirectPosition2D
;
11 import org
.geotools
.geometry
.jts
.JTS
;
12 import org
.geotools
.referencing
.CRS
;
13 import org
.opengis
.geometry
.DirectPosition
;
14 import org
.opengis
.referencing
.crs
.CoordinateReferenceSystem
;
15 import org
.opengis
.referencing
.operation
.MathTransform
;
17 import com
.vividsolutions
.jts
.geom
.Coordinate
;
18 import com
.vividsolutions
.jts
.geom
.Geometry
;
19 import com
.vividsolutions
.jts
.geom
.GeometryFactory
;
20 import com
.vividsolutions
.jts
.geom
.Point
;
22 /** Utilities to map JCR from/to JTS and GeoTools */
23 public class GeoJcrUtils
{
24 private final static Log log
= LogFactory
.getLog(GeoJcrUtils
.class);
26 /** Transforms a geometry node into position within its CRS */
27 public static DirectPosition
nodeToPosition(Node node
) {
29 if (node
.isNodeType(GisTypes
.GIS_POINT
)) {
30 CoordinateReferenceSystem crs
= getCoordinateReferenceSystem(node
);
31 Point point
= (Point
) nodeToGeometry(node
);
32 return new DirectPosition2D(crs
, point
.getX(), point
.getY());
34 throw new ArgeoException(node
+ " is not of a supported type");
36 } catch (Exception e
) {
37 throw new ArgeoException("Cannot extract position from " + node
, e
);
41 /** Transforms a geometry node into a JTS geometry. */
42 public static Geometry
nodeToGeometry(Node node
) {
44 if (node
.isNodeType(GisTypes
.GIS_POINT
)
45 || node
.isNodeType(GisTypes
.GIS_COORDINATE
)) {
47 if (node
.hasProperty(GisNames
.GIS_Z
))
48 coo
= new Coordinate(node
.getProperty(GisNames
.GIS_X
)
49 .getDouble(), node
.getProperty(GisNames
.GIS_Y
)
50 .getDouble(), node
.getProperty(GisNames
.GIS_Z
)
53 coo
= new Coordinate(node
.getProperty(GisNames
.GIS_X
)
54 .getDouble(), node
.getProperty(GisNames
.GIS_Y
)
57 // TODO: use factory finder
58 // GeometryFactory geometryFactory =
59 // JTSFactoryFinder.getGeometryFactory(null);
60 GeometryFactory geometryFactory
= new GeometryFactory();
61 return geometryFactory
.createPoint(coo
);
63 throw new ArgeoException(node
+ " is not of a supported type");
65 } catch (Exception e
) {
66 throw new ArgeoException("Cannot map " + node
+ " to a geometry", e
);
70 /** Reads and interpret the coordinate reference system from a node. */
71 public static CoordinateReferenceSystem
getCoordinateReferenceSystem(
74 if (!node
.isNodeType(GisTypes
.GIS_LOCATED
))
75 throw new ArgeoException(node
+ " is not of type "
76 + GisTypes
.GIS_LOCATED
);
77 // Coordinate reference system
78 String srs
= node
.getProperty(GisNames
.GIS_SRS
).getString();
79 CoordinateReferenceSystem crs
;
81 // first, try to decode an EPSG code
82 crs
= CRS
.decode(srs
);
83 } catch (Exception e
) {
84 // if it fails, try a WKT
86 crs
= CRS
.parseWKT(srs
);
87 } catch (Exception e1
) {
88 // if it fails as well, log the error
89 log
.error("Cannot parse WKT " + srs
, e1
);
90 // and then the previous error (probably more relevant)
95 } catch (Exception e
) {
96 throw new ArgeoException(
97 "Cannot get coordinate reference system for " + node
, e
);
101 public static Geometry
reproject(CoordinateReferenceSystem crs
,
102 Geometry geometry
, CoordinateReferenceSystem targetCrs
) {
104 MathTransform transform
= CRS
.findMathTransform(crs
, targetCrs
);
105 return JTS
.transform(geometry
, transform
);
106 } catch (Exception e
) {
107 throw new ArgeoException("Cannot reproject " + geometry
+ " from "
108 + crs
+ " to " + targetCrs
);