Search in sources :

Example 41 with SimpleFeatureBuilder

use of org.geotools.feature.simple.SimpleFeatureBuilder in project GeoGig by boundlessgeo.

the class CreateOSMChangesetOp method _call.

/**
     * Executes the diff operation.
     * 
     * @return an iterator to a set of differences between the two trees
     * @see DiffEntry
     */
@Override
protected Iterator<ChangeContainer> _call() {
    Iterator<DiffEntry> nodeIterator = command(DiffOp.class).setFilter(OSMUtils.NODE_TYPE_NAME).setNewVersion(newRefSpec).setOldVersion(oldRefSpec).setReportTrees(false).call();
    Iterator<DiffEntry> wayIterator = command(DiffOp.class).setFilter(OSMUtils.WAY_TYPE_NAME).setNewVersion(newRefSpec).setOldVersion(oldRefSpec).setReportTrees(false).call();
    Iterator<DiffEntry> iterator = Iterators.concat(nodeIterator, wayIterator);
    final EntityConverter converter = new EntityConverter();
    Function<DiffEntry, ChangeContainer> function = new Function<DiffEntry, ChangeContainer>() {

        @Override
        @Nullable
        public ChangeContainer apply(@Nullable DiffEntry diff) {
            NodeRef ref = diff.changeType().equals(ChangeType.REMOVED) ? diff.getOldObject() : diff.getNewObject();
            RevFeature revFeature = command(RevObjectParse.class).setObjectId(ref.objectId()).call(RevFeature.class).get();
            RevFeatureType revFeatureType = command(RevObjectParse.class).setObjectId(ref.getMetadataId()).call(RevFeatureType.class).get();
            SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder((SimpleFeatureType) revFeatureType.type());
            ImmutableList<PropertyDescriptor> descriptors = revFeatureType.sortedDescriptors();
            ImmutableList<Optional<Object>> values = revFeature.getValues();
            for (int i = 0; i < descriptors.size(); i++) {
                PropertyDescriptor descriptor = descriptors.get(i);
                Optional<Object> value = values.get(i);
                featureBuilder.set(descriptor.getName(), value.orNull());
            }
            SimpleFeature feature = featureBuilder.buildFeature(ref.name());
            Entity entity = converter.toEntity(feature, id);
            EntityContainer container;
            if (entity instanceof Node) {
                container = new NodeContainer((Node) entity);
            } else {
                container = new WayContainer((Way) entity);
            }
            ChangeAction action = diff.changeType().equals(ChangeType.ADDED) ? ChangeAction.Create : diff.changeType().equals(ChangeType.MODIFIED) ? ChangeAction.Modify : ChangeAction.Delete;
            return new ChangeContainer(container, action);
        }
    };
    return Iterators.transform(iterator, function);
}
Also used : Entity(org.openstreetmap.osmosis.core.domain.v0_6.Entity) WayContainer(org.openstreetmap.osmosis.core.container.v0_6.WayContainer) ChangeAction(org.openstreetmap.osmosis.core.task.common.ChangeAction) Node(org.openstreetmap.osmosis.core.domain.v0_6.Node) EntityContainer(org.openstreetmap.osmosis.core.container.v0_6.EntityContainer) NodeContainer(org.openstreetmap.osmosis.core.container.v0_6.NodeContainer) Way(org.openstreetmap.osmosis.core.domain.v0_6.Way) Function(com.google.common.base.Function) NodeRef(org.locationtech.geogig.api.NodeRef) RevFeature(org.locationtech.geogig.api.RevFeature) RevFeatureType(org.locationtech.geogig.api.RevFeatureType) DiffEntry(org.locationtech.geogig.api.plumbing.diff.DiffEntry) ChangeContainer(org.openstreetmap.osmosis.core.container.v0_6.ChangeContainer) PropertyDescriptor(org.opengis.feature.type.PropertyDescriptor) Optional(com.google.common.base.Optional) DiffOp(org.locationtech.geogig.api.porcelain.DiffOp) SimpleFeature(org.opengis.feature.simple.SimpleFeature) RevObjectParse(org.locationtech.geogig.api.plumbing.RevObjectParse) Nullable(javax.annotation.Nullable) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder)

Example 42 with SimpleFeatureBuilder

use of org.geotools.feature.simple.SimpleFeatureBuilder in project GeoGig by boundlessgeo.

the class MappingRule method getFeatureType.

/**
     * Returns the feature type defined by this rule. This is the feature type that features
     * transformed by this rule will have
     * 
     * @return
     */
public SimpleFeatureType getFeatureType() {
    if (featureType == null) {
        SimpleFeatureTypeBuilder fb = new SimpleFeatureTypeBuilder();
        fb.setName(name);
        fb.add("id", Long.class);
        if (defaultFields != null) {
            for (DefaultField df : defaultFields) {
                fb.add(df.name().toLowerCase(), df.getFieldClass());
            }
        }
        Set<String> keys = this.fields.keySet();
        for (String key : keys) {
            AttributeDefinition field = fields.get(key);
            Class<?> clazz = field.getType().getBinding();
            if (Geometry.class.isAssignableFrom(clazz)) {
                Preconditions.checkArgument(geometryType == null, "The mapping has more than one geometry attribute");
                CoordinateReferenceSystem epsg4326;
                try {
                    epsg4326 = CRS.decode("EPSG:4326", true);
                    fb.add(field.getName(), clazz, epsg4326);
                } catch (NoSuchAuthorityCodeException e) {
                } catch (FactoryException e) {
                }
                geometryType = clazz;
            } else {
                fb.add(field.getName(), clazz);
            }
        }
        Preconditions.checkNotNull(geometryType, "The mapping rule does not define a geometry field");
        if (!geometryType.equals(Point.class)) {
            fb.add("nodes", String.class);
        }
        featureType = fb.buildFeatureType();
        featureBuilder = new SimpleFeatureBuilder(featureType);
    }
    return featureType;
}
Also used : NoSuchAuthorityCodeException(org.opengis.referencing.NoSuchAuthorityCodeException) SimpleFeatureTypeBuilder(org.geotools.feature.simple.SimpleFeatureTypeBuilder) FactoryException(org.opengis.referencing.FactoryException) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) Point(com.vividsolutions.jts.geom.Point) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder)

Example 43 with SimpleFeatureBuilder

use of org.geotools.feature.simple.SimpleFeatureBuilder in project GeoGig by boundlessgeo.

the class OSMHistoryImport method toFeature.

private static SimpleFeature toFeature(Primitive feature, Geometry geom) {
    SimpleFeatureType ft = feature instanceof Node ? nodeType() : wayType();
    SimpleFeatureBuilder builder = new SimpleFeatureBuilder(ft);
    // "visible:Boolean,version:Int,timestamp:long,[location:Point | way:LineString];
    builder.set("visible", Boolean.valueOf(feature.isVisible()));
    builder.set("version", Integer.valueOf(feature.getVersion()));
    builder.set("timestamp", Long.valueOf(feature.getTimestamp()));
    builder.set("changeset", Long.valueOf(feature.getChangesetId()));
    String tags = buildTagsString(feature.getTags());
    builder.set("tags", tags);
    String user = feature.getUserName() + ":" + feature.getUserId();
    builder.set("user", user);
    if (feature instanceof Node) {
        builder.set("location", geom);
    } else if (feature instanceof Way) {
        builder.set("way", geom);
        String nodes = buildNodesString(((Way) feature).getNodes());
        builder.set("nodes", nodes);
    } else {
        throw new IllegalArgumentException();
    }
    String fid = String.valueOf(feature.getId());
    SimpleFeature simpleFeature = builder.buildFeature(fid);
    return simpleFeature;
}
Also used : SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) Node(org.locationtech.geogig.osm.internal.history.Node) Way(org.locationtech.geogig.osm.internal.history.Way) SimpleFeature(org.opengis.feature.simple.SimpleFeature) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder)

Example 44 with SimpleFeatureBuilder

use of org.geotools.feature.simple.SimpleFeatureBuilder in project GeoGig by boundlessgeo.

the class EntityConverter method toFeature.

public SimpleFeature toFeature(Entity entity, Geometry geom) {
    SimpleFeatureType ft = entity instanceof Node ? OSMUtils.nodeType() : OSMUtils.wayType();
    SimpleFeatureBuilder builder = new SimpleFeatureBuilder(ft, FEATURE_FACTORY);
    // TODO: Check this!
    builder.set("visible", Boolean.TRUE);
    builder.set("version", Integer.valueOf(entity.getVersion()));
    builder.set("timestamp", Long.valueOf(entity.getTimestamp().getTime()));
    builder.set("changeset", Long.valueOf(entity.getChangesetId()));
    String tags = OSMUtils.buildTagsString(entity.getTags());
    builder.set("tags", tags);
    String user = entity.getUser().getName() + ":" + Integer.toString(entity.getUser().getId());
    builder.set("user", user);
    if (entity instanceof Node) {
        builder.set("location", geom);
    } else if (entity instanceof Way) {
        builder.set("way", geom);
        String nodes = buildNodesString(((Way) entity).getWayNodes());
        builder.set("nodes", nodes);
    } else {
        throw new IllegalArgumentException();
    }
    String fid = String.valueOf(entity.getId());
    SimpleFeature simpleFeature = builder.buildFeature(fid);
    return simpleFeature;
}
Also used : SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) WayNode(org.openstreetmap.osmosis.core.domain.v0_6.WayNode) Node(org.openstreetmap.osmosis.core.domain.v0_6.Node) Way(org.openstreetmap.osmosis.core.domain.v0_6.Way) SimpleFeature(org.opengis.feature.simple.SimpleFeature) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder)

Example 45 with SimpleFeatureBuilder

use of org.geotools.feature.simple.SimpleFeatureBuilder in project sldeditor by robward-scisys.

the class InLineFeatureModel method removeColumn.

/**
 * Removes the column.
 *
 * @param columnName the column name
 */
public void removeColumn(String columnName) {
    if (featureCollection != null) {
        if (columnList.contains(columnName)) {
            columnList.remove(columnName);
            // Find field name to remote
            SimpleFeatureTypeBuilder featureTypeBuilder = new SimpleFeatureTypeBuilder();
            featureTypeBuilder.init(featureCollection.getSchema());
            featureTypeBuilder.remove(columnName);
            SimpleFeatureType newFeatureType = featureTypeBuilder.buildFeatureType();
            int attributeToRemoveIndex = 0;
            for (AttributeDescriptor descriptor : newFeatureType.getAttributeDescriptors()) {
                if (descriptor.getLocalName().compareTo(columnName) == 0) {
                    break;
                }
                attributeToRemoveIndex++;
            }
            String typeName = userLayer.getInlineFeatureType().getTypeName();
            try {
                SimpleFeatureSource featureSource = userLayer.getInlineFeatureDatastore().getFeatureSource(typeName);
                SimpleFeatureBuilder sfb = new SimpleFeatureBuilder(newFeatureType);
                ArrayList<SimpleFeature> featureList = new ArrayList<SimpleFeature>();
                SimpleFeatureIterator it = featureSource.getFeatures().features();
                try {
                    while (it.hasNext()) {
                        SimpleFeature sf = it.next();
                        List<Object> attributes = sf.getAttributes();
                        attributes.remove(attributeToRemoveIndex);
                        sfb.addAll(attributes);
                        featureList.add(sfb.buildFeature(null));
                    }
                } finally {
                    it.close();
                }
                SimpleFeatureCollection collection = new ListFeatureCollection(newFeatureType, featureList);
                featureCollection = collection;
                cachedFeature = null;
                lastRow = -1;
                DataStore dataStore = DataUtilities.dataStore(collection);
                userLayer.setInlineFeatureDatastore(dataStore);
                userLayer.setInlineFeatureType(newFeatureType);
            } catch (IOException e) {
                ConsoleManager.getInstance().exception(this, e);
            }
            this.fireTableStructureChanged();
            this.fireTableDataChanged();
            if (parentObj != null) {
                parentObj.inlineFeatureUpdated();
            }
        }
    }
}
Also used : SimpleFeatureTypeBuilder(org.geotools.feature.simple.SimpleFeatureTypeBuilder) SimpleFeatureSource(org.geotools.data.simple.SimpleFeatureSource) ArrayList(java.util.ArrayList) AttributeDescriptor(org.opengis.feature.type.AttributeDescriptor) IOException(java.io.IOException) SimpleFeature(org.opengis.feature.simple.SimpleFeature) SimpleFeatureCollection(org.geotools.data.simple.SimpleFeatureCollection) SimpleFeatureIterator(org.geotools.data.simple.SimpleFeatureIterator) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) DataStore(org.geotools.data.DataStore) ListFeatureCollection(org.geotools.data.collection.ListFeatureCollection) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder)

Aggregations

SimpleFeatureBuilder (org.geotools.feature.simple.SimpleFeatureBuilder)56 SimpleFeature (org.opengis.feature.simple.SimpleFeature)34 SimpleFeatureType (org.opengis.feature.simple.SimpleFeatureType)25 Optional (com.google.common.base.Optional)17 ArrayList (java.util.ArrayList)15 RevFeature (org.locationtech.geogig.api.RevFeature)14 RevFeatureType (org.locationtech.geogig.api.RevFeatureType)13 Test (org.junit.Test)11 SimpleFeatureCollection (org.geotools.data.simple.SimpleFeatureCollection)10 SimpleFeatureIterator (org.geotools.data.simple.SimpleFeatureIterator)10 File (java.io.File)9 IOException (java.io.IOException)9 HashMap (java.util.HashMap)9 SimpleFeatureSource (org.geotools.data.simple.SimpleFeatureSource)9 Coordinate (com.vividsolutions.jts.geom.Coordinate)8 GeometryFactory (com.vividsolutions.jts.geom.GeometryFactory)8 Point (com.vividsolutions.jts.geom.Point)8 SimpleFeatureStore (org.geotools.data.simple.SimpleFeatureStore)8 Geometry (com.vividsolutions.jts.geom.Geometry)7 List (java.util.List)7