Java源码示例:org.geotools.data.DefaultTransaction
示例1
/**
* 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
@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");
final 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();
}
示例3
@Test
public void testTemporal() throws CQLException, IOException, ParseException {
populate();
final Transaction transaction2 = new DefaultTransaction();
final Query query =
new Query(
"geostuff",
CQL.toFilter(
"BBOX(geometry,44,27,42,30) and start during 2005-05-16T20:32:56Z/2005-05-20T21:32:56Z and end during 2005-05-18T20:32:56Z/2005-05-22T21:32:56Z"),
new String[] {"geometry", "start", "end", "pid"});
final FeatureReader<SimpleFeatureType, SimpleFeature> reader =
dataStore.getFeatureReader(query, transaction2);
int c = 0;
while (reader.hasNext()) {
reader.next();
c++;
}
reader.close();
transaction2.commit();
transaction2.close();
assertEquals(2, c);
}
示例4
/**
* 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;
}
示例5
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();
}
}
示例6
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();
}
}
示例7
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");
}
}
示例8
public void populate() throws IOException, CQLException, ParseException {
final Transaction transaction1 = new DefaultTransaction();
final FeatureWriter<SimpleFeatureType, SimpleFeature> writer =
dataStore.getFeatureWriter(type.getTypeName(), transaction1);
assertFalse(writer.hasNext());
SimpleFeature newFeature = writer.next();
newFeature.setAttribute("pop", Long.valueOf(100));
newFeature.setAttribute("pid", UUID.randomUUID().toString());
newFeature.setAttribute("when", DateUtilities.parseISO("2005-05-19T20:32:56Z"));
newFeature.setAttribute("geometry", factory.createPoint(new Coordinate(43.454, 28.232)));
writer.write();
newFeature = writer.next();
newFeature.setAttribute("pop", Long.valueOf(100));
newFeature.setAttribute("pid", UUID.randomUUID().toString());
newFeature.setAttribute("when", DateUtilities.parseISO("2005-05-18T20:32:56Z"));
newFeature.setAttribute("geometry", factory.createPoint(new Coordinate(43.454, 27.232)));
writer.write();
newFeature = writer.next();
newFeature.setAttribute("pop", Long.valueOf(100));
newFeature.setAttribute("pid", UUID.randomUUID().toString());
newFeature.setAttribute("when", DateUtilities.parseISO("2005-05-17T20:32:56Z"));
newFeature.setAttribute("geometry", factory.createPoint(new Coordinate(43.454, 28.232)));
writer.write();
writer.close();
transaction1.commit();
transaction1.close();
}
示例9
@Test
public void test() throws IOException, CQLException, ParseException {
final Transaction transaction1 = new DefaultTransaction();
final FeatureWriter<SimpleFeatureType, SimpleFeature> writer =
dataStore.getFeatureWriter(type.getTypeName(), transaction1);
assertFalse(writer.hasNext());
SimpleFeature newFeature = writer.next();
newFeature.setAttribute("pop", Long.valueOf(100));
newFeature.setAttribute("pid", UUID.randomUUID().toString());
newFeature.setAttribute("when", DateUtilities.parseISO("2005-05-19T18:33:55Z"));
newFeature.setAttribute("geometry", factory.createPoint(new Coordinate(27.25, 41.25)));
newFeature = writer.next();
newFeature.setAttribute("pop", Long.valueOf(100));
newFeature.setAttribute("pid", UUID.randomUUID().toString());
newFeature.setAttribute("when", DateUtilities.parseISO("2005-05-19T20:33:55Z"));
newFeature.setAttribute("geometry", factory.createPoint(new Coordinate(27.25, 41.25)));
writer.write();
writer.close();
final FeatureReader<SimpleFeatureType, SimpleFeature> reader =
dataStore.getFeatureReader(query, transaction1);
assertTrue(reader.hasNext());
final SimpleFeature priorFeature = reader.next();
assertEquals(newFeature.getAttribute("pid"), priorFeature.getAttribute("pid"));
assertFalse(reader.hasNext());
reader.close();
transaction1.commit();
transaction1.close();
}
示例10
@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();
}
示例11
@Override
public void populate(final SimpleFeatureType type, final DataStore dataStore)
throws IOException, CQLException, ParseException {
dataStore.createSchema(type);
final Transaction transaction1 = new DefaultTransaction();
final FeatureWriter<SimpleFeatureType, SimpleFeature> writer =
dataStore.getFeatureWriter(type.getTypeName(), transaction1);
assertFalse(writer.hasNext());
SimpleFeature newFeature = writer.next();
newFeature.setAttribute("pop", Long.valueOf(77));
newFeature.setAttribute("pid", UUID.randomUUID().toString());
newFeature.setAttribute("when", DateUtilities.parseISO("2005-05-19T20:32:56Z"));
newFeature.setAttribute("geometry", factory.createPoint(new Coordinate(43.454, 28.232)));
writer.write();
newFeature = writer.next();
newFeature.setAttribute("pop", Long.valueOf(66));
newFeature.setAttribute("pid", UUID.randomUUID().toString());
newFeature.setAttribute("when", DateUtilities.parseISO("2005-05-18T20:32:56Z"));
newFeature.setAttribute("geometry", factory.createPoint(new Coordinate(43.454, 27.232)));
writer.write();
newFeature = writer.next();
newFeature.setAttribute("pop", Long.valueOf(100));
newFeature.setAttribute("pid", UUID.randomUUID().toString());
newFeature.setAttribute("when", DateUtilities.parseISO("2005-05-17T20:32:56Z"));
newFeature.setAttribute("geometry", factory.createPoint(new Coordinate(43.454, 28.242)));
writer.write();
writer.close();
transaction1.commit();
transaction1.close();
}
示例12
@Override
public void populate(final SimpleFeatureType type, final DataStore dataStore)
throws IOException, CQLException, ParseException {
dataStore.createSchema(type);
final Transaction transaction1 = new DefaultTransaction();
final SimpleFeatureStore source =
(SimpleFeatureStore) dataStore.getFeatureSource(type.getName());
final FeatureWriter<SimpleFeatureType, SimpleFeature> writer =
dataStore.getFeatureWriter(type.getTypeName(), transaction1);
assertFalse(writer.hasNext());
SimpleFeature newFeature = writer.next();
newFeature.setAttribute("pop", Long.valueOf(77));
newFeature.setAttribute("pid", UUID.randomUUID().toString());
newFeature.setAttribute("when", DateUtilities.parseISO("2005-05-19T20:32:56Z"));
newFeature.setAttribute("geometry", factory.createPoint(new Coordinate(43.454, 28.232)));
source.addFeatures(DataUtilities.collection(newFeature));
newFeature = writer.next();
newFeature.setAttribute("pop", Long.valueOf(66));
newFeature.setAttribute("pid", UUID.randomUUID().toString());
newFeature.setAttribute("when", DateUtilities.parseISO("2005-05-18T20:32:56Z"));
newFeature.setAttribute("geometry", factory.createPoint(new Coordinate(43.454, 27.232)));
source.addFeatures(DataUtilities.collection(newFeature));
newFeature = writer.next();
newFeature.setAttribute("pop", Long.valueOf(100));
newFeature.setAttribute("pid", UUID.randomUUID().toString());
newFeature.setAttribute("when", DateUtilities.parseISO("2005-05-17T20:32:56Z"));
newFeature.setAttribute("geometry", factory.createPoint(new Coordinate(43.454, 28.242)));
source.addFeatures(DataUtilities.collection(newFeature));
transaction1.commit();
transaction1.close();
}
示例13
public void populate() throws IOException, CQLException, ParseException {
final Transaction transaction1 = new DefaultTransaction();
final FeatureWriter<SimpleFeatureType, SimpleFeature> writer =
dataStore.getFeatureWriter(type.getTypeName(), transaction1);
assertFalse(writer.hasNext());
SimpleFeature newFeature = writer.next();
newFeature.setAttribute("pop", Long.valueOf(100));
newFeature.setAttribute("pid", UUID.randomUUID().toString());
newFeature.setAttribute("when", DateUtilities.parseISO("2005-05-19T20:32:56Z"));
newFeature.setAttribute("geometry", factory.createPoint(new Coordinate(43.454, 28.232)));
writer.write();
newFeature = writer.next();
newFeature.setAttribute("pop", Long.valueOf(100));
newFeature.setAttribute("pid", UUID.randomUUID().toString());
newFeature.setAttribute("when", DateUtilities.parseISO("2005-05-18T20:32:56Z"));
newFeature.setAttribute("geometry", factory.createPoint(new Coordinate(43.454, 27.232)));
writer.write();
newFeature = writer.next();
newFeature.setAttribute("pop", Long.valueOf(100));
newFeature.setAttribute("pid", UUID.randomUUID().toString());
newFeature.setAttribute("when", DateUtilities.parseISO("2005-05-17T20:32:56Z"));
newFeature.setAttribute("geometry", factory.createPoint(new Coordinate(43.454, 28.232)));
writer.write();
writer.close();
transaction1.commit();
transaction1.close();
}
示例14
@Before
public void setup() throws IOException, GeoWavePluginException, SchemaException {
geotoolsDataStore = createDataStore();
type = DataUtilities.createType(typeName, typeSpec);
geotoolsDataStore.createSchema(type);
final Transaction transaction = new DefaultTransaction();
final FeatureWriter<SimpleFeatureType, SimpleFeature> writer =
geotoolsDataStore.getFeatureWriter(type.getTypeName(), transaction);
assertFalse(writer.hasNext());
SimpleFeature newFeature = writer.next();
newFeature.setAttribute(
geometry_attribute,
GeometryUtils.GEOMETRY_FACTORY.createPoint(new Coordinate(41.25, 41.25)));
newFeature.setAttribute(long_attribute, 1l);
newFeature.setAttribute(string_attribute, "string1");
writer.write();
newFeature = writer.next();
newFeature.setAttribute(
geometry_attribute,
GeometryUtils.GEOMETRY_FACTORY.createPoint(new Coordinate(41.5, 41.5)));
newFeature.setAttribute(long_attribute, 2l);
newFeature.setAttribute(string_attribute, "string2");
writer.write();
newFeature = writer.next();
newFeature.setAttribute(
geometry_attribute,
GeometryUtils.GEOMETRY_FACTORY.createPoint(new Coordinate(41.75, 41.75)));
newFeature.setAttribute(long_attribute, 3l);
newFeature.setAttribute(string_attribute, "string3");
writer.write();
writer.close();
transaction.commit();
transaction.close();
}
示例15
public void populate() throws IOException, CQLException, ParseException {
final Transaction transaction1 = new DefaultTransaction();
final FeatureWriter<SimpleFeatureType, SimpleFeature> writer =
dataStore.getFeatureWriter(type.getTypeName(), transaction1);
assertFalse(writer.hasNext());
SimpleFeature newFeature = writer.next();
newFeature.setAttribute("pop", Long.valueOf(100));
newFeature.setAttribute("pid", UUID.randomUUID().toString());
newFeature.setAttribute("start", DateUtilities.parseISO("2005-05-17T20:32:56Z"));
newFeature.setAttribute("end", DateUtilities.parseISO("2005-05-19T20:32:56Z"));
newFeature.setAttribute("geometry", factory.createPoint(new Coordinate(43.454, 28.232)));
writer.write();
newFeature = writer.next();
newFeature.setAttribute("pop", Long.valueOf(100));
newFeature.setAttribute("pid", UUID.randomUUID().toString());
newFeature.setAttribute("start", DateUtilities.parseISO("2005-05-18T20:32:56Z"));
newFeature.setAttribute("end", DateUtilities.parseISO("2005-05-20T20:32:56Z"));
newFeature.setAttribute("geometry", factory.createPoint(new Coordinate(43.454, 27.232)));
writer.write();
newFeature = writer.next();
newFeature.setAttribute("pop", Long.valueOf(100));
newFeature.setAttribute("pid", UUID.randomUUID().toString());
newFeature.setAttribute("start", DateUtilities.parseISO("2005-05-21T20:32:56Z"));
newFeature.setAttribute("end", DateUtilities.parseISO("2005-05-22T20:32:56Z"));
newFeature.setAttribute("geometry", factory.createPoint(new Coordinate(43.454, 28.232)));
writer.write();
writer.close();
transaction1.commit();
transaction1.close();
}
示例16
@Test
public void test() throws ParseException, IOException {
final Calendar gmt = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
final Calendar local = Calendar.getInstance(TimeZone.getTimeZone("EDT"));
local.setTimeInMillis(gmt.getTimeInMillis());
final TemporalRange rGmt = new TemporalRange(gmt.getTime(), gmt.getTime());
final TemporalRange rLocal = new TemporalRange(local.getTime(), local.getTime());
rGmt.fromBinary(rGmt.toBinary());
assertEquals(gmt.getTime(), rGmt.getEndTime());
assertEquals(rLocal.getEndTime(), rGmt.getEndTime());
assertEquals(rLocal.getEndTime().getTime(), rGmt.getEndTime().getTime());
final Transaction transaction1 = new DefaultTransaction();
final FeatureWriter<SimpleFeatureType, SimpleFeature> writer =
dataStore.getFeatureWriter(type.getTypeName(), transaction1);
final SimpleFeature newFeature = writer.next();
newFeature.setAttribute("pop", Long.valueOf(77));
newFeature.setAttribute("pid", UUID.randomUUID().toString());
newFeature.setAttribute("when", DateUtilities.parseISO("2005-05-19T19:32:56-04:00"));
newFeature.setAttribute("geometry", factory.createPoint(new Coordinate(43.454, 28.232)));
final FeatureTimeRangeStatistics stats = new FeatureTimeRangeStatistics(null, "when");
stats.entryIngested(newFeature);
assertEquals(
DateUtilities.parseISO("2005-05-19T23:32:56Z"),
stats.asTemporalRange().getStartTime());
}
示例17
public void write(SimpleFeatureCollection obj, OutputStream output, SimpleFeatureType sft,
String layerId) throws IOException {
File shpFile = File.createTempFile("shpFile", ".shp");
ShapefileDataStoreFactory fact = new ShapefileDataStoreFactory();
Map<String, Serializable> params = new HashMap<String, Serializable>();
params.put(ShapefileDataStoreFactory.URLP.key, shpFile.toURI().toURL());
params.put(ShapefileDataStoreFactory.CREATE_SPATIAL_INDEX.key, Boolean.TRUE);
ShapefileDataStore shpDataStore = (ShapefileDataStore) fact.createNewDataStore(params);
shpDataStore.createSchema(sft);
SimpleFeatureStore store = (SimpleFeatureStore) shpDataStore.getFeatureSource(shpDataStore.getTypeNames()[0]);
Transaction transaction = new DefaultTransaction("create");
store.setTransaction(transaction);
store.addFeatures(obj);
transaction.commit();
ZipOutputStream os = new ZipOutputStream(output);
final String fileName = shpFile.getName().substring(0, shpFile.getName().lastIndexOf("."));
File[] shpFiles = new File(shpFile.getParent()).listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.contains(fileName);
}
});
for (File file : shpFiles) {
os.putNextEntry(new ZipEntry(layerId + file.getName().substring(file.getName().lastIndexOf("."))));
IOUtils.copy(new FileInputStream(file), os);
file.delete();
}
os.close();
output.flush();
}
示例18
private static void writeToFile(ShapefileDataStore dataStore, DefaultFeatureCollection collection) throws Exception {
// If you decide to use the TYPE type and create a Data Store with it,
// You will need to uncomment this line to set the Coordinate Reference System
// newDataStore.forceSchemaCRS(DefaultGeographicCRS.WGS84);
Transaction transaction = new DefaultTransaction("create");
String typeName = dataStore.getTypeNames()[0];
SimpleFeatureSource featureSource = dataStore.getFeatureSource(typeName);
if (featureSource instanceof SimpleFeatureStore) {
SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
featureStore.setTransaction(transaction);
try {
featureStore.addFeatures(collection);
transaction.commit();
} catch (Exception problem) {
problem.printStackTrace();
transaction.rollback();
} finally {
transaction.close();
}
System.exit(0); // success!
} else {
System.out.println(typeName + " does not support read/write access");
System.exit(1);
}
}
示例19
@Before
public void setup() throws SchemaException, CQLException, Exception {
setupConf();
try (final GeoWaveGeoIndexer indexer = new GeoWaveGeoIndexer()) {
indexer.setConf(conf);
dataStore = indexer.getGeoToolsDataStore();
// Clear old data
indexer.purge(conf);
type = DataUtilities.createType(
"GeoWaveFeatureReaderTest",
"geometry:Geometry:srid=4326,start:Date,end:Date,pop:java.lang.Long,pid:String");
dataStore.createSchema(type);
stime = DateUtilities.parseISO("2005-05-15T20:32:56Z");
etime = DateUtilities.parseISO("2005-05-20T20:32:56Z");
final Transaction transaction1 = new DefaultTransaction();
final FeatureWriter<SimpleFeatureType, SimpleFeature> writer = dataStore.getFeatureWriter(
type.getTypeName(),
transaction1);
assertFalse(writer.hasNext());
SimpleFeature newFeature = writer.next();
newFeature.setAttribute(
"pop",
Long.valueOf(100));
newFeature.setAttribute(
"pid",
"a" + UUID.randomUUID().toString());
newFeature.setAttribute(
"start",
stime);
newFeature.setAttribute(
"end",
etime);
newFeature.setAttribute(
"geometry",
factory.createPoint(new Coordinate(27.25, 41.25)));
fids.add(newFeature.getID());
pids.add(newFeature.getAttribute("pid").toString());
writer.write();
newFeature = writer.next();
newFeature.setAttribute(
"pop",
Long.valueOf(101));
newFeature.setAttribute(
"pid",
"b" + UUID.randomUUID().toString());
newFeature.setAttribute(
"start",
etime);
newFeature.setAttribute(
"geometry",
factory.createPoint(new Coordinate(28.25, 41.25)));
fids.add(newFeature.getID());
pids.add(newFeature.getAttribute("pid").toString());
writer.write();
writer.close();
transaction1.commit();
transaction1.close();
query = new Query(
"GeoWaveFeatureReaderTest",
ECQL.toFilter("IN ('" + fids.get(0) + "')"),
new String[] {
"geometry",
"pid"
});
}
}
示例20
@Execute
public void writeFeatureCollection() throws IOException {
if (!concatOr(!hasWritten, doReset)) {
return;
}
pm.beginTask("Writing features to shapefile...", -1);
if (!file.endsWith(".shp")) {
file = file + ".shp";
}
if (geodata != null && geodata.size() != 0) {
pType = geodata.getSchema();
}
File shapeFile = new File(file);
FileDataStoreFactorySpi factory = FileDataStoreFinder.getDataStoreFactory("shp");
Map<String, Serializable> map = new HashMap<>();
map.put("url", shapeFile.toURI().toURL());
String shpDoIndex = PreferencesHandler.getShpDoIndex();
if (shpDoIndex != null) {
map.put("create spatial index", new Boolean(shpDoIndex));
}
String shpCharset = PreferencesHandler.getShpCharset();
if (shpCharset != null) {
map.put("charset", shpCharset);
}
DataStore newDataStore = factory.createNewDataStore(map);
newDataStore.createSchema(pType);
Transaction transaction = new DefaultTransaction("create");
String typeName = newDataStore.getTypeNames()[0];
SimpleFeatureStore featureStore = (SimpleFeatureStore) newDataStore.getFeatureSource(typeName);
featureStore.setTransaction(transaction);
try {
if (geodata == null) {
featureStore.addFeatures(new DefaultFeatureCollection());
} else {
featureStore.addFeatures(geodata);
}
transaction.commit();
} catch (Exception problem) {
transaction.rollback();
throw new IOException(problem.getLocalizedMessage());
} finally {
transaction.close();
pm.done();
}
hasWritten = true;
}
示例21
@Override
public void output(final ResultSet results) {
int geometryColumn = -1;
for (int i = 0; i < results.columnCount(); i++) {
if (Geometry.class.isAssignableFrom(results.columnType(i))) {
geometryColumn = i;
break;
}
}
if (geometryColumn < 0) {
throw new RuntimeException(
"Unable to output results to a shapefile without a geometry column.");
}
final SimpleFeatureTypeBuilder ftb = new SimpleFeatureTypeBuilder();
ftb.setCRS(results.getCRS());
ftb.setName(typeName);
for (int i = 0; i < results.columnCount(); i++) {
final AttributeTypeBuilder atb = new AttributeTypeBuilder();
atb.setBinding(results.columnType(i));
atb.nillable(true);
if (i == geometryColumn) {
ftb.add(atb.buildDescriptor("the_geom"));
} else {
ftb.add(atb.buildDescriptor(results.columnName(i)));
}
}
final SimpleFeatureType sft = ftb.buildFeatureType();
final SimpleFeatureBuilder sfb = new SimpleFeatureBuilder(sft);
final AtomicLong nextId = new AtomicLong(0L);
final Iterator<SimpleFeature> features = Iterators.transform(results, r -> {
sfb.reset();
for (int i = 0; i < results.columnCount(); i++) {
sfb.add(r.columnValue(i));
}
final SimpleFeature feature = sfb.buildFeature(Long.toString(nextId.incrementAndGet()));
return feature;
});
final FileDataStoreFactorySpi factory = FileDataStoreFinder.getDataStoreFactory("shp");
final File file = new File(outputFile);
final Map<String, Serializable> params = Maps.newHashMap();
final Transaction transaction = new DefaultTransaction("Write Results");
try {
params.put("url", file.toURI().toURL());
final DataStore dataStore = factory.createNewDataStore(params);
dataStore.createSchema(sft);
final SimpleFeatureStore store =
(SimpleFeatureStore) dataStore.getFeatureSource(dataStore.getTypeNames()[0]);
store.setTransaction(transaction);
final SimpleFeatureCollection featureCollection =
DataUtilities.collection(new SimpleFeatureIterator() {
@Override
public boolean hasNext() {
return features.hasNext();
}
@Override
public SimpleFeature next() throws NoSuchElementException {
return features.next();
}
@Override
public void close() {}
});
store.addFeatures(featureCollection);
transaction.commit();
} catch (final Exception e) {
try {
transaction.rollback();
} catch (final IOException ioe) {
throw new RuntimeException("Encountered an error when rolling back transaction", ioe);
}
throw new RuntimeException(
"Encountered an error when writing the features to the file: " + e.getMessage(),
e);
}
}
示例22
@Before
public void setup() throws SchemaException, CQLException, Exception {
dataStore = createDataStore();
type =
DataUtilities.createType(
"GeoWaveFeatureReaderTest",
"geometry:Geometry:srid=4326,start:Date,end:Date,pop:java.lang.Long,pid:String");
((GeoWaveGTDataStore) dataStore).getIndexStore().addIndex(
new SpatialIndexBuilder().createIndex());
((GeoWaveGTDataStore) dataStore).getIndexStore().addIndex(
new SpatialTemporalIndexBuilder().createIndex());
dataStore.createSchema(type);
stime = DateUtilities.parseISO("2005-05-15T20:32:56Z");
mtime = DateUtilities.parseISO("2005-05-20T20:32:56Z");
etime = DateUtilities.parseISO("2005-05-25T20:32:56Z");
final Transaction transaction1 = new DefaultTransaction();
final FeatureWriter<SimpleFeatureType, SimpleFeature> writer =
dataStore.getFeatureWriter(type.getTypeName(), transaction1);
assertFalse(writer.hasNext());
SimpleFeature newFeature = writer.next();
newFeature.setAttribute("pop", Long.valueOf(100));
newFeature.setAttribute("pid", "a" + UUID.randomUUID().toString());
newFeature.setAttribute("start", stime);
newFeature.setAttribute("end", mtime);
newFeature.setAttribute("geometry", factory.createPoint(new Coordinate(27.25, 41.25)));
fids.add(newFeature.getID());
pids.add(newFeature.getAttribute("pid").toString());
writer.write();
newFeature = writer.next();
newFeature.setAttribute("pop", Long.valueOf(101));
newFeature.setAttribute("pid", "b" + UUID.randomUUID().toString());
newFeature.setAttribute("start", mtime);
newFeature.setAttribute("end", etime);
newFeature.setAttribute("geometry", factory.createPoint(new Coordinate(28.25, 41.25)));
fids.add(newFeature.getID());
pids.add(newFeature.getAttribute("pid").toString());
writer.write();
writer.close();
transaction1.commit();
transaction1.close();
query =
new Query(
"GeoWaveFeatureReaderTest",
ECQL.toFilter("IN ('" + fids.get(0) + "')"),
new String[] {"geometry", "pid"});
}
示例23
@Test
public void testInsertIsolation() throws IOException, CQLException {
final Transaction transaction1 = new DefaultTransaction();
final FeatureWriter<SimpleFeatureType, SimpleFeature> writer =
dataStore.getFeatureWriter(type.getTypeName(), transaction1);
assertFalse(writer.hasNext());
final SimpleFeature newFeature = writer.next();
newFeature.setAttribute("pop", Long.valueOf(100));
newFeature.setAttribute("pid", UUID.randomUUID().toString());
newFeature.setAttribute("geometry", factory.createPoint(new Coordinate(27.25, 41.25)));
writer.write();
writer.close();
FeatureReader<SimpleFeatureType, SimpleFeature> reader =
dataStore.getFeatureReader(query, transaction1);
assertTrue(reader.hasNext());
final SimpleFeature priorFeature = reader.next();
assertEquals(newFeature.getAttribute("pid"), priorFeature.getAttribute("pid"));
reader.close();
// uncommitted at this point, so this next transaction should not see
// it.
final Transaction transaction2 = new DefaultTransaction();
reader = dataStore.getFeatureReader(query, transaction2);
assertFalse(reader.hasNext());
reader.close();
transaction1.commit();
reader = dataStore.getFeatureReader(query, transaction1);
assertTrue(reader.hasNext());
reader.next();
assertFalse(reader.hasNext());
reader.close();
transaction1.close();
// since this implementation does not support serializable, transaction2
// can see the changes even though
// it started after transaction1 and before the commit.
reader = dataStore.getFeatureReader(query, transaction2);
assertTrue(reader.hasNext());
reader.next();
assertFalse(reader.hasNext());
reader.close();
transaction2.commit();
transaction2.close();
// stats check
final Transaction transaction3 = new DefaultTransaction();
reader =
((GeoWaveFeatureSource) ((GeoWaveGTDataStore) dataStore).getFeatureSource(
"geostuff",
transaction3)).getReaderInternal(query);
final Map<StatisticsId, InternalDataStatistics<SimpleFeature, ?, ?>> transStats =
((GeoWaveFeatureReader) reader).getTransaction().getDataStatistics();
assertNotNull(
transStats.get(
FeatureNumericRangeStatistics.STATS_TYPE.newBuilder().fieldName(
"pop").build().getId()));
transaction3.close();
}
示例24
@edu.umd.cs.findbugs.annotations.SuppressFBWarnings(
value = "RV_RETURN_VALUE_IGNORED_BAD_PRACTICE",
justification = "Directories may alreadybe there")
public static void writeShape(final String typeName, final File dir, final Geometry[] shapes)
throws IOException {
FileUtils.deleteDirectory(dir);
dir.mkdirs();
final SimpleFeatureBuilder featureBuilder =
new SimpleFeatureBuilder(createFeatureType(typeName, shapes[0] instanceof Point));
final ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
final Map<String, Serializable> params = new HashMap<>();
params.put("url", new File(dir.getAbsolutePath() + "/" + typeName + ".shp").toURI().toURL());
params.put("create spatial index", Boolean.TRUE);
final ShapefileDataStore newDataStore =
(ShapefileDataStore) dataStoreFactory.createNewDataStore(params);
newDataStore.createSchema(createFeatureType(typeName, shapes[0] instanceof Point));
final Transaction transaction = new DefaultTransaction("create");
try (final FeatureWriter<SimpleFeatureType, SimpleFeature> writer =
newDataStore.getFeatureWriterAppend(typeName, transaction)) {
final int i = 1;
for (final Geometry shape : shapes) {
featureBuilder.add(shape);
featureBuilder.add(Integer.valueOf(i));
final SimpleFeature feature = featureBuilder.buildFeature(null);
final SimpleFeature copy = writer.next();
for (final AttributeDescriptor attrD : feature.getFeatureType().getAttributeDescriptors()) {
// the null case should only happen for geometry
if (copy.getFeatureType().getDescriptor(attrD.getName()) != null) {
copy.setAttribute(attrD.getName(), feature.getAttribute(attrD.getName()));
}
}
// shape files force geometry name to be 'the_geom'. So isolate
// this change
copy.setDefaultGeometry(feature.getDefaultGeometry());
writer.write();
}
} catch (final IOException e) {
LOGGER.warn("Problem with the FeatureWritter", e);
transaction.rollback();
} finally {
transaction.commit();
transaction.close();
}
}
示例25
@edu.umd.cs.findbugs.annotations.SuppressFBWarnings(
value = "RV_RETURN_VALUE_IGNORED_BAD_PRACTICE",
justification = "Directories may alreadybe there")
public static void writeShape(final File dir, final List<SimpleFeature> shapes)
throws IOException {
FileUtils.deleteDirectory(dir);
dir.mkdirs();
final ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
final String typeName = shapes.get(0).getType().getTypeName();
final Map<String, Serializable> params = new HashMap<>();
params.put("url", new File(dir.getAbsolutePath() + "/" + typeName + ".shp").toURI().toURL());
params.put("create spatial index", Boolean.TRUE);
final ShapefileDataStore newDataStore =
(ShapefileDataStore) dataStoreFactory.createNewDataStore(params);
newDataStore.createSchema(shapes.get(0).getFeatureType());
final Transaction transaction = new DefaultTransaction("create");
try (final FeatureWriter<SimpleFeatureType, SimpleFeature> writer =
newDataStore.getFeatureWriterAppend(typeName, transaction)) {
for (final SimpleFeature shape : shapes) {
final SimpleFeature copy = writer.next();
for (final AttributeDescriptor attrD : copy.getFeatureType().getAttributeDescriptors()) {
// the null case should only happen for geometry
if (copy.getFeatureType().getDescriptor(attrD.getName()) != null) {
copy.setAttribute(attrD.getName(), shape.getAttribute(attrD.getName()));
}
}
// shape files force geometry name to be 'the_geom'. So isolate
// this change
copy.setDefaultGeometry(shape.getDefaultGeometry());
writer.write();
}
} catch (final IOException e) {
LOGGER.warn("Problem with the FeatureWritter", e);
transaction.rollback();
} finally {
transaction.commit();
transaction.close();
}
}