Hallo,
I already posted this problem previously. Anyway, I figured out what might
be the reason. Still I can't find the solution. I have several polygons and
want to write them to shapefile. The shapefiles along with the attributes I
defined are created, but no features in there. There seems to be a problem
with polygon and multipolygon which I cant figure out. Here is the error
stack trace and code examples. Please help. Thanks steffen.

org.geotools.data.DataSourceException: Could not create out out of provided
feature: My Name
        at
org.geotools.data.AbstractFeatureStore.addFeatures(AbstractFeatureStore.java:261)
        at test.TSXFootprint2.main(TSXFootprint2.java:126)
Caused by: org.geotools.feature.IllegalAttributeException: illegal attribute
        at
org.geotools.feature.DefaultFeature.setAttributes(DefaultFeature.java:309)
        at
org.geotools.data.AbstractFeatureStore.addFeatures(AbstractFeatureStore.java:259)
        ... 1 more
Caused by: java.lang.IllegalArgumentException
        at org.geotools.feature.AttributeImpl.setValue(AttributeImpl.java:106)
        at
org.geotools.feature.simple.SimpleFeatureImpl.setValue(SimpleFeatureImpl.java:101)
        at
org.geotools.feature.simple.SimpleFeatureImpl.setValues(SimpleFeatureImpl.java:107)
        at
org.geotools.feature.simple.SimpleFeatureImpl.setValues(SimpleFeatureImpl.java:112)
        at
org.geotools.feature.DefaultFeature.setAttributes(DefaultFeature.java:306)
        ... 2 more
Caused by: org.geotools.feature.IllegalAttributeException:
com.vividsolutions.jts.geom.Polygon is not an acceptable class for the_geom
as it is not assignable from class com.vividsolutions.jts.geom.MultiPolygon
        at org.geotools.feature.type.Types.validate(Types.java:534)
        at org.geotools.feature.type.Types.validate(Types.java:502)
        at org.geotools.feature.AttributeImpl.setValue(AttributeImpl.java:104)
        ... 6 more

I'm creating a collection of polygons as follows:

                                                for (int j = 0; j < 
footprint.length / 2; j++) {
                                                        Coordinate coord = new 
Coordinate(Double
                                                                        
.parseDouble(footprint[idx + 1]), Double
                                                                        
.parseDouble(footprint[idx]));
                                                        idx = idx + 2;
                                                        coords[j] = coord;
                                                } // endfor

                                                GeometryFactory gf = new 
GeometryFactory();
                                                LinearRing ring = 
gf.createLinearRing(coords);
                                                Polygon polygon = 
gf.createPolygon(ring, null);
                                                Feature feature = 
TYPE.create(new Object[] { polygon,
                                                                12, "aname" }, 
"My Name");

                                                collection.add(feature);

Feature type and collection are defined as:
                        CoordinateReferenceSystem crs = CRS.decode("EPSG:4326");
                        AttributeType GEOM = 
AttributeTypeFactory.newAttributeType(
                                        "the_geom", Polygon.class, true, null, 
null, crs);
                        AttributeType ID = 
AttributeTypeFactory.newAttributeType("IDENT",
                                        Integer.class, false, 14, 0);
                        AttributeType NAME = 
AttributeTypeFactory.newAttributeType("Name",
                                        String.class, true);
                        FeatureType TYPE = FeatureTypeFactory.newFeatureType(
                                        new AttributeType[] { GEOM, ID, NAME }, 
"bla");

                        FeatureCollection collection = 
FeatureCollections.newCollection();

Finally I want to write them to a polygon shapefile using:
                        DataStoreFactorySpi factory = new 
ShapefileDataStoreFactory();

                        Map<String, Object> create = new HashMap<String, 
Object>();
                        create.put("url", out.toURL());
                        create.put("create spatial index", Boolean.TRUE);

                        ShapefileDataStore newDataStore = (ShapefileDataStore) 
factory
                                        .createNewDataStore(create);
                        newDataStore.createSchema(TYPE);
                        newDataStore.forceSchemaCRS(DefaultGeographicCRS.WGS84);
                        Transaction transaction = new 
DefaultTransaction("create");
                        String typeName = newDataStore.getTypeNames()[0];
                        FeatureStore featureStore = (FeatureStore) newDataStore
                                        .getFeatureSource(typeName);
                        featureStore.setTransaction(transaction);
                        try {
                                featureStore.addFeatures(collection);
                                transaction.commit();
                        } catch (Exception problem) {
                                problem.printStackTrace();
                                transaction.rollback();
                        } finally {
                                transaction.close();
                        }
                        System.exit(0);
-- 
View this message in context: 
http://www.nabble.com/Writing-Polygon-collection-2-shapefile-tp16260096p16260096.html
Sent from the geotools-gt2-users mailing list archive at Nabble.com.


-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Geotools-gt2-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users

Reply via email to