View Javadoc
1 /* 2 * WKB4J - WKB reader for geographical mapping toolkits 3 * (C) 2002,2003, David Garnier, dgarnier@users.sourceforge.net 4 * 5 * This library is free software; you can redistribute it and/or 6 * modify it under the terms of the GNU Lesser General Public 7 * License as published by the Free Software Foundation, 8 * version 2.1 of the License. 9 * 10 * This library is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 * Lesser General Public License for more details. 14 * 15 * You should have received a copy of the GNU Lesser General Public 16 * License along with this library; if not, write to the Free Software 17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 * or visit the web to http://www.gnu.org. 19 * 20 */ 21 package org.wkb4j.jts; 22 23 import java.util.ArrayList; 24 25 import org.apache.log4j.Logger; 26 import org.wkb4j.engine.WKBFactory; 27 28 import com.vividsolutions.jts.geom.Coordinate; 29 import com.vividsolutions.jts.geom.Geometry; 30 import com.vividsolutions.jts.geom.LineString; 31 import com.vividsolutions.jts.geom.LinearRing; 32 import com.vividsolutions.jts.geom.MultiLineString; 33 import com.vividsolutions.jts.geom.MultiPoint; 34 import com.vividsolutions.jts.geom.MultiPolygon; 35 import com.vividsolutions.jts.geom.Point; 36 import com.vividsolutions.jts.geom.Polygon; 37 import com.vividsolutions.jts.geom.PrecisionModel; 38 39 /*** 40 * Expiremental implementation of a ligther JTSFactory. Not really worth it. 41 * NOTE: It doesn't work because of a bug in Point and Polygon 42 * handling that is fixed in the normal version. 43 * Creation date: 6 juil. 2002 23:10:29 44 * @author David Garnier 45 * @version $Revision: 1.1 $ $Date: 2003/08/02 23:52:18 $ 46 */ 47 public class FastJTSFactory{// implements WKBFactory { 48 protected static Logger log = Logger.getLogger(FastJTSFactory.class); 49 50 private PrecisionModel precisionModel = new PrecisionModel(); 51 52 private Coordinate[] coordinatesBuffer = new Coordinate[0]; 53 private LineString[] lineStringBuffer = new LineString[0]; 54 private Polygon[] polygonBuffer = new Polygon[0]; 55 private LinearRing[] linearRingBuffer = new LinearRing[0]; 56 /*** Stores a array of Point objects.*/ 57 private Point[] pointBuffer = new Point[0]; 58 private volatile int lineStringPointer = 0; 59 private volatile int polygonPointer = 0; 60 private volatile int linearRingPointer = 0; 61 /*** Points to the next free slot in the pointPointer.*/ 62 private volatile int pointPointer = 0; 63 private int srid = -1; 64 65 private final ArrayList geometries = new ArrayList(); 66 67 private Geometry currentGeometry = null; 68 69 public FastJTSFactory() { 70 //super(); 71 } 72 73 public FastJTSFactory(PrecisionModel pm) { 74 this(); 75 setPrecisionModel(pm); 76 } 77 78 /*** 79 * @see org.wkb4j.engine.WKBFactory#beginWork() 80 */ 81 public final void beginWork() { 82 //super.beginWork(); 83 } 84 85 /*** 86 * @see org.wkb4j.engine.WKBFactory#endWork() 87 */ 88 public final void endWork() { 89 //super.endWork(); 90 } 91 92 /*** 93 * @see org.wkb4j.engine.WKBFactory#abortWork() 94 */ 95 public final void abortWork() { 96 //super.abortWork(); 97 98 } 99 100 /*** 101 * @see org.wkb4j.engine.WKBFactory#beginUnit() 102 */ 103 public final void beginUnit(String[] words, int[] values) { 104 //super.beginUnit(srid); 105 // setSrid(srid); 106 } 107 108 /*** 109 * @see org.wkb4j.engine.WKBFactory#endUnit() 110 */ 111 public final void endUnit() { 112 //super.endUnit(); 113 geometries.add(currentGeometry); 114 lineStringBuffer = null; 115 //There is no point of setting srid to another value because all values are correct. We could try to make sure that the value is invaled somehow.*/ 116 } 117 118 /*** 119 * @see org.wkb4j.engine.WKBFactory#abortUnit() 120 */ 121 public final void abortUnit() { 122 //super.abortUnit(); 123 } 124 125 /*** 126 * @see org.wkb4j.engine.WKBFactory#beginGeometryCollection() 127 */ 128 public final boolean beginGeometryCollection(int count) { 129 //boolean retval = //super.beginGeometryCollection(count); 130 return true; 131 } 132 133 /*** 134 * @see org.wkb4j.engine.WKBFactory#endGeometryCollection() 135 */ 136 public final boolean endGeometryCollection() { 137 // boolean retval = //super.endGeometryCollection(); 138 return true; 139 } 140 141 /*** 142 * @see org.wkb4j.engine.WKBFactory#abortGeometryCollection() 143 */ 144 public final void abortGeometryCollection() { 145 //super.abortGeometryCollection(); 146 } 147 148 /*** 149 * @see org.wkb4j.engine.WKBFactory#beginLineString() 150 */ 151 public final boolean beginLineString(int count) { 152 return true; //super.beginLineString(count); 153 } 154 155 /*** 156 * @see org.wkb4j.engine.WKBFactory#endLineString() 157 */ 158 public final boolean endLineString() { 159 // if (//super.endLineString()) { 160 LineString ls = 161 new LineString(coordinatesBuffer, getPrecisionModel(), getSrid()); 162 lineStringBuffer[lineStringPointer] = ls; 163 lineStringPointer++; 164 currentGeometry = ls; 165 coordinatesBuffer = null; 166 return true; 167 // } else { 168 // return false; 169 // } 170 } 171 172 /*** 173 * @see org.wkb4j.engine.WKBFactory#abortLineString() 174 */ 175 public final void abortLineString() { 176 //super.abortLineString(); 177 coordinatesBuffer = null; 178 } 179 180 /*** 181 * @see org.wkb4j.engine.WKBFactory#beginPoint() 182 */ 183 public final boolean beginPoint() { 184 // return super.beginPoint(); 185 return true; 186 } 187 188 /*** Constructs a Point using the current buffer of Coordinates, 189 * resets the Coordinates buffer, add the Point to the PointBuffer 190 * and defines the new Point as the currentGeometry. 191 * @see org.wkb4j.engine.WKBFactory#endPoint() 192 */ 193 public final boolean endPoint() { 194 // if (super.endPoint()) { 195 if (coordinatesBuffer.length > 0) { 196 log.error( 197 "Trying to create a new Point with more than one Coordinates."); 198 return false; 199 } 200 Point ls = 201 new Point(coordinatesBuffer[0], getPrecisionModel(), getSrid()); 202 pointBuffer[pointPointer] = ls; 203 204 currentGeometry = ls; 205 coordinatesBuffer = null; 206 return true; 207 // } else { 208 // return false; 209 // } 210 } 211 212 /*** 213 * @see org.wkb4j.engine.WKBFactory#abortPoint() 214 */ 215 public final void abortPoint() { 216 // super.abortPoint(); 217 coordinatesBuffer = null; 218 } 219 220 /*** Constructs a new array of Points with the supplied number of elements. 221 * @see org.wkb4j.engine.WKBFactory#beginMultiPoint() 222 */ 223 public final boolean beginMultiPoint(int count) { 224 // if (super.beginMultiPoint(count)) { 225 pointBuffer = new Point[count]; 226 return true; 227 // } else { 228 // return false; 229 // } 230 } 231 232 /*** Constructs a MultiPoint using the current buffer of Points, 233 * resets the Points buffer 234 * and defines the new MultiPoint as the currentGeometry. 235 * @see org.wkb4j.engine.WKBFactory#endMultiPoint() 236 */ 237 public final boolean endMultiPoint() { 238 // if (super.endMultiPoint()) { 239 MultiPoint mls = 240 new MultiPoint(pointBuffer, getPrecisionModel(), getSrid()); 241 currentGeometry = mls; 242 coordinatesBuffer = null; 243 pointBuffer = null; 244 pointPointer = 0; 245 return true; 246 // } else { 247 // return false; 248 // } 249 } 250 251 /*** 252 * @see org.wkb4j.engine.WKBFactory#abortMultiPoint() 253 */ 254 public final void abortMultiPoint() { 255 // super.abortMultiPoint(); 256 } 257 /*** 258 * @see org.wkb4j.engine.WKBFactory#beginMultiLineString() 259 */ 260 public final boolean beginMultiLineString(int count) { 261 // if (//super.beginMultiLineString(count)) { 262 lineStringBuffer = new LineString[count]; 263 return true; 264 // } else { 265 // return false; 266 // } 267 } 268 269 /*** 270 * @see org.wkb4j.engine.WKBFactory#endMultiLineString() 271 */ 272 public final boolean endMultiLineString() { 273 // if (//super.endMultiLineString()) { 274 MultiLineString mls = 275 new MultiLineString( 276 lineStringBuffer, 277 getPrecisionModel(), 278 getSrid()); 279 currentGeometry = mls; 280 coordinatesBuffer = null; 281 lineStringBuffer = null; 282 lineStringPointer = 0; 283 return true; 284 // } else { 285 // return false; 286 // } 287 } 288 289 /*** 290 * @see org.wkb4j.engine.WKBFactory#abortMultiLineString() 291 */ 292 public final void abortMultiLineString() { 293 //super.abortMultiLineString(); 294 295 } 296 297 /*** 298 * @see org.wkb4j.engine.WKBFactory#beginMultiPolygon() 299 */ 300 public final boolean beginMultiPolygon(int count) { 301 // if (//super.beginMultiPolygon(count)) { 302 polygonBuffer = new Polygon[count]; 303 return true; 304 // } else { 305 // return false; 306 // } 307 } 308 309 /*** 310 * @see org.wkb4j.engine.WKBFactory#endMultiPolygon() 311 */ 312 public final boolean endMultiPolygon() { 313 // if (//super.endMultiPolygon()) { 314 MultiPolygon mls = 315 new MultiPolygon(polygonBuffer, getPrecisionModel(), getSrid()); 316 currentGeometry = mls; 317 polygonBuffer = null; 318 polygonPointer = 0; 319 return true; 320 // } else { 321 // return false; 322 // } 323 } 324 325 /*** 326 * @see org.wkb4j.engine.WKBFactory#abortMultiPolygon() 327 */ 328 public final void abortMultiPolygon() { 329 //super.abortMultiPolygon(); 330 } 331 332 /*** 333 * @see org.wkb4j.engine.WKBFactory#beginPolygon() 334 */ 335 public final boolean beginPolygon(int count) { 336 //if (//super.beginPolygon(count)) { 337 linearRingBuffer = new LinearRing[count]; 338 return true; 339 // } else { 340 // return false; 341 // } 342 } 343 344 /*** 345 * @see org.wkb4j.engine.WKBFactory#endPolygon() 346 */ 347 public final boolean endPolygon() { 348 // if (//super.endPolygon()) { 349 Polygon ls = null; 350 351 if (linearRingBuffer.length == 1) { 352 /*Constructs a Polygon with the given exterior boundary.*/ 353 ls = 354 new Polygon( 355 linearRingBuffer[0], 356 getPrecisionModel(), 357 getSrid()); 358 } else { 359 /*Constructs a Polygon with the given exterior boundary and interior boundaries.*/ 360 LinearRing exterior = linearRingBuffer[0]; 361 // Shift each LinearRing to the left, overwriting the first one (aka exterior) 362 System.arraycopy( 363 linearRingBuffer, 364 1, 365 linearRingBuffer, 366 0, 367 linearRingBuffer.length - 1); 368 ls = 369 new Polygon( 370 exterior, 371 linearRingBuffer, 372 getPrecisionModel(), 373 getSrid()); 374 375 } 376 polygonBuffer[lineStringPointer] = ls; 377 polygonPointer++; 378 currentGeometry = ls; 379 linearRingBuffer = null; 380 return true; 381 // } else { 382 // return false; 383 // } 384 } 385 386 /*** 387 * @see org.wkb4j.engine.WKBFactory#abortPolygon() 388 */ 389 public final void abortPolygon() { 390 //super.abortPolygon(); 391 392 } 393 394 /*** 395 * @see org.wkb4j.engine.WKBFactory#beginLinearRing() 396 */ 397 public final boolean beginLinearRing(int count) { 398 // if (//super.beginLinearRing(count)) { 399 coordinatesBuffer = new Coordinate[count]; 400 return true; 401 // } else { 402 // return false; 403 // } 404 } 405 406 /*** 407 * @see org.wkb4j.engine.WKBFactory#endLinearRing() 408 */ 409 public final boolean endLinearRing() { 410 //if (//super.endLinearRing()) { 411 LinearRing ls = 412 new LinearRing(coordinatesBuffer, getPrecisionModel(), getSrid()); 413 linearRingBuffer[linearRingPointer] = ls; 414 linearRingPointer++; 415 coordinatesBuffer = null; 416 return true; 417 // } else { 418 // return false; 419 // } 420 } 421 422 /*** 423 * @see org.wkb4j.engine.WKBFactory#abortLinearRing() 424 */ 425 public final void abortLinearRing() { 426 //super.abortLinearRing(); 427 428 } 429 430 /*** 431 * @see org.wkb4j.engine.WKBFactory#addPoints(double[]) 432 */ 433 public final boolean addPoints(double[] points) { 434 //if (//super.addPoints(points)) { 435 coordinatesBuffer = new Coordinate[points.length >> 1]; 436 for (int i = 0, size = points.length; i < size; i += 2) { 437 coordinatesBuffer[i >> 1] = 438 new Coordinate(points[i], points[i + 1]); 439 } 440 return true; 441 // } else { 442 // return false; 443 // } 444 445 } 446 /*** 447 * Returns the precisionModel. 448 * @return PrecisionModel 449 */ 450 public final PrecisionModel getPrecisionModel() { 451 return precisionModel; 452 } 453 454 /*** 455 * Sets the precisionModel. 456 * @param precisionModel The precisionModel to set 457 */ 458 public final void setPrecisionModel(PrecisionModel precisionModel) { 459 this.precisionModel = precisionModel; 460 } 461 462 /*** 463 * Returns the geometries. 464 * @return ArrayList 465 */ 466 public final ArrayList getGeometries() { 467 geometries.trimToSize(); 468 return geometries; 469 } 470 /*** 471 * Returns the srid. 472 * @return int 473 */ 474 public final int getSrid() { 475 return srid; 476 } 477 478 /*** 479 * Sets the srid. 480 * @param srid The srid to set 481 */ 482 private void setSrid(int srid) { 483 this.srid = srid; 484 } 485 486 }

This page was automatically generated by Maven