Class GeometryCollectionMapper

 1 /*
 2  * Copyright (c) 2016 Vivid Solutions.
 3  *
 4  * All rights reserved. This program and the accompanying materials
 5  * are made available under the terms of the Eclipse Public License 2.0
 6  * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
 7  * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v20.html
 8  * and the Eclipse Distribution License is available at
 9  *
10  * http://www.eclipse.org/org/documents/edl-v10.php.
11  */
12  
13 package org.locationtech.jts.geom.util;
14  
15 import java.util.ArrayList;
16 import java.util.List;
17  
18 import org.locationtech.jts.geom.Geometry;
19 import org.locationtech.jts.geom.GeometryCollection;
20 import org.locationtech.jts.geom.GeometryFactory;
21 import org.locationtech.jts.geom.util.GeometryMapper.MapOp;
22  
23 /**
24  * Maps the members of a {@link GeometryCollection}
25  * into another <tt>GeometryCollection</tt> via a defined
26  * mapping function.
27  * 
28  * @author Martin Davis
29  *
30  */
31 public class GeometryCollectionMapper 
32 {
33   public static GeometryCollection map(GeometryCollection gc, MapOp op)
34   {
35     GeometryCollectionMapper mapper = new GeometryCollectionMapper(op);
36     return mapper.map(gc);
37   }
38   
39   private MapOp mapOp = null;
40   
41   public GeometryCollectionMapper(MapOp mapOp) {
42     this.mapOp = mapOp;
43   }
44  
45   public GeometryCollection map(GeometryCollection gc)
46   {
47     List mapped = new ArrayList();
48     for (int i = 0; i < gc.getNumGeometries(); i++) {
49       Geometry g = mapOp.map(gc.getGeometryN(i));
50       if (!g.isEmpty())
51         mapped.add(g);
52     }
53     return gc.getFactory().createGeometryCollection(
54         GeometryFactory.toGeometryArray(mapped));
55   }
56 }
57