本文整理汇总了Java中org.geotools.data.simple.SimpleFeatureStore.addFeatures方法的典型用法代码示例。如果您正苦于以下问题:Java SimpleFeatureStore.addFeatures方法的具体用法?Java SimpleFeatureStore.addFeatures怎么用?Java SimpleFeatureStore.addFeatures使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.geotools.data.simple.SimpleFeatureStore
的用法示例。
在下文中一共展示了SimpleFeatureStore.addFeatures方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: addFeature
import org.geotools.data.simple.SimpleFeatureStore; //导入方法依赖的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.simple.SimpleFeatureStore; //导入方法依赖的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.simple.SimpleFeatureStore; //导入方法依赖的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: addFeaturesExample
import org.geotools.data.simple.SimpleFeatureStore; //导入方法依赖的package包/类
private void addFeaturesExample() throws Exception {
// addExample start
SimpleFeatureStore store = (SimpleFeatureStore) dataStore.getFeatureSource( typeName );
SimpleFeatureType featureType = store.getSchema();
SimpleFeatureBuilder build = new SimpleFeatureBuilder(featureType);
GeometryBuilder geom = new GeometryBuilder();
List<SimpleFeature> list = new ArrayList<SimpleFeature>();
list.add( build.buildFeature("fid1", new Object[]{ geom.point(1,1), "hello" } ) );
list.add( build.buildFeature("fid2", new Object[]{ geom.point(2,3), "martin" } ) );
SimpleFeatureCollection collection = new ListFeatureCollection(featureType, list);
Transaction transaction = new DefaultTransaction("Add Example");
store.setTransaction( transaction );
try {
store.addFeatures( collection );
transaction.commit(); // actually writes out the features in one go
}
catch( Exception eek){
transaction.rollback();
}
// addExample end
}
示例5: addFeatureIdExample
import org.geotools.data.simple.SimpleFeatureStore; //导入方法依赖的package包/类
private void addFeatureIdExample(SimpleFeatureCollection collection ) throws Exception {
// addFeatureIdExample start
Transaction transaction = new DefaultTransaction("Add Example");
SimpleFeatureStore store = (SimpleFeatureStore) dataStore.getFeatureSource(typeName);
store.setTransaction(transaction);
try {
List<FeatureId> added = store.addFeatures( collection );
System.out.println( added ); // prints out the temporary feature ids
transaction.commit();
System.out.println( added ); // prints out the final feature ids
Set<FeatureId> selection = new HashSet<FeatureId>( added );
FilterFactory ff = CommonFactoryFinder.getFilterFactory();
Filter selected = ff.id( selection ); // filter selecting all the features just added
}
catch( Exception problem){
transaction.rollback();
throw problem;
}
// addFeatureIdExample end
}
示例6: write
import org.geotools.data.simple.SimpleFeatureStore; //导入方法依赖的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);
}
}
示例7: collectionToShapeFile
import org.geotools.data.simple.SimpleFeatureStore; //导入方法依赖的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;
}
示例8: makePointLayer
import org.geotools.data.simple.SimpleFeatureStore; //导入方法依赖的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();
}
}
示例9: writeEsriShapefile
import org.geotools.data.simple.SimpleFeatureStore; //导入方法依赖的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");
}
}
示例10: addFeaturesEvents
import org.geotools.data.simple.SimpleFeatureStore; //导入方法依赖的package包/类
private void addFeaturesEvents(SimpleFeatureCollection collection ) throws Exception {
// addEvents start
Transaction transaction = new DefaultTransaction("Add Example");
SimpleFeatureStore store = (SimpleFeatureStore) dataStore.getFeatureSource(typeName);
store.setTransaction(transaction);
class CommitListener implements FeatureListener {
public void changed(FeatureEvent featureEvent) {
if (featureEvent instanceof BatchFeatureEvent ){
BatchFeatureEvent batchEvent = (BatchFeatureEvent) featureEvent;
System.out.println( "area changed:" + batchEvent.getBounds() );
System.out.println( "created fids:" + batchEvent.fids );
}
else {
System.out.println( "bounds:" + featureEvent.getBounds() );
System.out.println( "change:" + featureEvent.filter );
}
}
}
CommitListener listener = new CommitListener();
store.addFeatureListener( listener );
try {
List<FeatureId> added = store.addFeatures( collection );
transaction.commit();
}
catch( Exception problem){
transaction.rollback();
throw problem;
}
// addEvents end
}
示例11: exportToShapefile
import org.geotools.data.simple.SimpleFeatureStore; //导入方法依赖的package包/类
DataStore exportToShapefile(MemoryDataStore memory, String typeName, File directory)
throws IOException {
// existing feature source from MemoryDataStore
SimpleFeatureSource featureSource = memory.getFeatureSource(typeName);
SimpleFeatureType ft = featureSource.getSchema();
String fileName = ft.getTypeName();
File file = new File(directory, fileName + ".shp");
Map<String, java.io.Serializable> creationParams = new HashMap<String, java.io.Serializable>();
creationParams.put("url", DataUtilities.fileToURL(file));
FileDataStoreFactorySpi factory = FileDataStoreFinder.getDataStoreFactory("shp");
DataStore dataStore = factory.createNewDataStore(creationParams);
dataStore.createSchema(ft);
// The following workaround to write out the prj is no longer needed
// ((ShapefileDataStore)dataStore).forceSchemaCRS(ft.getCoordinateReferenceSystem());
SimpleFeatureStore featureStore = (SimpleFeatureStore) dataStore.getFeatureSource(typeName);
Transaction t = new DefaultTransaction();
try {
SimpleFeatureCollection collection = featureSource.getFeatures(); // grab all features
featureStore.addFeatures(collection);
t.commit(); // write it out
} catch (IOException eek) {
eek.printStackTrace();
try {
t.rollback();
} catch (IOException doubleEeek) {
// rollback failed?
}
} finally {
t.close();
}
return dataStore;
}
示例12: create
import org.geotools.data.simple.SimpleFeatureStore; //导入方法依赖的package包/类
@Override
@Transactional(rollbackFor = { Throwable.class })
public Object create(Object feature) throws LayerException {
SimpleFeatureSource source = getFeatureSource();
if (source instanceof SimpleFeatureStore) {
SimpleFeatureStore store = (SimpleFeatureStore) source;
DefaultFeatureCollection collection = new DefaultFeatureCollection();
collection.add((SimpleFeature) feature);
transactionSynchronization.synchTransaction(store);
try {
List<FeatureId> ids = store.addFeatures(collection);
// fetch it again to get the generated values !!!
if (ids.size() == 1) {
return read(ids.get(0).getID());
}
} catch (IOException ioe) {
featureModelUsable = false;
throw new LayerException(ioe, ExceptionCode.LAYER_MODEL_IO_EXCEPTION);
}
return feature;
} else {
log.error("Don't know how to create or update " + getFeatureSourceName() + ", class "
+ source.getClass().getName() + " does not implement SimpleFeatureStore");
throw new LayerException(ExceptionCode.CREATE_OR_UPDATE_NOT_IMPLEMENTED, getFeatureSourceName(), source
.getClass().getName());
}
}
示例13: write
import org.geotools.data.simple.SimpleFeatureStore; //导入方法依赖的package包/类
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();
}
示例14: zippedShapefileGet
import org.geotools.data.simple.SimpleFeatureStore; //导入方法依赖的package包/类
/** @return Evenly spaced travel time contours (isochrones) as a zipped shapefile. */
@GET
@Produces("application/x-zip-compressed")
public Response zippedShapefileGet(@QueryParam("stream") @DefaultValue("true") boolean stream) throws Exception {
SimpleFeatureCollection contourFeatures = makeContourFeatures();
/* Output the staged features to Shapefile */
final File shapeDir = Files.createTempDir();
File shapeFile = new File(shapeDir, this.shpName + ".shp");
LOG.debug("writing out shapefile {}", shapeFile);
ShapefileDataStore outStore = new ShapefileDataStore(shapeFile.toURI().toURL());
outStore.createSchema(contourSchema);
/*
* "FeatureSource is used to read features, the subclass FeatureStore is used for read/write
* access. The way to tell if a File can be written to in GeoTools is to use an instanceof
* check.
*/
SimpleFeatureStore featureStore = (SimpleFeatureStore) outStore.getFeatureSource();
featureStore.addFeatures(contourFeatures);
// close?
shapeDir.deleteOnExit(); // Note: the order is important
for (File f : shapeDir.listFiles()) {
f.deleteOnExit();
}
/* Zip up the shapefile components */
StreamingOutput output = new DirectoryZipper(shapeDir);
if (stream) {
return Response.ok().entity(output).build();
} else {
File zipFile = new File(shapeDir, this.shpName + ".zip");
OutputStream fos = new FileOutputStream(zipFile);
output.write(fos);
zipFile.deleteOnExit();
return Response.ok().entity(zipFile).build();
}
}
示例15: mergeShapeFile
import org.geotools.data.simple.SimpleFeatureStore; //导入方法依赖的package包/类
/**
*
* @param layer
* @param shpInput
* @param transform
* @return
* @throws Exception
*/
public static GeometryImage mergeShapeFile(SimpleFeatureCollection collectionsLayer, File shpInput,GeoTransform transform,Polygon bbox)throws Exception {
Map<String, Serializable> params = new HashMap<String, Serializable>();
params.put("url", shpInput.toURI().toURL());
//create a DataStore object to connect to the physical source
DataStore dataStore = DataStoreFinder.getDataStore(params);
SimpleFeatureSource shape2 = (SimpleFeatureSource) dataStore.getFeatureSource(dataStore.getTypeNames()[0]);
SimpleFeatureCollection collectionsShape2=shape2.getFeatures();
SimpleFeatureType schemaShape2=shape2.getSchema();
ClipProcess clip=new ClipProcess();
SimpleFeatureCollection fc=clip.execute(collectionsShape2, bbox,true);
SimpleFeatureType schemaLayer=collectionsLayer.getSchema();
//merge the schema and the types
SimpleFeatureTypeBuilder stb = new SimpleFeatureTypeBuilder();
stb.setName("merged_geom");
stb.setCRS(schemaLayer.getCoordinateReferenceSystem());
stb.addAll(schemaShape2.getAttributeDescriptors());
stb.addAll(schemaLayer.getAttributeDescriptors());
SimpleFeatureType newFeatureType = stb.buildFeatureType();
//create new datastore to save the new shapefile
FileDataStoreFactorySpi factory = new ShapefileDataStoreFactory();
File tmp=new File(SumoPlatform.getApplication().getCachePath()+"\\tmpshape_"+System.currentTimeMillis()+".shp");
Map<String, Serializable> params2 = new HashMap<String, Serializable>();
params2.put("url", tmp.toURI().toURL());
ShapefileDataStore newds=(ShapefileDataStore)factory.createNewDataStore(params2);
GeometryImage out=null;
try{
newds.createSchema(newFeatureType);
//merge the feaures
SimpleFeatureStore mergeFeat=(SimpleFeatureStore)newds.getFeatureSource();
mergeFeat.addFeatures(collectionsLayer);
mergeFeat.addFeatures(fc);
//save the new shape file
exportToShapefile(newds, mergeFeat.getFeatures());//,newds.getSchema());
//from here create the new GeometricLayer
Collection<PropertyDescriptor>descriptorsMerge=new ArrayList<>();
descriptorsMerge.addAll(schemaShape2.getDescriptors());
descriptorsMerge.addAll(schemaLayer.getDescriptors());
String[] schema = createSchema(descriptorsMerge);
String[] types = createTypes(descriptorsMerge);
String geoName = schemaLayer.getGeometryDescriptor().getType().getName().toString();
out=GeometryImage.createLayerFromFeatures(geoName, newds, mergeFeat.getFeatures(), schema, types,true,transform);
//out = GeometricLayer.createImageProjectedLayer(out, transform,null);
out.setName("merge_"+shpInput.getName()+"_"+LayerManager.getIstanceManager().getCurrentImageLayer().getName());
}finally{
if(newds!=null)
newds.dispose();
}
return out;
}