Project

General

Profile

« Previous | Next » 

Revision 3037

Added by perry about 18 years ago

Fixed polygon generator for cases where bbox crosses the dateline or the poles

View differences:

src/edu/ucsb/nceas/metacat/spatial/SpatialDocument.java
265 265

  
266 266

  
267 267
  /*
268
   * Returns polygon geometry
268
   * Returns a mutlipolygon geometry representing the geographic coverage(s) of the document
269 269
   */
270 270
  private MultiPolygon getPolygonGeometry() {
271 271

  
272 272
    PrecisionModel precModel = new PrecisionModel(); // default: Floating point
273 273
    GeometryFactory geomFac = new GeometryFactory( precModel, featureSchema.srid );
274

  
275 274
    Vector polygons = new Vector();
275
    Float w;
276
    Float s;
277
    Float e;
278
    Float n;
276 279

  
277 280
    if ( west.size() == south.size() && south.size() == east.size() && east.size() == north.size() ) {
278 281
        for (int i = 0; i < west.size(); i++) {
279 282

  
280
            Coordinate[] linestringCoordinates = new Coordinate[5];
283
            w = (Float)west.elementAt(i);
284
            s = (Float)south.elementAt(i);
285
            e = (Float)east.elementAt(i);
286
            n = (Float)north.elementAt(i);
281 287

  
282 288
            // Check if it's actually a valid polygon
283
            if ( (Float)west.elementAt(i) == 0.0 &&
284
                 (Float)east.elementAt(i) == 0.0 && 
285
                 (Float)north.elementAt(i) == 0.0 &&
286
                 (Float)south.elementAt(i) == 0.0) {
287

  
289
            if (  w == 0.0 && s == 0.0 && e == 0.0 && n == 0.0) {
288 290
                log.warn("        Invalid or empty coodinates ... skipping");
289 291
                continue;
290
            } else if( ((Float)west.elementAt(i)).compareTo( (Float)east.elementAt(i) ) == 0 && 
291
                       ((Float)north.elementAt(i)).compareTo( (Float)south.elementAt(i) ) == 0 ) {
292

  
292
            } else if( w.compareTo( e ) == 0 && n.compareTo( s ) == 0 ) {
293 293
                log.warn("        Point coordinates only.. skipping polygon generation");
294 294
                continue;
295 295
            }
296 296

  
297
            linestringCoordinates[0] = new Coordinate( (Float)west.elementAt(i), (Float)south.elementAt(i) );
298
            linestringCoordinates[1] = new Coordinate( (Float)west.elementAt(i), (Float)north.elementAt(i));
299
            linestringCoordinates[2] = new Coordinate( (Float)east.elementAt(i), (Float)north.elementAt(i));
300
            linestringCoordinates[3] = new Coordinate( (Float)east.elementAt(i), (Float)south.elementAt(i));
301
            linestringCoordinates[4] = new Coordinate( (Float)west.elementAt(i), (Float)south.elementAt(i));
302
            polygons.add( geomFac.createPolygon( geomFac.createLinearRing(linestringCoordinates), null) );
297
            // Handle the case of crossing the dateline and poles
298
            // Assumes all coordinates are confined to -180 -90 180 90
299
            Float dl = new Float("180.0");
300
            Float _dl = new Float("-180.0");
301
            Float pl = new Float("90.0");
302
            Float _pl = new Float("-90.0");
303
            
304
            if ( w > e && s > n ) {
305
                log.info( "Crosses both the dateline and the poles .. split into 4 polygons" );
306
                polygons.add( createPolygonFromBbox( geomFac,   w,   s, dl, pl ) );
307
                polygons.add( createPolygonFromBbox( geomFac,   w, _pl, dl,  n ) );
308
                polygons.add( createPolygonFromBbox( geomFac, _dl,   s,  e, pl ) );
309
                polygons.add( createPolygonFromBbox( geomFac, _dl, _pl,  e,  n ) );
310
            } else if ( w > e ) {
311
                log.info( "Crosses the dateline .. split into 2 polygons" );
312
                polygons.add( createPolygonFromBbox( geomFac,   w, s, dl, n ) );
313
                polygons.add( createPolygonFromBbox( geomFac, _dl, s,  e, n ) );
314
            } else if ( s > n ) {
315
                log.info( "Crosses the poles .. split into 2 polygons" );
316
                polygons.add( createPolygonFromBbox( geomFac, w, _pl, e,  n ) );
317
                polygons.add( createPolygonFromBbox( geomFac, w,   s, e, pl ) );
318
            } else {
319
                // Just a standard polygon that fits nicely onto our flat earth
320
                polygons.add( createPolygonFromBbox( geomFac, w, s, e, n ) );    
321
            }
322

  
323
             
303 324
        }
304 325
    } else {
305 326
       log.error(" *** Something went wrong.. your east,west,north and south bounding arrays are different sizes!");
......
315 336

  
316 337
  }
317 338
   
339

  
318 340
  /*
341
   * returns a polygon given the four bounding box coordinates
342
   */
343
  private Polygon createPolygonFromBbox( GeometryFactory geomFac, Float w, Float s, Float e, Float n ) {
344

  
345
        Coordinate[] linestringCoordinates = new Coordinate[5];
346

  
347
        linestringCoordinates[0] = new Coordinate( w, s );
348
        linestringCoordinates[1] = new Coordinate( w, n );
349
        linestringCoordinates[2] = new Coordinate( e, n );
350
        linestringCoordinates[3] = new Coordinate( e, s );
351
        linestringCoordinates[4] = new Coordinate( w, s );
352

  
353
        return geomFac.createPolygon( geomFac.createLinearRing(linestringCoordinates), null);
354
  }
355

  
356

  
357
  /*
319 358
   * returns a point geometry
320 359
   */
321 360
  private MultiPoint getPointGeometry() {

Also available in: Unified diff