Hi John,

I had some example code laying around that should do this.  It doesn't use a Geotools Filter, but uses the low-level Geometry methods in the JTS library.  It is probably not the best way to do it but it works for me.

Steve



import java.io.*;
import java.net.*;
import java.util.*;

import org.geotools.feature.*;
import org.geotools.data.*;
import org.geotools.data.shapefile.*;
import com.vividsolutions.jts.geom.*;

/**
 *  Description of the Class
 *
 * @author    steve.ansari
 */
public class ClipShapefile {

   /**
    *  Description of the Method
    *
    * @param  shpFileURL                 Description of the Parameter
    * @exception  IOException            Description of the Exception
    * @exception  MalformedURLException  Description of the Exception
    */
   public static void process(URL shpFileURL, File clipOutputShpFile)
      throws IOException, MalformedURLException, IllegalAttributeException {

      // Set up tile geometry using JTS library from:
      // http://www.vividsolutions.com/jts/JTSHome.htm
      Envelope envelope = new Envelope(-100.0, -80.0, 30.0, 40.0);
      GeometryFactory geomFactory = new GeometryFactory();
      Geometry tileGeometry = geomFactory.toGeometry(envelope);


     
      // Load shapefile
      ShapefileDataStore ds = new ShapefileDataStore(shpFileURL);
      FeatureSource fs = ds.getFeatureSource();
      FeatureCollection fc = fs.getFeatures();
     
      // Create the output shapefile
      ShapefileDataStore outStore = new ShapefileDataStore(clipOutputShpFile.toURL());
      outStore.createSchema(fs.getSchema());
      FeatureWriter outFeatureWriter = outStore.getFeatureWriter(outStore.getTypeNames()[0], Transaction.AUTO_COMMIT);

      // Process shapefile     
      Iterator iterator = fc.iterator();
      try {
        
         Object[] att = null;
         int count = 0;
         while (iterator.hasNext()) {
            // Extract feature and geometry
            Feature feature = (Feature) iterator.next();
            Geometry featureGeometry = feature.getDefaultGeometry();
           
            if (featureGeometry.intersects(tileGeometry)) {                                
              
               // Clip
               Geometry newGeom = featureGeometry.intersection(tileGeometry);

               // The original schema is expecting the Geometry type to be MultiPolygon,
               // but the clip method sometimes returns the type Polygon.  If that is the
               // case, convert to MultiPolygon
               if (newGeom instanceof Polygon) {            
                  newGeom = geomFactory.createMultiPolygon(new Polygon[] { (Polygon)newGeom });
               }

               // Adjust the feature to represent the clipped geometry with same attributes
               feature.setDefaultGeometry(newGeom);
              

               // Get the next, empty feature from the writer              
               Feature writeFeature = outFeatureWriter.next();
               att = feature.getAttributes(att);
               // Set the attributes of the new feature by copying the old, adjusted feature,
               // which includes the geometry (which is an attribute)
               for (int n=0; n<att.length; n++) {
                  writeFeature.setAttribute(n, att[n]); 
               }
               outFeatureWriter.write();
                             
               count++;
            }
         }

         System.out.println("ORIGINAL NUMBER OF FEATURES: "+fc.size());
         System.out.println("FILTERED NUMBER OF FEATURES: "+count);
         outFeatureWriter.close();
        
      } finally {
         fc.close(iterator);
      }

   }


   /**
    *  The main program for the ClipShapefile class
    *
    * @param  args  The command line arguments
    */
   public static void main(String[] args) {

      try {

         URL url = new File("C:\\devel\\jnx\\trunk\\build\\shapefiles\\counties.shp").toURL();
         File outFile = new File("H:\\Nexrad_Viewer_Test\\GT_Examples\\counties-clip.shp");
        
         process(url, outFile);

      } catch (Exception e) {
         e.printStackTrace();
      }

   }







Jesse Eichar wrote:
Hi you can use the FeatureSource/Store API:

ShapefileDatastore ds;

FeatureSource source=ds.getFeatureSource( typename) ;

if( source instanceof FeatureStore ){
	// this means you can add/modify features
	FeatureStore store=(FeatureStore)store;
	store.addFeatures( featureCollection );
	// make sure that the features in the feature collection have the
	// same feature type as returned by: store.getSchema()
}

source.getFeatures(filter);

Hope this helps,

Jesse
On 27-Jul-06, at 8:13 AM, John Cartwright wrote:

  
Hello All,

I'd like to build a simple application that splits a shapefile into a
series of new shapefile "tiles".  Can anyone point me to a simple
example for reading/writing shapefiles using GT2.2?

The example in the ShapefileReader javadocs (listed below) doesn't  
seem
to  be correct.

Thanks!

-- john

      FileChannel in = new FileInputStream(filename).getChannel();
      ShapefileReader r = new ShapefileReader( in );
      while (r.hasNext()) {
         Geometry shape = (Geometry) r.nextRecord().shape()
         // do stuff
      }
      r.close();


---------------------------------------------------------------------- 
---
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to  
share your
opinions on IT & business topics through brief surveys -- and earn  
cash
http://www.techsay.com/default.php? 
page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Geotools-gt2-users mailing list
Geotools-gt2-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users
    


-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Geotools-gt2-users mailing list
Geotools-gt2-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users
  

-- 
Steve Ansari
Physical Scientist
NOAA's National Climatic Data Center
Veach-Baley Federal Building
151 Patton Avenue
Asheville, NC 28801
Ph: 828-271-4611
Fax: 828-271-4022