use of org.geotools.feature.DefaultFeatureCollection in project spatial-portal by AtlasOfLivingAustralia.
the class ShapefileUtils method saveShapefile.
public static void saveShapefile(File shpfile, String wktString, String name) {
try {
final SimpleFeatureType type = createFeatureType();
List<SimpleFeature> features = new ArrayList<SimpleFeature>();
SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(type);
WKTReader wkt = new WKTReader();
Geometry geom = wkt.read(wktString);
if (geom instanceof GeometryCollection) {
GeometryCollection gc = (GeometryCollection) geom;
for (int i = 0; i < gc.getNumGeometries(); i++) {
Geometry g = gc.getGeometryN(i);
if (g instanceof Polygon) {
g = new GeometryBuilder().multiPolygon((Polygon) g);
}
featureBuilder.add(g);
SimpleFeature feature = featureBuilder.buildFeature(null);
feature.setAttribute("name", name);
features.add(feature);
}
} else {
Geometry g = geom;
if (g instanceof Polygon) {
g = new GeometryBuilder().multiPolygon((Polygon) g);
}
featureBuilder.add(g);
SimpleFeature feature = featureBuilder.buildFeature(null);
features.add(feature);
}
ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
Map<String, Serializable> params = new HashMap<String, Serializable>();
params.put("url", shpfile.toURI().toURL());
params.put("create spatial index", Boolean.TRUE);
ShapefileDataStore newDataStore = (ShapefileDataStore) dataStoreFactory.createNewDataStore(params);
newDataStore.createSchema(type);
newDataStore.forceSchemaCRS(DefaultGeographicCRS.WGS84);
Transaction transaction = new DefaultTransaction("create");
String typeName = newDataStore.getTypeNames()[0];
SimpleFeatureSource featureSource = newDataStore.getFeatureSource(typeName);
if (featureSource instanceof SimpleFeatureStore) {
SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
DefaultFeatureCollection collection = new DefaultFeatureCollection();
collection.addAll(features);
featureStore.setTransaction(transaction);
try {
featureStore.addFeatures(collection);
transaction.commit();
} catch (Exception problem) {
LOGGER.error("error pricessing shape file: " + shpfile.getAbsolutePath(), problem);
transaction.rollback();
} finally {
transaction.close();
}
}
LOGGER.debug("Active Area shapefile written to: " + shpfile.getAbsolutePath());
} catch (Exception e) {
LOGGER.error("Unable to save shapefile: " + shpfile.getAbsolutePath(), e);
}
}
use of org.geotools.feature.DefaultFeatureCollection in project incubator-rya by apache.
the class GeoMesaGeoIndexer method deleteStatements.
private void deleteStatements(final Collection<RyaStatement> ryaStatements) throws IOException {
// create a feature collection
final DefaultFeatureCollection featureCollection = new DefaultFeatureCollection();
for (final RyaStatement ryaStatement : ryaStatements) {
final Statement statement = RyaToRdfConversions.convertStatement(ryaStatement);
// if the predicate list is empty, accept all predicates.
// Otherwise, make sure the predicate is on the "valid" list
final boolean isValidPredicate = validPredicates.isEmpty() || validPredicates.contains(statement.getPredicate());
if (isValidPredicate && (statement.getObject() instanceof Literal)) {
try {
final SimpleFeature feature = createFeature(featureType, statement);
featureCollection.add(feature);
} catch (final ParseException e) {
logger.warn("Error getting geo from statement: " + statement.toString(), e);
}
}
}
// remove this feature collection from the store
if (!featureCollection.isEmpty()) {
final Set<Identifier> featureIds = new HashSet<Identifier>();
final FilterFactory filterFactory = CommonFactoryFinder.getFilterFactory(null);
final Set<String> stringIds = DataUtilities.fidSet(featureCollection);
for (final String id : stringIds) {
featureIds.add(filterFactory.featureId(id));
}
final Filter filter = filterFactory.id(featureIds);
featureStore.removeFeatures(filter);
}
}
use of org.geotools.feature.DefaultFeatureCollection in project tutorials by eugenp.
the class GeoToolsUnitTest method givenFeatureType_whenAddLocations_returnFeatureCollection.
@Test
public void givenFeatureType_whenAddLocations_returnFeatureCollection() {
DefaultFeatureCollection collection = new DefaultFeatureCollection();
SimpleFeatureType CITY = ShapeFile.createFeatureType();
ShapeFile.addLocations(CITY, collection);
assertNotNull(collection);
}
use of org.geotools.feature.DefaultFeatureCollection in project polymap4-core by Polymap4.
the class UnitOfWork method prepare.
/**
* Starts a new {@link Transaction} for the underlying {@link FeatureStore},
* checks concurrent modifications and updates {@link FeatureStore}.
*
* @param monitor
* @throws IOException
* @throws {@link ConcurrentModificationException}
*/
public void prepare(IProgressMonitor monitor) throws ConcurrentModificationException, IOException {
assert tx == null : "Pending transaction found.";
if (modified.isEmpty()) {
return;
}
monitor.beginTask("Prepare commit: " + getLabel(), modified.size());
// set transaction
tx = new DefaultTransaction(getClass().getSimpleName() + "-" + hashCode());
fs.setTransaction(tx);
DefaultFeatureCollection added = new DefaultFeatureCollection();
Set<Identifier> removed = new HashSet();
int count = 0;
for (FeatureBufferState buffered : modified.values()) {
if (monitor.isCanceled()) {
return;
}
if ((++count % 100) == 0) {
monitor.subTask("(" + count + ")");
}
if (buffered.isAdded()) {
// no check if fid was created already since it is propably the 'primary key'
added.add((SimpleFeature) buffered.feature());
} else if (buffered.isModified()) {
checkSubmitModified(buffered);
} else if (buffered.isRemoved()) {
removed.add(buffered.feature().getIdentifier());
} else {
log.warn("Buffered feature is not added/removed/modified!");
}
monitor.worked(1);
}
if (!added.isEmpty()) {
fs.addFeatures(added);
monitor.worked(added.size());
}
if (!removed.isEmpty()) {
fs.removeFeatures(ff.id(removed));
monitor.worked(added.size());
}
monitor.done();
}
use of org.geotools.feature.DefaultFeatureCollection in project OpenTripPlanner by opentripplanner.
the class LIsochrone method makeContourFeatures.
/**
* Create a geotools feature collection from a list of isochrones in the OTPA internal format.
* Once in a FeatureCollection, they can for example be exported as GeoJSON.
*/
public static SimpleFeatureCollection makeContourFeatures(List<IsochroneData> isochrones) {
DefaultFeatureCollection featureCollection = new DefaultFeatureCollection(null, contourSchema);
SimpleFeatureBuilder fbuilder = new SimpleFeatureBuilder(contourSchema);
for (IsochroneData isochrone : isochrones) {
fbuilder.add(isochrone.geometry);
fbuilder.add(isochrone.cutoffSec);
featureCollection.add(fbuilder.buildFeature(null));
}
return featureCollection;
}
Aggregations