1
|
/**
|
2
|
* '$RCSfile$'
|
3
|
* Copyright: 2003 Regents of the University of California.
|
4
|
*
|
5
|
* Author: Matthew Perry
|
6
|
* '$Date: 2006-09-11 14:22:51 -0700 (Mon, 11 Sep 2006) $'
|
7
|
* '$Revision: 3040 $'
|
8
|
*
|
9
|
* This program is free software; you can redistribute it and/or modify
|
10
|
* it under the terms of the GNU General Public License as published by
|
11
|
* the Free Software Foundation; either version 2 of the License, or
|
12
|
* (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
|
package edu.ucsb.nceas.metacat.spatial;
|
24
|
|
25
|
import org.geotools.feature.FeatureType;
|
26
|
import org.geotools.data.shapefile.ShapefileDataStore;
|
27
|
import org.geotools.data.FeatureStore;
|
28
|
import org.geotools.data.DefaultTransaction;
|
29
|
import org.geotools.data.Transaction;
|
30
|
import org.geotools.feature.Feature;
|
31
|
import org.geotools.filter.Filter;
|
32
|
import org.geotools.filter.CompareFilter;
|
33
|
import org.geotools.filter.FilterFactory;
|
34
|
import org.geotools.filter.FilterFactoryFinder;
|
35
|
import org.geotools.filter.IllegalFilterException;
|
36
|
import org.geotools.feature.FeatureCollection;
|
37
|
import org.geotools.feature.FeatureCollections;
|
38
|
|
39
|
import org.apache.log4j.Logger;
|
40
|
|
41
|
import java.io.File;
|
42
|
import java.io.IOException;
|
43
|
|
44
|
import java.net.URI;
|
45
|
import java.net.URL;
|
46
|
import java.net.MalformedURLException;
|
47
|
|
48
|
import edu.ucsb.nceas.metacat.MetaCatUtil;
|
49
|
|
50
|
/**
|
51
|
* Class providing direct read/write access to the
|
52
|
* persistent spatial cache.
|
53
|
*/
|
54
|
public class SpatialDataset {
|
55
|
|
56
|
SpatialFeatureSchema featureSchema = new SpatialFeatureSchema();
|
57
|
|
58
|
//public String polygonShpUri = MetaCatUtil.getOption("certPath") + "data/metacat_shps/data_bounds.shp";
|
59
|
//public String pointShpUri = MetaCatUtil.getOption("certPath")+ "data/metacat_shps/data_points.shp";
|
60
|
|
61
|
FeatureCollection polygonCollection = FeatureCollections.newCollection();
|
62
|
FeatureCollection pointCollection = FeatureCollections.newCollection();
|
63
|
|
64
|
private static Logger log =
|
65
|
Logger.getLogger(SpatialDataset.class.getName());
|
66
|
|
67
|
/**
|
68
|
* empty constructor for SpatialDataset
|
69
|
*/
|
70
|
public SpatialDataset() {
|
71
|
|
72
|
}
|
73
|
|
74
|
/**
|
75
|
* Adds a new feature (from a SpatialDocument)
|
76
|
* This is faster than insertOrUpdate but
|
77
|
* relying on this method might cause duplication
|
78
|
* of docids in the spatial cache. Therefore, its really only useful when
|
79
|
* regenerating the entire cache.
|
80
|
*
|
81
|
* @param geomType The kind of feature to be added. Currently "polygon" and "point" supported.
|
82
|
* @param feature The geotools feature to be added to the spatial cache.
|
83
|
*/
|
84
|
public void add( String geomType, Feature feature ) {
|
85
|
if( geomType.equals("polygon") ) {
|
86
|
// Check if feature actually is a multipolygon
|
87
|
if( feature != null)
|
88
|
polygonCollection.add( feature );
|
89
|
|
90
|
} else if( geomType.equals("point") ) {
|
91
|
// Check if feature actually is a multipoint
|
92
|
if( feature != null)
|
93
|
pointCollection.add( feature );
|
94
|
}
|
95
|
}
|
96
|
|
97
|
/**
|
98
|
* Deletes given docid from the spatial cache.
|
99
|
*
|
100
|
* @param geomType The kind of feature to be added. Currently "polygon" and "point" supported.
|
101
|
* @param docid The document to be deleted from the spatial cache.
|
102
|
*/
|
103
|
public void delete( String geomType, String docid ) throws IOException {
|
104
|
|
105
|
FilterFactory filterFactory = FilterFactoryFinder.createFilterFactory();
|
106
|
CompareFilter filter = null;
|
107
|
FeatureStore fStore = null;
|
108
|
ShapefileDataStore dStore = null;
|
109
|
|
110
|
try {
|
111
|
// Create the filter
|
112
|
filter = filterFactory.createCompareFilter(CompareFilter.COMPARE_EQUALS);
|
113
|
filter.addLeftValue(filterFactory.createAttributeExpression("docid"));
|
114
|
filter.addRightValue(filterFactory.createLiteralExpression(docid));
|
115
|
} catch (IllegalFilterException e) {
|
116
|
e.printStackTrace();
|
117
|
}
|
118
|
|
119
|
// Begin new transaction
|
120
|
Transaction t = new DefaultTransaction("handle");
|
121
|
t.putProperty( "updating spatial cache", new Integer(7) );
|
122
|
String lockId = "SpatialDataset.delete";
|
123
|
|
124
|
try {
|
125
|
|
126
|
boolean validGeomType = false;
|
127
|
|
128
|
if( geomType.equals("polygon") ) {
|
129
|
dStore = new ShapefileDataStore( (new File( featureSchema.polygonShpUri )).toURL() );
|
130
|
fStore = (FeatureStore) dStore.getFeatureSource(dStore.getTypeNames()[0]);
|
131
|
validGeomType = true;
|
132
|
} else if( geomType.equals("point") ) {
|
133
|
dStore = new ShapefileDataStore( (new File( featureSchema.pointShpUri )).toURL() );
|
134
|
fStore = (FeatureStore) dStore.getFeatureSource(dStore.getTypeNames()[0]);
|
135
|
validGeomType = true;
|
136
|
}
|
137
|
|
138
|
// Initiate the transaction
|
139
|
fStore.setTransaction( t );
|
140
|
t.addAuthorization( lockId );
|
141
|
|
142
|
if( validGeomType == true) {
|
143
|
// Remove old feature
|
144
|
fStore.removeFeatures( filter );
|
145
|
|
146
|
// Commit changes to shapefile
|
147
|
t.commit();
|
148
|
log.info(" Delete docid " + docid + " from spatial cache");
|
149
|
}
|
150
|
|
151
|
} catch (MalformedURLException e) {
|
152
|
e.printStackTrace();
|
153
|
t.rollback(); // cancel opperations
|
154
|
} catch (IOException e) {
|
155
|
e.printStackTrace();
|
156
|
t.rollback(); // cancel opperations
|
157
|
} finally {
|
158
|
// Close out the transaction
|
159
|
t.close();
|
160
|
}
|
161
|
|
162
|
}
|
163
|
|
164
|
/**
|
165
|
* Either inserts or updates the spatial cache with the new
|
166
|
* spatial document depending on if it currently exists.
|
167
|
* Docid is also passed in for quicker searching.
|
168
|
*
|
169
|
* @param geomType The kind of feature to be added. Currently "polygon" and "point" supported.
|
170
|
* @param feature The geotools feature to be added to the spatial cache.
|
171
|
* @param docid The document id to be inserted or updated. Used to filter for existing features.
|
172
|
*/
|
173
|
public void insertOrUpdate( String geomType, Feature feature, String docid ) throws IOException {
|
174
|
|
175
|
FeatureCollection fColl = FeatureCollections.newCollection();
|
176
|
FilterFactory filterFactory = FilterFactoryFinder.createFilterFactory();
|
177
|
CompareFilter filter = null;
|
178
|
FeatureStore fStore = null;
|
179
|
ShapefileDataStore dStore = null;
|
180
|
|
181
|
// Explain why geotools fails to create the projection info from the shapefile
|
182
|
//log.info( " The '.prj' errors below are related to a geotools bug " +
|
183
|
// " (http://jira.codehaus.org/browse/GEOT-604) and can be ignored");
|
184
|
|
185
|
try {
|
186
|
// Create the filter
|
187
|
filter = filterFactory.createCompareFilter(CompareFilter.COMPARE_EQUALS);
|
188
|
filter.addLeftValue(filterFactory.createAttributeExpression("docid"));
|
189
|
filter.addRightValue(filterFactory.createLiteralExpression(docid));
|
190
|
} catch (IllegalFilterException e) {
|
191
|
e.printStackTrace();
|
192
|
}
|
193
|
|
194
|
// Begin new transaction
|
195
|
Transaction t = new DefaultTransaction("handle");
|
196
|
t.putProperty( "updating spatial cache", new Integer(7) );
|
197
|
String lockId = "SpatialDataset.insertOrUpdate";
|
198
|
|
199
|
try {
|
200
|
|
201
|
boolean validGeomType = false;
|
202
|
|
203
|
if( geomType.equals("polygon") ) {
|
204
|
dStore = new ShapefileDataStore( (new File( featureSchema.polygonShpUri )).toURL() );
|
205
|
fStore = (FeatureStore) dStore.getFeatureSource(dStore.getTypeNames()[0]);
|
206
|
validGeomType = true;
|
207
|
} else if( geomType.equals("point") ) {
|
208
|
dStore = new ShapefileDataStore( (new File( featureSchema.pointShpUri )).toURL() );
|
209
|
fStore = (FeatureStore) dStore.getFeatureSource(dStore.getTypeNames()[0]);
|
210
|
validGeomType = true;
|
211
|
}
|
212
|
|
213
|
// Initiate the transaction
|
214
|
fStore.setTransaction( t );
|
215
|
t.addAuthorization( lockId );
|
216
|
|
217
|
if( feature != null && validGeomType == true) {
|
218
|
// Remove old feature
|
219
|
fStore.removeFeatures( filter );
|
220
|
|
221
|
// Create new feature collection then add it to feature Store
|
222
|
fColl.add( feature );
|
223
|
fStore.addFeatures(fColl);
|
224
|
|
225
|
// Commit changes to shapefile
|
226
|
t.commit();
|
227
|
log.info(" Insert or Update docid " + docid + " from spatial cache");
|
228
|
}
|
229
|
|
230
|
|
231
|
} catch (MalformedURLException e) {
|
232
|
e.printStackTrace();
|
233
|
t.rollback(); // cancel opperations
|
234
|
} catch (IOException e) {
|
235
|
e.printStackTrace();
|
236
|
t.rollback(); // cancel opperations
|
237
|
} finally {
|
238
|
// Close out the transaction
|
239
|
t.close();
|
240
|
}
|
241
|
|
242
|
|
243
|
}
|
244
|
|
245
|
/**
|
246
|
* Saves the SpatialDataset object to the spatial cache.
|
247
|
*/
|
248
|
public void save() {
|
249
|
// Save Polygons
|
250
|
try {
|
251
|
URL anURL = (new File( featureSchema.polygonShpUri )).toURL();
|
252
|
ShapefileDataStore polygonDatastore = new ShapefileDataStore(anURL);
|
253
|
FeatureType polygonType = featureSchema.getPolygonFeatureType();
|
254
|
polygonDatastore.createSchema( polygonType );
|
255
|
FeatureStore polygonFeatureStore = (FeatureStore) polygonDatastore.getFeatureSource( polygonType.getTypeName() );
|
256
|
polygonFeatureStore.addFeatures( polygonCollection );
|
257
|
log.info(" ---- Polygons saved to " + featureSchema.polygonShpUri );
|
258
|
} catch(java.net.MalformedURLException e) {
|
259
|
log.error("Malformed URL Exception : "+e);
|
260
|
} catch(java.io.IOException e) {
|
261
|
log.error("IO Exception : "+e);
|
262
|
|
263
|
}
|
264
|
|
265
|
// Save Points
|
266
|
try {
|
267
|
URL anURL = (new File( featureSchema.pointShpUri )).toURL();
|
268
|
ShapefileDataStore pointDatastore = new ShapefileDataStore(anURL);
|
269
|
FeatureType pointsType = featureSchema.getPointFeatureType();
|
270
|
pointDatastore.createSchema( pointsType );
|
271
|
FeatureStore pointFeatureStore = (FeatureStore) pointDatastore.getFeatureSource( pointsType.getTypeName() );
|
272
|
pointFeatureStore.addFeatures( pointCollection );
|
273
|
log.info(" ---- Polygons saved to " + featureSchema.pointShpUri );
|
274
|
} catch(java.net.MalformedURLException e) {
|
275
|
log.error("Malformed URL Exception : "+e);
|
276
|
} catch(java.io.IOException e) {
|
277
|
log.error("IO Exception : "+e);
|
278
|
}
|
279
|
}
|
280
|
|
281
|
}
|