svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.geometry / org.gvsig.fmap.geometry.jts / src / main / java / org / gvsig / fmap / geom / jts / spatialindex / AbstractSpatialIndex.java @ 44669
History | View | Annotate | Download (4.97 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.jts.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.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 |
if( this.parameters == null ) { |
72 |
return null; |
73 |
} |
74 |
return this.parameters.getDynValue(name); |
75 |
} |
76 |
|
77 |
public void query(Geometry geom, Visitor visitor) { |
78 |
this.query(geom.getEnvelope(), visitor);
|
79 |
} |
80 |
|
81 |
public Iterator query(Geometry geom) { |
82 |
return this.query(geom.getEnvelope()); |
83 |
} |
84 |
|
85 |
public Iterator query(org.gvsig.fmap.geom.primitive.Envelope envelope) { |
86 |
return this.query(envelope, 0); |
87 |
} |
88 |
|
89 |
public Iterator query(Geometry geom, long limit) { |
90 |
return this.query(geom.getEnvelope(), limit); |
91 |
} |
92 |
|
93 |
public Iterator queryNearest(Envelope envelope) { |
94 |
return this.queryNearest(envelope, 0); |
95 |
} |
96 |
|
97 |
public Iterator queryNearest(Geometry geom) { |
98 |
return this.queryNearest(geom.getEnvelope(), 0); |
99 |
} |
100 |
|
101 |
public Iterator queryNearest(Geometry geom, long limit) { |
102 |
return this.queryNearest(geom.getEnvelope(), limit); |
103 |
} |
104 |
|
105 |
public void insert(Geometry geom, Object data) { |
106 |
this.insert(geom.getEnvelope(), data);
|
107 |
} |
108 |
|
109 |
public void insert(Geometry geom) { |
110 |
this.insert(geom.getEnvelope(), geom);
|
111 |
} |
112 |
|
113 |
public boolean remove(Geometry geom) { |
114 |
return this.remove(geom.getEnvelope(), geom); |
115 |
} |
116 |
|
117 |
public boolean remove(Geometry geom, Object data) { |
118 |
return this.remove(geom.getEnvelope(), data); |
119 |
} |
120 |
|
121 |
public List queryAsList(Envelope envelope) { |
122 |
return asList(query(envelope));
|
123 |
} |
124 |
|
125 |
public List queryAsList(Geometry geom) { |
126 |
return asList(query(geom));
|
127 |
} |
128 |
|
129 |
public List queryAllAsList() { |
130 |
return asList(queryAll());
|
131 |
} |
132 |
|
133 |
protected List asList(Iterator it) { |
134 |
List l = new ArrayList(); |
135 |
while (it.hasNext()) {
|
136 |
l.add(it.next()); |
137 |
} |
138 |
return l;
|
139 |
} |
140 |
|
141 |
protected Object coerceData(Object data) { |
142 |
if( data == null ) { |
143 |
return data;
|
144 |
} |
145 |
|
146 |
if( this.coercion == null ) { |
147 |
int dataType = this.getFactory().getDataTypeSupported(); |
148 |
DataTypesManager typeManager = ToolsLocator.getDataTypesManager(); |
149 |
this.coercion = typeManager.getCoercion(dataType);
|
150 |
} |
151 |
try {
|
152 |
return this.coercion.coerce(data); |
153 |
} catch (CoercionException ex) {
|
154 |
logger.warn("Can't coerce data to the index date type.",ex);
|
155 |
throw new IllegalArgumentException("Can't coerce data to the index date type.",ex); |
156 |
} |
157 |
} |
158 |
|
159 |
public void flush() { |
160 |
// Do nothing, overwrite in each implementation if need
|
161 |
} |
162 |
|
163 |
|
164 |
} |