[udig-devel] creating a feature with default values
andrea antonello
andrea.antonello at gmail.com
Sat May 3 00:29:27 PDT 2008
Alright, part II:
I didn't get that right with the getFeatureType from the
featurecollection, instead I was really searching for the getSchema
method.
FeatureType featureType = selectedFeatureCollection.getSchema();
Object attributes[] = DataUtilities.defaultValues(featureType);
attributes[0] = gFac.createPoint(new Coordinate(0.0, 0.0));
featureToUse = featureType.create(attributes);
And that does what I need. I will have to investigate how the
different datastores work for this.
But now I think I will celebrate the Saturday :)
Ciao
Andrea
On Sat, May 3, 2008 at 8:56 AM, andrea antonello
<andrea.antonello at gmail.com> wrote:
> Alright, next step:
>
> Since I tried like this and it worked perfectly:
>
> GeometryFactory gF = new GeometryFactory();
> GeometryAttributeType geometryAttribute =
> (GeometryAttributeType) AttributeTypeFactory
> .newAttributeType("the_geom", MultiPoint.class, false, 1, gF
> .createMultiPoint(new Coordinate[]{new
> Coordinate(0.0, 0.0)}));
> AttributeType catAttribute =
> AttributeTypeFactory.newAttributeType("cat",
> Integer.class, true, 1, 1);
> AttributeType levelAttribute =
> AttributeTypeFactory.newAttributeType("level",
> Float.class);
>
> FeatureType featureType = null;
> featureType = FeatureTypeBuilder.newFeatureType(new
> AttributeType[]{geometryAttribute,
> catAttribute, levelAttribute}, "name");
>
> AttributeType attributeType = featureType.getAttributeType(0);
>
> System.out.println(attributeType);
>
> Object attributes[] = DataUtilities.defaultValues(featureType);
> Feature featureToUse = featureType.create(attributes);
>
> System.out.println(featureToUse.getDefaultGeometry().getGeometryType());
>
>
> I felt kind of strange, so I tried to have a look at what the
> featuretype of a collection and a feature is:
>
> FeatureType featureType =
> selectedFeatureCollection.getFeatureType();
> Feature next = selectedFeatureCollection.features().next();
> FeatureType featureType2 = next.getFeatureType();
>
> and the are really different:
>
> featureType:
> DefaultFeatureType [name=AbstractFeatureCollectionType ,
> namespace=http://www.opengis.net/gml , abstract=false ,
> types=(org.geotools.feature.type.FeatureAttributeType at e834ce,)]
>
> whereas featureType2 is:
> DefaultFeatureType [name=stazioni_meteo ,
> namespace=http://www.opengis.net/gml , abstract=false ,
> types=(DefaultAttributeType [name=the_geom , type=class
> com.vividsolutions.jts.geom.Point , nillable=true, min=1,
> max=1],DefaultAttributeType [name=ID , type=class java.lang.Long ,
> nillable=true, min=1, max=1],DefaultAttributeType [name=IDSTAZIONE ,
> type=class java.lang.Long , nillable=true, min=1,
> max=1],DefaultAttributeType [name=Y , type=class java.lang.Double ,
> nillable=true, min=1, max=1],DefaultAttributeType [name=X , type=class
> java.lang.Double , nillable=true, min=1, max=1],DefaultAttributeType
> [name=DESCRIZION , type=class java.lang.String , nillable=true, min=1,
> max=1],DefaultAttributeType [name=TIPOLOGIA_ , type=class
> java.lang.Long , nillable=true, min=1, max=1],DefaultAttributeType
> [name=RELATED_ID , type=class java.lang.Long , nillable=true, min=1,
> max=1],DefaultAttributeType [name=RETE_ID , type=class java.lang.Long
> , nillable=true, min=1, max=1],DefaultAttributeType [name=ATTIVO ,
> type=class java.lang.String , nillable=true, min=1, max=1],)]
>
>
> which is what I really want. But how do I get it from a layer that
> doesn't have any feature on it?
>
> Ciao
> Andrea
>
>
>
> On Sat, May 3, 2008 at 12:08 AM, andrea antonello
>
> <andrea.antonello at gmail.com> wrote:
>
>
> > Alright, I think I miss something.
> >
> > [...]
> >
> > > Object attributes[] = DataUtilities.defaultValues( featureType );
> >
> > That is what I thought I would need, btut attributes[0], seems to
> > contain it all:
> >
> > [Feature[ id=fid-861f70d_119aa748a37__7fec , the_geom=null , ID=null ,
> > IDSTAZIONE=null , Y=null , X=null , DESCRIZION=null , TIPOLOGIA_=null
> > , RELATED_ID=null , RETE_ID=null , ATTIVO=null ]]
> >
> > Isn't this a full feature structure?
> >
> > > attributes[0] = geometry?
> >
> > If I do this, then I get an exception in the next:
> >
> > > featureType.create( attributes );
> >
> > telling me that:
> >
> > IllegalAttribute: org.geotools.feature.Feature , but got
> > com.vividsolutions.jts.geom.Point
> >
> > So the attribute is expected to be a feature, instead of one of the
> > attributes, geometry or non geometry?
> >
> > Investigating further...
> > Andrea
> >
> >
> >
> >
> >
> >
> > >
> > > There is also a method for dealing with a single AttributeType
> > >
> > > Object value = DataUtilities.defaultValue( attributeTypes[0] );
> > >
> > > > Should this work?
> > > >
> > > >
> > > Your example looked fine (and similar to the code in DataUtilities), I
> > > think you now may be arguing with your DataStore or working with invalid
> > > data?
> > >
> > >
> > > Jody
> > > _______________________________________________
> > > User-friendly Desktop Internet GIS (uDig)
> > > http://udig.refractions.net
> > > http://lists.refractions.net/mailman/listinfo/udig-devel
> > >
> >
>
More information about the udig-devel
mailing list