Geotools中读取shapefile路网数据,并创建DirectedGraph

记录一下如何创建DirectedGraph,便于以后查找使用

  

     static ShapefileDataStore sds= null;
     static DirectedGraph graph = null;
     ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
        try {
            sds = (ShapefileDataStore)dataStoreFactory.createDataStore(new File("E://桌面//route_LI.shp").toURI().toURL());
        } catch (IOException e) {
            e.printStackTrace();
        }
        //设置编码
        Charset charset = Charset.forName("GBK");
        sds.setCharset(charset);
        String typeName = null;
        try {
            typeName = sds.getTypeNames()[0];
        } catch (IOException e) {
            e.printStackTrace();
        }
       
        FeatureSource featureSource = null;
        try {
            featureSource =  sds.getFeatureSource (typeName);
        } catch (IOException e) {
            e.printStackTrace();
        }
       
        SimpleFeatureCollection fCollection =null;
        try {
            fCollection = (SimpleFeatureCollection) featureSource.getFeatures();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
     
        DirectedLineStringGraphGenerator lineStringGen = new DirectedLineStringGraphGenerator();
        FeatureGraphGenerator featureGen = new FeatureGraphGenerator(lineStringGen);
        featureGen.setGraphBuilder(new BasicDirectedLineGraphBuilder());
        SimpleFeatureIterator iterator = fCollection.features();
        try {
            while (iterator.hasNext()) {
                SimpleFeature feature = iterator.next();
                featureGen.add(feature);
            }
        } finally {
            iterator.close();
        }
        sds.dispose();
        graph = (DirectedGraph)featureGen.getGraph();

猜你喜欢

转载自www.cnblogs.com/help-silence/p/11872194.html