Search in sources :

Example 6 with NameImpl

use of org.geotools.feature.NameImpl in project GeoGig by boundlessgeo.

the class SLExport method runInternal.

/**
     * Executes the export command using the provided options.
     */
@Override
protected void runInternal(GeogigCLI cli) throws IOException {
    if (args.isEmpty()) {
        printUsage(cli);
        throw new CommandFailedException();
    }
    String path = args.get(0);
    String tableName = args.get(1);
    checkParameter(tableName != null && !tableName.isEmpty(), "No table name specified");
    DataStore dataStore = getDataStore();
    ObjectId featureTypeId = null;
    if (!Arrays.asList(dataStore.getTypeNames()).contains(tableName)) {
        SimpleFeatureType outputFeatureType;
        if (sFeatureTypeId != null) {
            // Check the feature type id string is a correct id
            Optional<ObjectId> id = cli.getGeogig().command(RevParse.class).setRefSpec(sFeatureTypeId).call();
            checkParameter(id.isPresent(), "Invalid feature type reference", sFeatureTypeId);
            TYPE type = cli.getGeogig().command(ResolveObjectType.class).setObjectId(id.get()).call();
            checkParameter(type.equals(TYPE.FEATURETYPE), "Provided reference does not resolve to a feature type: ", sFeatureTypeId);
            outputFeatureType = (SimpleFeatureType) cli.getGeogig().command(RevObjectParse.class).setObjectId(id.get()).call(RevFeatureType.class).get().type();
            featureTypeId = id.get();
        } else {
            try {
                SimpleFeatureType sft = getFeatureType(path, cli);
                outputFeatureType = new SimpleFeatureTypeImpl(new NameImpl(tableName), sft.getAttributeDescriptors(), sft.getGeometryDescriptor(), sft.isAbstract(), sft.getRestrictions(), sft.getSuper(), sft.getDescription());
            } catch (GeoToolsOpException e) {
                throw new CommandFailedException("No features to export.", e);
            }
        }
        try {
            dataStore.createSchema(outputFeatureType);
        } catch (IOException e) {
            throw new CommandFailedException("Cannot create new table in database", e);
        }
    } else {
        if (!overwrite) {
            throw new CommandFailedException("The selected table already exists. Use -o to overwrite");
        }
    }
    SimpleFeatureSource featureSource = dataStore.getFeatureSource(tableName);
    if (!(featureSource instanceof SimpleFeatureStore)) {
        throw new CommandFailedException("Can't write to the selected table");
    }
    SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
    if (overwrite) {
        try {
            featureStore.removeFeatures(Filter.INCLUDE);
        } catch (IOException e) {
            throw new CommandFailedException("Error truncating table: " + e.getMessage(), e);
        }
    }
    ExportOp op = cli.getGeogig().command(ExportOp.class).setFeatureStore(featureStore).setPath(path).setFilterFeatureTypeId(featureTypeId).setAlter(alter);
    if (defaultType) {
        op.exportDefaultFeatureType();
    }
    try {
        op.setProgressListener(cli.getProgressListener()).call();
    } catch (IllegalArgumentException iae) {
        throw new org.locationtech.geogig.cli.InvalidParameterException(iae.getMessage(), iae);
    } catch (GeoToolsOpException e) {
        switch(e.statusCode) {
            case MIXED_FEATURE_TYPES:
                throw new CommandFailedException("The selected tree contains mixed feature types. Use --defaulttype or --featuretype <feature_type_ref> to export.", e);
            default:
                throw new CommandFailedException("Could not export. Error:" + e.statusCode.name(), e);
        }
    }
    cli.getConsole().println(path + " exported successfully to " + tableName);
}
Also used : NameImpl(org.geotools.feature.NameImpl) ObjectId(org.locationtech.geogig.api.ObjectId) SimpleFeatureSource(org.geotools.data.simple.SimpleFeatureSource) IOException(java.io.IOException) InvalidParameterException(org.locationtech.geogig.cli.InvalidParameterException) CommandFailedException(org.locationtech.geogig.cli.CommandFailedException) SimpleFeatureTypeImpl(org.geotools.feature.simple.SimpleFeatureTypeImpl) GeoToolsOpException(org.locationtech.geogig.geotools.plumbing.GeoToolsOpException) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) DataStore(org.geotools.data.DataStore) SimpleFeatureStore(org.geotools.data.simple.SimpleFeatureStore) ExportOp(org.locationtech.geogig.geotools.plumbing.ExportOp) TYPE(org.locationtech.geogig.api.RevObject.TYPE)

Example 7 with NameImpl

use of org.geotools.feature.NameImpl in project GeoGig by boundlessgeo.

the class FormatCommonV2 method readName.

private static Name readName(DataInput in) throws IOException {
    String namespace = in.readUTF();
    String localPart = in.readUTF();
    return new NameImpl(namespace.length() == 0 ? null : namespace, localPart.length() == 0 ? null : localPart);
}
Also used : NameImpl(org.geotools.feature.NameImpl) Integer.toBinaryString(java.lang.Integer.toBinaryString)

Example 8 with NameImpl

use of org.geotools.feature.NameImpl in project GeoGig by boundlessgeo.

the class ImportOp method overrideGeometryName.

private SimpleFeatureType overrideGeometryName(SimpleFeatureType featureType) {
    if (geomName == null) {
        return featureType;
    }
    SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
    List<AttributeDescriptor> newAttributes = Lists.newArrayList();
    String oldGeomName = featureType.getGeometryDescriptor().getName().getLocalPart();
    Collection<AttributeDescriptor> descriptors = featureType.getAttributeDescriptors();
    for (AttributeDescriptor descriptor : descriptors) {
        String name = descriptor.getName().getLocalPart();
        Preconditions.checkArgument(!name.equals(geomName), "The provided geom name is already in use by another attribute");
        if (name.equals(oldGeomName)) {
            AttributeDescriptorImpl newDescriptor = new AttributeDescriptorImpl(descriptor.getType(), new NameImpl(geomName), descriptor.getMinOccurs(), descriptor.getMaxOccurs(), descriptor.isNillable(), descriptor.getDefaultValue());
            newAttributes.add(newDescriptor);
        } else {
            newAttributes.add(descriptor);
        }
    }
    builder.setAttributes(newAttributes);
    builder.setName(featureType.getName());
    builder.setCRS(featureType.getCoordinateReferenceSystem());
    featureType = builder.buildFeatureType();
    return featureType;
}
Also used : NameImpl(org.geotools.feature.NameImpl) SimpleFeatureTypeBuilder(org.geotools.feature.simple.SimpleFeatureTypeBuilder) AttributeDescriptor(org.opengis.feature.type.AttributeDescriptor) AttributeDescriptorImpl(org.geotools.feature.type.AttributeDescriptorImpl)

Example 9 with NameImpl

use of org.geotools.feature.NameImpl in project ddf by codice.

the class CswQueryFactory method buildSort.

private SortBy buildSort(SortByType sort) throws CswException {
    if (sort == null || sort.getSortProperty() == null) {
        return null;
    }
    SortBy[] sortByArr = parseSortBy(sort);
    if (sortByArr.length > 1) {
        LOGGER.debug("Query request has multiple sort criteria, only primary will be used");
    }
    SortBy sortBy = sortByArr[0];
    if (sortBy.getPropertyName() == null) {
        LOGGER.debug("No property name in primary sort criteria");
        return null;
    }
    if (!attributeRegistry.lookup(sortBy.getPropertyName().getPropertyName()).isPresent() && !DefaultCswRecordMap.hasDefaultMetacardFieldForPrefixedString(sortBy.getPropertyName().getPropertyName(), sortBy.getPropertyName().getNamespaceContext())) {
        throw new CswException("Property " + sortBy.getPropertyName().getPropertyName() + " is not a valid SortBy Field", CswConstants.INVALID_PARAMETER_VALUE, "SortProperty");
    }
    String name = DefaultCswRecordMap.getDefaultMetacardFieldForPrefixedString(sortBy.getPropertyName().getPropertyName(), sortBy.getPropertyName().getNamespaceContext());
    PropertyName propName = new AttributeExpressionImpl(new NameImpl(name));
    return new SortByImpl(propName, sortBy.getSortOrder());
}
Also used : PropertyName(org.opengis.filter.expression.PropertyName) NameImpl(org.geotools.feature.NameImpl) AttributeExpressionImpl(org.geotools.filter.AttributeExpressionImpl) SortByImpl(ddf.catalog.filter.impl.SortByImpl) SortBy(org.opengis.filter.sort.SortBy) CswException(org.codice.ddf.spatial.ogc.csw.catalog.common.CswException)

Example 10 with NameImpl

use of org.geotools.feature.NameImpl in project ddf by codice.

the class TestCswRecordMapperFilterVisitor method testVisitWithBoundingBoxProperty.

@Test
public void testVisitWithBoundingBoxProperty() {
    AttributeExpressionImpl propName = new AttributeExpressionImpl(new NameImpl(new QName(CswConstants.DUBLIN_CORE_SCHEMA, CswConstants.OWS_BOUNDING_BOX, CswConstants.DUBLIN_CORE_NAMESPACE_PREFIX)));
    CswRecordMapperFilterVisitor visitor = new CswRecordMapperFilterVisitor(metacardType, mockMetacardTypeList);
    PropertyName propertyName = (PropertyName) visitor.visit(propName, null);
    assertThat(propertyName.getPropertyName(), is(Metacard.ANY_GEO));
}
Also used : NameImpl(org.geotools.feature.NameImpl) PropertyName(org.opengis.filter.expression.PropertyName) AttributeExpressionImpl(org.geotools.filter.AttributeExpressionImpl) QName(javax.xml.namespace.QName) CswQueryFactoryTest(org.codice.ddf.spatial.ogc.csw.catalog.endpoint.CswQueryFactoryTest) Test(org.junit.Test)

Aggregations

NameImpl (org.geotools.feature.NameImpl)13 AttributeExpressionImpl (org.geotools.filter.AttributeExpressionImpl)5 SimpleFeatureType (org.opengis.feature.simple.SimpleFeatureType)5 IOException (java.io.IOException)4 QName (javax.xml.namespace.QName)4 DataStore (org.geotools.data.DataStore)4 SimpleFeatureSource (org.geotools.data.simple.SimpleFeatureSource)4 SimpleFeatureStore (org.geotools.data.simple.SimpleFeatureStore)4 SimpleFeatureTypeImpl (org.geotools.feature.simple.SimpleFeatureTypeImpl)4 Test (org.junit.Test)4 ObjectId (org.locationtech.geogig.api.ObjectId)4 TYPE (org.locationtech.geogig.api.RevObject.TYPE)4 CommandFailedException (org.locationtech.geogig.cli.CommandFailedException)4 InvalidParameterException (org.locationtech.geogig.cli.InvalidParameterException)4 ExportOp (org.locationtech.geogig.geotools.plumbing.ExportOp)4 GeoToolsOpException (org.locationtech.geogig.geotools.plumbing.GeoToolsOpException)4 CswQueryFactoryTest (org.codice.ddf.spatial.ogc.csw.catalog.endpoint.CswQueryFactoryTest)3 PropertyName (org.opengis.filter.expression.PropertyName)3 SimpleFeatureTypeBuilder (org.geotools.feature.simple.SimpleFeatureTypeBuilder)2 LineString (com.vividsolutions.jts.geom.LineString)1