本文整理汇总了Java中org.geotools.data.DefaultTransaction类的典型用法代码示例。如果您正苦于以下问题:Java DefaultTransaction类的具体用法?Java DefaultTransaction怎么用?Java DefaultTransaction使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
DefaultTransaction类属于org.geotools.data包,在下文中一共展示了DefaultTransaction类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: addFeature
import org.geotools.data.DefaultTransaction; //导入依赖的package包/类
/**
* Add a feature to the store.
*
* @param feature the feature to add.
*/
default public void addFeature( SimpleFeature feature) {
FeatureStoreInfo featureStoreInfo = getFeatureStoreInfo();
SimpleFeatureStore featureStore = featureStoreInfo.getFeatureStore();
if (featureStore != null) {
Transaction transaction = new DefaultTransaction("add");
featureStore.setTransaction(transaction);
try {
DefaultFeatureCollection fc = new DefaultFeatureCollection();
fc.add(feature);
featureStore.addFeatures(fc);
transaction.commit();
} catch (Exception eek) {
try {
transaction.rollback();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
示例2: makeLineLayer
import org.geotools.data.DefaultTransaction; //导入依赖的package包/类
private void makeLineLayer( IEpanetType[] types, File baseFolder, CoordinateReferenceSystem mapCrs )
throws MalformedURLException, IOException {
SimpleFeatureTypeBuilder b = new SimpleFeatureTypeBuilder();
String shapefileName = types[0].getShapefileName();
String typeName = types[0].getName();
b.setName(typeName);
b.setCRS(mapCrs);
b.add("the_geom", LineString.class);
for( IEpanetType type : types ) {
b.add(type.getAttributeName(), type.getClazz());
}
SimpleFeatureType tanksType = b.buildFeatureType();
ShapefileDataStoreFactory factory = new ShapefileDataStoreFactory();
File file = new File(baseFolder, shapefileName);
Map<String, Serializable> create = new HashMap<String, Serializable>();
create.put("url", file.toURI().toURL());
ShapefileDataStore newDataStore = (ShapefileDataStore) factory.createNewDataStore(create);
newDataStore.createSchema(tanksType);
Transaction transaction = new DefaultTransaction();
SimpleFeatureStore featureStore = (SimpleFeatureStore) newDataStore.getFeatureSource();
featureStore.setTransaction(transaction);
try {
featureStore.addFeatures(new DefaultFeatureCollection());
transaction.commit();
} catch (Exception problem) {
problem.printStackTrace();
transaction.rollback();
} finally {
transaction.close();
}
}
示例3: writeShapefile
import org.geotools.data.DefaultTransaction; //导入依赖的package包/类
private void writeShapefile(MemoryDataStore memoryDataStore, File outputFile) throws Exception {
SimpleFeatureSource featureSource = memoryDataStore.getFeatureSource(memoryDataStore.getTypeNames()[0]);
SimpleFeatureType featureType = featureSource.getSchema();
Map<String, java.io.Serializable> creationParams = new HashMap<String, java.io.Serializable>();
creationParams.put("url", DataUtilities.fileToURL(outputFile));
ShapefileDataStoreFactory factory = (ShapefileDataStoreFactory) FileDataStoreFinder.getDataStoreFactory("shp");
ShapefileDataStore dataStore = (ShapefileDataStore) factory.createNewDataStore(creationParams);
dataStore.setCharset(Charset.forName("UTF-8"));
dataStore.createSchema(featureType);
SimpleFeatureStore featureStore = (SimpleFeatureStore) dataStore.getFeatureSource(dataStore.getTypeNames()[0]);
Transaction transaction = new DefaultTransaction();
try {
SimpleFeatureCollection collection = featureSource.getFeatures();
featureStore.addFeatures(collection);
transaction.commit();
} catch (IOException e) {
try {
transaction.rollback();
throw new Exception("Failed to commit data to feature store", e);
} catch (IOException e2 ) {
logger.error("Failed to commit data to feature store", e);
throw new Exception("Transaction rollback failed", e2);
}
} finally {
transaction.close();
}
}
示例4: main
import org.geotools.data.DefaultTransaction; //导入依赖的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();
}
示例5: testRelockLock
import org.geotools.data.DefaultTransaction; //导入依赖的package包/类
@Test
public void testRelockLock()
throws InterruptedException,
IOException {
final LockingManagement memoryLockManager = new MemoryLockManager(
"default");
final DefaultTransaction t1 = new DefaultTransaction();
memoryLockManager.lock(
t1,
"f8");
memoryLockManager.lock(
t1,
"f8");
t1.commit();
t1.close();
}
示例6: exportToShapefile
import org.geotools.data.DefaultTransaction; //导入依赖的package包/类
/**
* Export features to a new shapefile using the map projection in which
* they are currently displayed
*/
public static void exportToShapefile(DataStore newDataStore, FeatureCollection<SimpleFeatureType,SimpleFeature> featureCollection)/*,SimpleFeatureType ftype)*/ throws Exception {
// carefully open an iterator and writer to process the results
Transaction transaction = new DefaultTransaction();
FeatureWriter<SimpleFeatureType, SimpleFeature> writer = newDataStore.getFeatureWriter(newDataStore.getTypeNames()[0], transaction);
FeatureIterator<SimpleFeature> iterator = featureCollection.features();
try {
while( iterator.hasNext() ){
SimpleFeature feature = iterator.next();
SimpleFeature copy = writer.next();
//copy.setAttributes( feature.getAttributes() );
Geometry geometry = (Geometry) feature.getDefaultGeometry();
if(geometry!=null){
copy.setDefaultGeometry( geometry.buffer(0));
writer.write();
}else{
logger.warn("Warning:geometry null");
}
}
transaction.commit();
logger.info("Export to shapefile complete" );
} catch (Exception problem) {
problem.printStackTrace();
transaction.rollback();
logger.error("Export to shapefile failed",problem );
} finally {
writer.close();
iterator.close();
transaction.close();
}
}
示例7: write
import org.geotools.data.DefaultTransaction; //导入依赖的package包/类
public static <T> void write(File file,
Consumer<SimpleFeatureTypeBuilder> fun1,
BiConsumer<SimpleFeatureBuilder, T> fun2,
List<T> geometries) {
try {
SimpleFeatureTypeBuilder typeBuilder = new SimpleFeatureTypeBuilder();
typeBuilder.setName("MyFeatureType");
typeBuilder.setCRS(DefaultGeographicCRS.WGS84);
fun1.accept(typeBuilder);
SimpleFeatureType type = typeBuilder.buildFeatureType();
SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(type);
AtomicInteger id = new AtomicInteger(0);
List<SimpleFeature> geoms = geometries.stream().map(g -> {
fun2.accept(featureBuilder, g);
return featureBuilder.buildFeature(String.valueOf(id.getAndIncrement()));
}).collect(toList());
ShapefileDataStoreFactory datastoreFactory = new ShapefileDataStoreFactory();
Map<String, Serializable> params = newHashMap();
params.put("url", file.toURI().toURL());
params.put("create spatial index", Boolean.TRUE);
ShapefileDataStore datastore = (ShapefileDataStore) datastoreFactory.createNewDataStore(params);
datastore.createSchema(type);
Transaction transaction = new DefaultTransaction("create");
String typeName = datastore.getTypeNames()[0];
SimpleFeatureSource featureSource = datastore.getFeatureSource(typeName);
SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
SimpleFeatureCollection collection = new ListFeatureCollection(type, geoms);
featureStore.setTransaction(transaction);
featureStore.addFeatures(collection);
transaction.commit();
transaction.close();
datastore.dispose();
}
catch (IOException e) {
throw propagate(e);
}
}
示例8: init
import org.geotools.data.DefaultTransaction; //导入依赖的package包/类
@Override
public void init() {
LOGGER.info("Output as shapefile: " + outFile);
file = new File(outFile);
try {
// Define Schema
final SimpleFeatureType TYPE = createFeatureType();
featureBuilder = new SimpleFeatureBuilder(TYPE);
geomF = new GeometryFactory();
ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
// define parameter for ShapeFileDataStore object
Map<String, Serializable> params = new HashMap<String, Serializable>();
params.put("url", file.toURI().toURL());
params.put("create spatial index", Boolean.TRUE);
dataStore = (ShapefileDataStore) dataStoreFactory.createNewDataStore(params);
dataStore.createSchema(TYPE);
// define transaction
transaction = new DefaultTransaction("create");
String typeName = dataStore.getTypeNames()[0];
SimpleFeatureSource featureSource = dataStore.getFeatureSource(typeName);
if (featureSource instanceof SimpleFeatureStore) {
featureStore = (SimpleFeatureStore) featureSource;
featureStore.setTransaction(transaction);
}
} catch (IOException e1) {
e1.printStackTrace();
}
}
示例9: SoilSealingAdministrativeUnit
import org.geotools.data.DefaultTransaction; //导入依赖的package包/类
/**
* Default constructor
*
* @param au
* @param geoCodingReference
* @param populationReference
* @throws IOException
*/
public SoilSealingAdministrativeUnit(String au, FeatureTypeInfo geoCodingReference,
FeatureTypeInfo populationReference) throws IOException {
if (!au.contains("_")) {
throw new IOException("Invalid Administrative Unit name");
}
this.geoCodingReference = geoCodingReference;
this.populationReference = populationReference;
this.name = au.split("_")[0];
this.parent = au.split("_")[1];
FeatureReader<SimpleFeatureType, SimpleFeature> ftReader = null;
Transaction transaction = new DefaultTransaction();
try {
final JDBCDataStore ds = (JDBCDataStore) geoCodingReference.getStore().getDataStore(null);
Filter nameFilter = ff.equals(ff.property("name"), ff.literal(this.name));
Filter parentFilter = ff.equals(ff.property("parent"), ff.literal(this.parent));
Filter queryFilter = ff.and(Arrays.asList(nameFilter, parentFilter));
Query query = new Query(geoCodingReference.getFeatureType().getName().getLocalPart(), queryFilter);
ftReader = ds.getFeatureReader(query, transaction);
while (ftReader.hasNext()) {
Feature feature = ftReader.next();
this.type = AuType.getType((Integer) feature.getProperty("type").getValue());
this.the_geom = (Geometry) feature.getDefaultGeometryProperty().getValue();
break;
}
} finally {
if (ftReader != null) {
ftReader.close();
}
transaction.commit();
transaction.close();
}
if (this.type == null || this.the_geom == null) {
throw new IOException("Invalid Administrative Unit name: no record found!");
}
switch (this.type) {
case MUNICIPALITY:
loadPopulationStatistics(this);
break;
case DISTRICT:
case REGION:
loadSubs(this);
break;
default:
break;
}
}
示例10: loadPopulationStatistics
import org.geotools.data.DefaultTransaction; //导入依赖的package包/类
/**
*
* @param soilSealingAdministrativeUnit
* @throws IOException
*/
private void loadPopulationStatistics(SoilSealingAdministrativeUnit soilSealingAdministrativeUnit) throws IOException {
FeatureReader<SimpleFeatureType, SimpleFeature> ftReader = null;
Transaction transaction = new DefaultTransaction();
try {
final JDBCDataStore ds = (JDBCDataStore) populationReference.getStore().getDataStore(null);
Filter auNameFilter = ff.equals(ff.property("au_name"), ff.literal(this.name));
Filter queryFilter = ff.and(Arrays.asList(auNameFilter));
Query query = new Query(populationReference.getFeatureType().getName().getLocalPart(), queryFilter);
ftReader = ds.getFeatureReader(query, transaction);
while (ftReader.hasNext()) {
Feature feature = ftReader.next();
Collection<Property> properties = feature.getProperties();
for (Property prop : properties) {
if (prop.getName().getLocalPart().startsWith("a_")) {
Object yearPopulationValue = prop.getValue();
if (yearPopulationValue != null) {
population.put(prop.getName().getLocalPart().split("a_")[1], ((BigDecimal) yearPopulationValue).intValue());
}
}
}
}
} finally {
if (ftReader != null) {
ftReader.close();
}
transaction.commit();
transaction.close();
}
}
示例11: collectionToShapeFile
import org.geotools.data.DefaultTransaction; //导入依赖的package包/类
/**
* <p>
* The easy way to create a shapefile from attributes and geometries
* </p>
* <p>
* <b>NOTE: this doesn't support date attributes</b>
* </p>
*
* @param shapeFilePath the shapefile name
* @param crs the destination crs
* @param fet the featurecollection
* @throws IOException
*/
public static boolean collectionToShapeFile( String shapeFilePath, CoordinateReferenceSystem crs,
SimpleFeatureCollection fet ) throws IOException {
// Create the file you want to write to
File file = null;
if (shapeFilePath.toLowerCase().endsWith(".shp")) { //$NON-NLS-1$
file = new File(shapeFilePath);
} else {
file = new File(shapeFilePath + ".shp"); //$NON-NLS-1$
}
ShapefileDataStoreFactory factory = new ShapefileDataStoreFactory();
Map<String, Serializable> create = new HashMap<String, Serializable>();
create.put("url", file.toURI().toURL());
ShapefileDataStore newDataStore = (ShapefileDataStore) factory.createNewDataStore(create);
newDataStore.createSchema(fet.getSchema());
if (crs != null)
newDataStore.forceSchemaCRS(crs);
Transaction transaction = new DefaultTransaction();
SimpleFeatureStore featureStore = (SimpleFeatureStore) newDataStore.getFeatureSource();
featureStore.setTransaction(transaction);
try {
featureStore.addFeatures(fet);
transaction.commit();
} catch (Exception problem) {
problem.printStackTrace();
transaction.rollback();
} finally {
transaction.close();
}
return true;
}
示例12: modifyFeatureAttribute
import org.geotools.data.DefaultTransaction; //导入依赖的package包/类
/**
* Modify the attributes of this feature.
*
* @param names the names of the attributes to modify.
* @param values the values to set.
* @return the new feature if the editing has been successful.
*/
default public SimpleFeature modifyFeatureAttribute( String[] names, Object[] values ) {
FeatureStoreInfo featureStoreInfo = getFeatureStoreInfo();
SimpleFeatureStore featureStore = featureStoreInfo.getFeatureStore();
if (featureStore != null) {
Transaction transaction = new DefaultTransaction("modify");
featureStore.setTransaction(transaction);
FilterFactory ff = CommonFactoryFinder.getFilterFactory(GeoTools.getDefaultHints());
Filter filter = ff.id(Collections.singleton(ff.featureId(getFeature().getID())));
try {
featureStore.modifyFeatures(names, values, filter);
transaction.commit();
SimpleFeature modifiedFeature = featureStore.getFeatures(filter).features().next();
return modifiedFeature;
} catch (Exception eek) {
try {
transaction.rollback();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
示例13: makePointLayer
import org.geotools.data.DefaultTransaction; //导入依赖的package包/类
private void makePointLayer( IEpanetType[] types, File baseFolder, CoordinateReferenceSystem mapCrs )
throws MalformedURLException, IOException {
SimpleFeatureTypeBuilder b = new SimpleFeatureTypeBuilder();
String shapefileName = types[0].getShapefileName();
String typeName = types[0].getName();
b.setName(typeName);
b.setCRS(mapCrs);
b.add("the_geom", Point.class);
for( IEpanetType type : types ) {
b.add(type.getAttributeName(), type.getClazz());
}
SimpleFeatureType tanksType = b.buildFeatureType();
ShapefileDataStoreFactory factory = new ShapefileDataStoreFactory();
File file = new File(baseFolder, shapefileName);
Map<String, Serializable> create = new HashMap<String, Serializable>();
create.put("url", file.toURI().toURL());
ShapefileDataStore newDataStore = (ShapefileDataStore) factory.createNewDataStore(create);
newDataStore.createSchema(tanksType);
Transaction transaction = new DefaultTransaction();
SimpleFeatureStore featureStore = (SimpleFeatureStore) newDataStore.getFeatureSource();
featureStore.setTransaction(transaction);
try {
featureStore.addFeatures(new DefaultFeatureCollection());
transaction.commit();
} catch (Exception problem) {
problem.printStackTrace();
transaction.rollback();
} finally {
transaction.close();
}
}
示例14: writeEsriShapefile
import org.geotools.data.DefaultTransaction; //导入依赖的package包/类
static void writeEsriShapefile(Class<?> geomType, List<SimpleFeature> features, File file) throws IOException {
String geomName = geomType.getSimpleName();
String basename = file.getName();
if (basename.endsWith(FILE_EXTENSION_SHAPEFILE)) {
basename = basename.substring(0, basename.length() - 4);
}
File file1 = new File(file.getParentFile(), basename + "_" + geomName + FILE_EXTENSION_SHAPEFILE);
SimpleFeature simpleFeature = features.get(0);
SimpleFeatureType simpleFeatureType = changeGeometryType(simpleFeature.getType(), geomType);
ShapefileDataStoreFactory factory = new ShapefileDataStoreFactory();
Map<String, Serializable> map = Collections.singletonMap("url", file1.toURI().toURL());
ShapefileDataStore dataStore = (ShapefileDataStore) factory.createNewDataStore(map);
dataStore.createSchema(simpleFeatureType);
String typeName = dataStore.getTypeNames()[0];
SimpleFeatureSource featureSource = dataStore.getFeatureSource(typeName);
DefaultTransaction transaction = new DefaultTransaction("X");
if (featureSource instanceof SimpleFeatureStore) {
SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
SimpleFeatureCollection collection = new ListFeatureCollection(simpleFeatureType, features);
featureStore.setTransaction(transaction);
// I'm not sure why the next line is necessary (mp/20170627)
// Without it is not working, the wrong feature type is used for writing
// But it is not mentioned in the tutorials
dataStore.getEntry(featureSource.getName()).getState(transaction).setFeatureType(simpleFeatureType);
try {
featureStore.addFeatures(collection);
transaction.commit();
} catch (Exception problem) {
transaction.rollback();
throw new IOException(problem);
} finally {
transaction.close();
}
} else {
throw new IOException(typeName + " does not support read/write access");
}
}
示例15: testLockReleaseOfBulkAuthLock
import org.geotools.data.DefaultTransaction; //导入依赖的package包/类
@Test
public void testLockReleaseOfBulkAuthLock()
throws InterruptedException,
IOException {
final LockingManagement memoryLockManager = new MemoryLockManager(
"default");
final Transaction t1 = Transaction.AUTO_COMMIT;
final DefaultTransaction t2 = new DefaultTransaction();
t2.addAuthorization("auth1");
FeatureLock lock = new FeatureLock(
"auth1",
1 /* minute */);
memoryLockManager.lockFeatureID(
"sometime",
"f4",
t1,
lock);
memoryLockManager.lock(
t2,
"f4");
t2.commit();
// commit should not take away the lock
assertTrue(memoryLockManager.exists("auth1"));
memoryLockManager.release(
"auth1",
t1);
assertFalse(memoryLockManager.exists("auth1"));
t1.close();
}