本文整理汇总了Java中org.geotools.data.FeatureStore.setTransaction方法的典型用法代码示例。如果您正苦于以下问题:Java FeatureStore.setTransaction方法的具体用法?Java FeatureStore.setTransaction怎么用?Java FeatureStore.setTransaction使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.geotools.data.FeatureStore
的用法示例。
在下文中一共展示了FeatureStore.setTransaction方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import org.geotools.data.FeatureStore; //导入方法依赖的package包/类
public static void main(String[] args) throws MalformedURLException,
IOException, NoSuchAuthorityCodeException, FactoryException {
if (args.length < 3) {
System.err.println("Usage: ReprojectShapeFile in.shp out.shp epsg:code");
System.exit(3);
}
File in = new File(args[0]);
File out = new File(args[1]);
CoordinateReferenceSystem crs = CRS.decode(args[2]);
ShapefileDataStoreFactory fac = new ShapefileDataStoreFactory();
FileDataStore inStore = fac.createDataStore(in.toURI().toURL());
FileDataStore outStore = fac.createDataStore(out.toURI().toURL());
SimpleFeatureCollection inFeatures = inStore.getFeatureSource()
.getFeatures();
ReprojectShapeFile reprojector = new ReprojectShapeFile();
SimpleFeatureCollection outFeatures = reprojector
.reproject(inFeatures, crs);
outStore.createSchema(outFeatures.getSchema());
Transaction transaction = new DefaultTransaction("create");
String outName = outStore.getNames().get(0).getLocalPart();
SimpleFeatureSource featureSource = outStore.getFeatureSource(outName);
FeatureStore featureStore = (FeatureStore) featureSource;
featureStore.setTransaction(transaction);
featureStore.addFeatures(outFeatures);
transaction.commit();
outStore.dispose();
}
示例2: createShapeFile
import org.geotools.data.FeatureStore; //导入方法依赖的package包/类
private File createShapeFile(FeatureCollection<SimpleFeatureType, SimpleFeature> collection) throws IOException {
String shapeFileSuffix = ".shp";
File tempBaseFile = File.createTempFile("resolveDir", ".tmp");
tempBaseFile.deleteOnExit();
File parent = tempBaseFile.getParentFile();
File shpBaseDirectory = new File(parent, UUID.randomUUID().toString());
if (!shpBaseDirectory.mkdir()) {
throw new IllegalStateException("Could not create temporary shp directory.");
}
File tempSHPfile = File.createTempFile("shp", shapeFileSuffix, shpBaseDirectory);
tempSHPfile.deleteOnExit();
DataStoreFactorySpi dataStoreFactory = new ShapefileDataStoreFactory();
Map<String, Serializable> params = new HashMap<String, Serializable>();
params.put("url", tempSHPfile.toURI().toURL());
params.put("create spatial index", Boolean.TRUE);
ShapefileDataStore newDataStore = (ShapefileDataStore) dataStoreFactory
.createNewDataStore(params);
newDataStore.createSchema(collection.getSchema());
if(collection.getSchema().getCoordinateReferenceSystem()==null){
newDataStore.forceSchemaCRS(getCRS_WGS84());
}else{
newDataStore.forceSchemaCRS(collection.getSchema()
.getCoordinateReferenceSystem());
}
Transaction transaction = new DefaultTransaction("create");
String typeName = newDataStore.getTypeNames()[0];
@SuppressWarnings("unchecked")
FeatureStore<SimpleFeatureType, SimpleFeature> featureStore = (FeatureStore<SimpleFeatureType, SimpleFeature>) newDataStore
.getFeatureSource(typeName);
featureStore.setTransaction(transaction);
try {
featureStore.addFeatures(collection);
transaction.commit();
} catch (Exception problem) {
transaction.rollback();
} finally {
transaction.close();
}
// Get names of additional files
String path = tempSHPfile.getAbsolutePath();
String baseName = path.substring(0, path.length() - shapeFileSuffix.length());
File shx = new File(baseName + ".shx");
File dbf = new File(baseName + ".dbf");
File prj = new File(baseName + ".prj");
// mark created files for delete
tempSHPfile.deleteOnExit();
shx.deleteOnExit();
dbf.deleteOnExit();
prj.deleteOnExit();
shpBaseDirectory.deleteOnExit();
return shpBaseDirectory;
}