Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.geometry / org.gvsig.fmap.geometry.operation / src / main / java / org / gvsig / fmap / geom / operation / tojts / MultiCurveToJTS.java @ 40767

History | View | Annotate | Download (3.82 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.fmap.geom.operation.tojts;
25

    
26
import java.util.ArrayList;
27
import java.util.List;
28

    
29
import com.vividsolutions.jts.geom.LineString;
30
import com.vividsolutions.jts.geom.MultiLineString;
31

    
32
import org.slf4j.Logger;
33
import org.slf4j.LoggerFactory;
34

    
35
import org.gvsig.fmap.geom.Geometry;
36
import org.gvsig.fmap.geom.aggregate.MultiCurve;
37
import org.gvsig.fmap.geom.operation.GeometryOperationContext;
38
import org.gvsig.fmap.geom.operation.GeometryOperationException;
39
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
40

    
41
/**
42
 * @author <a href="mailto:jpiera@gvsig.org">Jorge Piera</a>
43
 */
44
public class MultiCurveToJTS extends ToJTS{
45
    
46
    private static Logger logger = LoggerFactory.getLogger(MultiCurveToJTS.class);
47
        /*
48
         * (non-Javadoc)
49
         * @see org.gvsig.fmap.geom.operation.tojts.ToJTS#invoke(org.gvsig.fmap.geom.Geometry, org.gvsig.fmap.geom.operation.GeometryOperationContext)
50
         */
51
        public Object invoke(Geometry geom, GeometryOperationContext ctx) throws GeometryOperationException {
52
                int srid = -1;
53
                if (ctx != null){
54
                        srid = ((JTSGeometryOperationContext)ctx).getSrid();
55
                }
56
                MultiCurve multiCurve = (MultiCurve)geom;
57
                Object item = null;
58
                List ls_list = new ArrayList();
59

    
60
                int prim_n = multiCurve.getPrimitivesNumber();
61
                
62
        for (int i = 0; i < prim_n; i++){
63
                try {
64
                    item = multiCurve.getPrimitiveAt(i).invokeOperation(CODE, ctx);
65
                    
66
                    if (item instanceof MultiLineString) {
67
                        
68
                    /*
69
                     * A multilinestring can also be returned
70
                     */
71
                        MultiLineString mls = (MultiLineString) item;
72
                        for (int k = 0; k<mls.getNumGeometries(); k++) {
73
                            com.vividsolutions.jts.geom.Geometry p = mls.getGeometryN(k);
74
                            if (p instanceof LineString) {
75
                                ls_list.add(p);
76
                            } 
77
                        }
78
                        
79
                    } else {
80
                    if (item instanceof LineString) {
81
                        /*
82
                         * Including primitives converted to linestring.
83
                         * Short primitives can return a point
84
                         */
85
                        ls_list.add(item);
86
                    } else {
87
                        String txt = (item == null) ? "NULL" : item.getClass().getName();
88
                        logger.info("Warning: excluding primitive in MultiCurveToJTS because it converted to a JTS " + txt);
89
                    }
90
                    }
91
                    
92
                        } catch (GeometryOperationNotSupportedException e) {
93
                                throw new GeometryOperationException(e);
94
                        }
95
        }
96

    
97
        LineString[] lines = (LineString[]) ls_list.toArray(new LineString[0]);
98

    
99
        MultiLineString lineString = new com.vividsolutions.jts.geom.GeometryFactory().createMultiLineString(lines);
100
        lineString.setSRID(srid);
101
        return lineString;
102
        }        
103
}