use of org.geotools.feature.simple.SimpleFeatureTypeBuilder in project spatial-portal by AtlasOfLivingAustralia.
the class ShapefileUtils method createFeatureType.
private static SimpleFeatureType createFeatureType() {
SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
builder.setName("ActiveArea");
builder.setCRS(DefaultGeographicCRS.WGS84);
builder.add("the_geom", MultiPolygon.class);
builder.add("name", String.class);
// build the type
return builder.buildFeatureType();
}
use of org.geotools.feature.simple.SimpleFeatureTypeBuilder in project GeoGig by boundlessgeo.
the class RevFeatureTypeSerializationTest method testSerializationWGS84.
@Test
public void testSerializationWGS84() throws Exception {
SimpleFeatureTypeBuilder ftb = new SimpleFeatureTypeBuilder();
ftb.add("geom", Polygon.class, DefaultGeographicCRS.WGS84);
ftb.setName("type");
SimpleFeatureType ftype = ftb.buildFeatureType();
RevFeatureType revFeatureType = RevFeatureTypeImpl.build(ftype);
ObjectWriter<RevFeatureType> writer = factory.createObjectWriter(TYPE.FEATURETYPE);
ByteArrayOutputStream output = new ByteArrayOutputStream();
writer.write(revFeatureType, output);
byte[] data = output.toByteArray();
assertTrue(data.length > 0);
ObjectReader<RevFeatureType> reader = factory.createObjectReader(TYPE.FEATURETYPE);
ByteArrayInputStream input = new ByteArrayInputStream(data);
RevFeatureType rft = reader.read(revFeatureType.getId(), input);
assertNotNull(rft);
FeatureType serializedFeatureType = rft.type();
assertEquals("EPSG:4326", CRS.toSRS(serializedFeatureType.getCoordinateReferenceSystem()));
}
use of org.geotools.feature.simple.SimpleFeatureTypeBuilder in project GeoGig by boundlessgeo.
the class TestHelper method createFactoryWithGetFeatureSourceException.
public static AbstractDataStoreFactory createFactoryWithGetFeatureSourceException() throws Exception {
SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
builder.setCRS(CRS.decode("EPSG:4326"));
builder.add("geom", Point.class);
builder.add("label", String.class);
builder.setName("table1");
SimpleFeatureType type = builder.buildFeatureType();
SimpleFeatureTypeBuilder builder2 = new SimpleFeatureTypeBuilder();
builder2.setCRS(CRS.decode("EPSG:4326"));
builder2.add("geom", Point.class);
builder2.add("name", String.class);
builder2.setName("table2");
SimpleFeatureType type2 = builder2.buildFeatureType();
GeometryFactory gf = new GeometryFactory();
SimpleFeature f1 = SimpleFeatureBuilder.build(type, new Object[] { gf.createPoint(new Coordinate(5, 8)), "feature1" }, null);
SimpleFeature f2 = SimpleFeatureBuilder.build(type, new Object[] { gf.createPoint(new Coordinate(5, 4)), "feature2" }, null);
SimpleFeature f3 = SimpleFeatureBuilder.build(type2, new Object[] { gf.createPoint(new Coordinate(3, 2)), "feature3" }, null);
MemoryDataStore testDataStore = new MemoryDataStore();
testDataStore.addFeature(f1);
testDataStore.addFeature(f2);
testDataStore.addFeature(f3);
MemoryDataStore spyDataStore = spy(testDataStore);
when(spyDataStore.getFeatureSource("table1")).thenThrow(new IOException("Exception"));
final AbstractDataStoreFactory factory = mock(AbstractDataStoreFactory.class);
when(factory.createDataStore(anyMapOf(String.class, Serializable.class))).thenReturn(spyDataStore);
when(factory.canProcess(anyMapOf(String.class, Serializable.class))).thenReturn(true);
return factory;
}
use of org.geotools.feature.simple.SimpleFeatureTypeBuilder in project GeoGig by boundlessgeo.
the class ApplyPatchOp method getFeatureType.
private RevFeatureType getFeatureType(FeatureDiff diff, RevFeature oldFeature, RevFeatureType oldRevFeatureType) {
List<String> removed = Lists.newArrayList();
List<AttributeDescriptor> added = Lists.newArrayList();
Set<Entry<PropertyDescriptor, AttributeDiff>> featureDiffs = diff.getDiffs().entrySet();
for (Iterator<Entry<PropertyDescriptor, AttributeDiff>> iterator = featureDiffs.iterator(); iterator.hasNext(); ) {
Entry<PropertyDescriptor, AttributeDiff> entry = iterator.next();
if (entry.getValue().getType() == TYPE.REMOVED) {
removed.add(entry.getKey().getName().getLocalPart());
} else if (entry.getValue().getType() == TYPE.ADDED) {
PropertyDescriptor pd = entry.getKey();
added.add((AttributeDescriptor) pd);
}
}
SimpleFeatureType sft = (SimpleFeatureType) oldRevFeatureType.type();
List<AttributeDescriptor> descriptors = (sft).getAttributeDescriptors();
SimpleFeatureTypeBuilder featureTypeBuilder = new SimpleFeatureTypeBuilder();
featureTypeBuilder.setCRS(sft.getCoordinateReferenceSystem());
featureTypeBuilder.setDefaultGeometry(sft.getGeometryDescriptor().getLocalName());
featureTypeBuilder.setName(sft.getName());
for (int i = 0; i < descriptors.size(); i++) {
AttributeDescriptor descriptor = descriptors.get(i);
if (!removed.contains(descriptor.getName().getLocalPart())) {
featureTypeBuilder.add(descriptor);
}
}
for (AttributeDescriptor descriptor : added) {
featureTypeBuilder.add(descriptor);
}
SimpleFeatureType featureType = featureTypeBuilder.buildFeatureType();
return RevFeatureTypeImpl.build(featureType);
}
use of org.geotools.feature.simple.SimpleFeatureTypeBuilder in project GeoGig by boundlessgeo.
the class ShpExportDiff method runInternal.
/**
* Executes the export command using the provided options.
*/
@Override
protected void runInternal(GeogigCLI cli) throws IOException {
if (args.size() != 4) {
printUsage(cli);
throw new CommandFailedException();
}
String commitOld = args.get(0);
String commitNew = args.get(1);
String path = args.get(2);
String shapefile = args.get(3);
ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
File file = new File(shapefile);
if (file.exists() && !overwrite) {
throw new CommandFailedException("The selected shapefile already exists. Use -o to overwrite");
}
Map<String, Serializable> params = new HashMap<String, Serializable>();
params.put(ShapefileDataStoreFactory.URLP.key, file.toURI().toURL());
params.put(ShapefileDataStoreFactory.CREATE_SPATIAL_INDEX.key, Boolean.FALSE);
params.put(ShapefileDataStoreFactory.ENABLE_SPATIAL_INDEX.key, Boolean.FALSE);
ShapefileDataStore dataStore = (ShapefileDataStore) dataStoreFactory.createNewDataStore(params);
SimpleFeatureType outputFeatureType;
try {
outputFeatureType = getFeatureType(path, cli);
} catch (GeoToolsOpException e) {
cli.getConsole().println("No features to export.");
return;
}
SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
builder.add("geogig_fid", String.class);
for (AttributeDescriptor descriptor : outputFeatureType.getAttributeDescriptors()) {
builder.add(descriptor);
}
builder.setName(outputFeatureType.getName());
builder.setCRS(outputFeatureType.getCoordinateReferenceSystem());
outputFeatureType = builder.buildFeatureType();
dataStore.createSchema(outputFeatureType);
final String typeName = dataStore.getTypeNames()[0];
final SimpleFeatureSource featureSource = dataStore.getFeatureSource(typeName);
if (!(featureSource instanceof SimpleFeatureStore)) {
throw new CommandFailedException("Could not create feature store.");
}
final SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
Function<Feature, Optional<Feature>> function = getTransformingFunction(dataStore.getSchema());
ExportDiffOp op = cli.getGeogig().command(ExportDiffOp.class).setFeatureStore(featureStore).setPath(path).setOldRef(commitOld).setNewRef(commitNew).setUseOld(old).setTransactional(false).setFeatureTypeConversionFunction(function);
try {
op.setProgressListener(cli.getProgressListener()).call();
} catch (IllegalArgumentException iae) {
throw new org.locationtech.geogig.cli.InvalidParameterException(iae.getMessage(), iae);
} catch (GeoToolsOpException e) {
file.delete();
switch(e.statusCode) {
case MIXED_FEATURE_TYPES:
throw new CommandFailedException("Error: The selected tree contains mixed feature types.", e);
default:
throw new CommandFailedException("Could not export. Error:" + e.statusCode.name(), e);
}
}
cli.getConsole().println(path + " exported successfully to " + shapefile);
}
Aggregations