Search in sources :

Example 41 with SimpleFeatureType

use of org.opengis.feature.simple.SimpleFeatureType in project sldeditor by robward-scisys.

the class CreateInternalDataSource method connect.

/**
 * Creates the.
 *
 * @param typeName the type name
 * @param geometryFieldName the geometry field name
 * @param editorFile the editor file
 * @return the list of datastores
 */
@Override
public List<DataSourceInfo> connect(String typeName, String geometryFieldName, SLDEditorFileInterface editorFile) {
    List<DataSourceInfo> dataSourceInfoList = new ArrayList<DataSourceInfo>();
    dataSourceInfoList.add(dsInfo);
    dsInfo.reset();
    if (editorFile != null) {
        StyledLayerDescriptor sld = editorFile.getSLD();
        determineGeometryType(sld);
        ExtendedSimpleFeatureTypeBuilder b = new ExtendedSimpleFeatureTypeBuilder();
        // set the name
        typeName = INTERNAL_SCHEMA_NAME;
        dsInfo.setTypeName(typeName);
        b.setName(typeName);
        String namespace = null;
        b.setNamespaceURI(namespace);
        // add a geometry property
        // set crs first
        b.setCRS(DefaultGeographicCRS.WGS84);
        SLDDataInterface sldData = editorFile.getSLDData();
        List<DataSourceAttributeData> fieldList = sldData.getFieldList();
        // Set the geometry field by default
        geometryField.reset();
        if (geometryFieldName != null) {
            geometryField.setGeometryFieldName(geometryFieldName);
        }
        List<AttributeDescriptor> attrDescList = new ArrayList<AttributeDescriptor>();
        if ((fieldList == null) || fieldList.isEmpty()) {
            // Read the fields from the SLD
            ExtractAttributes extract = new ExtractAttributes();
            extract.extractDefaultFields(sld);
            fieldList = extract.getFields();
            // Add non-geometry fields to the feature type builder
            for (DataSourceAttributeData dsAttribute : fieldList) {
                if (dsAttribute.getName() != null) {
                    b.add(dsAttribute.getName(), dsAttribute.getType());
                }
            }
            List<String> geometryFields = extract.getGeometryFields();
            for (String localGeometryFieldName : geometryFields) {
                geometryField.setGeometryFieldName(localGeometryFieldName);
            }
        } else {
            addFields(attrDescList, b, fieldList);
        }
        attrDescList.add(addGeometryField(b, geometryField.getGeometryFieldName()));
        b.addAll(attrDescList);
        // Store the fields
        sldData.setFieldList(fieldList);
        // Build the feature type
        SimpleFeatureType schema = b.buildFeatureType();
        dsInfo.setSchema(schema);
        CreateSampleData sampleData = new CreateSampleData();
        sampleData.create(schema, fieldList);
        MemoryDataStore dataStore = sampleData.getDataStore();
        dsInfo.setDataStore(dataStore);
        dsInfo.populateFieldMap();
    }
    return dataSourceInfoList;
}
Also used : DataSourceAttributeData(com.sldeditor.datasource.attribute.DataSourceAttributeData) ArrayList(java.util.ArrayList) MemoryDataStore(org.geotools.data.memory.MemoryDataStore) AttributeDescriptor(org.opengis.feature.type.AttributeDescriptor) LineString(com.vividsolutions.jts.geom.LineString) StyledLayerDescriptor(org.geotools.styling.StyledLayerDescriptor) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) SLDDataInterface(com.sldeditor.common.SLDDataInterface)

Example 42 with SimpleFeatureType

use of org.opengis.feature.simple.SimpleFeatureType in project sldeditor by robward-scisys.

the class CreateSampleData method create.

/**
 * Creates the sample data from the supplied schema.
 *
 * @param schema the schema
 * @param fieldList the field list
 */
public void create(FeatureType schema, List<DataSourceAttributeData> fieldList) {
    if (schema == null) {
        return;
    }
    // Put fields into map for speed
    Map<String, DataSourceAttributeData> fieldMap = new HashMap<String, DataSourceAttributeData>();
    if (fieldList != null) {
        for (DataSourceAttributeData attributeData : fieldList) {
            fieldMap.put(attributeData.getName(), attributeData);
        }
    }
    SimpleFeatureType featureType = (SimpleFeatureType) schema;
    memory = new MemoryDataStore();
    try {
        memory.createSchema(featureType);
    } catch (IOException e) {
        ConsoleManager.getInstance().exception(this, e);
        memory = null;
        return;
    }
    SimpleFeatureBuilder builder = new SimpleFeatureBuilder(featureType);
    SimpleFeature feature = DataUtilities.template(featureType);
    builder.init((SimpleFeature) feature);
    int index = 0;
    for (AttributeDescriptor descriptor : featureType.getAttributeDescriptors()) {
        AttributeType attributeType = descriptor.getType();
        Object value = null;
        Class<?> fieldType = attributeType.getBinding();
        if (attributeType instanceof GeometryTypeImpl) {
            geometryType = GeometryTypeMapping.getGeometryType(fieldType);
            switch(geometryType) {
                case POLYGON:
                    ExamplePolygonInterface examplePolygon = DataSourceFactory.createExamplePolygon(null);
                    value = examplePolygon.getPolygon();
                    break;
                case LINE:
                    ExampleLineInterface exampleLine = DataSourceFactory.createExampleLine(null);
                    value = exampleLine.getLine();
                    break;
                case POINT:
                default:
                    ExamplePointInterface examplePoint = DataSourceFactory.createExamplePoint(null);
                    value = examplePoint.getPoint();
                    break;
            }
        } else {
            if ((fieldList != null) && (index < fieldList.size())) {
                DataSourceAttributeData attrData = fieldMap.get(descriptor.getLocalName());
                if (attrData != null) {
                    value = attrData.getValue();
                }
            }
            value = getFieldTypeValue(index, attributeType.getName().getLocalPart(), fieldType, value);
        }
        builder.add(value);
        index++;
    }
    SimpleFeature newFeature = builder.buildFeature("1234");
    memory.addFeature(newFeature);
}
Also used : DataSourceAttributeData(com.sldeditor.datasource.attribute.DataSourceAttributeData) HashMap(java.util.HashMap) MemoryDataStore(org.geotools.data.memory.MemoryDataStore) AttributeDescriptor(org.opengis.feature.type.AttributeDescriptor) IOException(java.io.IOException) SimpleFeature(org.opengis.feature.simple.SimpleFeature) ExamplePointInterface(com.sldeditor.datasource.example.ExamplePointInterface) GeometryTypeImpl(org.geotools.feature.type.GeometryTypeImpl) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) AttributeType(org.opengis.feature.type.AttributeType) ExampleLineInterface(com.sldeditor.datasource.example.ExampleLineInterface) SimpleFeatureBuilder(org.geotools.feature.simple.SimpleFeatureBuilder) ExamplePolygonInterface(com.sldeditor.datasource.example.ExamplePolygonInterface)

Example 43 with SimpleFeatureType

use of org.opengis.feature.simple.SimpleFeatureType in project sldeditor by robward-scisys.

the class DataSourceImpl method getUserLayerFeatureSource.

/*
     * (non-Javadoc)
     * 
     * @see com.sldeditor.datasource.DataSourceInterface#getUserLayerFeatureSource()
     */
@Override
public // CHECKSTYLE:OFF
Map<UserLayer, FeatureSource<SimpleFeatureType, SimpleFeature>> getUserLayerFeatureSource() {
    // CHECKSTYLE:ON
    Map<UserLayer, FeatureSource<SimpleFeatureType, SimpleFeature>> map = new HashMap<UserLayer, FeatureSource<SimpleFeatureType, SimpleFeature>>();
    for (DataSourceInfo dsInfo : userLayerDataSourceInfo) {
        FeatureSource<SimpleFeatureType, SimpleFeature> features = dsInfo.getFeatures();
        UserLayer userLayer = dsInfo.getUserLayer();
        map.put(userLayer, features);
    }
    return map;
}
Also used : FeatureSource(org.geotools.data.FeatureSource) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) HashMap(java.util.HashMap) UserLayer(org.geotools.styling.UserLayer) SimpleFeature(org.opengis.feature.simple.SimpleFeature)

Example 44 with SimpleFeatureType

use of org.opengis.feature.simple.SimpleFeatureType in project sldeditor by robward-scisys.

the class ExtractValidFieldTypes method fieldTypesUpdated.

/**
 * Evaluate fields types.
 *
 * @return true, field types updated
 */
public static boolean fieldTypesUpdated() {
    boolean fieldsUpdated = false;
    SLDStyleFactory styleFactory = new SLDStyleFactory();
    StyledLayerDescriptor sld = SelectedSymbol.getInstance().getSld();
    if (sld != null) {
        List<StyledLayer> styledLayerList = sld.layers();
        for (StyledLayer styledLayer : styledLayerList) {
            List<org.geotools.styling.Style> styleList = null;
            if (styledLayer instanceof NamedLayerImpl) {
                NamedLayerImpl namedLayerImpl = (NamedLayerImpl) styledLayer;
                styleList = namedLayerImpl.styles();
            } else if (styledLayer instanceof UserLayerImpl) {
                UserLayerImpl userLayerImpl = (UserLayerImpl) styledLayer;
                styleList = userLayerImpl.userStyles();
            }
            if (styleList != null) {
                for (Style style : styleList) {
                    for (FeatureTypeStyle fts : style.featureTypeStyles()) {
                        for (Rule rule : fts.rules()) {
                            for (Symbolizer symbolizer : rule.symbolizers()) {
                                FeatureSource<SimpleFeatureType, SimpleFeature> featureList = DataSourceFactory.getDataSource().getFeatureSource();
                                if (featureList != null) {
                                    Object drawMe = null;
                                    try {
                                        drawMe = featureList.getFeatures().features().next();
                                    } catch (NoSuchElementException e) {
                                        e.printStackTrace();
                                    } catch (IOException e) {
                                        e.printStackTrace();
                                    }
                                    try {
                                        styleFactory.createStyle(drawMe, symbolizer);
                                    } catch (IllegalArgumentException e) {
                                        String message = e.getMessage();
                                        if (message.startsWith(UNABLE_TO_DECODE_PREFIX) && message.endsWith(UNABLE_TO_DECODE_SUFFIX)) {
                                            String fieldName = message.substring(UNABLE_TO_DECODE_PREFIX.length(), message.length() - UNABLE_TO_DECODE_SUFFIX.length());
                                            DataSourceFactory.getDataSource().updateFieldType(fieldName, Long.class);
                                            fieldsUpdated = true;
                                        } else {
                                            ConsoleManager.getInstance().exception(ExtractValidFieldTypes.class, e);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return fieldsUpdated;
}
Also used : StyledLayer(org.geotools.styling.StyledLayer) NamedLayerImpl(org.geotools.styling.NamedLayerImpl) IOException(java.io.IOException) Symbolizer(org.geotools.styling.Symbolizer) SimpleFeature(org.opengis.feature.simple.SimpleFeature) StyledLayerDescriptor(org.geotools.styling.StyledLayerDescriptor) UserLayerImpl(org.geotools.styling.UserLayerImpl) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) SLDStyleFactory(org.geotools.renderer.style.SLDStyleFactory) Style(org.geotools.styling.Style) FeatureTypeStyle(org.geotools.styling.FeatureTypeStyle) FeatureTypeStyle(org.geotools.styling.FeatureTypeStyle) Rule(org.geotools.styling.Rule) NoSuchElementException(java.util.NoSuchElementException)

Example 45 with SimpleFeatureType

use of org.opengis.feature.simple.SimpleFeatureType in project sldeditor by robward-scisys.

the class InLineFeatureModel method removeFeature.

/**
 * Removes the feature.
 *
 * @param selectedRow the selected row
 */
public void removeFeature(int selectedRow) {
    if ((selectedRow < 0) || (selectedRow >= getRowCount())) {
        return;
    }
    SimpleFeatureType featureType = userLayer.getInlineFeatureType();
    String typeName = userLayer.getInlineFeatureType().getTypeName();
    try {
        SimpleFeatureSource featureSource = userLayer.getInlineFeatureDatastore().getFeatureSource(typeName);
        SimpleFeatureBuilder sfb = new SimpleFeatureBuilder(featureType);
        ArrayList<SimpleFeature> featureList = new ArrayList<SimpleFeature>();
        SimpleFeatureIterator it = featureSource.getFeatures().features();
        try {
            int index = 0;
            while (it.hasNext()) {
                SimpleFeature sf = it.next();
                if (index != selectedRow) {
                    List<Object> attributeValueList = sf.getAttributes();
                    sfb.addAll(attributeValueList);
                    featureList.add(sfb.buildFeature(null));
                }
                index++;
            }
        } finally {
            it.close();
        }
        SimpleFeatureCollection collection = new ListFeatureCollection(featureType, featureList);
        featureCollection = collection;
        cachedFeature = null;
        lastRow = -1;
        DataStore dataStore = DataUtilities.dataStore(collection);
        userLayer.setInlineFeatureDatastore(dataStore);
    } catch (IOException e) {
        ConsoleManager.getInstance().exception(this, e);
    }
    this.fireTableStructureChanged();
    this.fireTableDataChanged();
    if (parentObj != null) {
        parentObj.inlineFeatureUpdated();
    }
}
Also used : SimpleFeatureSource(org.geotools.data.simple.SimpleFeatureSource) ArrayList(java.util.ArrayList) 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

SimpleFeatureType (org.opengis.feature.simple.SimpleFeatureType)149 SimpleFeature (org.opengis.feature.simple.SimpleFeature)75 Test (org.junit.Test)47 IOException (java.io.IOException)46 SimpleFeatureSource (org.geotools.data.simple.SimpleFeatureSource)29 SimpleFeatureBuilder (org.geotools.feature.simple.SimpleFeatureBuilder)24 DataStore (org.geotools.data.DataStore)23 AttributeDescriptor (org.opengis.feature.type.AttributeDescriptor)23 SimpleFeatureStore (org.geotools.data.simple.SimpleFeatureStore)21 HashMap (java.util.HashMap)20 ObjectId (org.locationtech.geogig.api.ObjectId)20 SimpleFeatureTypeBuilder (org.geotools.feature.simple.SimpleFeatureTypeBuilder)19 RevFeatureType (org.locationtech.geogig.api.RevFeatureType)18 Feature (org.opengis.feature.Feature)18 ArrayList (java.util.ArrayList)16 File (java.io.File)15 SimpleFeatureIterator (org.geotools.data.simple.SimpleFeatureIterator)15 NodeRef (org.locationtech.geogig.api.NodeRef)15 InvalidParameterException (org.locationtech.geogig.cli.InvalidParameterException)15 DefaultTransaction (org.geotools.data.DefaultTransaction)14