More about this, but it doesn't work:

public static void main(String[] args) throws Exception {

    File inFile = new File("C:\\test.xml");
    File outFile = new File("C:\\result.shp");

    // Parseo el GML
    GMLConfiguration gml = new GMLConfiguration();
    Parser parser = new Parser(gml);
    FeatureCollection collection = (FeatureCollection) parser.parse(new
FileInputStream(inFile));

    // Selecciono lo que puedo transformar y creo un SimpleFeatureType
    FeatureType featureType = collection.getSchema();

    SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
    builder.setName(featureType.getName());
    builder.setNamespaceURI("http://localhost/";);
    builder.setCRS(DefaultGeographicCRS.WGS84);


   
builder.setDefaultGeometry(featureType.getGeometryDescriptor().getLocalName());

    
    Iterator iter = featureType.getDescriptors().iterator();
    while (iter.hasNext()) {
      PropertyDescriptor prop = (PropertyDescriptor) iter.next();
      if (isSupportedShapefileType(prop.getType())) {
        //if (prop.getName().getLocalPart().equals("the_geom"))
        builder.add(prop.getName().getLocalPart(),
prop.getType().getBinding());
      }
    }
  
    
    SimpleFeatureType outFeatureType = builder.buildFeatureType();
    System.out.println("Geometry:
"+featureType.getGeometryDescriptor().getLocalName());
    System.out.println("Geometry: "+outFeatureType);

    // Donde lo voy a guardar
    Map params = Collections.singletonMap("url", outFile.toURI().toURL());
    DataStore dataStore = new
ShapefileDataStoreFactory().createNewDataStore(params);
    dataStore.createSchema(outFeatureType);

    // Ahora, la transaccion
    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();

    FeatureIterator featureIter = collection.features();
    try {
      while (featureIter.hasNext()) {
        Feature feature = featureIter.next();
        /*SimpleFeatureBuilder build = new
SimpleFeatureBuilder(outFeatureType);
        build.add(feature.getDefaultGeometryProperty().getValue());
        System.out.println(feature.getDefaultGeometryProperty().getValue());
       
outCollection.add(build.buildFeature(feature.getIdentifier().getID()));
        */
        SimpleFeatureBuilder build = new
SimpleFeatureBuilder(outFeatureType);
        for (AttributeType attributeType : outFeatureType.getTypes()) {
         
build.add(feature.getProperty(attributeType.getName()).getValue());
          System.out.println("value: "+attributeType.getName()+" :
"+feature.getProperty(attributeType.getName()).getValue());
        }
     
       
outCollection.add(build.buildFeature(feature.getIdentifier().getID()));
      }
    } finally {
      collection.close(featureIter);
    }

    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-tp4736160p4924322.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

Reply via email to