Statistics
| Revision:

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 / SpatialIndexJSIRTree.java @ 43513

History | View | Annotate | Download (4.84 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 com.infomatiq.jsi.IntProcedure;
26
import java.util.ArrayList;
27
import java.util.Iterator;
28
import java.util.List;
29

    
30
import org.gvsig.fmap.geom.SpatialIndex;
31
import org.gvsig.fmap.geom.SpatialIndexFactory;
32
import org.gvsig.fmap.geom.primitive.Envelope;
33
import org.gvsig.tools.visitor.Visitor;
34

    
35
import com.infomatiq.jsi.Rectangle;
36
import com.infomatiq.jsi.rtree.RTree;
37
import java.util.Collection;
38
import java.util.Properties;
39
import org.gvsig.fmap.geom.GeometryManager;
40
import org.gvsig.fmap.geom.primitive.Point;
41
import org.gvsig.tools.dynobject.DynObject;
42
import org.gvsig.tools.exception.BaseException;
43

    
44
public class SpatialIndexJSIRTree extends AbstractSpatialIndex implements SpatialIndex {
45

    
46
    private RTree index = null;
47

    
48
    class Values implements IntProcedure {
49

    
50
        List elements = new ArrayList();
51

    
52
        public boolean execute(int arg0) {
53
            elements.add(new Integer(arg0));
54
            return true;
55
        }
56

    
57
        public List getElements() {
58
            return elements;
59
        }
60
    }
61

    
62
    public SpatialIndexJSIRTree(GeometryManager geometryManager, SpatialIndexFactory factory, DynObject parameters) {
63
        super(geometryManager, factory, parameters);
64
        this.index = null;
65
        this.open();
66
    }
67

    
68
    public void open() {
69
        this.index = new RTree();
70
        Properties props = new Properties();
71
        if( this.getParameter("MaxNodeEntries")!=null ) {
72
            props.setProperty("MaxNodeEntries", ((Integer)this.getParameter("MaxNodeEntries")).toString());
73
        }
74
        if( this.getParameter("MinNodeEntries")!=null ) {
75
            props.setProperty("MinNodeEntries", ((Integer)this.getParameter("MinNodeEntries")).toString());
76
        }
77
        this.index.init(props);
78
    }
79

    
80
    public void close() {
81
        this.index = null;
82
    }
83

    
84
    private Rectangle asJSI(Envelope envelope) {
85
        Point min = envelope.getLowerCorner();
86
        Point max = envelope.getUpperCorner();
87

    
88
        Rectangle rectangle =
89
            new Rectangle((float) min.getX(), (float) min.getY(),
90
                (float) max.getX(), (float) max.getY());
91
        return rectangle;
92
    }
93

    
94
    public long size() {
95
        return this.index.size();
96
    }
97

    
98
    public void query(Envelope envelope, final Visitor visitor) {
99
        this.index.intersects(asJSI(envelope), new IntProcedure() {
100
            public boolean execute(int i) {
101
                try {
102
                    visitor.visit(new Integer(i));
103
                } catch (BaseException ex) {
104
                    return false;
105
                }
106
                return true;
107
            }
108
        });
109
    }
110

    
111
    public Iterator query(Envelope envelope, final long limit) {
112
        final List results = new ArrayList();
113

    
114
        this.index.intersects(asJSI(envelope), new IntProcedure() {
115
            public boolean execute(int i) {
116
                results.add(new Integer(i));
117
                return results.size()<limit;
118
            }
119
        });
120
        return results.iterator();
121
    }
122

    
123
    public Iterator queryNearest(Envelope envelope, long limit) {
124
        Collection results = this.index.nearest(asJSI(envelope), (int)limit);
125
        return results.iterator();
126
    }
127

    
128
    public Iterator queryAll() {
129
        return this.index.iterator();
130
    }
131

    
132
    public void insert(Envelope envelope, Object data) {
133
        Integer number = (Integer) this.coerceData(data);
134
        if( number == null ) {
135
            throw new IllegalArgumentException("null data are not allowed.");
136
        }
137
        this.index.add(asJSI(envelope), number.intValue());
138
    }
139

    
140
    public boolean remove(Envelope envelope, Object data) {
141
        Integer number = (Integer) this.coerceData(data);
142
        if( number == null ) {
143
            throw new IllegalArgumentException("null data are not allowed.");
144
        }
145
        return this.index.delete(asJSI(envelope), number.intValue());
146
    }
147

    
148
    public void removeAll() {
149
        this.close();
150
        this.open();
151
    }
152

    
153

    
154
}