1 |
|
---|
2 |
|
---|
3 | /*
|
---|
4 | * The JCS Conflation Suite (JCS) is a library of Java classes that
|
---|
5 | * can be used to build automated or semi-automated conflation solutions.
|
---|
6 | *
|
---|
7 | * Copyright (C) 2003 Vivid Solutions
|
---|
8 | *
|
---|
9 | * This program is free software; you can redistribute it and/or
|
---|
10 | * modify it under the terms of the GNU General Public License
|
---|
11 | * as published by the Free Software Foundation; either version 2
|
---|
12 | * of the License, or (at your option) any later version.
|
---|
13 | *
|
---|
14 | * This program is distributed in the hope that it will be useful,
|
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
17 | * GNU General Public License for more details.
|
---|
18 | *
|
---|
19 | * You should have received a copy of the GNU General Public License
|
---|
20 | * along with this program; if not, write to the Free Software
|
---|
21 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
---|
22 | *
|
---|
23 | * For more information, contact:
|
---|
24 | *
|
---|
25 | * Vivid Solutions
|
---|
26 | * Suite #1A
|
---|
27 | * 2328 Government Street
|
---|
28 | * Victoria BC V8T 5G5
|
---|
29 | * Canada
|
---|
30 | *
|
---|
31 | * (250)385-6040
|
---|
32 | * www.vividsolutions.com
|
---|
33 | */
|
---|
34 |
|
---|
35 | package com.vividsolutions.jcs.conflate.polygonmatch;
|
---|
36 |
|
---|
37 | import com.vividsolutions.jts.geom.Envelope;
|
---|
38 | import com.vividsolutions.jts.util.Assert;
|
---|
39 | import com.vividsolutions.jump.feature.Feature;
|
---|
40 | import com.vividsolutions.jump.feature.FeatureCollection;
|
---|
41 | import com.vividsolutions.jump.feature.FeatureDataset;
|
---|
42 | import com.vividsolutions.jump.feature.FeatureSchema;
|
---|
43 | import java.util.ArrayList;
|
---|
44 | import java.util.Collection;
|
---|
45 | import java.util.Iterator;
|
---|
46 | import java.util.List;
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * A FeatureCollection that stores the "score" of each Feature. The score is
|
---|
50 | * a number between 0.0 and 1.0 that indicates the confidence of a match.
|
---|
51 | */
|
---|
52 | public class Matches implements FeatureCollection, Cloneable {
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Creates a Matches object.
|
---|
56 | * @param schema metadata applicable to the features that will be stored in
|
---|
57 | * this Matches object
|
---|
58 | */
|
---|
59 | public Matches(FeatureSchema schema) {
|
---|
60 | dataset = new FeatureDataset(schema);
|
---|
61 | }
|
---|
62 |
|
---|
63 | @Override
|
---|
64 | protected Object clone() {
|
---|
65 | Matches clone = new Matches(dataset.getFeatureSchema());
|
---|
66 | for (int i = 0; i < size(); i++) {
|
---|
67 | clone.add(getFeature(i), getScore(i));
|
---|
68 | }
|
---|
69 | return clone;
|
---|
70 | }
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * Creates a Matches object, initialized with the given Feature's.
|
---|
74 | * @param schema metadata applicable to the features that will be stored in
|
---|
75 | * this Matches object
|
---|
76 | * @param features added to the Matches, each with the max score (1.0)
|
---|
77 | */
|
---|
78 | public Matches(FeatureSchema schema, List features) {
|
---|
79 | this(schema);
|
---|
80 | for (Iterator i = features.iterator(); i.hasNext();) {
|
---|
81 | Feature match = (Feature) i.next();
|
---|
82 | add(match, 1);
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | private FeatureDataset dataset;
|
---|
87 | private ArrayList scores = new ArrayList();
|
---|
88 |
|
---|
89 | /**
|
---|
90 | * This method is not supported, because added features need to be associated
|
---|
91 | * with a score. Use #add(Feature, double) instead.
|
---|
92 | * @param feature a feature to add as a match
|
---|
93 | * @see #add(Feature, double)
|
---|
94 | */
|
---|
95 | public void add(Feature feature) {
|
---|
96 | throw new UnsupportedOperationException("Use #add(feature, score) instead");
|
---|
97 | }
|
---|
98 |
|
---|
99 | /**
|
---|
100 | * This method is not supported, because added features need to be associated
|
---|
101 | * with a score. Use #add(Feature, double) instead.
|
---|
102 | */
|
---|
103 | public void addAll(Collection features) {
|
---|
104 | throw new UnsupportedOperationException("Use #add(feature, score) instead");
|
---|
105 | }
|
---|
106 |
|
---|
107 | /**
|
---|
108 | * This method is not supported, because added features need to be associated
|
---|
109 | * with a score. Use #add(Feature, double) instead.
|
---|
110 | * @param feature a feature to add as a match
|
---|
111 | * @see #add(Feature, double)
|
---|
112 | */
|
---|
113 | public void add(int index, Feature feature) {
|
---|
114 | throw new UnsupportedOperationException("Use #add(feature, score) instead");
|
---|
115 | }
|
---|
116 |
|
---|
117 | /**
|
---|
118 | * This method is not supported, because Matches should not normally need to
|
---|
119 | * have matches removed.
|
---|
120 | */
|
---|
121 | public Collection remove(Envelope envelope) {
|
---|
122 | //If we decide to implement this, remember to remove the corresponding
|
---|
123 | //score. [Jon Aquino]
|
---|
124 | throw new UnsupportedOperationException();
|
---|
125 | }
|
---|
126 |
|
---|
127 | /**
|
---|
128 | * This method is not supported, because Matches should not normally need to
|
---|
129 | * have matches removed.
|
---|
130 | */
|
---|
131 | public void clear() {
|
---|
132 | //If we decide to implement this, remember to remove the corresponding
|
---|
133 | //score. [Jon Aquino]
|
---|
134 | throw new UnsupportedOperationException();
|
---|
135 | }
|
---|
136 |
|
---|
137 | /**
|
---|
138 | * This method is not supported, because Matches should not normally need to
|
---|
139 | * have matches removed.
|
---|
140 | */
|
---|
141 | public void removeAll(Collection features) {
|
---|
142 | //If we decide to implement this, remember to remove the corresponding
|
---|
143 | //score. [Jon Aquino]
|
---|
144 | throw new UnsupportedOperationException();
|
---|
145 | }
|
---|
146 |
|
---|
147 | /**
|
---|
148 | * This method is not supported, because Matches should not normally need to
|
---|
149 | * have matches removed.
|
---|
150 | * @param feature a feature to remove
|
---|
151 | */
|
---|
152 | public void remove(Feature feature) {
|
---|
153 | //If we decide to implement this, remember to remove the corresponding
|
---|
154 | //score. [Jon Aquino]
|
---|
155 | throw new UnsupportedOperationException();
|
---|
156 | }
|
---|
157 | /**
|
---|
158 | * Adds a match. Features with zero-scores are ignored.
|
---|
159 | * @param feature a feature to add as a match
|
---|
160 | * @param score the confidence of the match, ranging from 0 to 1
|
---|
161 | */
|
---|
162 | public void add(Feature feature, double score) {
|
---|
163 | Assert.isTrue(0 <= score && score <= 1, "Score = " + score);
|
---|
164 | if (score == 0) {
|
---|
165 | return;
|
---|
166 | }
|
---|
167 | scores.add(new Double(score));
|
---|
168 | dataset.add(feature);
|
---|
169 | if (score > topScore) {
|
---|
170 | topScore = score;
|
---|
171 | topMatch = feature;
|
---|
172 | }
|
---|
173 | }
|
---|
174 |
|
---|
175 | private Feature topMatch;
|
---|
176 | private double topScore = 0;
|
---|
177 |
|
---|
178 | public double getTopScore() {
|
---|
179 | return topScore;
|
---|
180 | }
|
---|
181 |
|
---|
182 | /**
|
---|
183 | * @return the feature with the highest score
|
---|
184 | */
|
---|
185 | public Feature getTopMatch() {
|
---|
186 | return topMatch;
|
---|
187 | }
|
---|
188 |
|
---|
189 | /**
|
---|
190 | * Returns the score of the ith feature
|
---|
191 | * @param i 0, 1, 2, ...
|
---|
192 | * @return the confidence of the ith match
|
---|
193 | */
|
---|
194 | public double getScore(int i) {
|
---|
195 | return ((Double) scores.get(i)).doubleValue();
|
---|
196 | }
|
---|
197 |
|
---|
198 | public FeatureSchema getFeatureSchema() {
|
---|
199 | return dataset.getFeatureSchema();
|
---|
200 | }
|
---|
201 |
|
---|
202 | public Envelope getEnvelope() {
|
---|
203 | return dataset.getEnvelope();
|
---|
204 | }
|
---|
205 |
|
---|
206 | public int size() {
|
---|
207 | return dataset.size();
|
---|
208 | }
|
---|
209 |
|
---|
210 | public boolean isEmpty() {
|
---|
211 | return dataset.isEmpty();
|
---|
212 | }
|
---|
213 |
|
---|
214 | public Feature getFeature(int index) {
|
---|
215 | return dataset.getFeature(index);
|
---|
216 | }
|
---|
217 |
|
---|
218 | public List getFeatures() {
|
---|
219 | return dataset.getFeatures();
|
---|
220 | }
|
---|
221 |
|
---|
222 | public Iterator iterator() {
|
---|
223 | return dataset.iterator();
|
---|
224 | }
|
---|
225 |
|
---|
226 | public List query(Envelope envelope) {
|
---|
227 | return dataset.query(envelope);
|
---|
228 | }
|
---|
229 | }
|
---|