Hi Judy,
I got an error trying to execute your exaples... Suppose the following code:
static final String url = "D:\\Tomcat4.1\\webapps\\geoserver\\data\\featureTypes\\Spst\\";
public static void main(String args) {
try{
ShapefileDataStore data = new ShapefileDataStore((new
File(url+"Spst.shp").toURL()));
Query query = new DefaultQuery( "Spst", Filter.NONE );
FeatureReader reader = data.getFeatureReader( query, Transaction.AUTO_COMMIT ); // <--Line 36 (Exception is thrown here)
try {
while( reader.hasNext() ){
Feature feature = reader.next();
System.out.println( "> "+reader );
}
}
finally {
reader.close();
}
}catch(Exception e){e.printStackTrace();}
Threw the following exception:
java.lang.NoSuchMethodError: org.geotools.feature.DefaultFeatureTypeFactory.getDefaultGeometry()Lorg/geotools/feature/AttributeType;
at org.geotools.feature.DefaultFeatureTypeFactory.createAbstractType(DefaultFeatureTypeFactory.java:85)
at org.geotools.feature.DefaultFeatureTypeFactory.createFeatureType(DefaultFeatureTypeFactory.java:48)
at org.geotools.feature.FeatureTypeFactory.getFeatureType(FeatureTypeFactory.java:621)
at org.geotools.feature.FeatureTypeFactory.newFeatureType(FeatureTypeFactory.java:163)
at org.geotools.feature.FeatureTypeFactory.newFeatureType(FeatureTypeFactory.java:185)
at org.geotools.feature.FeatureTypeFactory.getBuiltinTypes(FeatureTypeFactory.java:700)
at org.geotools.feature.FeatureTypeFactory.getSuperTypes(FeatureTypeFactory.java:303)
at org.geotools.feature.DefaultFeatureTypeFactory.createFeatureType(DefaultFeatureTypeFactory.java:51)
at org.geotools.feature.FeatureTypeFactory.getFeatureType(FeatureTypeFactory.java:621)
at org.geotools.feature.FeatureTypeFactory.newFeatureType(FeatureTypeFactory.java:163)
at org.geotools.feature.FeatureTypeFactory.newFeatureType(FeatureTypeFactory.java:185)
at org.geotools.feature.FeatureTypeFactory.newFeatureType(FeatureTypeFactory.java:226)
at org.geotools.data.shapefile.ShapefileDataStore.getSchema(ShapefileDataStore.java:278)
at org.geotools.data.AbstractDataStore.getFeatureReader(AbstractDataStore.java:305)
at calegari.util.SHPMapMan.main(SHPMapMan.java:36)
I downloaded I the sources from the CVS once the class ShapefileDataStore and others weren't in the "regular" file download.
Regards,
Aurelio
----- Original Message -----
From: "Jody Garnett" <jgarnett@anonymised.com>
To: "Aurélio Calegari" <aurelio@anonymised.com>
Cc: "Andrea Aime" <andrea.aime@anonymised.com>; "geosrvd" <geoserver-devel@lists.sourceforge.net>; "James Macgill"
<jmacgill@anonymised.com>; "geotools-devel" <geotools-devel@lists.sourceforge.net>
Sent: Wednesday, January 21, 2004 1:17 PM
Subject: Re: [Geoserver-devel] Getting shapefile
Hey Aurelio!
Glad you are enjoying yourself. There are some DataStore tutorials on
the main Geotools2 website that you could look at.
I would point you to them but they are out of date! (Ping James can we
update this?)
You can also use FeatureReader to do the work: you can then work with
shapefiles that are bigger then you can fit into memory. In you example
you used a FeatureCollection which loads everything into memory before
you can use it.
>ShapefileDataStore data = new ShapefileDataStore((new
>File(url+"Spst.shp").toURL()));
>Query query = new DefaultQuery( "Spst", Filter.NONE );
>FeatureReader reader = data.getFeatureReader( query, Transaction.AUTO_COMMIT );
>try {
> while( reader.hasNext() ){
> Feature feature = reader.next();
> System.out.println( "> "+reader );
> }
>}
>finally {
> reader.close();
>}
I did not need to call feature.toString() as the + opperator does that
by default.
I am afraid someone else will have to help you with constructing
filters, there are a few examples in the DataStore tutorial on the web site.
Oh I suppose I could make it clear - Query.ALL does not really work when
used with DataStore.getFeatureReader - we need to know the typeName.
While this seems obvious for shapefile that only handles one file, other
DataStores let you work with all of the files in a directory and
typeName is used to specify which file.
You can use FeatureSource and FeatureResults as you did to work around
this limitation:
>ShapefileDataStore data = new ShapefileDataStore((newFile(url+"Spst.shp").toURL()));
>FeatureSource source = data.getFeatureSource( "Spst" );
>FeatureResults results = source.getFeatures( Query.ALL );
>FeatureReader reader = results.reader();
>try {
> while( reader.hasNext() ){
> Feature feature = reader.next();
> System.out.println( "> "+reader );
> }
>}
>finally {
> reader.close();
>}
>
You can perform other interesting Queries with results such as
results.getBounds() and results.getCount() and results.collection() to
arrive at the FeatureCollection you used in your example (in case you
want everything in memory for speed).
Cheers,
Jody
>
>Is that what you mean? It's really much better!
>
> ShapefileDataSource data = new ShapefileDataSource((new
>File(url+"Spst.shp").toURL()));
> FeatureCollection fc = data.getFeatures();
> FeatureIterator fi = fc.features();
> while(fi.hasNext()){
> Feature f = fi.next();
> System.out.println("> "+f.toString());
> }
>
>What's more, would you know where I could find some examples on how to
>correctly use Filters and Queries to narrow the search?
>
>Thanks once more!
>
>Best regards,
>
>Aurelio
>
>----- Original Message -----
>From: "Andrea Aime" <andrea.aime@anonymised.com>
>To: "Aurélio Calegari" <aurelio@anonymised.com>
>Cc: "geosrvd" <geoserver-devel@lists.sourceforge.net>
>Sent: Wednesday, January 21, 2004 12:34 PM
>Subject: Re: [Geoserver-devel] Getting shapefile
>
>
>
>
>>Aurélio Calegari wrote:
>>
>>
>>
>>>Hello,
>>>
>>>Well, I just got started with GT2 and GeoServer. I having some dauts
>>>concerning the link between SHP registers and DBF registers.
>>>Using the geotools classes, and reading the shapefile ESRI doc I
>>>
>>>
>undestood
>
>
>>>that the registers in SHP and DBF are in a one-to-one relationship (and
>>>
>>>
>in
>
>
>>>the same order). Is that right?
>>>
>>>
>>Yes, by shapefile format definition.
>>
>>
>>
>>>If so, that source bellow should be all right bringing attributes plus
>>>
>>>
>geo
>
>
>>>info, right?
>>>
>>> (...)
>>> FileChannel in = new FileInputStream(url+"Spst.shp").getChannel();
>>> ShapefileReader r = new ShapefileReader( in );
>>> org.geotools.data.shapefile.shp.ShapefileHeader sh =
>>>
>>>
>r.getHeader();
>
>
>>> System.out.println(sh.toString()+" magic:"+sh.MAGIC);
>>>
>>>
>>> FileChannel in2 = new
>>>
>>>
>FileInputStream(url+"Spst.dbf").getChannel();
>
>
>>> DbaseFileReader r2 = new DbaseFileReader( in2 );
>>> DbaseFileHeader d = r2.getHeader();
>>> System.out.println(d.toString());
>>> Object fields = new Object[d.getNumFields()];
>>>
>>> while (r2.hasNext()) {
>>> MultiLineString mls = (MultiLineString)r.nextRecord().shape();
>>>
>>> r2.readEntry(fields);
>>> System.out.print("\n"+mls.toText());
>>> for(int i=0;i<r2.getHeader().getNumFields();i++){}
>>> System.out.print(" "+fields[i].toString()+" ");
>>> }
>>> r.close();
>>> (...)
>>>
>>>
>>It seems ok to me. But I don't understand why you don't simply get a
>>
>>
>feature reader
>
>
>>from the ShapefileDataStore and get features one by one (or get the
>>
>>
>FeatureSource
>
>
>>from the DataStore and perform a query, if you want less attributes, or
>>
>>
>want
>
>
>>to specify a selection predicate).
>>
>>Best regards
>>Andrea Aime
>>
>>
>>
>>
>
>
>
>-------------------------------------------------------
>The SF.Net email is sponsored by EclipseCon 2004
>Premiere Conference on Open Tools Development and Integration
>See the breadth of Eclipse activity. February 3-5 in Anaheim, CA.
>http://www.eclipsecon.org/osdn
>_______________________________________________
>Geoserver-devel mailing list
>Geoserver-devel@lists.sourceforge.net
>https://lists.sourceforge.net/lists/listinfo/geoserver-devel
>
>