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