001 //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/tags/2.1/src/org/deegree/framework/xml/GeometryUtils.java $ 002 /*---------------- FILE HEADER ------------------------------------------ 003 004 This file is part of deegree. 005 Copyright (C) 2001-2006 by: 006 EXSE, Department of Geography, University of Bonn 007 http://www.giub.uni-bonn.de/deegree/ 008 lat/lon GmbH 009 http://www.lat-lon.de 010 011 This library is free software; you can redistribute it and/or 012 modify it under the terms of the GNU Lesser General Public 013 License as published by the Free Software Foundation; either 014 version 2.1 of the License, or (at your option) any later version. 015 016 This library is distributed in the hope that it will be useful, 017 but WITHOUT ANY WARRANTY; without even the implied warranty of 018 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 019 Lesser General Public License for more details. 020 021 You should have received a copy of the GNU Lesser General Public 022 License along with this library; if not, write to the Free Software 023 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 024 025 Contact: 026 027 Andreas Poth 028 lat/lon GmbH 029 Aennchenstr. 19 030 53177 Bonn 031 Germany 032 E-Mail: poth@lat-lon.de 033 034 Prof. Dr. Klaus Greve 035 Department of Geography 036 University of Bonn 037 Meckenheimer Allee 166 038 53115 Bonn 039 Germany 040 E-Mail: greve@giub.uni-bonn.de 041 042 ---------------------------------------------------------------------------*/ 043 package org.deegree.framework.xml; 044 045 import org.deegree.framework.util.StringTools; 046 import org.deegree.model.crs.GeoTransformer; 047 import org.deegree.model.spatialschema.Envelope; 048 import org.deegree.model.spatialschema.GMLGeometryAdapter; 049 import org.deegree.model.spatialschema.Geometry; 050 import org.deegree.model.spatialschema.MultiSurface; 051 import org.deegree.model.spatialschema.Point; 052 import org.deegree.model.spatialschema.Position; 053 import org.deegree.model.spatialschema.Ring; 054 import org.deegree.model.spatialschema.Surface; 055 import org.w3c.dom.Element; 056 import org.w3c.dom.Node; 057 058 /** 059 * Utility methods for handling geometries within XSLT transformations 060 * 061 * 062 * @version $Revision: 7236 $ 063 * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a> 064 * @author last edited by: $Author: rbezema $ 065 * 066 * @version 1.0. $Revision: 7236 $, $Date: 2007-05-22 11:10:39 +0200 (Di, 22 Mai 2007) $ 067 * 068 * @since 2.0 069 */ 070 public class GeometryUtils { 071 072 /** 073 * 074 * @param node 075 * @return 076 */ 077 public static String getPolygonCoordinatesFromEnvelope( Node node ) { 078 StringBuffer sb = new StringBuffer( 500 ); 079 try { 080 Envelope env = GMLGeometryAdapter.wrapBox( (Element) node, null ); 081 sb.append( env.getMin().getX() ).append( ',' ).append( env.getMin().getY() ).append( 082 ' ' ); 083 sb.append( env.getMin().getX() ).append( ',' ).append( env.getMax().getY() ).append( 084 ' ' ); 085 sb.append( env.getMax().getX() ).append( ',' ).append( env.getMax().getY() ).append( 086 ' ' ); 087 sb.append( env.getMax().getX() ).append( ',' ).append( env.getMin().getY() ).append( 088 ' ' ); 089 sb.append( env.getMin().getX() ).append( ',' ).append( env.getMin().getY() ); 090 } catch ( Exception e ) { 091 e.printStackTrace(); 092 sb.append( StringTools.stackTraceToString( e ) ); 093 } 094 return sb.toString(); 095 } 096 097 /** 098 * returns the coordinates of the out ring of a polygon as comma seperated 099 * list. The coordinate tuples are seperated by a blank. If required the 100 * polygon will first transformed to the target CRS 101 * 102 * @param node 103 * @param sourceCRS 104 * @param targetCRS 105 * @return 106 */ 107 public static String getPolygonOuterRing( Node node, String sourceCRS, String targetCRS ) { 108 StringBuffer coords = new StringBuffer( 10000 ); 109 110 try { 111 Surface surface = (Surface) GMLGeometryAdapter.wrap( (Element) node, sourceCRS ); 112 if ( !targetCRS.equals( sourceCRS ) ) { 113 GeoTransformer gt = new GeoTransformer( targetCRS ); 114 surface = (Surface) gt.transform( surface ); 115 } 116 Position[] pos = surface.getSurfaceBoundary().getExteriorRing().getPositions(); 117 int dim = pos[0].getCoordinateDimension(); 118 for ( int i = 0; i < pos.length; i++ ) { 119 coords.append( pos[i].getX() ).append( ',' ).append( pos[i].getY() ); 120 if ( dim == 3 ) { 121 coords.append( ',' ).append( pos[i].getZ() ); 122 } 123 coords.append( ' ' ); 124 } 125 } catch ( Exception e ) { 126 e.printStackTrace(); 127 } 128 129 return coords.toString(); 130 } 131 132 /** 133 * 134 * @param node 135 * @param index 136 * @param sourceCRS 137 * @param targetCRS 138 * @return 139 */ 140 public static String getPolygonInnerRing( Node node, int index, String sourceCRS, 141 String targetCRS ) { 142 StringBuffer coords = new StringBuffer( 10000 ); 143 144 if ( "Polygon".equals( node.getLocalName() ) || 145 "Surface".equals( node.getLocalName() ) ) { 146 try { 147 Surface surface = (Surface) GMLGeometryAdapter.wrap( (Element) node, sourceCRS ); 148 if ( !targetCRS.equals( sourceCRS ) ) { 149 GeoTransformer gt = new GeoTransformer( targetCRS ); 150 surface = (Surface) gt.transform( surface ); 151 } 152 Position[] pos = surface.getSurfaceBoundary().getInteriorRings()[index-1].getPositions(); 153 int dim = pos[0].getCoordinateDimension(); 154 for ( int i = 0; i < pos.length; i++ ) { 155 coords.append( pos[i].getX() ).append( ',' ).append( pos[i].getY() ); 156 if ( dim == 3 ) { 157 coords.append( ',' ).append( pos[i].getZ() ); 158 } 159 coords.append( ' ' ); 160 } 161 } catch ( Exception e ) { 162 e.printStackTrace(); 163 } 164 } 165 return coords.toString(); 166 } 167 168 /** 169 * 170 * @param node 171 * @return 172 */ 173 public static double calcArea( Node node ) { 174 double area = -1; 175 try { 176 Geometry geom = GMLGeometryAdapter.wrap( (Element) node, null ); 177 if ( geom instanceof Surface ) { 178 area = ( (Surface) geom ).getArea(); 179 } else if ( geom instanceof MultiSurface ) { 180 area = ( (MultiSurface) geom ).getArea(); 181 } 182 } catch ( Exception e ) { 183 e.printStackTrace(); 184 } 185 return area; 186 } 187 188 /** 189 * 190 * @param node 191 * @return 192 */ 193 public static double calcOuterBoundaryLength( Node node ) { 194 double length = 0; 195 try { 196 Geometry geom = GMLGeometryAdapter.wrap( (Element) node, null ); 197 if ( geom instanceof Surface ) { 198 Ring ring = ( (Surface) geom ).getSurfaceBoundary().getExteriorRing(); 199 length = ring.getAsCurveSegment().getLength(); 200 } else if ( geom instanceof MultiSurface ) { 201 MultiSurface ms =( (MultiSurface) geom ); 202 for ( int i = 0; i < ms.getSize(); i++ ) { 203 Ring ring = ms.getSurfaceAt( i ).getSurfaceBoundary().getExteriorRing(); 204 length += ring.getAsCurveSegment().getLength(); 205 } 206 } 207 } catch ( Exception e ) { 208 e.printStackTrace(); 209 } 210 return length; 211 } 212 213 214 /** 215 * returns the centroid X coordinate of the geometry represented by the 216 * passed Node 217 * 218 * @param node 219 * @param targetCRS 220 * @return 221 */ 222 public static double getCentroidX( Node node, String targetCRS ) { 223 if ( node != null ) { 224 Point point = null; 225 try { 226 if ( "Envelope".equals( node.getLocalName() ) ) { 227 Envelope env = GMLGeometryAdapter.wrapBox( (Element) node, null ); 228 point = env.getCentroid(); 229 } else { 230 Geometry geom = GMLGeometryAdapter.wrap( (Element) node, null ); 231 point = geom.getCentroid(); 232 } 233 GeoTransformer gt = new GeoTransformer( targetCRS ); 234 point = (Point) gt.transform( point ); 235 } catch ( Exception e ) { 236 e.printStackTrace(); 237 } 238 239 return point.getX(); 240 } 241 return -1; 242 } 243 244 /** 245 * returns the centroid Y coordinate of the geometry represented by the 246 * passed Node 247 * 248 * @param node 249 * @param targetCRS 250 * @return 251 */ 252 public static double getCentroidY( Node node, String targetCRS ) { 253 if ( node != null ) { 254 Point point = null; 255 try { 256 if ( "Envelope".equals( node.getLocalName() ) ) { 257 Envelope env = GMLGeometryAdapter.wrapBox( (Element) node, null ); 258 point = env.getCentroid(); 259 } else { 260 Geometry geom = GMLGeometryAdapter.wrap( (Element) node, null ); 261 point = geom.getCentroid(); 262 } 263 GeoTransformer gt = new GeoTransformer( targetCRS ); 264 point = (Point) gt.transform( point ); 265 } catch ( Exception e ) { 266 e.printStackTrace(); 267 } 268 return point.getY(); 269 } 270 return -1; 271 } 272 273 }