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);
}
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());
}
}
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());
}
}
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());
}
}
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);
}
}
Aggregations