svn-gvsig-desktop / branches / v2_0_0_prep / applications / appGazetteer / src / org / gvsig / gazetteer / utils / GazetteerGeometriesFactory.java @ 34026
History | View | Annotate | Download (4.61 KB)
1 |
package org.gvsig.gazetteer.utils; |
---|---|
2 |
|
3 |
import java.awt.geom.Point2D; |
4 |
import java.util.ArrayList; |
5 |
import java.util.Map; |
6 |
|
7 |
import com.vividsolutions.jts.geom.Geometry; |
8 |
import com.vividsolutions.jts.geom.GeometryFactory; |
9 |
import com.vividsolutions.jts.geom.Point; |
10 |
import com.vividsolutions.jts.io.gml2.GMLReader; |
11 |
|
12 |
import org.slf4j.Logger; |
13 |
import org.slf4j.LoggerFactory; |
14 |
|
15 |
import org.gvsig.gazetteer.querys.Feature; |
16 |
import org.gvsig.xmlschema.lib.api.som.IXSTypeDefinition; |
17 |
|
18 |
|
19 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
20 |
*
|
21 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
22 |
*
|
23 |
* This program is free software; you can redistribute it and/or
|
24 |
* modify it under the terms of the GNU General Public License
|
25 |
* as published by the Free Software Foundation; either version 2
|
26 |
* of the License, or (at your option) any later version.
|
27 |
*
|
28 |
* This program is distributed in the hope that it will be useful,
|
29 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
30 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
31 |
* GNU General Public License for more details.
|
32 |
*
|
33 |
* You should have received a copy of the GNU General Public License
|
34 |
* along with this program; if not, write to the Free Software
|
35 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
36 |
*
|
37 |
* For more information, contact:
|
38 |
*
|
39 |
* Generalitat Valenciana
|
40 |
* Conselleria d'Infraestructures i Transport
|
41 |
* Av. Blasco Ib??ez, 50
|
42 |
* 46010 VALENCIA
|
43 |
* SPAIN
|
44 |
*
|
45 |
* +34 963862235
|
46 |
* gvsig@gva.es
|
47 |
* www.gvsig.gva.es
|
48 |
*
|
49 |
* or
|
50 |
*
|
51 |
* IVER T.I. S.A
|
52 |
* Salamanca 50
|
53 |
* 46005 Valencia
|
54 |
* Spain
|
55 |
*
|
56 |
* +34 963163400
|
57 |
* dac@iver.es
|
58 |
*/
|
59 |
/* CVS MESSAGES:
|
60 |
*
|
61 |
* $Id: GazetteerGeometriesFactory.java 572 2007-07-27 12:40:11 +0000 (Fri, 27 Jul 2007) jpiera $
|
62 |
* $Log$
|
63 |
* Revision 1.1.2.2 2007/07/13 12:00:35 jorpiell
|
64 |
* Add the posibility to add a new panel
|
65 |
*
|
66 |
* Revision 1.1.2.1 2007/07/10 11:18:04 jorpiell
|
67 |
* Added the registers
|
68 |
*
|
69 |
*
|
70 |
*/
|
71 |
/**
|
72 |
* @author Jorge Piera LLodr? (jorge.piera@iver.es)
|
73 |
*/
|
74 |
public class GazetteerGeometriesFactory { |
75 |
private static final Logger logger = LoggerFactory.getLogger(GazetteerGeometriesFactory.class); |
76 |
private String attName = null; |
77 |
|
78 |
public GazetteerGeometriesFactory(String attName) { |
79 |
super();
|
80 |
this.attName = attName;
|
81 |
} |
82 |
|
83 |
/*
|
84 |
* (non-Javadoc)
|
85 |
* @see org.gvsig.remoteClient.gml.factories.IGeometriesFactory#createGeometry(java.lang.String)
|
86 |
*/
|
87 |
public Object createGeometry(String gmlGeometry) { |
88 |
GMLReader reader = new GMLReader();
|
89 |
try {
|
90 |
Geometry geom = reader.read(gmlGeometry,new GeometryFactory());
|
91 |
Point point = geom.getInteriorPoint();
|
92 |
return new Point2D.Double(point.getX(),point.getY()); |
93 |
} catch (Exception e){ |
94 |
logger.error("Can't parse: " + gmlGeometry,e);
|
95 |
} |
96 |
return null; |
97 |
} |
98 |
|
99 |
/*
|
100 |
* (non-Javadoc)
|
101 |
* @see org.gvsig.remoteClient.gml.factories.IGeometriesFactory#createMultipoint2D(double[], double[])
|
102 |
*/
|
103 |
public Object createMultipoint2D(double[] x, double[] y) { |
104 |
double xTotal = 0; |
105 |
double yTotal = 0; |
106 |
for (int i=0 ; i<x.length ; i++){ |
107 |
xTotal = xTotal + x[i]; |
108 |
yTotal = yTotal + y[i]; |
109 |
} |
110 |
if (x.length > 0){ |
111 |
return new Point2D.Double(xTotal/x.length, |
112 |
yTotal/y.length); |
113 |
} |
114 |
return new Point2D.Double(0, |
115 |
0);
|
116 |
} |
117 |
|
118 |
/*
|
119 |
* (non-Javadoc)
|
120 |
* @see org.gvsig.remoteClient.gml.factories.IGeometriesFactory#createPoint2D(java.awt.geom.Point2D)
|
121 |
*/
|
122 |
public Object createPoint2D(Point2D point) { |
123 |
return point;
|
124 |
} |
125 |
|
126 |
/*
|
127 |
* (non-Javadoc)
|
128 |
* @see org.gvsig.remoteClient.gml.factories.IGeometriesFactory#createPoint2D(double, double)
|
129 |
*/
|
130 |
public Object createPoint2D(double x, double y) { |
131 |
return new Point2D.Double(x,y); |
132 |
} |
133 |
|
134 |
/*
|
135 |
* (non-Javadoc)
|
136 |
* @see org.gvsig.remoteClient.gml.factories.IGeometriesFactory#createSimpleFeature(java.lang.String, org.gvsig.remoteClient.gml.types.IXMLType, java.util.Map, java.lang.Object)
|
137 |
*/
|
138 |
public Object createSimpleFeature(String element, IXSTypeDefinition type,Map values,Object geometry) { |
139 |
String sGeoname = element;
|
140 |
if (attName != null){ |
141 |
if (values.get(attName) != null){ |
142 |
ArrayList array = (ArrayList)values.get(attName); |
143 |
if (array.size() > 0){ |
144 |
sGeoname = (String)array.get(0); |
145 |
} |
146 |
} |
147 |
} |
148 |
return new Feature(null, |
149 |
sGeoname, |
150 |
sGeoname, |
151 |
(Point2D)geometry);
|
152 |
} |
153 |
|
154 |
/*
|
155 |
* (non-Javadoc)
|
156 |
* @see org.gvsig.remoteClient.gml.factories.IGeometriesFactory#createPolygon2D(double[], double[])
|
157 |
*/
|
158 |
public Object createPolygon2D(double[] x, double[] y) { |
159 |
double avX = 0; |
160 |
double avY = 0; |
161 |
for (int i=0 ; i<x.length ; i++){ |
162 |
avX = avX + x[i]; |
163 |
avY = avY + y[i]; |
164 |
} |
165 |
if (x.length > 0){ |
166 |
avX = avX/x.length; |
167 |
avY = avY/y.length; |
168 |
} |
169 |
return new Point2D.Double(avX, avY); |
170 |
} |
171 |
} |