在Geotools中,如何从shapefile获取道路网络数据并构建DirectedGraph
最编程
2024-07-23 16:39:03
...
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();