Statistics
| Revision:

root / trunk / applications / appCatalogAndGazetteerClient / src / es / gva / cit / gazetteer / utils / GazetteerGeometriesFactory.java @ 15558

History | View | Annotate | Download (4.65 KB)

1
package es.gva.cit.gazetteer.utils;
2

    
3
import java.awt.geom.Point2D;
4
import java.util.ArrayList;
5
import java.util.Map;
6

    
7
import org.apache.log4j.Logger;
8
import org.gvsig.remoteClient.gml.factories.IGeometriesFactory;
9
import org.gvsig.remoteClient.gml.types.IXMLType;
10

    
11
import com.vividsolutions.jts.geom.Geometry;
12
import com.vividsolutions.jts.geom.GeometryFactory;
13
import com.vividsolutions.jts.geom.Point;
14
import com.vividsolutions.jts.io.gml2.GMLReader;
15

    
16
import es.gva.cit.gazetteer.querys.Feature;
17

    
18
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
19
 *
20
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
21
 *
22
 * This program is free software; you can redistribute it and/or
23
 * modify it under the terms of the GNU General Public License
24
 * as published by the Free Software Foundation; either version 2
25
 * of the License, or (at your option) any later version.
26
 *
27
 * This program is distributed in the hope that it will be useful,
28
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30
 * GNU General Public License for more details.
31
 *
32
 * You should have received a copy of the GNU General Public License
33
 * along with this program; if not, write to the Free Software
34
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
35
 *
36
 * For more information, contact:
37
 *
38
 *  Generalitat Valenciana
39
 *   Conselleria d'Infraestructures i Transport
40
 *   Av. Blasco Ib??ez, 50
41
 *   46010 VALENCIA
42
 *   SPAIN
43
 *
44
 *      +34 963862235
45
 *   gvsig@gva.es
46
 *      www.gvsig.gva.es
47
 *
48
 *    or
49
 *
50
 *   IVER T.I. S.A
51
 *   Salamanca 50
52
 *   46005 Valencia
53
 *   Spain
54
 *
55
 *   +34 963163400
56
 *   dac@iver.es
57
 */
58
/* CVS MESSAGES:
59
 *
60
 * $Id: GazetteerGeometriesFactory.java 572 2007-07-27 12:40:11 +0000 (Fri, 27 Jul 2007) jpiera $
61
 * $Log$
62
 * Revision 1.1.2.2  2007/07/13 12:00:35  jorpiell
63
 * Add the posibility to add a new panel
64
 *
65
 * Revision 1.1.2.1  2007/07/10 11:18:04  jorpiell
66
 * Added the registers
67
 *
68
 *
69
 */
70
/**
71
 * @author Jorge Piera LLodr? (jorge.piera@iver.es)
72
 */
73
public class GazetteerGeometriesFactory implements IGeometriesFactory{
74
        private static Logger logger = Logger.getLogger(GazetteerGeometriesFactory.class);
75
        private String attName = null;
76
                
77
        public GazetteerGeometriesFactory(String attName) {
78
                super();
79
                this.attName = attName;
80
        }
81

    
82
        /*
83
         * (non-Javadoc)
84
         * @see org.gvsig.remoteClient.gml.factories.IGeometriesFactory#createGeometry(java.lang.String)
85
         */
86
        public Object createGeometry(String gmlGeometry) {
87
                GMLReader reader = new GMLReader();
88
                try {
89
                        Geometry geom = reader.read(gmlGeometry,new GeometryFactory());
90
                        Point point = geom.getInteriorPoint();
91
                        return new Point2D.Double(point.getX(),point.getY());
92
                }  catch (Exception e){
93
                        logger.error("Can't parse: " + gmlGeometry,e);
94
                }
95
                return null;
96
        }
97

    
98
        /*
99
         * (non-Javadoc)
100
         * @see org.gvsig.remoteClient.gml.factories.IGeometriesFactory#createMultipoint2D(double[], double[])
101
         */
102
        public Object createMultipoint2D(double[] x, double[] y) {
103
                double xTotal = 0;
104
                double yTotal = 0;
105
                for (int i=0 ; i<x.length ; i++){
106
                        xTotal = xTotal + x[i];
107
                        yTotal = yTotal + y[i];
108
                }
109
                if (x.length > 0){
110
                        return new Point2D.Double(xTotal/x.length,
111
                                        yTotal/y.length);
112
                }
113
                return new Point2D.Double(0,
114
                                0);
115
        }
116

    
117
        /*
118
         * (non-Javadoc)
119
         * @see org.gvsig.remoteClient.gml.factories.IGeometriesFactory#createPoint2D(java.awt.geom.Point2D)
120
         */
121
        public Object createPoint2D(Point2D point) {
122
                return point;
123
        }
124

    
125
        /*
126
         * (non-Javadoc)
127
         * @see org.gvsig.remoteClient.gml.factories.IGeometriesFactory#createPoint2D(double, double)
128
         */
129
        public Object createPoint2D(double x, double y) {
130
                return new Point2D.Double(x,y);
131
        }
132

    
133
        /*
134
         * (non-Javadoc)
135
         * @see org.gvsig.remoteClient.gml.factories.IGeometriesFactory#createSimpleFeature(java.lang.String, org.gvsig.remoteClient.gml.types.IXMLType, java.util.Map, java.lang.Object)
136
         */
137
        public Object createSimpleFeature(String element, IXMLType type,Map values,Object geometry) {
138
                String sGeoname = element;
139
                if (attName != null){
140
                        if (values.get(attName) != null){
141
                                ArrayList array = (ArrayList)values.get(attName);
142
                                if (array.size() > 0){
143
                                        sGeoname = (String)array.get(0);
144
                                }
145
                        }
146
                }                
147
                return new Feature(null,
148
                                sGeoname,
149
                                sGeoname,
150
                                (Point2D)geometry);
151
        }
152
        
153
        /*
154
         * (non-Javadoc)
155
         * @see org.gvsig.remoteClient.gml.factories.IGeometriesFactory#createPolygon2D(double[], double[])
156
         */
157
        public Object createPolygon2D(double[] x, double[] y) {
158
                double avX = 0;
159
                double avY = 0;
160
                for (int i=0 ; i<x.length ; i++){
161
                        avX = avX + x[i];
162
                        avY = avY + y[i];
163
                }
164
                if (x.length > 0){
165
                        avX = avX/x.length;
166
                        avY = avY/y.length;
167
                }
168
                return new Point2D.Double(avX, avY);                
169
        }
170
}