svn-gvsig-desktop / tags / v2_0_0_Build_2058 / libraries / libFMap_geometries / src / org / gvsig / fmap / geom / operation / tojts / MultiSurfaceToJTS.java @ 39246
History | View | Annotate | Download (3.74 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
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 2
|
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 |
*/
|
22 |
|
23 |
/*
|
24 |
* AUTHORS (In addition to CIT):
|
25 |
* 2009 {Iver T.I.} {Task}
|
26 |
*/
|
27 |
|
28 |
package org.gvsig.fmap.geom.operation.tojts; |
29 |
|
30 |
import java.util.ArrayList; |
31 |
import java.util.List; |
32 |
|
33 |
import com.vividsolutions.jts.geom.MultiPolygon; |
34 |
import com.vividsolutions.jts.geom.Polygon; |
35 |
|
36 |
import org.slf4j.Logger; |
37 |
import org.slf4j.LoggerFactory; |
38 |
|
39 |
import org.gvsig.fmap.geom.Geometry; |
40 |
import org.gvsig.fmap.geom.aggregate.MultiSurface; |
41 |
import org.gvsig.fmap.geom.operation.GeometryOperationContext; |
42 |
import org.gvsig.fmap.geom.operation.GeometryOperationException; |
43 |
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException; |
44 |
|
45 |
/**
|
46 |
* @author <a href="mailto:jpiera@gvsig.org">Jorge Piera</a>
|
47 |
*/
|
48 |
public class MultiSurfaceToJTS extends ToJTS{ |
49 |
|
50 |
private static Logger logger = LoggerFactory.getLogger(MultiSurfaceToJTS.class); |
51 |
/*
|
52 |
* (non-Javadoc)
|
53 |
* @see org.gvsig.fmap.geom.operation.tojts.ToJTS#invoke(org.gvsig.fmap.geom.Geometry, org.gvsig.fmap.geom.operation.GeometryOperationContext)
|
54 |
*/
|
55 |
public Object invoke(Geometry geom, GeometryOperationContext ctx) throws GeometryOperationException { |
56 |
int srid = -1; |
57 |
if (ctx != null){ |
58 |
srid = ((JTSGeometryOperationContext)ctx).getSrid(); |
59 |
} |
60 |
MultiSurface multiSurface = (MultiSurface)geom; |
61 |
|
62 |
List prim_list = new ArrayList(); |
63 |
int prim_n = multiSurface.getPrimitivesNumber();
|
64 |
Object item = null; |
65 |
|
66 |
for (int i = 0; i < prim_n; i++){ |
67 |
try {
|
68 |
item = multiSurface.getPrimitiveAt(i).invokeOperation(CODE, ctx); |
69 |
if (item instanceof MultiPolygon) { |
70 |
|
71 |
/*
|
72 |
* Considering also multi polygons
|
73 |
*/
|
74 |
MultiPolygon mpo = (MultiPolygon) item; |
75 |
for (int k=0; k<mpo.getNumGeometries(); k++) { |
76 |
if (mpo.getGeometryN(k) instanceof Polygon) { |
77 |
prim_list.add(mpo.getGeometryN(k)); |
78 |
} |
79 |
} |
80 |
|
81 |
} else {
|
82 |
if (item instanceof Polygon) { |
83 |
/*
|
84 |
* Including primitives converted to polygon.
|
85 |
* Short primitives can return a point or line
|
86 |
*/
|
87 |
prim_list.add(item); |
88 |
} else {
|
89 |
String txt = (item == null) ? "NULL" : item.getClass().getName(); |
90 |
logger.info("Warning: excluding primitive in MultiSurfaceToJTS because it converted to a JTS " + txt);
|
91 |
} |
92 |
} |
93 |
} catch (GeometryOperationNotSupportedException e) {
|
94 |
throw new GeometryOperationException(e); |
95 |
} |
96 |
} |
97 |
|
98 |
Polygon[] polygons = (Polygon[]) prim_list.toArray(new Polygon[0]); |
99 |
MultiPolygon polygon = new com.vividsolutions.jts.geom.GeometryFactory().createMultiPolygon(polygons);
|
100 |
polygon.setSRID(srid); |
101 |
return polygon;
|
102 |
} |
103 |
} |