Index: src/test/java/org/geotools/data/shapefile/ShapefileAttributeReaderTest.java =================================================================== --- src/test/java/org/geotools/data/shapefile/ShapefileAttributeReaderTest.java (revision 0) +++ src/test/java/org/geotools/data/shapefile/ShapefileAttributeReaderTest.java (revision 0) @@ -0,0 +1,61 @@ +package org.geotools.data.shapefile; + +import java.io.IOException; +import java.util.Collections; +import java.util.Iterator; + +import org.geotools.TestData; +import org.geotools.data.DefaultQuery; +import org.geotools.data.Query; +import org.geotools.data.shapefile.indexed.IndexedShapefileDataStore; +import org.geotools.factory.CommonFactoryFinder; +import org.geotools.feature.FeatureCollection; +import org.opengis.feature.simple.SimpleFeature; +import org.opengis.feature.simple.SimpleFeatureType; +import org.opengis.filter.Filter; +import org.opengis.filter.FilterFactory; +import org.opengis.filter.identity.FeatureId; + +public class ShapefileAttributeReaderTest extends TestCaseSupport { + + public final String STATEPOP = "shapes/statepop.shp"; + + public ShapefileAttributeReaderTest(String name) throws IOException { + super(name); + } + + public void testAttributeReader() throws IOException { + copyShapefiles(STATEPOP); + ShapefileDataStoreFactory factory = new ShapefileDataStoreFactory(); + + //creates both indexed and regular shapefile data store + IndexedShapefileDataStore indexedstore = new IndexedShapefileDataStore(TestData.url(STATEPOP)); + ShapefileDataStore store = new ShapefileDataStore(TestData.url(STATEPOP)); + + //get a random feature id from one of the stores + FeatureId fid = indexedstore.getFeatureSource().getFeatures().iterator().next().getIdentifier(); + + //query the datastore + FilterFactory ff = CommonFactoryFinder.getFilterFactory(null); + Filter idFilter = ff.id(Collections.singleton(fid)); + final Query query = new DefaultQuery(indexedstore.getSchema().getName().getLocalPart(), idFilter, new String[] { "STATE_NAME"}); + final FeatureCollection indexedfeatures = indexedstore.getFeatureSource().getFeatures(query); + final FeatureCollection features = store.getFeatureSource().getFeatures(query); + + //compare the results + Iterator indexIterator = indexedfeatures.iterator(); + SimpleFeature indexedFeature = indexIterator.next(); + + Iterator iterator = features.iterator(); + SimpleFeature feature = iterator.next(); + + String indexedStateName = (String) indexedFeature.getAttribute("STATE_NAME"); + String stateName = (String) feature.getAttribute("STATE_NAME"); + + System.out.println(indexedStateName); + System.out.println(stateName); + + assertEquals(indexedStateName, stateName); + } + +} Index: src/main/java/org/geotools/data/shapefile/indexed/IndexedShapefileDataStore.java =================================================================== --- src/main/java/org/geotools/data/shapefile/indexed/IndexedShapefileDataStore.java (revision 34158) +++ src/main/java/org/geotools/data/shapefile/indexed/IndexedShapefileDataStore.java (working copy) @@ -292,7 +292,7 @@ FilterAttributeExtractor fae = new FilterAttributeExtractor(); query.getFilter().accept(fae, null); - Set attributes = new LinkedHashSet(Arrays.asList(propertyNames)); + Set attributes = new LinkedHashSet(Arrays.asList(propertyNames)); attributes.addAll(fae.getAttributeNameSet()); propertyNames = (String[]) attributes.toArray(new String[attributes @@ -313,9 +313,14 @@ readDbf = false; readGeometry = false; newSchema = createSubType( propertyNames); - } else if(propertyNames.length > 0) { - newSchema = createSubType( propertyNames); } + else if( propertyNames.length > 0 && !propertyNames[0].equals(defaultGeomName) ){ + readGeometry = false; + newSchema = createSubType(propertyNames); + } + else if(propertyNames.length > 0) { + newSchema = createSubType(propertyNames); + } return createFeatureReader(typeName, getAttributesReader(readDbf, readGeometry, query, newSchema), newSchema); Index: src/main/java/org/geotools/data/shapefile/ShapefileAttributeReader.java =================================================================== --- src/main/java/org/geotools/data/shapefile/ShapefileAttributeReader.java (revision 34158) +++ src/main/java/org/geotools/data/shapefile/ShapefileAttributeReader.java (working copy) @@ -47,10 +47,10 @@ protected DbaseFileReader.Row row; protected ShapefileReader.Record record; int cnt; - int[] dbfindexes; + protected int[] dbfindexes; protected Envelope targetBBox; double simplificationDistance; - Object geometry; + protected Object geometry; public ShapefileAttributeReader(List atts, ShapefileReader shp, DbaseFileReader dbf) { @@ -90,12 +90,15 @@ if(dbf != null) { dbfindexes = new int[atts.length]; DbaseFileHeader head = dbf.getHeader(); - for (int i = 1; i < atts.length; i++) { + AT: for (int i = 1; i < atts.length; i++) { String attName = atts[i].getLocalName(); for(int j = 0; j < head.getNumFields(); j++) { - if(head.getFieldName(j).equals(attName)) + if(head.getFieldName(j).equals(attName)){ dbfindexes[i] = j; + continue AT; + } } + dbfindexes[i] = -1; // geometry } } } @@ -169,15 +172,16 @@ public Object read(int param) throws IOException, java.lang.ArrayIndexOutOfBoundsException { - switch (param) { - case 0: - return geometry; + int index = dbfindexes[param]; + + switch (index) { + case -1: + return geometry; // geometry is considered dbf index -1 default: if (row != null) { - return row.read(dbfindexes[param]); + return row.read( index ); } - return null; } }