Finally, I found a solution, certainly not quite right but I'm in way.
public static void main(String[] args) throws Exception {
File outFile = new File("C:\\result.shp");
File inFile = new File("C:\\test.xml");
InputStream in = new FileInputStream(inFile);
GMLConfiguration config = new GMLConfiguration();
StreamingParser parser = new StreamingParser(config, in,
SimpleFeature.class);
SimpleFeature sf = (SimpleFeature) parser.parse();
if (sf == null) {
return;
}
// SimpleFeatureType
SimpleFeatureType inType = sf.getFeatureType();
SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
builder.setName(inType.getName());
builder.setNamespaceURI(inType.getName().getNamespaceURI());
builder.setCRS(DefaultGeographicCRS.WGS84);
builder.setDefaultGeometry(sf.getDefaultGeometryProperty().getName().getLocalPart());
for (Property prop : sf.getProperties()) {
if (isSupportedShapefileType(prop.getType()) && (prop.getValue() !=
null)) {
builder.add(prop.getName().getLocalPart(),
prop.getType().getBinding());
}
}
SimpleFeatureType type = builder.buildFeatureType();
IndexedShapefileDataStore dataStore = new
IndexedShapefileDataStore(outFile.toURI().toURL());
dataStore.createSchema(type);
dataStore.forceSchemaCRS(type.getCoordinateReferenceSystem());
Transaction transaction = new DefaultTransaction("create");
String typeName = dataStore.getTypeNames()[0];
FeatureStore<SimpleFeatureType, SimpleFeature> store =
(FeatureStore<SimpleFeatureType, SimpleFeature>)
dataStore.getFeatureSource(typeName);
store.setTransaction(transaction);
FeatureCollection<SimpleFeatureType, SimpleFeature> outCollection =
FeatureCollections.newCollection();
do {
SimpleFeatureBuilder build = new SimpleFeatureBuilder(type);
for (AttributeType attributeType : type.getTypes()) {
build.add(sf.getProperty(attributeType.getName()).getValue());
//System.out.println("value: "+attributeType.getName()+" :
"+sf.getProperty(attributeType.getName()).getValue());
}
outCollection.add(build.buildFeature(sf.getIdentifier().getID()));
} while ((sf = (SimpleFeature) parser.parse()) != null);
try {
store.addFeatures(outCollection);
transaction.commit();
} catch (Exception problem) {
problem.printStackTrace();
transaction.rollback();
} finally {
transaction.close();
}
}
private static boolean isSupportedShapefileType(PropertyType type) {
String supported[] = {
"String", "Integer", "Double", "Boolean", "Date",
"LineString", "MultiLineString", "Polygon", "MultiPolygon",
"Point", "MultiPoint"};
for (String iter : supported) {
if (type.getBinding().getSimpleName().equals(iter)) {
return true;
}
}
return false;
}
--
View this message in context:
http://n2.nabble.com/Help-convert-gml-to-shp-tp4736160p4930662.html
Sent from the geotools-gt2-users mailing list archive at Nabble.com.
------------------------------------------------------------------------------
_______________________________________________
Geotools-gt2-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users