Geotools establishment of buffer zones in shapefile road network data and obtain the elements in the buffer zone

The record about how to create and obtain elements to create a buffer in the buffer zone, easy to find later use

     static SimpleFeatureSource featureSource = null;
     static CoordinateReferenceSystem targetCRS;
   static String geometryPropertyName;
     static FilterFactory2 ff;
   public SimpleFeatureCollection grabFeaturesInBoundingBox(double x1, double y1, double x2, double y2)
            throws Exception {
        ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
        ShapefileDataStore sds = (ShapefileDataStore)dataStoreFactory.createDataStore
                (new File("E://Merge_direction.shp").toURI().toURL());
        
        Charset charset = Charset.forName("GBK");
        sds.setCharset(charset);
        String typeName = sds.getTypeNames()[0];
        featureSource =  sds.getFeatureSource (typeName);
        FilterFactory2 ff = CommonFactoryFinder.getFilterFactory2();
        FeatureType schema = featureSource.getSchema();

        geometryPropertyName = schema.getGeometryDescriptor().getLocalName();
        targetCRS = schema.getGeometryDescriptor().getCoordinateReferenceSystem();

        ReferencedEnvelope bbox = new ReferencedEnvelope(x1, y1, x2, y2, targetCRS);

        Filter filter = ff.bbox(ff.property(geometryPropertyName), bbox);
        return featureSource.getFeatures(filter);
    }

 

Guess you like

Origin www.cnblogs.com/help-silence/p/11872257.html