天天看點

arcobjects for java 幾何網絡分析之上下遊分析

String inFGDB = "D:\\share\\arcmap\\test.mdb";
        String mxdDoc = "D:\\share\\arcmap\\無标題.mxd";
        try {
            // 打開工作空間
            IWorkspaceFactory workspaceFactory = new WorkspaceFactory(new AccessWorkspaceFactory());
            IFeatureWorkspace featureWorkspace = (IFeatureWorkspace) workspaceFactory.openFromFile(inFGDB, 0);
            // 擷取要素集
            IFeatureDataset featureDataset = new FeatureDataset(featureWorkspace.openFeatureDataset("QZgeo"));
            INetworkCollection networkCollection = (INetworkCollection) featureDataset;
            // 擷取指定的network
            IGeometricNetwork geometricNetwork = networkCollection.getGeometricNetwork(0);
            INetwork network = geometricNetwork.getNetwork();
            ITraceFlowSolverGEN traceFlowSolverGEN = new TraceFlowSolver();
            INetSolver netSolver = (INetSolver) traceFlowSolverGEN;
            netSolver.setSourceNetworkByRef(network);

            IPoint point = new Point();
            point.putCoords(118.762761, 24.8786);

            IMap map = new com.esri.arcgis.carto.Map();
            IFeatureClassContainer featureClassContainer = (IFeatureClassContainer) featureDataset;
            int count = featureClassContainer.getClassCount();
            for(int i=0; i<count; i++){
                IFeatureClass featureClass = featureClassContainer.esri_getClass(i);
                IFeatureLayer featureLayer = new FeatureLayer();
                featureLayer.setFeatureClassByRef(featureClass);
                map.addLayer(featureLayer);
            }

            IPointToEID pointToEID = new PointToEID();
            pointToEID.setGeometricNetworkByRef(geometricNetwork);
            pointToEID.setSnapTolerance(1000);
            pointToEID.setSourceMapByRef(map);

            int[] nearestJunctionEID = new int[1];
            IPoint[] location = new Point[1];
            pointToEID.getNearestJunction(point, nearestJunctionEID, location);

            int eid = nearestJunctionEID[0];

            IEdgeFlag[] edgeFlags = new EdgeFlag[1];
            INetFlag edgeFlag = new EdgeFlag();
            INetElements netElements = new UtilityNetwork(geometricNetwork.getNetwork());
            int[] userClassId = new int[1];
            int[] userId = new int[1];
            int[] userSubId = new int[1];
            netElements.queryIDs(eid, esriElementType.esriETEdge, userClassId, userId, userSubId);
            edgeFlag.setUserClassID(userClassId[0]);
            edgeFlag.setUserID(userId[0]);
            edgeFlag.setUserSubID(userSubId[0]);
            edgeFlags[0] = (IEdgeFlag) edgeFlag;

            traceFlowSolverGEN.putEdgeOrigins(edgeFlags);

            IEnumNetEID[] junctionEIDs = new IEnumNetEID[1];
            IEnumNetEID[] edgeEIDs = new IEnumNetEID[1];
            traceFlowSolverGEN.findFlowElements(esriFlowMethod.esriFMUpstream, esriFlowElements.esriFEJunctionsAndEdges,
                    junctionEIDs, edgeEIDs);
            if(edgeEIDs[0] != null){
                System.out.println("---線---");
                int edgeEID;
                while((edgeEID = edgeEIDs[0].next()) != 0){
                    System.out.println(edgeEID);
                    IGeometry geometry = geometricNetwork.getGeometryForEdgeEID(edgeEID);
                    IPointCollection collection = (IPointCollection) geometry;
                    int pointCount = collection.getPointCount();
                    for(int i=0; i<pointCount; i++){
                        IPoint point1 = collection.getPoint(i);
                        double[] x = new double[1];
                        double[] y = new double[1];
                        point1.queryCoords(x, y);
                        System.out.print("[" + x[0] + "," + y[0] + "]");
                    }
                    System.out.println();
                }
            }
            if(junctionEIDs[0] != null){
                System.out.println("---點---");
                int junctionEID;
                while((junctionEID = junctionEIDs[0].next()) != 0){
                    System.out.println(junctionEID);
                    IGeometry geometry = geometricNetwork.getGeometryForJunctionEID(junctionEID);
                    IPoint point1 = (IPoint) geometry;
                    double[] x = new double[1];
                    double[] y = new double[1];
                    point1.queryCoords(x, y);
                    System.out.println(x[0] + " | " + y[0]);
                }
            }
        }catch(Exception e){
            e.printStackTrace();
        }