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
.referencing
.CRS
;
12 import org
.opengis
.geometry
.DirectPosition
;
13 import org
.opengis
.referencing
.crs
.CoordinateReferenceSystem
;
15 import com
.vividsolutions
.jts
.geom
.Coordinate
;
16 import com
.vividsolutions
.jts
.geom
.Geometry
;
17 import com
.vividsolutions
.jts
.geom
.GeometryFactory
;
18 import com
.vividsolutions
.jts
.geom
.Point
;
20 /** Utilities to map JCR from/to JTS and GeoTools */
21 public class GeoJcrUtils
{
22 private final static Log log
= LogFactory
.getLog(GeoJcrUtils
.class);
24 /** Transforms a geometry node into position within its CRS */
25 public static DirectPosition
nodeToPosition(Node node
) {
27 if (node
.isNodeType(GisTypes
.GIS_POINT
)) {
28 CoordinateReferenceSystem crs
= getCoordinateReferenceSystem(node
);
29 Point point
= (Point
) nodeToGeometry(node
);
30 return new DirectPosition2D(crs
, point
.getX(), point
.getY());
32 throw new ArgeoException(node
+ " is not of a supported type");
34 } catch (Exception e
) {
35 throw new ArgeoException("Cannot extract position from " + node
, e
);
39 /** Transforms a geometry node into a JTS geometry. */
40 public static Geometry
nodeToGeometry(Node node
) {
42 if (node
.isNodeType(GisTypes
.GIS_POINT
)
43 || node
.isNodeType(GisTypes
.GIS_COORDINATE
)) {
45 if (node
.hasProperty(GisNames
.GIS_Z
))
46 coo
= new Coordinate(node
.getProperty(GisNames
.GIS_X
)
47 .getDouble(), node
.getProperty(GisNames
.GIS_Y
)
48 .getDouble(), node
.getProperty(GisNames
.GIS_Z
)
51 coo
= new Coordinate(node
.getProperty(GisNames
.GIS_X
)
52 .getDouble(), node
.getProperty(GisNames
.GIS_Y
)
55 // TODO: use factory finder
56 // GeometryFactory geometryFactory =
57 // JTSFactoryFinder.getGeometryFactory(null);
58 GeometryFactory geometryFactory
= new GeometryFactory();
59 return geometryFactory
.createPoint(coo
);
61 throw new ArgeoException(node
+ " is not of a supported type");
63 } catch (Exception e
) {
64 throw new ArgeoException("Cannot map " + node
+ " to a geometry", e
);
68 /** Reads and interpret the coordinate reference system from a node. */
69 public static CoordinateReferenceSystem
getCoordinateReferenceSystem(
72 if (!node
.isNodeType(GisTypes
.GIS_LOCATED
))
73 throw new ArgeoException(node
+ " is not of type "
74 + GisTypes
.GIS_LOCATED
);
75 // Coordinate reference system
76 String srs
= node
.getProperty(GisNames
.GIS_SRS
).getString();
77 CoordinateReferenceSystem crs
;
79 // first, try to decode an EPSG code
80 crs
= CRS
.decode(srs
);
81 } catch (Exception e
) {
82 // if it fails, try a WKT
84 crs
= CRS
.parseWKT(srs
);
85 } catch (Exception e1
) {
86 // if it fails as well, log the error
87 log
.error("Cannot parse WKT " + srs
, e1
);
88 // and then the previous error (probably more relevant)
93 } catch (Exception e
) {
94 throw new ArgeoException(
95 "Cannot get coordinate reference system for " + node
, e
);