001 //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/io/shpapi/shape_new/ShapePolygon.java $ 002 /*---------------------------------------------------------------------------- 003 This file is part of deegree, http://deegree.org/ 004 Copyright (C) 2001-2009 by: 005 Department of Geography, University of Bonn 006 and 007 lat/lon GmbH 008 009 This library is free software; you can redistribute it and/or modify it under 010 the terms of the GNU Lesser General Public License as published by the Free 011 Software Foundation; either version 2.1 of the License, or (at your option) 012 any later version. 013 This library is distributed in the hope that it will be useful, but WITHOUT 014 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 015 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 016 details. 017 You should have received a copy of the GNU Lesser General Public License 018 along with this library; if not, write to the Free Software Foundation, Inc., 019 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 020 021 Contact information: 022 023 lat/lon GmbH 024 Aennchenstr. 19, 53177 Bonn 025 Germany 026 http://lat-lon.de/ 027 028 Department of Geography, University of Bonn 029 Prof. Dr. Klaus Greve 030 Postfach 1147, 53001 Bonn 031 Germany 032 http://www.geographie.uni-bonn.de/deegree/ 033 034 e-mail: info@deegree.org 035 ----------------------------------------------------------------------------*/ 036 package org.deegree.io.shpapi.shape_new; 037 038 import java.util.Arrays; 039 import java.util.Collections; 040 import java.util.List; 041 042 import org.deegree.model.crs.CoordinateSystem; 043 import org.deegree.model.spatialschema.ByteUtils; 044 import org.deegree.model.spatialschema.Curve; 045 import org.deegree.model.spatialschema.Geometry; 046 import org.deegree.model.spatialschema.GeometryException; 047 import org.deegree.model.spatialschema.GeometryFactory; 048 import org.deegree.model.spatialschema.Position; 049 050 import com.vividsolutions.jts.algorithm.CGAlgorithms; 051 import com.vividsolutions.jts.geom.Coordinate; 052 053 /** 054 * <code>ShapePolygon</code> corresponds to the Polygon, PolygonM and PolygonZ shapes of the 055 * shapefile spec. 056 * 057 * @author <a href="mailto:schmitz@lat-lon.de">Andreas Schmitz</a> 058 * @author last edited by: $Author: mschneider $ 059 * 060 * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18 Jun 2009) $ 061 */ 062 public class ShapePolygon extends ShapePolyline { 063 064 private boolean normalized; 065 066 /** 067 * Creates a new Polygon/M/Z. 068 * 069 * @param z 070 * @param m 071 */ 072 public ShapePolygon( boolean z, boolean m ) { 073 super( z, m ); 074 } 075 076 /** 077 * Creates a new Polygon/M/Z. 078 * 079 * @param z 080 * @param m 081 * @param crs 082 * CoordinateSystem of the shape 083 */ 084 public ShapePolygon( boolean z, boolean m, CoordinateSystem crs ) { 085 super( z, m, crs ); 086 } 087 /** 088 * Creates a PolygonZ from deegree Curves. 089 * 090 * @param curves 091 */ 092 public ShapePolygon( List<Curve> curves ) { 093 super( curves ); 094 } 095 096 private Coordinate[] getAsCoordinates( ShapePoint[] ps ) { 097 Coordinate[] cs = new Coordinate[ps.length]; 098 099 for ( int i = 0; i < cs.length; ++i ) { 100 cs[i] = ps[i].export(); 101 } 102 103 return cs; 104 } 105 106 /** 107 * Normalizes the orientation etc. of this polygon as required by the shapefile spec. This means 108 * (to my understanding), that the outer ring runs clockwise while the inner rings run 109 * counter-clockwise. 110 */ 111 public void normalize() { 112 if ( normalized ) { 113 return; 114 } 115 116 normalized = true; 117 118 Coordinate[] cs = getAsCoordinates( points[0] ); 119 120 if ( CGAlgorithms.isCCW( cs ) ) { 121 List<ShapePoint> list = Arrays.asList( points[0] ); 122 Collections.reverse( list ); 123 points[0] = list.toArray( new ShapePoint[points[0].length] ); 124 } 125 126 for ( int i = 1; i < points.length; ++i ) { 127 cs = getAsCoordinates( points[i] ); 128 if ( !CGAlgorithms.isCCW( cs ) ) { 129 List<ShapePoint> list = Arrays.asList( points[i] ); 130 Collections.reverse( list ); 131 points[i] = list.toArray( new ShapePoint[points[i].length] ); 132 } 133 } 134 } 135 136 /* 137 * (non-Javadoc) 138 * 139 * @see org.deegree.io.shpapi.Shape#read(byte[], int) 140 */ 141 @Override 142 public int read( byte[] bytes, int offset ) { 143 int off = offset; 144 145 int type = ByteUtils.readLEInt( bytes, off ); 146 off += 4; 147 148 if ( type == ShapeFile.NULL ) { 149 return off; 150 } 151 152 if ( type == ShapeFile.POLYGON ) { 153 return readPolyline( bytes, off ); 154 } 155 156 if ( type == ShapeFile.POLYGONZ ) { 157 return readPolylineZ( bytes, off ); 158 } 159 160 if ( type == ShapeFile.POLYGONM ) { 161 return readPolylineM( bytes, off ); 162 } 163 164 return -1; 165 } 166 167 /** 168 * Note that the normalize method will be called automatically, if it has not been called 169 * before. 170 * 171 * @see org.deegree.io.shpapi.shape_new.Shape#write(byte[], int) 172 */ 173 @Override 174 public int write( byte[] bytes, int offset ) { 175 if ( !normalized ) { 176 normalize(); 177 } 178 if ( isZ ) { 179 ByteUtils.writeLEInt( bytes, offset, ShapeFile.POLYGONZ ); 180 return writePolylineZ( bytes, offset + 4 ); 181 } 182 if ( isM ) { 183 ByteUtils.writeLEInt( bytes, offset, ShapeFile.POLYGONM ); 184 return writePolylineM( bytes, offset + 4 ); 185 } 186 ByteUtils.writeLEInt( bytes, offset, ShapeFile.POLYGON ); 187 return writePolyline( bytes, offset + 4 ); 188 } 189 190 /* 191 * (non-Javadoc) 192 * 193 * @see org.deegree.io.shpapi.shape_new.Shape#getType() 194 */ 195 @Override 196 public int getType() { 197 if ( isZ ) { 198 return ShapeFile.POLYGONZ; 199 } 200 if ( isM ) { 201 return ShapeFile.POLYGONM; 202 } 203 return ShapeFile.POLYGON; 204 } 205 206 /** 207 * This creates a Surface object. 208 */ 209 @Override 210 public Geometry getGeometry() 211 throws ShapeGeometryException { 212 if ( points == null ) { 213 return null; 214 } 215 try { 216 Position[] outer = new Position[points[0].length]; 217 218 Position[][] inner = new Position[points.length - 1][]; 219 220 for ( int i = 0; i < points[0].length; ++i ) { 221 if ( isZ ) { 222 outer[i] = GeometryFactory.createPosition( points[0][i].x, points[0][i].y, points[0][i].z ); 223 } else { 224 outer[i] = GeometryFactory.createPosition( points[0][i].x, points[0][i].y ); 225 } 226 } 227 228 for ( int k = 1; k < points.length; ++k ) { 229 inner[k - 1] = new Position[points[k].length]; 230 for ( int i = 0; i < points[k].length; ++i ) { 231 if ( isZ ) { 232 inner[k - 1][i] = GeometryFactory.createPosition( points[k][i].x, points[k][i].y, 233 points[k][i].z ); 234 } else { 235 inner[k - 1][i] = GeometryFactory.createPosition( points[k][i].x, points[k][i].y ); 236 } 237 } 238 } 239 240 // not sure if interpolation can be null 241 return GeometryFactory.createSurface( outer, inner, null, crs ); 242 } catch ( GeometryException e ) { 243 throw new ShapeGeometryException( "Surface could not be constructed from Polygon.", e ); 244 } 245 } 246 247 }