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