Search in sources :

Example 1 with SimpleFeatureTypeBuilder

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();
}
Also used : SimpleFeatureTypeBuilder(org.geotools.feature.simple.SimpleFeatureTypeBuilder)

Example 2 with SimpleFeatureTypeBuilder

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()));
}
Also used : RevFeatureType(org.locationtech.geogig.api.RevFeatureType) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) FeatureType(org.opengis.feature.type.FeatureType) SimpleFeatureTypeBuilder(org.geotools.feature.simple.SimpleFeatureTypeBuilder) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) Test(org.junit.Test)

Example 3 with SimpleFeatureTypeBuilder

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;
}
Also used : AbstractDataStoreFactory(org.geotools.data.AbstractDataStoreFactory) Serializable(java.io.Serializable) SimpleFeatureTypeBuilder(org.geotools.feature.simple.SimpleFeatureTypeBuilder) GeometryFactory(com.vividsolutions.jts.geom.GeometryFactory) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) Coordinate(com.vividsolutions.jts.geom.Coordinate) MemoryDataStore(org.geotools.data.memory.MemoryDataStore) IOException(java.io.IOException) Matchers.anyString(org.mockito.Matchers.anyString) SimpleFeature(org.opengis.feature.simple.SimpleFeature)

Example 4 with SimpleFeatureTypeBuilder

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);
}
Also used : SimpleFeatureTypeBuilder(org.geotools.feature.simple.SimpleFeatureTypeBuilder) PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) AttributeDescriptor(org.opengis.feature.type.AttributeDescriptor) Entry(java.util.Map.Entry) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) AttributeDiff(org.locationtech.geogig.api.plumbing.diff.AttributeDiff)

Example 5 with SimpleFeatureTypeBuilder

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);
}
Also used : Serializable(java.io.Serializable) ShapefileDataStore(org.geotools.data.shapefile.ShapefileDataStore) SimpleFeatureTypeBuilder(org.geotools.feature.simple.SimpleFeatureTypeBuilder) Optional(com.google.common.base.Optional) HashMap(java.util.HashMap) SimpleFeatureSource(org.geotools.data.simple.SimpleFeatureSource) ExportDiffOp(org.locationtech.geogig.geotools.plumbing.ExportDiffOp) AttributeDescriptor(org.opengis.feature.type.AttributeDescriptor) InvalidParameterException(org.locationtech.geogig.cli.InvalidParameterException) Feature(org.opengis.feature.Feature) CommandFailedException(org.locationtech.geogig.cli.CommandFailedException) GeoToolsOpException(org.locationtech.geogig.geotools.plumbing.GeoToolsOpException) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) SimpleFeatureStore(org.geotools.data.simple.SimpleFeatureStore) ShapefileDataStoreFactory(org.geotools.data.shapefile.ShapefileDataStoreFactory) File(java.io.File)

Aggregations

SimpleFeatureTypeBuilder (org.geotools.feature.simple.SimpleFeatureTypeBuilder)17 SimpleFeatureType (org.opengis.feature.simple.SimpleFeatureType)12 SimpleFeature (org.opengis.feature.simple.SimpleFeature)7 AttributeDescriptor (org.opengis.feature.type.AttributeDescriptor)6 Test (org.junit.Test)5 Coordinate (com.vividsolutions.jts.geom.Coordinate)4 MemoryDataStore (org.geotools.data.memory.MemoryDataStore)4 GeometryFactory (com.vividsolutions.jts.geom.GeometryFactory)3 Serializable (java.io.Serializable)3 SimpleFeatureSource (org.geotools.data.simple.SimpleFeatureSource)3 SimpleFeatureStore (org.geotools.data.simple.SimpleFeatureStore)3 SimpleFeatureBuilder (org.geotools.feature.simple.SimpleFeatureBuilder)3 Feature (org.opengis.feature.Feature)3 Point (com.vividsolutions.jts.geom.Point)2 AbstractDataStoreFactory (org.geotools.data.AbstractDataStoreFactory)2 SimpleFeatureCollection (org.geotools.data.simple.SimpleFeatureCollection)2 SimpleFeatureIterator (org.geotools.data.simple.SimpleFeatureIterator)2 NameImpl (org.geotools.feature.NameImpl)2 RevCommit (org.locationtech.geogig.api.RevCommit)2 RevFeatureType (org.locationtech.geogig.api.RevFeatureType)2