From: Hari S. K. <ha...@la...> - 2006-12-04 19:25:18
|
This works perfectly! I knew there had to be a simple way to solve this problem, and your explanation helps me to understand things better. Since there doesn't appear to be an example of this functionality in the code snippets I hope this post can help people in the future to be able to use this simple but essential functionality. Thank you, Hari At 09:59 AM 12/2/2006, Andrea Aime wrote: >Hari S. Khalsa ha scritto: >>Does anyone know whether it is possible to read in only the features from >>a point shapefile that are contained within a specific geometry? >>I'm trying to read in business locations from a point shapefile that are >>contained within a polygon using a Geometry filter. I've successfully >>used the bounding Box filter and so I could use this to get all of the >>points within the envelope of a polygon. Then I could traverse through >>these points and get just the ones that are in the polygon. I'd rather >>not go through this extra processing though. I'm including the (rough) >>code that I've tried to use but with no success. If it is possible to do >>what I'm trying, can someone point me at an example or guide me how to >>modify my code below. >>Thanks, >>Hari >> *public* FeatureCollection getBusinesses(Geometry businessArea, String >> businessDataset) >> { >> *try* { >> File businessFile = *new* File(businessDataset); >> URL businessURL = businessFile.toURL(); >> >> //get feature results >> ShapefileDataStore store = *new* ShapefileDataStore(businessURL); >> //feature type name is defaulted to the name of shapefile (without >> extension >> String name = store.getTypeNames()[0]; >> FeatureSource source = store.getFeatureSource(name); >> >> //source.getBounds().intersects((Envelope)businessArea.getEnvelope()); >> >> FeatureCollection currentFeatures; >> FilterFactory filterFactory = FilterFactoryFinder./createFilterFactory/(); >> >> //GeometryFilter businessAreaFilter = >> filterFactory.createGeometryFilter(FilterType.GEOMETRY_BBOX); >> //GeometryFilter businessAreaFilter = >> filterFactory.createGeometryFilter(FilterType.GEOMETRY_CONTAINS); >> //GeometryFilter businessAreaFilter = >> filterFactory.createGeometryFilter(FilterType.GEOMETRY_WITHIN); >> GeometryFilter businessAreaFilter = >> filterFactory.createGeometryFilter(AbstractFilter. /GEOMETRY_WITHIN/ ); >> >> //businessAreaFilter.addRightGeometry(filterFactory. >> // createBBoxExpression(overLappingEnvelope)); >> >> >>businessAreaFilter.addRightGeometry(filterFactory.createAttributeExpression(businessArea.toString())); > >Here lies the error, you want to compare against a constant, a literal, >not an attribute of the features you're loading. > >businessAreaFilter.addRightGeometry(filterFactory.createLiteralExpression(businessArea))) > >should do the trick (haven't tested the above, let me know). > >Cheers >Andrea |