Revision 3200
Added by Jing Tao almost 18 years ago
src/edu/ucsb/nceas/metacat/spatial/SpatialDataset.java | ||
---|---|---|
53 | 53 |
*/ |
54 | 54 |
public class SpatialDataset { |
55 | 55 |
|
56 |
SpatialFeatureSchema featureSchema = new SpatialFeatureSchema(); |
|
57 |
|
|
56 |
private static SpatialFeatureSchema featureSchema = new SpatialFeatureSchema(); |
|
57 |
private static ShapefileDataStore polygonStore = null; |
|
58 |
private static ShapefileDataStore pointStore = null; |
|
59 |
private static FeatureStore featureStore = null; |
|
60 |
|
|
61 |
|
|
58 | 62 |
//public String polygonShpUri = MetaCatUtil.getOption("certPath") + "data/metacat_shps/data_bounds.shp"; |
59 | 63 |
//public String pointShpUri = MetaCatUtil.getOption("certPath")+ "data/metacat_shps/data_points.shp"; |
60 | 64 |
|
... | ... | |
67 | 71 |
/** |
68 | 72 |
* empty constructor for SpatialDataset |
69 | 73 |
*/ |
70 |
public SpatialDataset() { |
|
74 |
public SpatialDataset() throws IOException { |
|
75 |
|
|
76 |
polygonStore = new ShapefileDataStore( (new File( featureSchema.polygonShpUri)).toURL() ); |
|
77 |
pointStore = new ShapefileDataStore( (new File( featureSchema.pointShpUri)).toURL() ); |
|
71 | 78 |
|
72 | 79 |
} |
73 | 80 |
|
... | ... | |
114 | 121 |
filter.addRightValue(filterFactory.createLiteralExpression(docid)); |
115 | 122 |
} catch (IllegalFilterException e) { |
116 | 123 |
e.printStackTrace(); |
124 |
throw new IOException(e.getMessage()); |
|
117 | 125 |
} |
118 | 126 |
|
119 |
// Begin new transaction |
|
120 |
Transaction t = new DefaultTransaction("handle"); |
|
121 |
t.putProperty( "updating spatial cache", new Integer(7) ); |
|
122 |
String lockId = "SpatialDataset.delete"; |
|
127 |
if( geomType.equals("polygon") ) { |
|
128 |
deleteFromDataStore(polygonStore, filter, docid); |
|
129 |
}else if( geomType.equals("point") ) { |
|
130 |
deleteFromDataStore(pointStore, filter, docid); |
|
131 |
} |
|
132 |
else |
|
133 |
{ |
|
134 |
throw new IOException("Unkown Geo Type "+geomType); |
|
135 |
} |
|
136 |
|
|
123 | 137 |
|
124 |
try { |
|
125 |
|
|
126 |
boolean validGeomType = false; |
|
127 | 138 |
|
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 | 139 |
} |
140 |
|
|
141 |
/* |
|
142 |
* Delete a feature from given spatail data store |
|
143 |
*/ |
|
144 |
private void deleteFromDataStore(ShapefileDataStore dStore, CompareFilter filter, String docid) |
|
145 |
throws IOException |
|
146 |
{ |
|
147 |
if (dStore != null && filter != null) |
|
148 |
{ |
|
149 |
synchronized(dStore) |
|
150 |
{ |
|
151 |
// Begin new transaction |
|
152 |
Transaction t = new DefaultTransaction("handle"); |
|
153 |
t.putProperty( "updating spatial cache", new Integer(7) ); |
|
154 |
String lockId = "SpatialDataset.delete"; |
|
155 |
FeatureStore fStore = (FeatureStore) dStore.getFeatureSource(dStore.getTypeNames()[0]); |
|
156 |
try { |
|
157 |
|
|
158 |
if (fStore != null) |
|
159 |
{ |
|
160 |
// Initiate the transaction |
|
161 |
fStore.setTransaction( t ); |
|
162 |
t.addAuthorization( lockId ); |
|
163 |
|
|
164 |
// Remove old feature |
|
165 |
fStore.removeFeatures( filter ); |
|
166 |
|
|
167 |
// Commit changes to shapefile |
|
168 |
t.commit(); |
|
169 |
log.info(" Delete docid " + docid + " from spatial cache"); |
|
170 |
} |
|
171 |
else |
|
172 |
{ |
|
173 |
log.error("Feature store is null"); |
|
174 |
} |
|
175 |
|
|
176 |
|
|
177 |
} catch (MalformedURLException e) { |
|
178 |
e.printStackTrace(); |
|
179 |
t.rollback(); // cancel opperations |
|
180 |
} catch (IOException e) { |
|
181 |
e.printStackTrace(); |
|
182 |
t.rollback(); // cancel opperations |
|
183 |
} finally { |
|
184 |
// Close out the transaction |
|
185 |
t.close(); |
|
186 |
} |
|
187 |
} |
|
188 |
} |
|
189 |
else if (dStore == null) |
|
190 |
{ |
|
191 |
log.error("Shape file store is null"); |
|
192 |
} |
|
193 |
} |
|
194 |
|
|
195 |
|
|
163 | 196 |
|
164 | 197 |
/** |
165 | 198 |
* Either inserts or updates the spatial cache with the new |
... | ... | |
189 | 222 |
filter.addRightValue(filterFactory.createLiteralExpression(docid)); |
190 | 223 |
} catch (IllegalFilterException e) { |
191 | 224 |
e.printStackTrace(); |
225 |
throw new IOException(e.getMessage()); |
|
192 | 226 |
} |
193 | 227 |
|
194 |
// Begin new transaction |
|
195 |
Transaction t = new DefaultTransaction("handle"); |
|
196 |
t.putProperty( "updating spatial cache", new Integer(7) ); |
|
197 |
String lockId = "SpatialDataset.insertOrUpdate"; |
|
228 |
if( geomType.equals("polygon") ) { |
|
229 |
|
|
230 |
insertOrUpdateDataStore(polygonStore,filter, feature, fColl, docid); |
|
231 |
} else if( geomType.equals("point") ) { |
|
232 |
insertOrUpdateDataStore(pointStore,filter, feature, fColl, docid); |
|
233 |
}else{ |
|
234 |
throw new IOException("Unkown Geo Type "+geomType); |
|
235 |
} |
|
198 | 236 |
|
199 |
try { |
|
200 |
|
|
201 |
boolean validGeomType = false; |
|
202 | 237 |
|
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 |
} |
|
238 |
|
|
212 | 239 |
|
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 | 240 |
|
221 |
// Create new feature collection then add it to feature Store |
|
222 |
fColl.add( feature ); |
|
223 |
fStore.addFeatures(fColl); |
|
241 |
} |
|
242 |
|
|
243 |
/* |
|
244 |
* Insert or update new feature in shape file |
|
245 |
*/ |
|
246 |
private void insertOrUpdateDataStore(ShapefileDataStore dStore, CompareFilter filter, Feature feature, |
|
247 |
FeatureCollection fColl, String docid) throws IOException |
|
248 |
{ |
|
249 |
if (dStore != null && filter != null && feature != null && fColl != null) |
|
250 |
{ |
|
251 |
synchronized(dStore) |
|
252 |
{ |
|
253 |
// Begin new transaction |
|
254 |
Transaction t = new DefaultTransaction("handle"); |
|
255 |
t.putProperty( "updating spatial cache", new Integer(7) ); |
|
256 |
String lockId = "SpatialDataset.insertOrUpdate"; |
|
257 |
|
|
258 |
try { |
|
259 |
FeatureStore fStore = (FeatureStore) dStore.getFeatureSource(dStore.getTypeNames()[0]); |
|
260 |
if( fStore != null) { |
|
261 |
// Initiate the transaction |
|
262 |
fStore.setTransaction( t ); |
|
263 |
t.addAuthorization( lockId ); |
|
264 |
// Remove old feature |
|
265 |
fStore.removeFeatures( filter ); |
|
224 | 266 |
|
225 |
// Commit changes to shapefile |
|
226 |
t.commit(); |
|
227 |
log.info(" Insert or Update docid " + docid + " from spatial cache"); |
|
228 |
} |
|
267 |
// Create new feature collection then add it to feature Store |
|
268 |
fColl.add( feature ); |
|
269 |
fStore.addFeatures(fColl); |
|
229 | 270 |
|
271 |
// Commit changes to shapefile |
|
272 |
t.commit(); |
|
273 |
log.info(" Insert or Update docid " + docid + " from spatial cache"); |
|
274 |
} |
|
275 |
else |
|
276 |
{ |
|
277 |
log.error("Feature store is null"); |
|
278 |
} |
|
230 | 279 |
|
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 | 280 |
|
281 |
} catch (MalformedURLException e) { |
|
282 |
e.printStackTrace(); |
|
283 |
t.rollback(); // cancel opperations |
|
284 |
} catch (IOException e) { |
|
285 |
e.printStackTrace(); |
|
286 |
t.rollback(); // cancel opperations |
|
287 |
} finally { |
|
288 |
// Close out the transaction |
|
289 |
t.close(); |
|
290 |
} |
|
242 | 291 |
|
292 |
} |
|
293 |
} |
|
294 |
else if (dStore == null) |
|
295 |
{ |
|
296 |
log.error("Shape file store is null"); |
|
297 |
} |
|
243 | 298 |
} |
244 | 299 |
|
245 | 300 |
/** |
... | ... | |
248 | 303 |
public void save() { |
249 | 304 |
// Save Polygons |
250 | 305 |
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 ); |
|
306 |
//URL anURL = (new File( featureSchema.polygonShpUri )).toURL(); |
|
307 |
//ShapefileDataStore polygonDatastore = new ShapefileDataStore(anURL); |
|
308 |
if (polygonStore != null) |
|
309 |
{ |
|
310 |
|
|
311 |
synchronized(polygonStore) |
|
312 |
{ |
|
313 |
FeatureType polygonType = featureSchema.getPolygonFeatureType(); |
|
314 |
polygonStore.createSchema( polygonType ); |
|
315 |
FeatureStore polygonFeatureStore = (FeatureStore) polygonStore.getFeatureSource( polygonType.getTypeName() ); |
|
316 |
polygonFeatureStore.addFeatures( polygonCollection ); |
|
317 |
log.info(" ---- Polygons saved to " + featureSchema.polygonShpUri ); |
|
318 |
} |
|
319 |
} |
|
320 |
else |
|
321 |
{ |
|
322 |
log.error("Couldn't find the polygon shape file store"); |
|
323 |
} |
|
258 | 324 |
} catch(java.net.MalformedURLException e) { |
259 | 325 |
log.error("Malformed URL Exception : "+e); |
260 | 326 |
} catch(java.io.IOException e) { |
... | ... | |
264 | 330 |
|
265 | 331 |
// Save Points |
266 | 332 |
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 ); |
|
333 |
//URL anURL = (new File( featureSchema.pointShpUri )).toURL(); |
|
334 |
//ShapefileDataStore pointDatastore = new ShapefileDataStore(anURL); |
|
335 |
if (pointStore != null) |
|
336 |
{ |
|
337 |
synchronized(pointStore) |
|
338 |
{ |
|
339 |
FeatureType pointsType = featureSchema.getPointFeatureType(); |
|
340 |
pointStore.createSchema( pointsType ); |
|
341 |
FeatureStore pointFeatureStore = (FeatureStore) pointStore.getFeatureSource( pointsType.getTypeName() ); |
|
342 |
pointFeatureStore.addFeatures( pointCollection ); |
|
343 |
log.info(" ---- Polygons saved to " + featureSchema.pointShpUri ); |
|
344 |
} |
|
345 |
} |
|
346 |
else |
|
347 |
{ |
|
348 |
log.error("Couldn't find the shape point file store"); |
|
349 |
} |
|
274 | 350 |
} catch(java.net.MalformedURLException e) { |
275 | 351 |
log.error("Malformed URL Exception : "+e); |
276 | 352 |
} catch(java.io.IOException e) { |
277 | 353 |
log.error("IO Exception : "+e); |
278 | 354 |
} |
279 | 355 |
} |
356 |
|
|
357 |
|
|
280 | 358 |
|
281 | 359 |
} |
Also available in: Unified diff
Change the shape file store object to be static and make them synchronized in order to be thread safe.