Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.geometry / org.gvsig.fmap.geometry.impl / src / main / java / org / gvsig / fmap / geom / impl / spatialIndex / AbstractSpatialIndex.java @ 41246

History | View | Annotate | Download (4.92 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 modify it under
7
 * the terms of the GNU General Public License as published by the Free Software
8
 * Foundation; either version 3 of the License, or (at your option) any later
9
 * version.
10
 *
11
 * This program is distributed in the hope that it will be useful, but WITHOUT
12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14
 * details.
15
 *
16
 * You should have received a copy of the GNU General Public License along with
17
 * this program; if not, write to the Free Software Foundation, Inc., 51
18
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
 *
20
 * For any additional information, do not hesitate to contact us at info AT
21
 * gvsig.com, or visit our website www.gvsig.com.
22
 */
23
package org.gvsig.fmap.geom.impl.spatialIndex;
24

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

    
29
import org.gvsig.fmap.geom.Geometry;
30
import org.gvsig.fmap.geom.GeometryManager;
31
import org.gvsig.fmap.geom.SpatialIndex;
32
import org.gvsig.fmap.geom.SpatialIndexFactory;
33
import org.gvsig.fmap.geom.primitive.Envelope;
34
import org.gvsig.tools.ToolsLocator;
35
import org.gvsig.tools.dataTypes.CoercionException;
36
import org.gvsig.tools.dataTypes.DataTypesManager;
37
import org.gvsig.tools.dataTypes.DataTypesManager.Coercion;
38
import org.gvsig.tools.dynobject.DynObject;
39
import org.gvsig.tools.service.Manager;
40
import org.gvsig.tools.visitor.Visitor;
41
import org.slf4j.Logger;
42
import org.slf4j.LoggerFactory;
43

    
44
public abstract class AbstractSpatialIndex implements SpatialIndex {
45
    private static Logger logger = LoggerFactory.getLogger(AbstractSpatialIndex.class);
46
    
47
    private SpatialIndexFactory factory = null;
48
    private GeometryManager manager = null;
49
    private DynObject parameters = null;
50
    private Coercion coercion = null;
51

    
52
    public AbstractSpatialIndex(GeometryManager geometryManager, SpatialIndexFactory factory, DynObject parameters) {
53
        this.factory = factory;
54
        this.manager = geometryManager;
55
        this.parameters = parameters;
56
    }
57
    
58
    public Manager getManager() {
59
        return this.manager;
60
    }
61
            
62
    public SpatialIndexFactory getFactory() {
63
        return this.factory;
64
    }
65

    
66
    public DynObject getParameters() {
67
        return this.parameters;
68
    }
69
    
70
    public Object getParameter(String name) {
71
        return this.parameters.getDynValue(name);
72
    }
73
    
74
    public void query(Geometry geom, Visitor visitor) {
75
        this.query(geom.getEnvelope(), visitor);
76
    }
77

    
78
    public Iterator query(Geometry geom) {
79
        return this.query(geom.getEnvelope());
80
    }
81

    
82
    public Iterator query(org.gvsig.fmap.geom.primitive.Envelope envelope) {
83
        return this.query(envelope, 0);
84
    }
85

    
86
    public Iterator query(Geometry geom, long limit) {
87
        return this.query(geom.getEnvelope(), limit);
88
    }
89

    
90
    public Iterator queryNearest(Envelope envelope) {
91
        return this.queryNearest(envelope, 0);
92
    }
93

    
94
    public Iterator queryNearest(Geometry geom) {
95
        return this.queryNearest(geom.getEnvelope(), 0);
96
    }
97

    
98
    public Iterator queryNearest(Geometry geom, long limit) {
99
        return this.queryNearest(geom.getEnvelope(), limit);
100
    }
101

    
102
    public void insert(Geometry geom, Object data) {
103
        this.insert(geom.getEnvelope(), data);
104
    }
105

    
106
    public void insert(Geometry geom) {
107
        this.insert(geom.getEnvelope(), geom);
108
    }
109

    
110
    public boolean remove(Geometry geom) {
111
        return this.remove(geom.getEnvelope(), geom);
112
    }
113

    
114
    public boolean remove(Geometry geom, Object data) {
115
        return this.remove(geom.getEnvelope(), data);
116
    }
117

    
118
    public List queryAsList(Envelope envelope) {
119
        return asList(query(envelope));
120
    }
121

    
122
    public List queryAsList(Geometry geom) {
123
        return asList(query(geom));
124
    }
125

    
126
    public List queryAllAsList() {
127
        return asList(queryAll());
128
    }
129

    
130
    protected List asList(Iterator it) {
131
        List l = new ArrayList();
132
        while (it.hasNext()) {
133
            l.add(it.next());
134
        }
135
        return l;
136
    }
137
    
138
    protected Object coerceData(Object data) {
139
        if( data == null ) {
140
            return data;
141
        }
142
        
143
        if( this.coercion == null ) {
144
            int dataType = this.getFactory().getDataTypeSupported();
145
            DataTypesManager typeManager = ToolsLocator.getDataTypesManager();
146
            this.coercion = typeManager.getCoercion(dataType);
147
        }
148
        try {
149
            return this.coercion.coerce(data);
150
        } catch (CoercionException ex) {
151
            logger.warn("Can't coerce data to the index date type.",ex);
152
            throw new IllegalArgumentException("Can't coerce data to the index date type.",ex);
153
        }
154
    }
155

    
156
    public void flush() {
157
        // Do nothing, overwrite in each implementation if need
158
    }
159

    
160
    
161
}