forked from OSGeo/gdal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gnmmanage.cpp
993 lines (858 loc) · 32.1 KB
/
gnmmanage.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
/******************************************************************************
* Project: Geography Network utility
* Purpose: To manage GNM networks
* Authors: Mikhail Gusev, gusevmihs at gmail dot com
* Dmitry Baryshnikov, [email protected]
*
******************************************************************************
* Copyright (c) 2014, Mikhail Gusev
* Copyright (c) 2014-2015, NextGIS <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
****************************************************************************/
#include "commonutils.h"
#include "cpl_string.h"
#include "gdal_version.h"
#include "gdal.h"
#include "gnm.h"
#include "gnm_priv.h"
// #include "ogr_p.h"
// #include "gnm.h"
// #include "gnm_api.h"
enum operation
{
op_unknown = 0, /** no operation */
op_info, /** print information about network */
op_create, /** create a new network */
op_import, /** add a OGR layer to the network */
op_connect, /** connect features from layers added to the network */
op_disconnect, /** disconnect features from layers added to the network */
op_rule, /** add connect rule */
op_autoconnect, /** try to connect features base on their tolerance */
op_delete, /** delete network */
op_change_st /** change vertex or edge blocking state */
};
/************************************************************************/
/* Usage() */
/************************************************************************/
static void Usage(const char *pszAdditionalMsg,
int bShort = TRUE) CPL_NO_RETURN;
static void Usage(const char *pszAdditionalMsg, int bShort)
{
printf("Usage: gnmmanage [--help][-q][-quiet][--long-usage]\n"
" [info]\n"
" [create [-f format_name] [-t_srs srs_name] [-dsco "
"NAME=VALUE]... ]\n"
" [import src_dataset_name] [-l layer_name]\n"
" [connect gfid_src gfid_tgt gfid_con [-c cost] "
"[-ic inv_cost] [-dir dir]]\n"
" [disconnect gfid_src gfid_tgt gfid_con]\n"
" [rule rule_str]\n"
" [autoconnect tolerance]\n"
" [delete]\n"
" [change [-bl gfid][-unbl gfid][-unblall]]\n"
" gnm_name [layer [layer ...]]\n");
if (bShort)
{
printf("\nNote: gnmmanage --long-usage for full help.\n");
if (pszAdditionalMsg)
fprintf(stderr, "\nFAILURE: %s\n", pszAdditionalMsg);
exit(1);
}
printf("\n info: different information about network: system and class "
"layers, network metadata, network spatial reference\n"
" create: create network\n"
" -f format_name: output file format name, possible values "
"are:\n");
int nGNMDriverCounter = 1;
for (int iDr = 0; iDr < GDALGetDriverCount(); iDr++)
{
GDALDriverH hDriver = GDALGetDriver(iDr);
const char *pszRFlag = "", *pszWFlag, *pszVirtualIO, *pszSubdatasets;
char **papszMD = GDALGetMetadata(hDriver, nullptr);
if (CPLFetchBool(papszMD, GDAL_DCAP_RASTER, false))
continue;
if (CPLFetchBool(papszMD, GDAL_DCAP_VECTOR, false))
continue;
if (CPLFetchBool(papszMD, GDAL_DCAP_OPEN, false))
pszRFlag = "r";
if (CPLFetchBool(papszMD, GDAL_DCAP_CREATE, false))
pszWFlag = "w+";
else if (CPLFetchBool(papszMD, GDAL_DCAP_CREATECOPY, false))
pszWFlag = "w";
else
pszWFlag = "o";
if (CPLFetchBool(papszMD, GDAL_DCAP_VIRTUALIO, false))
pszVirtualIO = "v";
else
pszVirtualIO = "";
if (CPLFetchBool(papszMD, GDAL_DMD_SUBDATASETS, false))
pszSubdatasets = "s";
else
pszSubdatasets = "";
printf(" %d. %s (%s%s%s%s): %s\n", nGNMDriverCounter++,
GDALGetDriverShortName(hDriver), pszRFlag, pszWFlag,
pszVirtualIO, pszSubdatasets, GDALGetDriverLongName(hDriver));
}
printf(
" -t_srs srs_name: spatial reference input\n"
" -dsco NAME=VALUE: network creation option set as pair=value\n"
" import src_dataset_name: import external layer where "
"src_dataset_name is a dataset name to copy from\n"
" -l layer_name: layer name in dataset. If unset, 0 layer is "
"copied\n"
" connect gfid_src gfid_tgt gfid_con: make a topological connection, "
"where the gfid_src and gfid_tgt are vertices and gfid_con is edge "
"(gfid_con can be -1, so the virtual connection will be created)\n"
" -c cost -ic inv_cost -dir dir: manually assign the following "
"values: the cost (weight), inverse cost and direction of the edge "
"(optional)\n"
" disconnect gfid_src gfid_tgt gfid_con: removes the connection from "
"the graph\n"
" rule rule_str: creates a rule in the network by the given rule_str "
"string\n"
" autoconnect tolerance: create topology automatically with the "
"given double tolerance\n"
" delete: delete network\n"
" change: modify blocking state of vertices and edges ans save them "
"in the network"
" -bl gfid: block feature before the main operation. Blocking "
"features are saved in the special layer\n"
" -unbl gfid: unblock feature before the main operation\n"
" -unblall: unblock all blocked features before the main operation\n"
" gnm_name: the network to work with (path and name)\n");
if (pszAdditionalMsg)
fprintf(stderr, "\nFAILURE: %s\n", pszAdditionalMsg);
exit(1);
}
static void Usage(int bShort = TRUE)
{
Usage(nullptr, bShort);
}
/************************************************************************/
/* main() */
/************************************************************************/
#define CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(nExtraArg) \
do \
{ \
if (iArg + nExtraArg >= nArgc) \
Usage(CPLSPrintf("%s option requires %d argument(s)", \
papszArgv[iArg], nExtraArg)); \
} while (false)
MAIN_START(nArgc, papszArgv)
{
int bQuiet = FALSE;
const char *pszFormat = nullptr;
const char *pszSRS = nullptr;
GNMGFID nSrcFID = -1;
GNMGFID nTgtFID = -1;
GNMGFID nConFID = -1;
double dfDirCost = 1.0;
double dfInvCost = 1.0;
GNMDirection eDir = GNM_EDGE_DIR_BOTH;
const char *pszRuleStr = "";
const char *pszDataSource = nullptr;
char **papszDSCO = nullptr;
const char *pszInputDataset = nullptr;
const char *pszInputLayer = nullptr;
double dfTolerance = 0.0001;
operation stOper = op_unknown;
char **papszLayers = nullptr;
GNMNetwork *poDS = nullptr;
std::vector<GNMGFID> anFIDsToBlock;
std::vector<GNMGFID> anFIDsToUnblock;
bool bUnblockAll = false;
int nRet = 0;
// Check strict compilation and runtime library version as we use C++ API
if (!GDAL_CHECK_VERSION(papszArgv[0]))
exit(1);
EarlySetConfigOptions(nArgc, papszArgv);
/* -------------------------------------------------------------------- */
/* Register format(s). */
/* -------------------------------------------------------------------- */
GDALAllRegister();
/* -------------------------------------------------------------------- */
/* Processing command line arguments. */
/* -------------------------------------------------------------------- */
nArgc = GDALGeneralCmdLineProcessor(nArgc, &papszArgv, GDAL_OF_GNM);
if (nArgc < 1)
{
exit(-nArgc);
}
for (int iArg = 1; iArg < nArgc; iArg++)
{
if (EQUAL(papszArgv[1], "--utility_version"))
{
printf("%s was compiled against GDAL %s and is running against "
"GDAL %s\n",
papszArgv[0], GDAL_RELEASE_NAME,
GDALVersionInfo("RELEASE_NAME"));
CSLDestroy(papszArgv);
return 0;
}
else if (EQUAL(papszArgv[iArg], "--help"))
{
Usage();
}
else if (EQUAL(papszArgv[iArg], "--long-usage"))
{
Usage(FALSE);
}
else if (EQUAL(papszArgv[iArg], "-q") ||
EQUAL(papszArgv[iArg], "-quiet"))
{
bQuiet = TRUE;
}
else if (EQUAL(papszArgv[iArg], "info"))
{
stOper = op_info;
}
else if (EQUAL(papszArgv[iArg], "-f") || EQUAL(papszArgv[iArg], "-of"))
{
CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(1);
// coverity[tainted_data]
pszFormat = papszArgv[++iArg];
}
else if (EQUAL(papszArgv[iArg], "-dsco"))
{
CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(1);
// coverity[tainted_data]
papszDSCO = CSLAddString(papszDSCO, papszArgv[++iArg]);
}
else if (EQUAL(papszArgv[iArg], "create"))
{
stOper = op_create;
}
else if (EQUAL(papszArgv[iArg], "-t_srs"))
{
CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(1);
// coverity[tainted_data]
pszSRS = papszArgv[++iArg];
}
else if (EQUAL(papszArgv[iArg], "import"))
{
stOper = op_import;
CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(1);
// coverity[tainted_data]
pszInputDataset = papszArgv[++iArg];
}
else if (EQUAL(papszArgv[iArg], "-l"))
{
CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(1);
// coverity[tainted_data]
pszInputLayer = papszArgv[++iArg];
}
else if (EQUAL(papszArgv[iArg], "connect"))
{
CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(3);
stOper = op_connect;
// coverity[tainted_data]
nSrcFID = atoi(papszArgv[++iArg]);
// coverity[tainted_data]
nTgtFID = atoi(papszArgv[++iArg]);
// coverity[tainted_data]
nConFID = atoi(papszArgv[++iArg]);
}
else if (EQUAL(papszArgv[iArg], "-c"))
{
CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(1);
// coverity[tainted_data]
dfDirCost = CPLAtofM(papszArgv[++iArg]);
}
else if (EQUAL(papszArgv[iArg], "-ic"))
{
CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(1);
// coverity[tainted_data]
dfInvCost = CPLAtofM(papszArgv[++iArg]);
}
else if (EQUAL(papszArgv[iArg], "-dir"))
{
CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(1);
// coverity[tainted_data]
eDir = atoi(papszArgv[++iArg]);
}
else if (EQUAL(papszArgv[iArg], "disconnect"))
{
CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(3);
stOper = op_disconnect;
// coverity[tainted_data]
nSrcFID = atoi(papszArgv[++iArg]);
// coverity[tainted_data]
nTgtFID = atoi(papszArgv[++iArg]);
// coverity[tainted_data]
nConFID = atoi(papszArgv[++iArg]);
}
else if (EQUAL(papszArgv[iArg], "autoconnect"))
{
CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(1);
stOper = op_autoconnect;
// coverity[tainted_data]
dfTolerance = CPLAtofM(papszArgv[++iArg]);
}
else if (EQUAL(papszArgv[iArg], "rule"))
{
CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(1);
stOper = op_rule;
// coverity[tainted_data]
pszRuleStr = papszArgv[++iArg];
}
else if (EQUAL(papszArgv[iArg], "delete"))
{
stOper = op_delete;
}
else if (EQUAL(papszArgv[iArg], "change"))
{
stOper = op_change_st;
}
else if (EQUAL(papszArgv[iArg], "-bl"))
{
CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(1);
// coverity[tainted_data]
anFIDsToBlock.push_back(atoi(papszArgv[++iArg]));
}
else if (EQUAL(papszArgv[iArg], "-unbl"))
{
CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(1);
// coverity[tainted_data]
anFIDsToUnblock.push_back(atoi(papszArgv[++iArg]));
}
else if (EQUAL(papszArgv[iArg], "-unblall"))
{
bUnblockAll = true;
}
else if (papszArgv[iArg][0] == '-')
{
Usage(CPLSPrintf("Unknown option name '%s'", papszArgv[iArg]));
}
else if (pszDataSource == nullptr)
pszDataSource = papszArgv[iArg];
else
papszLayers = CSLAddString(papszLayers, papszArgv[iArg]);
}
// do the work
// ////////////////////////////////////////////////////////////////
if (stOper == op_info)
{
if (pszDataSource == nullptr)
Usage("No network dataset provided");
// TODO for output:
// stats about graph and blocked features
// open
poDS = cpl::down_cast<GNMNetwork *>(static_cast<GDALDataset *>(
GDALOpenEx(pszDataSource, GDAL_OF_READONLY | GDAL_OF_GNM, nullptr,
nullptr, nullptr)));
GDALDriver *poDriver = nullptr;
if (poDS != nullptr)
poDriver = poDS->GetDriver();
if (poDS == nullptr)
{
fprintf(stderr, "FAILURE:\nUnable to open datasource `%s'.\n",
pszDataSource);
exit(1);
}
if (poDriver == nullptr)
{
CPLAssert(false);
exit(1);
}
printf("INFO: Open of `%s'\n using driver `%s' successful.\n",
pszDataSource, poDriver->GetDescription());
// Report projection.
int nMajor = poDS->GetVersion() / 100;
printf("Network version: %d.%d.\n", nMajor,
poDS->GetVersion() - nMajor * 100);
const char *pszName = poDS->GetName();
if (nullptr != pszName)
printf("Network name: %s.\n", pszName);
const char *pszDescript = poDS->GetDescription();
if (nullptr != pszDescript)
printf("Network description: %s.\n", pszDescript);
char *pszProjection = const_cast<char *>(poDS->GetProjectionRef());
OGRSpatialReferenceH hSRS = OSRNewSpatialReference(nullptr);
if (OSRImportFromWkt(hSRS, &pszProjection) == CE_None)
{
char *pszPrettyWkt = nullptr;
OSRExportToPrettyWkt(hSRS, &pszPrettyWkt, FALSE);
printf("Coordinate System is:\n%s\n", pszPrettyWkt);
CPLFree(pszPrettyWkt);
}
else
{
printf("Coordinate System is '%s'\n", pszProjection);
}
OSRDestroySpatialReference(hSRS);
// report layers
if (poDS->GetLayerCount() > 0)
{
printf("\nNetwork\'s layers: \n");
for (int iLayer = 0; iLayer < poDS->GetLayerCount(); iLayer++)
{
OGRLayer *poLayer = poDS->GetLayer(iLayer);
if (poLayer != nullptr)
{
printf(" %d: %s", iLayer + 1, poLayer->GetName());
int nGeomFieldCount =
poLayer->GetLayerDefn()->GetGeomFieldCount();
if (nGeomFieldCount > 1)
{
printf(" (");
for (int iGeom = 0; iGeom < nGeomFieldCount; iGeom++)
{
if (iGeom > 0)
printf(", ");
OGRGeomFieldDefn *poGFldDefn =
poLayer->GetLayerDefn()->GetGeomFieldDefn(
iGeom);
printf("%s", OGRGeometryTypeToName(
poGFldDefn->GetType()));
}
printf(")");
}
else if (poLayer->GetGeomType() != wkbUnknown)
printf(" (%s)",
OGRGeometryTypeToName(poLayer->GetGeomType()));
printf("\n");
}
}
}
// report rules
GNMGenericNetwork *poGenericNetwork =
dynamic_cast<GNMGenericNetwork *>(poDS);
if (nullptr != poGenericNetwork)
{
CPLStringList oList(poGenericNetwork->GetRules());
if (oList.Count() > 0)
{
printf("\nNetwork\'s rules: \n");
for (int iRule = 0; iRule < oList.Count(); ++iRule)
{
printf(" %d: %s\n", iRule + 1, oList[iRule]);
}
}
}
}
else if (stOper == op_create)
{
const char *pszPath;
const char *pszNetworkName = CSLFetchNameValue(papszDSCO, GNM_MD_NAME);
if (pszDataSource == nullptr)
Usage("No network dataset provided");
// the DSCO have priority on input keys
if (nullptr == pszNetworkName)
{
pszPath = CPLGetPath(pszDataSource);
pszNetworkName = CPLGetBasename(pszDataSource);
papszDSCO = CSLAddNameValue(papszDSCO, GNM_MD_NAME, pszNetworkName);
}
else
{
pszPath = pszDataSource;
}
if (pszNetworkName == nullptr)
Usage("No dataset name provided");
const char *pszFinalSRS = CSLFetchNameValue(papszDSCO, GNM_MD_SRS);
if (nullptr == pszFinalSRS)
{
pszFinalSRS = pszSRS;
papszDSCO = CSLAddNameValue(papszDSCO, GNM_MD_SRS, pszSRS);
}
if (nullptr == pszFinalSRS)
Usage("No spatial reference provided");
if (pszFormat == nullptr)
Usage("No output format provided");
GDALDriver *poDriver =
GetGDALDriverManager()->GetDriverByName(pszFormat);
if (poDriver == nullptr)
{
Usage(CPLSPrintf("%s driver not available", pszFormat));
}
else
{
char **papszMD = poDriver->GetMetadata();
if (!CPLFetchBool(papszMD, GDAL_DCAP_GNM, false))
Usage("not a GNM driver");
poDS = cpl::down_cast<GNMNetwork *>(
poDriver->Create(pszPath, 0, 0, 0, GDT_Unknown, papszDSCO));
if (nullptr == poDS)
{
fprintf(
stderr,
"\nFAILURE: Failed to create network in a new dataset at "
"%s and with driver %s\n",
CPLFormFilename(pszPath, pszNetworkName, nullptr),
pszFormat);
nRet = 1;
}
else
{
if (bQuiet == FALSE)
printf("\nNetwork created successfully in a "
"new dataset at %s\n",
CPLFormFilename(pszPath, pszNetworkName, nullptr));
}
}
}
else if (stOper == op_import)
{
if (pszDataSource == nullptr)
Usage("No network dataset provided");
if (pszInputDataset == nullptr)
Usage("No input dataset name provided");
// open
poDS = cpl::down_cast<GNMNetwork *>(static_cast<GDALDataset *>(
GDALOpenEx(pszDataSource, GDAL_OF_READONLY | GDAL_OF_GNM, nullptr,
nullptr, nullptr)));
if (nullptr == poDS)
{
printf("\nFailed to open network at %s\n", pszDataSource);
goto exit;
}
GDALDataset *poSrcDS = static_cast<GDALDataset *>(
GDALOpenEx(pszInputDataset, GDAL_OF_VECTOR | GDAL_OF_READONLY,
nullptr, nullptr, nullptr));
if (nullptr == poSrcDS)
{
fprintf(stderr, "\nFAILURE: Can not open dataset at %s\n",
pszInputDataset);
nRet = 1;
goto exit;
}
OGRLayer *poSrcLayer;
if (pszInputLayer != nullptr)
poSrcLayer = poSrcDS->GetLayerByName(pszInputLayer);
else
poSrcLayer = poSrcDS->GetLayer(0);
if (nullptr == poSrcLayer)
{
if (pszInputLayer != nullptr)
fprintf(stderr, "\nFAILURE: Can not open layer %s in %s\n",
pszInputLayer, pszInputDataset);
else
fprintf(stderr, "\nFAILURE: Can not open layer in %s\n",
pszInputDataset);
GDALClose(poSrcDS);
nRet = 1;
goto exit;
}
OGRLayer *poLayer = poDS->CopyLayer(poSrcLayer, poSrcLayer->GetName());
if (nullptr == poLayer)
{
if (pszInputLayer != nullptr)
fprintf(stderr, "\nFAILURE: Can not copy layer %s from %s\n",
pszInputLayer, pszInputDataset);
else
fprintf(stderr, "\nFAILURE: Can not copy layer from %s\n",
pszInputDataset);
GDALClose(poSrcDS);
nRet = 1;
goto exit;
}
if (bQuiet == FALSE)
{
if (pszInputLayer != nullptr)
printf("\nLayer %s successfully copied from %s and added to "
"the network at %s\n",
pszInputLayer, pszInputDataset, pszDataSource);
else
printf("\nLayer successfully copied from %s and added to the "
"network at %s\n",
pszInputDataset, pszDataSource);
}
GDALClose(poSrcDS);
}
else if (stOper == op_connect)
{
if (pszDataSource == nullptr)
Usage("No network dataset provided");
// open
poDS = cpl::down_cast<GNMNetwork *>(static_cast<GDALDataset *>(
GDALOpenEx(pszDataSource, GDAL_OF_UPDATE | GDAL_OF_GNM, nullptr,
nullptr, nullptr)));
if (nullptr == poDS)
{
fprintf(stderr, "\nFailed to open network at %s\n", pszDataSource);
nRet = 1;
goto exit;
}
GNMGenericNetwork *poGenericNetwork =
dynamic_cast<GNMGenericNetwork *>(poDS);
if (nullptr == poGenericNetwork)
{
fprintf(stderr,
"\nUnsupported datasource type for this operation\n");
nRet = 1;
goto exit;
}
if (poGenericNetwork->ConnectFeatures(nSrcFID, nTgtFID, nConFID,
dfDirCost, dfInvCost,
eDir) != CE_None)
{
fprintf(stderr, "Failed to connect features\n");
nRet = 1;
goto exit;
}
if (bQuiet == FALSE)
{
printf("Features connected successfully\n");
}
}
else if (stOper == op_disconnect)
{
if (pszDataSource == nullptr)
Usage("No network dataset provided");
// open
poDS = cpl::down_cast<GNMNetwork *>(static_cast<GDALDataset *>(
GDALOpenEx(pszDataSource, GDAL_OF_UPDATE | GDAL_OF_GNM, nullptr,
nullptr, nullptr)));
if (nullptr == poDS)
{
fprintf(stderr, "\nFailed to open network at %s\n", pszDataSource);
nRet = 1;
goto exit;
}
GNMGenericNetwork *poGenericNetwork =
dynamic_cast<GNMGenericNetwork *>(poDS);
if (nullptr == poGenericNetwork)
{
fprintf(stderr,
"\nUnsupported datasource type for this operation\n");
nRet = 1;
goto exit;
}
if (poGenericNetwork->DisconnectFeatures(nSrcFID, nTgtFID, nConFID) !=
CE_None)
{
fprintf(stderr, "Failed to disconnect features\n");
nRet = 1;
goto exit;
}
if (bQuiet == FALSE)
{
printf("Features disconnected successfully\n");
}
}
else if (stOper == op_rule)
{
if (pszDataSource == nullptr)
Usage("No network dataset provided");
// open
poDS = cpl::down_cast<GNMNetwork *>(static_cast<GDALDataset *>(
GDALOpenEx(pszDataSource, GDAL_OF_UPDATE | GDAL_OF_GNM, nullptr,
nullptr, nullptr)));
if (nullptr == poDS)
{
fprintf(stderr, "\nFailed to open network at %s\n", pszDataSource);
nRet = 1;
goto exit;
}
GNMGenericNetwork *poGenericNetwork =
dynamic_cast<GNMGenericNetwork *>(poDS);
if (nullptr == poGenericNetwork)
{
fprintf(stderr,
"\nUnsupported datasource type for this operation\n");
nRet = 1;
goto exit;
}
if (poGenericNetwork->CreateRule(pszRuleStr) != CE_None)
{
fprintf(stderr, "Failed to create rule %s\n", pszRuleStr);
nRet = 1;
goto exit;
}
if (bQuiet == FALSE)
{
printf("Create rule '%s' successfully\n", pszRuleStr);
}
}
else if (stOper == op_autoconnect)
{
if (pszDataSource == nullptr)
Usage("No network dataset provided");
// open
poDS = cpl::down_cast<GNMNetwork *>(static_cast<GDALDataset *>(
GDALOpenEx(pszDataSource, GDAL_OF_UPDATE | GDAL_OF_GNM, nullptr,
nullptr, nullptr)));
if (nullptr == poDS)
{
fprintf(stderr, "\nFailed to open network at %s\n", pszDataSource);
nRet = 1;
goto exit;
}
GNMGenericNetwork *poGenericNetwork =
dynamic_cast<GNMGenericNetwork *>(poDS);
if (nullptr == poGenericNetwork)
{
fprintf(stderr,
"\nUnsupported datasource type for this operation\n");
nRet = 1;
goto exit;
}
if (CSLCount(papszLayers) == 0 && poDS->GetLayerCount() > 1)
{
if (bQuiet == FALSE)
{
printf("No layers provided. Use all layers of network:\n");
}
for (int i = 0; i < poDS->GetLayerCount(); ++i)
{
OGRLayer *poLayer = poDS->GetLayer(i);
if (bQuiet == FALSE)
{
printf("%d. %s\n", i + 1, poLayer->GetName());
}
papszLayers = CSLAddString(papszLayers, poLayer->GetName());
}
}
if (poGenericNetwork->ConnectPointsByLines(papszLayers, dfTolerance,
dfDirCost, dfInvCost,
eDir) != CE_None)
{
fprintf(stderr, "Failed to autoconnect features\n");
nRet = 1;
goto exit;
}
if (bQuiet == FALSE)
{
printf("Features connected successfully\n");
}
}
else if (stOper == op_delete)
{
if (pszDataSource == nullptr)
Usage("No network dataset provided");
// open
poDS = cpl::down_cast<GNMNetwork *>(static_cast<GDALDataset *>(
GDALOpenEx(pszDataSource, GDAL_OF_UPDATE | GDAL_OF_GNM, nullptr,
nullptr, nullptr)));
if (nullptr == poDS)
{
fprintf(stderr, "\nFailed to open network at %s\n", pszDataSource);
nRet = 1;
goto exit;
}
if (poDS->Delete() != CE_None)
{
fprintf(stderr, "Delete failed.\n");
nRet = 1;
goto exit;
}
if (bQuiet == FALSE)
{
printf("Delete successfully\n");
}
/** if hDriver == NULL this code delete everything in folder,
* not only GNM files
GDALDriverH hDriver = NULL;
if( pszFormat != NULL )
{
hDriver = GDALGetDriverByName( pszFormat );
if( hDriver == NULL )
{
fprintf( stderr, "Unable to find driver named '%s'.\n",
pszFormat );
exit( 1 );
}
}
GDALDeleteDataset( hDriver, pszDataSource );
*/
}
else if (stOper == op_change_st)
{
if (pszDataSource == nullptr)
Usage("No dataset in input");
// open
poDS = cpl::down_cast<GNMNetwork *>(static_cast<GDALDataset *>(
GDALOpenEx(pszDataSource, GDAL_OF_UPDATE | GDAL_OF_GNM, nullptr,
nullptr, nullptr)));
if (nullptr == poDS)
{
fprintf(stderr, "\nFailed to open network at %s\n", pszDataSource);
nRet = 1;
goto exit;
}
GNMGenericNetwork *poGenericNetwork =
dynamic_cast<GNMGenericNetwork *>(poDS);
if (nullptr == poGenericNetwork)
{
fprintf(stderr,
"\nUnsupported datasource type for this operation\n");
nRet = 1;
goto exit;
}
if (bUnblockAll)
{
if (poGenericNetwork->ChangeAllBlockState(false) != CE_None)
{
fprintf(stderr, "\nChange all block state failed\n");
nRet = 1;
goto exit;
}
}
else
{
size_t i;
for (i = 0; i < anFIDsToBlock.size(); ++i)
{
if (poGenericNetwork->ChangeBlockState(anFIDsToBlock[i],
true) != CE_None)
{
fprintf(stderr,
"\nChange block state of id " GNMGFIDFormat
" failed\n",
anFIDsToBlock[i]);
nRet = 1;
goto exit;
}
}
for (i = 0; i < anFIDsToUnblock.size(); ++i)
{
if (poGenericNetwork->ChangeBlockState(anFIDsToUnblock[i],
false) != CE_None)
{
fprintf(stderr,
"\nChange block state of id " GNMGFIDFormat
" failed\n",
anFIDsToBlock[i]);
nRet = 1;
goto exit;
}
}
}
if (bQuiet == FALSE)
{
printf("Change block state successfully\n");
}
}
else
{
printf(
"\nNeed an operation. See help what you can do with gnmmanage:\n");
Usage();
}
exit:
CSLDestroy(papszArgv);
CSLDestroy(papszDSCO);
CSLDestroy(papszLayers);
if (poDS != nullptr)
GDALClose(poDS);
GDALDestroyDriverManager();
return nRet;
}
MAIN_END