Search in sources :

Example 1 with DataSourceAttributeData

use of com.sldeditor.datasource.attribute.DataSourceAttributeData 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 2 with DataSourceAttributeData

use of com.sldeditor.datasource.attribute.DataSourceAttributeData 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 3 with DataSourceAttributeData

use of com.sldeditor.datasource.attribute.DataSourceAttributeData in project sldeditor by robward-scisys.

the class DataSourceImpl method readAttributes.

/**
 * Read attributes.
 *
 * @param attributeData the attribute data
 */
/*
     * (non-Javadoc)
     * 
     * @see com.sldeditor.datasource.DataSourceInterface#updateAttributes(com.sldeditor.render.iface. RenderAttributeDataInterface)
     */
@Override
public void readAttributes(DataSourceAttributeListInterface attributeData) {
    if (attributeData == null) {
        return;
    }
    List<DataSourceAttributeData> valueMap = new ArrayList<DataSourceAttributeData>();
    SimpleFeatureCollection featureCollection = dataSourceInfo.getFeatureCollection();
    if (featureCollection != null) {
        SimpleFeatureIterator iterator = featureCollection.features();
        Map<Integer, Name> fieldNameMap = dataSourceInfo.getFieldNameMap();
        Map<Integer, Class<?>> fieldTypeMap = dataSourceInfo.getFieldTypeMap();
        if (iterator.hasNext()) {
            SimpleFeature feature = iterator.next();
            List<Object> attributes = feature.getAttributes();
            for (int i = 0; i < attributes.size(); i++) {
                Name name = fieldNameMap.get(i);
                if (name != null) {
                    String fieldName = name.getLocalPart();
                    Class<?> fieldType = fieldTypeMap.get(i);
                    if (fieldType == Geometry.class) {
                        Object value = feature.getAttribute(fieldName);
                        fieldType = value.getClass();
                    }
                    Object value = CreateSampleData.getFieldTypeValue(i, fieldName, fieldType, attributes.get(i));
                    DataSourceAttributeData data = new DataSourceAttributeData(fieldName, fieldType, value);
                    valueMap.add(data);
                }
            }
        }
        iterator.close();
    }
    attributeData.setData(valueMap);
}
Also used : DataSourceAttributeData(com.sldeditor.datasource.attribute.DataSourceAttributeData) ArrayList(java.util.ArrayList) SimpleFeature(org.opengis.feature.simple.SimpleFeature) SimpleFeatureCollection(org.geotools.data.simple.SimpleFeatureCollection) Name(org.opengis.feature.type.Name) SimpleFeatureIterator(org.geotools.data.simple.SimpleFeatureIterator)

Example 4 with DataSourceAttributeData

use of com.sldeditor.datasource.attribute.DataSourceAttributeData in project sldeditor by robward-scisys.

the class DataSourceImpl method updateFields.

/**
 * Update fields.
 *
 * @param attributeData the attribute data
 */
/*
     * (non-Javadoc)
     * 
     * @see com.sldeditor.datasource.DataSourceInterface#updateFields(com.sldeditor.render.iface. RenderAttributeDataInterface)
     */
@Override
public void updateFields(DataSourceAttributeListInterface attributeData) {
    if (attributeData != null) {
        List<DataSourceAttributeData> attributeDataList = attributeData.getData();
        if (this.editorFileInterface != null) {
            this.dataSourceProperties = editorFileInterface.getDataSource();
            SLDDataInterface sldData = this.editorFileInterface.getSLDData();
            if (sldData != null) {
                sldData.setFieldList(attributeDataList);
            }
        }
        this.connectedToDataSourceFlag = false;
        createInternalDataSource();
        notifyDataSourceLoaded();
    }
}
Also used : DataSourceAttributeData(com.sldeditor.datasource.attribute.DataSourceAttributeData) SLDDataInterface(com.sldeditor.common.SLDDataInterface)

Example 5 with DataSourceAttributeData

use of com.sldeditor.datasource.attribute.DataSourceAttributeData in project sldeditor by robward-scisys.

the class DataSourceImpl method addField.

/**
 * Adds the field.
 *
 * @param dataSourceField the data source field
 */
@Override
public void addField(DataSourceAttributeData dataSourceField) {
    if (dataSourceField != null) {
        if (connectedToDataSourceFlag == false) {
            SLDDataInterface sldData = this.editorFileInterface.getSLDData();
            List<DataSourceAttributeData> fieldList = sldData.getFieldList();
            if (fieldList == null) {
                fieldList = new ArrayList<DataSourceAttributeData>();
                sldData.setFieldList(fieldList);
            }
            fieldList.add(dataSourceField);
            createInternalDataSource();
            notifyDataSourceLoaded();
        }
    }
}
Also used : DataSourceAttributeData(com.sldeditor.datasource.attribute.DataSourceAttributeData) SLDDataInterface(com.sldeditor.common.SLDDataInterface)

Aggregations

DataSourceAttributeData (com.sldeditor.datasource.attribute.DataSourceAttributeData)40 Test (org.junit.Test)23 ExtractAttributes (com.sldeditor.datasource.impl.ExtractAttributes)12 ArrayList (java.util.ArrayList)11 StyledLayerDescriptor (org.geotools.styling.StyledLayerDescriptor)10 Rule (org.geotools.styling.Rule)8 Filter (org.opengis.filter.Filter)8 LineString (com.vividsolutions.jts.geom.LineString)5 SLDDataInterface (com.sldeditor.common.SLDDataInterface)4 DataSourceAttributeList (com.sldeditor.datasource.attribute.DataSourceAttributeList)4 Point (com.vividsolutions.jts.geom.Point)4 IOException (java.io.IOException)4 SimpleFeature (org.opengis.feature.simple.SimpleFeature)4 SimpleFeatureType (org.opengis.feature.simple.SimpleFeatureType)4 DataSourceImpl (com.sldeditor.datasource.impl.DataSourceImpl)3 MultiLineString (com.vividsolutions.jts.geom.MultiLineString)3 MultiPoint (com.vividsolutions.jts.geom.MultiPoint)3 AttributeDescriptor (org.opengis.feature.type.AttributeDescriptor)3 SLDData (com.sldeditor.common.data.SLDData)2 CreateDataSourceInterface (com.sldeditor.datasource.impl.CreateDataSourceInterface)2