use of org.geotools.data.simple.SimpleFeatureCollection in project GeoGig by boundlessgeo.
the class ExportOpTest method testExportFromHEAD.
@Test
public void testExportFromHEAD() throws Exception {
Feature[] points = new Feature[] { points1, points2, points3 };
for (Feature feature : points) {
insert(feature);
}
geogig.command(AddOp.class).call();
geogig.command(CommitOp.class).setAll(true).call();
MemoryDataStore dataStore = new MemoryDataStore(pointsType);
final String typeName = dataStore.getTypeNames()[0];
SimpleFeatureSource featureSource = dataStore.getFeatureSource(typeName);
SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
geogig.command(ExportOp.class).setFeatureStore(featureStore).setPath("HEAD:" + pointsName).call();
featureSource = dataStore.getFeatureSource(typeName);
featureStore = (SimpleFeatureStore) featureSource;
SimpleFeatureCollection featureCollection = featureStore.getFeatures();
assertEquals(featureCollection.size(), points.length);
SimpleFeatureIterator features = featureCollection.features();
assertTrue(collectionsAreEqual(features, points));
}
use of org.geotools.data.simple.SimpleFeatureCollection in project GeoGig by boundlessgeo.
the class ExportOpTest method testExportWithAlterUsingDefaultFeatureType.
@Test
public void testExportWithAlterUsingDefaultFeatureType() throws Exception {
Feature[] points = new Feature[] { points2, points1B, points3 };
for (Feature feature : points) {
insert(feature);
}
MemoryDataStore dataStore = new MemoryDataStore(pointsType);
final String typeName = dataStore.getTypeNames()[0];
SimpleFeatureSource featureSource = dataStore.getFeatureSource(typeName);
SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
geogig.command(ExportOp.class).setFeatureStore(featureStore).setPath(pointsName).setAlter(true).call();
featureSource = dataStore.getFeatureSource(typeName);
featureStore = (SimpleFeatureStore) featureSource;
SimpleFeatureCollection featureCollection = featureStore.getFeatures();
assertEquals(featureCollection.size(), points.length);
SimpleFeatureIterator features = featureCollection.features();
assertTrue(collectionsAreEqual(features, points));
}
use of org.geotools.data.simple.SimpleFeatureCollection in project GeoGig by boundlessgeo.
the class ExportOpTest method testExportFromTreeWithSeveralFeatureTypesUsingDefaultFeatureType.
@Test
public void testExportFromTreeWithSeveralFeatureTypesUsingDefaultFeatureType() throws Exception {
Feature[] points = new Feature[] { points2, points1B, points3 };
for (Feature feature : points) {
insert(feature);
}
Feature[] expectedPoints = new Feature[] { points2, points3 };
MemoryDataStore dataStore = new MemoryDataStore(pointsType);
final String typeName = dataStore.getTypeNames()[0];
SimpleFeatureSource featureSource = dataStore.getFeatureSource(typeName);
SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
geogig.command(ExportOp.class).setFeatureStore(featureStore).setPath(pointsName).exportDefaultFeatureType().call();
featureSource = dataStore.getFeatureSource(typeName);
featureStore = (SimpleFeatureStore) featureSource;
SimpleFeatureCollection featureCollection = featureStore.getFeatures();
assertEquals(featureCollection.size(), expectedPoints.length);
SimpleFeatureIterator features = featureCollection.features();
assertTrue(collectionsAreEqual(features, expectedPoints));
}
use of org.geotools.data.simple.SimpleFeatureCollection in project GeoGig by boundlessgeo.
the class GeoGigFeatureSourceTest method testGetFeatures.
@Test
public void testGetFeatures() throws Exception {
SimpleFeatureCollection collection;
Set<List<Object>> actual;
Set<List<Object>> expected;
collection = pointsSource.getFeatures();
assertEquals(pointsType, collection.getSchema());
actual = Sets.newHashSet();
for (Feature f : toList(collection)) {
SimpleFeature sf = (SimpleFeature) f;
actual.add(sf.getAttributes());
}
expected = ImmutableSet.of(((SimpleFeature) points1).getAttributes(), ((SimpleFeature) points2).getAttributes(), ((SimpleFeature) points3).getAttributes());
assertEquals(expected, actual);
collection = linesSource.getFeatures();
assertEquals(linesType, collection.getSchema());
actual = Sets.newHashSet();
for (Feature f : toList(collection)) {
actual.add(((SimpleFeature) f).getAttributes());
}
expected = ImmutableSet.of(((SimpleFeature) lines1).getAttributes(), ((SimpleFeature) lines2).getAttributes(), ((SimpleFeature) lines3).getAttributes());
assertEquals(expected, actual);
}
use of org.geotools.data.simple.SimpleFeatureCollection in project OpenTripPlanner by opentripplanner.
the class PointSet method fromShapefile.
public static PointSet fromShapefile(File file, String originIDField, List<String> propertyFields) throws IOException, NoSuchAuthorityCodeException, FactoryException, EmptyPolygonException, UnsupportedGeometryException {
if (!file.exists())
throw new RuntimeException("Shapefile does not exist.");
FileDataStore store = FileDataStoreFinder.getDataStore(file);
SimpleFeatureSource featureSource = store.getFeatureSource();
CoordinateReferenceSystem sourceCRS = featureSource.getInfo().getCRS();
CoordinateReferenceSystem WGS84 = CRS.decode("EPSG:4326", true);
Query query = new Query();
query.setCoordinateSystem(sourceCRS);
query.setCoordinateSystemReproject(WGS84);
SimpleFeatureCollection featureCollection = featureSource.getFeatures(query);
// Set up fields based on first feature in collection
// This assumes that all features have the same set of properties, which I think is always the case for shapefiles
SimpleFeatureIterator it = featureCollection.features();
SimpleFeature protoFt = it.next();
if (propertyFields == null) {
propertyFields = new ArrayList<String>();
// No property fields specified, so use all property fields
for (Property p : protoFt.getProperties()) {
propertyFields.add(p.getName().toString());
}
// If ID field is specified, don't use it as a property
if (originIDField != null && propertyFields.contains(originIDField)) {
propertyFields.remove(originIDField);
}
}
// Reset iterator
it = featureCollection.features();
PointSet ret = new PointSet(featureCollection.size());
int i = 0;
while (it.hasNext()) {
SimpleFeature feature = it.next();
Geometry geom = (Geometry) feature.getDefaultGeometry();
PointFeature ft = new PointFeature();
ft.setGeom(geom);
// Set feature's ID to the specified ID field, or to index if none is specified
if (originIDField == null) {
ft.setId(Integer.toString(i));
} else {
ft.setId(feature.getProperty(originIDField).getValue().toString());
}
for (Property prop : feature.getProperties()) {
String propName = prop.getName().toString();
if (propertyFields.contains(propName)) {
Object binding = prop.getType().getBinding();
// attempt to coerce the prop's value into an integer
int val;
if (binding.equals(Integer.class)) {
val = (Integer) prop.getValue();
} else if (binding.equals(Long.class)) {
val = ((Long) prop.getValue()).intValue();
} else if (binding.equals(String.class)) {
try {
val = Integer.parseInt((String) prop.getValue());
} catch (NumberFormatException ex) {
continue;
}
} else {
LOG.debug("Property {} of feature {} could not be interpreted as int, skipping", prop.getName().toString(), ft.getId());
continue;
}
ft.addAttribute(propName, val);
} else {
LOG.debug("Property {} not requested; igoring", propName);
}
}
ret.addFeature(ft, i);
i++;
}
ArrayList<String> IDlist = new ArrayList<String>();
for (String id : ret.ids) {
IDlist.add(id);
}
LOG.debug("Created PointSet from shapefile with IDs {}", IDlist);
return ret;
}
Aggregations