| 767 | } |
| 768 | |
| 769 | OGRLayer *GNMGenericNetwork::GetPath(GNMGFID nStartFID, GNMGFID nEndFID, |
| 770 | GNMGraphAlgorithmType eAlgorithm, |
| 771 | CSLConstList papszOptions) |
| 772 | { |
| 773 | |
| 774 | if (!m_bIsGraphLoaded && LoadGraph() != CE_None) |
| 775 | { |
| 776 | return nullptr; |
| 777 | } |
| 778 | |
| 779 | GDALDriver *poMEMDrv = |
| 780 | OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName("Memory"); |
| 781 | if (poMEMDrv == nullptr) |
| 782 | { |
| 783 | CPLError(CE_Failure, CPLE_AppDefined, "Cannot load 'Memory' driver"); |
| 784 | return nullptr; |
| 785 | } |
| 786 | |
| 787 | GDALDataset *poMEMDS = |
| 788 | poMEMDrv->Create("dummy_name", 0, 0, 0, GDT_Unknown, nullptr); |
| 789 | OGRSpatialReference oDstSpaRef(GetProjectionRef()); |
| 790 | OGRLayer *poMEMLayer = |
| 791 | poMEMDS->CreateLayer(GetAlgorithmName(eAlgorithm, true), &oDstSpaRef, |
| 792 | wkbGeometryCollection, nullptr); |
| 793 | |
| 794 | OGRGNMWrappedResultLayer *poResLayer = |
| 795 | new OGRGNMWrappedResultLayer(poMEMDS, poMEMLayer); |
| 796 | |
| 797 | const bool bReturnEdges = |
| 798 | CPLFetchBool(papszOptions, GNM_MD_FETCHEDGES, true); |
| 799 | const bool bReturnVertices = |
| 800 | CPLFetchBool(papszOptions, GNM_MD_FETCHVERTEX, true); |
| 801 | |
| 802 | switch (eAlgorithm) |
| 803 | { |
| 804 | case GATDijkstraShortestPath: |
| 805 | { |
| 806 | GNMPATH path = m_oGraph.DijkstraShortestPath(nStartFID, nEndFID); |
| 807 | |
| 808 | // fill features in result layer |
| 809 | FillResultLayer(poResLayer, path, 1, bReturnVertices, bReturnEdges); |
| 810 | } |
| 811 | break; |
| 812 | case GATKShortestPath: |
| 813 | { |
| 814 | int nK = |
| 815 | atoi(CSLFetchNameValueDef(papszOptions, GNM_MD_NUM_PATHS, "1")); |
| 816 | |
| 817 | CPLDebug("GNM", "Search %d path(s)", nK); |
| 818 | |
| 819 | std::vector<GNMPATH> paths = |
| 820 | m_oGraph.KShortestPaths(nStartFID, nEndFID, nK); |
| 821 | |
| 822 | // fill features in result layer |
| 823 | for (size_t i = 0; i < paths.size(); ++i) |
| 824 | { |
| 825 | FillResultLayer(poResLayer, paths[i], static_cast<int>(i + 1), |
| 826 | bReturnVertices, bReturnEdges); |