Search in sources :

Example 21 with SimpleFeatureSource

use of org.geotools.data.simple.SimpleFeatureSource in project sldeditor by robward-scisys.

the class InlineFeatureUtils method getInlineFeaturesText.

/**
 * Gets the inline features text.
 *
 * @param userLayer the user layer
 * @return the inline features text
 */
public static String getInlineFeaturesText(UserLayer userLayer) {
    if (userLayer == null) {
        return "";
    }
    // Handle the case where there is a featurecollection but no features
    if (userLayer.getInlineFeatureDatastore() != null) {
        String typeName = userLayer.getInlineFeatureType().getTypeName();
        SimpleFeatureSource featureSource;
        try {
            featureSource = userLayer.getInlineFeatureDatastore().getFeatureSource(typeName);
            if (featureSource.getFeatures().isEmpty()) {
                return "";
            }
        } catch (IOException e) {
            ConsoleManager.getInstance().exception(InlineFeatureUtils.class, e);
            return "";
        }
    }
    // Inline features
    SLDTransformer transform = new SLDTransformer();
    StringWriter stringWriter = new StringWriter();
    try {
        transform.setIndentation(2);
        transform.setOmitXMLDeclaration(true);
        transform.createTransformer();
        transform.transform(userLayer, stringWriter);
    } catch (TransformerException e) {
        ConsoleManager.getInstance().exception(InlineFeatureUtils.class, e);
        return null;
    }
    String userLayerXML = stringWriter.toString();
    // Check to see if there are any inline features
    if (!userLayerXML.contains(SLD_INLINE_FEATURE_START)) {
        return "";
    }
    int beginIndex = userLayerXML.indexOf(GML_START);
    StringBuilder sb = new StringBuilder();
    sb.append("\n");
    int index = beginIndex - 1;
    while (index > 0) {
        if (userLayerXML.charAt(index) != ' ') {
            break;
        } else {
            sb.append(" ");
            index--;
        }
    }
    int endIndex = userLayerXML.lastIndexOf(GML_END) + GML_END.length();
    if (beginIndex < 0) {
        beginIndex = 0;
    }
    String extract = userLayerXML.substring(beginIndex, endIndex);
    extract = extract.replace(sb.toString(), "\n");
    return (extract);
}
Also used : StringWriter(java.io.StringWriter) SimpleFeatureSource(org.geotools.data.simple.SimpleFeatureSource) SLDTransformer(org.geotools.styling.SLDTransformer) IOException(java.io.IOException) TransformerException(javax.xml.transform.TransformerException)

Example 22 with SimpleFeatureSource

use of org.geotools.data.simple.SimpleFeatureSource in project sldeditor by robward-scisys.

the class DataSourceInfoTest method testGetFeatures.

/**
 * Test method for {@link com.sldeditor.datasource.impl.DataSourceInfo#getFeatures()}.
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testGetFeatures() {
    URL url = SLDEditorFile.class.getClassLoader().getResource("point/sld/shp/sld_cookbook_point.shp");
    Map map = new HashMap();
    map.put("url", url);
    DataStore dataStore;
    try {
        dataStore = DataStoreFinder.getDataStore(map);
        DataSourceInfo dsInfo = new DataSourceInfo();
        String typeName = dataStore.getTypeNames()[0];
        dsInfo.setTypeName(typeName);
        SimpleFeatureSource source = dataStore.getFeatureSource(typeName);
        SimpleFeatureType schema = source.getSchema();
        assertNull(dsInfo.getFeatures());
        dsInfo.setSchema(schema);
        assertNull(dsInfo.getFeatures());
        dsInfo.setDataStore(dataStore);
        assertTrue(dsInfo.getFeatures() != null);
    } catch (IOException e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : DataSourceInfo(com.sldeditor.datasource.impl.DataSourceInfo) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) HashMap(java.util.HashMap) SimpleFeatureSource(org.geotools.data.simple.SimpleFeatureSource) DataStore(org.geotools.data.DataStore) SLDEditorFile(com.sldeditor.datasource.SLDEditorFile) IOException(java.io.IOException) HashMap(java.util.HashMap) Map(java.util.Map) URL(java.net.URL) Test(org.junit.Test)

Example 23 with SimpleFeatureSource

use of org.geotools.data.simple.SimpleFeatureSource in project sldeditor by robward-scisys.

the class DataSourceInfoTest method testGetGeometryFieldName.

/**
 * Test method for {@link com.sldeditor.datasource.impl.DataSourceInfo#getGeometryFieldName()}.
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testGetGeometryFieldName() {
    URL url = SLDEditorFile.class.getClassLoader().getResource("point/sld/shp/sld_cookbook_point.shp");
    Map map = new HashMap();
    map.put("url", url);
    DataStore dataStore;
    try {
        dataStore = DataStoreFinder.getDataStore(map);
        DataSourceInfo dsInfo = new DataSourceInfo();
        String typeName = dataStore.getTypeNames()[0];
        dsInfo.setTypeName(typeName);
        SimpleFeatureSource source = dataStore.getFeatureSource(typeName);
        SimpleFeatureType schema = source.getSchema();
        assertNull(dsInfo.getGeometryFieldName());
        dsInfo.setSchema(schema);
        assertEquals("the_geom", dsInfo.getGeometryFieldName());
    } catch (IOException e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : DataSourceInfo(com.sldeditor.datasource.impl.DataSourceInfo) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) HashMap(java.util.HashMap) SimpleFeatureSource(org.geotools.data.simple.SimpleFeatureSource) DataStore(org.geotools.data.DataStore) SLDEditorFile(com.sldeditor.datasource.SLDEditorFile) IOException(java.io.IOException) HashMap(java.util.HashMap) Map(java.util.Map) URL(java.net.URL) Test(org.junit.Test)

Example 24 with SimpleFeatureSource

use of org.geotools.data.simple.SimpleFeatureSource in project sldeditor by robward-scisys.

the class DataSourceInfoTest method testGetFeatureCollection.

/**
 * Test method for {@link com.sldeditor.datasource.impl.DataSourceInfo#getFeatureCollection()}.
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testGetFeatureCollection() {
    URL url = SLDEditorFile.class.getClassLoader().getResource("point/sld/shp/sld_cookbook_point.shp");
    Map map = new HashMap();
    map.put("url", url);
    DataStore dataStore;
    try {
        dataStore = DataStoreFinder.getDataStore(map);
        DataSourceInfo dsInfo = new DataSourceInfo();
        String typeName = dataStore.getTypeNames()[0];
        dsInfo.setTypeName(typeName);
        SimpleFeatureSource source = dataStore.getFeatureSource(typeName);
        SimpleFeatureType schema = source.getSchema();
        assertNull(dsInfo.getGeometryFieldName());
        dsInfo.setSchema(schema);
        assertEquals("the_geom", dsInfo.getGeometryFieldName());
    } catch (IOException e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : DataSourceInfo(com.sldeditor.datasource.impl.DataSourceInfo) SimpleFeatureType(org.opengis.feature.simple.SimpleFeatureType) HashMap(java.util.HashMap) SimpleFeatureSource(org.geotools.data.simple.SimpleFeatureSource) DataStore(org.geotools.data.DataStore) SLDEditorFile(com.sldeditor.datasource.SLDEditorFile) IOException(java.io.IOException) HashMap(java.util.HashMap) Map(java.util.Map) URL(java.net.URL) Test(org.junit.Test)

Example 25 with SimpleFeatureSource

use of org.geotools.data.simple.SimpleFeatureSource in project tutorials by eugenp.

the class ShapeFile method writeToFile.

private static void writeToFile(ShapefileDataStore dataStore, DefaultFeatureCollection collection) throws Exception {
    // If you decide to use the TYPE type and create a Data Store with it,
    // You will need to uncomment this line to set the Coordinate Reference System
    // newDataStore.forceSchemaCRS(DefaultGeographicCRS.WGS84);
    Transaction transaction = new DefaultTransaction("create");
    String typeName = dataStore.getTypeNames()[0];
    SimpleFeatureSource featureSource = dataStore.getFeatureSource(typeName);
    if (featureSource instanceof SimpleFeatureStore) {
        SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
        featureStore.setTransaction(transaction);
        try {
            featureStore.addFeatures(collection);
            transaction.commit();
        } catch (Exception problem) {
            problem.printStackTrace();
            transaction.rollback();
        } finally {
            transaction.close();
        }
        // success!
        System.exit(0);
    } else {
        System.out.println(typeName + " does not support read/write access");
        System.exit(1);
    }
}
Also used : Transaction(org.geotools.data.Transaction) DefaultTransaction(org.geotools.data.DefaultTransaction) SimpleFeatureSource(org.geotools.data.simple.SimpleFeatureSource) SimpleFeatureStore(org.geotools.data.simple.SimpleFeatureStore) DefaultTransaction(org.geotools.data.DefaultTransaction)

Aggregations

SimpleFeatureSource (org.geotools.data.simple.SimpleFeatureSource)54 SimpleFeatureType (org.opengis.feature.simple.SimpleFeatureType)29 IOException (java.io.IOException)28 SimpleFeatureStore (org.geotools.data.simple.SimpleFeatureStore)25 SimpleFeature (org.opengis.feature.simple.SimpleFeature)23 DataStore (org.geotools.data.DataStore)22 Test (org.junit.Test)21 SimpleFeatureCollection (org.geotools.data.simple.SimpleFeatureCollection)20 SimpleFeatureIterator (org.geotools.data.simple.SimpleFeatureIterator)19 Feature (org.opengis.feature.Feature)16 HashMap (java.util.HashMap)15 MemoryDataStore (org.geotools.data.memory.MemoryDataStore)13 URL (java.net.URL)10 CommandFailedException (org.locationtech.geogig.cli.CommandFailedException)10 GeoToolsOpException (org.locationtech.geogig.geotools.plumbing.GeoToolsOpException)10 ExportOp (org.locationtech.geogig.geotools.plumbing.ExportOp)9 ArrayList (java.util.ArrayList)8 Map (java.util.Map)8 SimpleFeatureBuilder (org.geotools.feature.simple.SimpleFeatureBuilder)8 InvalidParameterException (org.locationtech.geogig.cli.InvalidParameterException)8