node获取任意文件的路径

var fs = require('fs');

// 获取路径下的所有文件
function getRoute(path) {
    // 定义一个对象
    let obj = {};
    // 读取文件,返回一个对象
    let resDir = fs.readdirSync(path);
    // 循环遍历对象
    for (let i in resDir) {
        // 输入的路径拼接下一个文件(文件夹)
        let road = path + '/' + resDir[i];
        // 获取Stats 所有属性
        let res = fs.statSync(road);
        // 获取到的文件是否是文件夹
        if (res.isDirectory()) {
            // 把文件夹名赋给obj对象当属性,回调函数为值
            obj[resDir[i]] = getRoute(road);
        }else{
            // 把文件名赋给obj对象当属性,true为值
            obj[resDir[i]] = true;
        }
    }
    return obj;
}

console.log(getRoute('C:/wamp/www/nodeItem/demo0716/day0716'));

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个示例代码,演示如何使用Geotools读取shp文件,并计算任意两点间的最短路径: ```java import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.geotools.data.DataStore; import org.geotools.data.DataStoreFinder; import org.geotools.data.DefaultTransaction; import org.geotools.data.Transaction; import org.geotools.data.simple.SimpleFeatureIterator; import org.geotools.data.simple.SimpleFeatureSource; import org.geotools.factory.CommonFactoryFinder; import org.geotools.feature.FeatureIterator; import org.geotools.feature.simple.SimpleFeatureBuilder; import org.geotools.feature.simple.SimpleFeatureTypeBuilder; import org.geotools.geometry.jts.JTSFactoryFinder; import org.geotools.graph.build.GraphBuilder; import org.geotools.graph.build.line.BasicLineGraphGenerator; import org.geotools.graph.path.DijkstraShortestPathFinder; import org.geotools.graph.structure.Edge; import org.geotools.graph.structure.Graph; import org.geotools.graph.structure.Node; import org.geotools.graph.structure.basic.BasicEdge; import org.geotools.graph.structure.basic.BasicNode; import org.geotools.referencing.CRS; import org.opengis.feature.simple.SimpleFeature; import org.opengis.feature.simple.SimpleFeatureType; import org.opengis.filter.FilterFactory2; import org.opengis.geometry.Geometry; import org.opengis.geometry.primitive.Point; import org.opengis.referencing.crs.CoordinateReferenceSystem; import com.vividsolutions.jts.geom.Coordinate; import com.vividsolutions.jts.geom.Envelope; import com.vividsolutions.jts.geom.GeometryFactory; public class ShpFileShortestPath { public static void main(String[] args) throws Exception { // 读取shp文件 File file = new File("path/to/shapefile.shp"); DataStore dataStore = DataStoreFinder.getDataStore(file); String typeName = dataStore.getTypeNames()[0]; SimpleFeatureSource featureSource = dataStore.getFeatureSource(typeName); // 创建图形数据 GraphBuilder graphBuilder = new GraphBuilder(JTSFactoryFinder.getGeometryFactory()); SimpleFeatureIterator featureIterator = featureSource.getFeatures().features(); try { while (featureIterator.hasNext()) { SimpleFeature feature = featureIterator.next(); Geometry geometry = (Geometry) feature.getDefaultGeometry(); // 添加节点 Coordinate[] coordinates = geometry.getCoordinates(); for (Coordinate coordinate : coordinates) { Node node = new BasicNode(); node.setObject(coordinate); graphBuilder.addNode(node); } // 添加边 for (int i = 0; i < coordinates.length - 1; i++) { Coordinate start = coordinates[i]; Coordinate end = coordinates[i + 1]; double weight = start.distance(end); Edge edge = new BasicEdge(graphBuilder.getNode(start), graphBuilder.getNode(end)); edge.setObject(weight); graphBuilder.addEdge(edge); } } } finally { featureIterator.close(); } Graph graph = graphBuilder.getGraph(); // 计算最短路径 DijkstraShortestPathFinder finder = new DijkstraShortestPathFinder(graph); Node startNode = graph.getNode(new Coordinate(起点经度, 起点纬度)); Node endNode = graph.getNode(new Coordinate(终点经度, 终点纬度)); List<Node> path = finder.getPath(startNode, endNode); // 输出最短路径 System.out.println("最短路径:"); for (Node node : path) { Coordinate coordinate = (Coordinate) node.getObject(); System.out.println(coordinate.x + "," + coordinate.y); } } } ``` 其中,需要根据实际情况替换以下代码: - ``path/to/shapefile.shp``:shp文件路径。 - ``起点经度``、``起点纬度``、``终点经度``、``终点纬度``:起点和终点的经纬度坐标。 请注意,这只是一个简单的示例,实际应用中需要根据具体情况进行适当调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值